From 9d6eda0d35d9fef0ffd58d7061f0d5f509c212d4 Mon Sep 17 00:00:00 2001 From: Bruce Beare Date: Fri, 25 Dec 2015 12:00:46 -0800 Subject: Sensor HAL: Add LSM303D accel and orient BUG=NONE Signed-off-by: Bruce Beare Change-Id: I455f8e4e51d6a2e7945b54f3d51613294431ff6f --- peripheral/sensors/edison_arduino/Android.mk | 8 +++ .../sensors/LSM303dAccelerometer.cpp | 78 ++++++++++++++++++++++ .../sensors/LSM303dAccelerometer.hpp | 76 +++++++++++++++++++++ .../edison_arduino/sensors/LSM303dOrientation.cpp | 70 +++++++++++++++++++ .../edison_arduino/sensors/LSM303dOrientation.hpp | 72 ++++++++++++++++++++ 5 files changed, 304 insertions(+) create mode 100644 peripheral/sensors/edison_arduino/sensors/LSM303dAccelerometer.cpp create mode 100644 peripheral/sensors/edison_arduino/sensors/LSM303dAccelerometer.hpp create mode 100644 peripheral/sensors/edison_arduino/sensors/LSM303dOrientation.cpp create mode 100644 peripheral/sensors/edison_arduino/sensors/LSM303dOrientation.hpp (limited to 'peripheral') diff --git a/peripheral/sensors/edison_arduino/Android.mk b/peripheral/sensors/edison_arduino/Android.mk index 9384434..7b2ae83 100644 --- a/peripheral/sensors/edison_arduino/Android.mk +++ b/peripheral/sensors/edison_arduino/Android.mk @@ -37,6 +37,14 @@ ifneq (,$(filter LSM9DS0Accelerometer, $(PLATFORM_SENSOR_LIST))) LOCAL_SRC_FILES += sensors/LSM9DS0Accelerometer.cpp endif +ifneq (,$(filter LSM303dAccelerometer, $(PLATFORM_SENSOR_LIST))) +LOCAL_SRC_FILES += sensors/LSM303dAccelerometer.cpp +endif + +ifneq (,$(filter LSM303dOrientation, $(PLATFORM_SENSOR_LIST))) +LOCAL_SRC_FILES += sensors/LSM303dOrientation.cpp +endif + ifneq (,$(filter GroveLight, $(PLATFORM_SENSOR_LIST))) LOCAL_SRC_FILES += sensors/GroveLight.cpp endif diff --git a/peripheral/sensors/edison_arduino/sensors/LSM303dAccelerometer.cpp b/peripheral/sensors/edison_arduino/sensors/LSM303dAccelerometer.cpp new file mode 100644 index 0000000..dc6cd1c --- /dev/null +++ b/peripheral/sensors/edison_arduino/sensors/LSM303dAccelerometer.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2015 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "LSM303dAccelerometer.hpp" +#include "SensorsHAL.hpp" +#include "SensorUtils.hpp" + +struct sensor_t LSM303dAccelerometer::sensorDescription = { + .name = "LSM303d Accelerometer", + .vendor = "Unknown", + .version = 1, + .handle = -1, + .type = SENSOR_TYPE_ACCELEROMETER, + .maxRange = 16.0f, + .resolution = 0.00003f, + .power = 0.0003f, + .minDelay = 0, + .fifoReservedEventCount = 0, + .fifoMaxEventCount = 0, + .stringType = SENSOR_STRING_TYPE_ACCELEROMETER, + .requiredPermission = "", + .maxDelay = 0, + .flags = SENSOR_FLAG_CONTINUOUS_MODE, + .reserved = {}, +}; + +Sensor * LSM303dAccelerometer::createSensor(int pollFd) { + return new LSM303dAccelerometer(pollFd, SensorUtils::getI2cBusNumber()); +} + +void LSM303dAccelerometer::initModule() { + SensorContext::addSensorModule(&sensorDescription, createSensor); +} + +LSM303dAccelerometer::LSM303dAccelerometer(int pollFd, + int bus, int address, int scale) + : LSM303d(bus, address, scale), pollFd(pollFd), scale(scale) { + this->type = SENSOR_TYPE_ACCELEROMETER; + this->handle = sensorDescription.handle; +} + +LSM303dAccelerometer::~LSM303dAccelerometer() {} + +/* + * The raw data from the X,Y,Z axis are expressed in a 16 bit two's complement + * format. + * 1. We divide the 16bit value by (2**15) to convert it to floating point +-[0..1] + * 2. We multiply by the scaling factor to adjust for max range (2,4,6,8,16 G) + * 3. We multiply by the gravitational accelleration to convert from "g" to m/s**2 + */ +int LSM303dAccelerometer::pollEvents(sensors_event_t* data, int count) { + getAcceleration(); + int16_t *rawdatap = getRawAccelData(); + double conversion_constant = (double)scale * (double)Sensor::kGravitationalAcceleration / pow(2,15); + data->acceleration.x = (double)rawdatap[0] * conversion_constant; + data->acceleration.y = (double)rawdatap[1] * conversion_constant; + data->acceleration.z = (double)rawdatap[2] * conversion_constant; + return 1; +} + +int LSM303dAccelerometer::activate(int handle, int enabled) { + /* start or stop the acquisition thread */ + return activateAcquisitionThread(pollFd, handle, enabled); +} diff --git a/peripheral/sensors/edison_arduino/sensors/LSM303dAccelerometer.hpp b/peripheral/sensors/edison_arduino/sensors/LSM303dAccelerometer.hpp new file mode 100644 index 0000000..9171799 --- /dev/null +++ b/peripheral/sensors/edison_arduino/sensors/LSM303dAccelerometer.hpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2015 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LSM303d_ACCELEROMETER_HPP +#define LSM303d_ACCELEROMETER_HPP + +#include +#include "Sensor.hpp" +#include "lsm303d.h" + +struct sensors_event_t; + +/** + * LSM303dAccelerometer exposes the LSM303d accelerometer sensor + * + * Overrides the pollEvents & activate Sensor methods. + */ +class LSM303dAccelerometer : public Sensor, public upm::LSM303d { + public: + /** + * LSM303dAccelerometer constructor + * @param pollFd poll file descriptor + * @param bus number of the bus + * @param address device address + * @param magAddress magnetometer address + * @param scale Sensor sensitivity scaling + */ + LSM303dAccelerometer(int pollFd, + int bus=0, + int address = LSM303d_ADDR, + int scale = LM303D_SCALE_2G); + + /** + * LSM303dAccelerometer destructor + */ + ~LSM303dAccelerometer() override; + + /** + * Poll for events + * @param data where to store the events + * @param count the number of events returned must be <= to the count + * @return 0 on success and a negative error number otherwise + */ + int pollEvents(sensors_event_t* data, int count) override; + + /** + * Activate the sensor + * @param handle sensor identifier + * @param enabled 1 for enabling and 0 for disabling + * @return 0 on success and a negative error number otherwise + */ + int activate(int handle, int enabled); + + private: + static Sensor * createSensor(int pollFd); + static void initModule() __attribute__((constructor)); + + int pollFd; + int scale; + static struct sensor_t sensorDescription; +}; + +#endif // LSM303d_ACCELEROMETER_HPP diff --git a/peripheral/sensors/edison_arduino/sensors/LSM303dOrientation.cpp b/peripheral/sensors/edison_arduino/sensors/LSM303dOrientation.cpp new file mode 100644 index 0000000..f766dfb --- /dev/null +++ b/peripheral/sensors/edison_arduino/sensors/LSM303dOrientation.cpp @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2015 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "LSM303dOrientation.hpp" +#include "SensorsHAL.hpp" +#include "SensorUtils.hpp" + +struct sensor_t LSM303dOrientation::sensorDescription = { + .name = "LSM303d Orientation", + .vendor = "Unknown", + .version = 1, + .handle = -1, + .type = SENSOR_TYPE_ORIENTATION, + .maxRange = 12.0f, + .resolution = .00003f, + .power = 0.0003f, + .minDelay = 0, + .fifoReservedEventCount = 0, + .fifoMaxEventCount = 0, + .stringType = SENSOR_STRING_TYPE_ORIENTATION, + .requiredPermission = "", + .maxDelay = 0, + .flags = SENSOR_FLAG_CONTINUOUS_MODE, + .reserved = {}, +}; + +Sensor * LSM303dOrientation::createSensor(int pollFd) { + return new LSM303dOrientation(pollFd, SensorUtils::getI2cBusNumber()); +} + +void LSM303dOrientation::initModule() { + SensorContext::addSensorModule(&sensorDescription, createSensor); +} + +LSM303dOrientation::LSM303dOrientation(int pollFd, + int bus, int address) + : LSM303d(bus, address), pollFd(pollFd) { + this->type = SENSOR_TYPE_ORIENTATION; + this->handle = sensorDescription.handle; +} + +LSM303dOrientation::~LSM303dOrientation() {} + +int LSM303dOrientation::pollEvents(sensors_event_t* data, int count) { + getCoordinates(); + int16_t *rawdatap = getRawCoorData(); + data->orientation.x = (double)rawdatap[0]; + data->orientation.y = (double)rawdatap[1]; + data->orientation.z = (double)rawdatap[2]; + return 1; +} + +int LSM303dOrientation::activate(int handle, int enabled) { + /* start or stop the acquisition thread */ + return activateAcquisitionThread(pollFd, handle, enabled); +} diff --git a/peripheral/sensors/edison_arduino/sensors/LSM303dOrientation.hpp b/peripheral/sensors/edison_arduino/sensors/LSM303dOrientation.hpp new file mode 100644 index 0000000..99ba077 --- /dev/null +++ b/peripheral/sensors/edison_arduino/sensors/LSM303dOrientation.hpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2015 Intel Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LSM303d_ORIENTATION_HPP +#define LSM303d_ORIENTATION_HPP + +#include +#include "Sensor.hpp" +#include "lsm303d.h" + +struct sensors_event_t; + +/** + * LSM303dOrientation exposes the LSM303d orientation sensor + * + * Overrides the pollEvents & activate Sensor methods. + */ +class LSM303dOrientation : public Sensor, public upm::LSM303d { + public: + /** + * LSM303dOrientation constructor + * @param pollFd poll file descriptor + * @param bus number of the bus + * @param address device address + */ + LSM303dOrientation(int pollFd, + int bus=0, + int address = LSM303d_ADDR); + + /** + * LSM303dOrientation destructor + */ + ~LSM303dOrientation() override; + + /** + * Poll for events + * @param data where to store the events + * @param count the number of events returned must be <= to the count + * @return 0 on success and a negative error number otherwise + */ + int pollEvents(sensors_event_t* data, int count) override; + + /** + * Activate the sensor + * @param handle sensor identifier + * @param enabled 1 for enabling and 0 for disabling + * @return 0 on success and a negative error number otherwise + */ + int activate(int handle, int enabled); + + private: + static Sensor * createSensor(int pollFd); + static void initModule() __attribute__((constructor)); + + int pollFd; + static struct sensor_t sensorDescription; +}; + +#endif // LSM303d_ORIENTATION_HPP -- cgit v1.2.3