diff options
author | Leon Lindenfelser <llindenfelser@multitech.com> | 2015-12-02 16:40:57 -0600 |
---|---|---|
committer | Leon Lindenfelser <llindenfelser@multitech.com> | 2015-12-02 16:40:57 -0600 |
commit | e0116023a927495c8cfa9249925157228efe3fa9 (patch) | |
tree | 6f8f27c30a6b83c57448bbcb061ec0852607e44b | |
parent | ab6c90739bc2efb5eaf94474be8916fe0f990707 (diff) | |
download | mtdot-box-evb-factory-firmware-e0116023a927495c8cfa9249925157228efe3fa9.tar.gz mtdot-box-evb-factory-firmware-e0116023a927495c8cfa9249925157228efe3fa9.tar.bz2 mtdot-box-evb-factory-firmware-e0116023a927495c8cfa9249925157228efe3fa9.zip |
added sensor handler
-rw-r--r-- | Mode/Mode.cpp | 30 | ||||
-rw-r--r-- | libs/SensorHandler/SensorHandler.cpp | 119 | ||||
-rw-r--r-- | libs/SensorHandler/SensorHandler.h | 38 |
3 files changed, 187 insertions, 0 deletions
diff --git a/Mode/Mode.cpp b/Mode/Mode.cpp index f049e33..73292dc 100644 --- a/Mode/Mode.cpp +++ b/Mode/Mode.cpp @@ -4,6 +4,15 @@ const char* Mode::_file_name = "SurveyData.txt"; /* + * union for converting from 32-bit to 4 8-bit values + */ +union convert32 { + int32_t f_s; // convert from signed 32 bit int + uint32_t f_u; // convert from unsigned 32 bit int + uint8_t t_u[4]; // convert to 8 bit unsigned array +}convertL; + +/* * union for converting from 16- bit to 2 8-bit values */ union convert16 { @@ -271,6 +280,27 @@ std::vector<uint8_t> Mode::formatSurveyData(DataItem& data) { std::vector<uint8_t> Mode::formatSensorData(SensorItem& data) { std::vector<uint8_t> send_data; + send_data.clear(); + send_data.push_back(0x0E); // key for Current Acceleration 3-Axis Value + convertS.f_s = data.accel_data._x *4; // shift data 2 bits while retaining sign + send_data.push_back(convertS.t_u[1]); // get 8 MSB of 14 bit value + convertS.f_s = data.accel_data._y * 4; // shift data 2 bits while retaining sign + send_data.push_back(convertS.t_u[1]); // get 8 MSB of 14 bit value + convertS.f_s = data.accel_data._z * 4; // shift data 2 bits while retaining sign + send_data.push_back(convertS.t_u[1]); // get 8 MSB of 14 bit value + send_data.push_back(0x08); // key for Current Pressure Value + convertL.f_u = data.pressure; // pressure data is 20 bits unsigned + send_data.push_back(convertL.t_u[2]); + send_data.push_back(convertL.t_u[1]); + send_data.push_back(convertL.t_u[0]); + send_data.push_back(0x05); // key for Current Ambient Light Value + convertS.f_u = data.lux_data; // data is 16 bits unsigned + send_data.push_back(convertS.t_u[1]); + send_data.push_back(convertS.t_u[0]); + send_data.push_back(0x0B); // key for Current Temperature Value + convertS.f_s = data.baro_data._temp; // temperature is signed 12 bit + send_data.push_back(convertS.t_u[1]); + send_data.push_back(convertS.t_u[0]); return send_data; } diff --git a/libs/SensorHandler/SensorHandler.cpp b/libs/SensorHandler/SensorHandler.cpp new file mode 100644 index 0000000..151028a --- /dev/null +++ b/libs/SensorHandler/SensorHandler.cpp @@ -0,0 +1,119 @@ +/** + * @file SensorHandler.cpp + * @brief Reads on board sensors... acceleration, pressure, light and temperture. + * @author Leon Lindenfelser + * @version 1.0 + * + */ +#include "SensorHandler.h" +#include "mbed.h" +#include "MMA845x.h" +#include "MPL3115A2.h" +#include "ISL29011.h" +#include "DOGS102.h" +#include "rtos.h" +#include <string> +#include <vector> + +SensorHandler::SensorHandler() + : _getSensorThread(&SensorHandler::startSensorThread,this), + _mDoti2c(PC_9,PA_8), + _accelerometer(_mDoti2c,MMA845x::SA0_VSS), + _barometricSensor(_mDoti2c), + _lightSensor(_mDoti2c) +{ + _getSensorThread.signal_set(START_THREAD); + +} + +SensorHandler::~SensorHandler(void) +{ + _getSensorThread.terminate(); +} + +void SensorHandler::initSensors(){ + // Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz + // normal oversample mode, High pass filter off + _accelerometer.setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, + MMA845x::DR_1_56,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); + + // Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. + //and a sample taken every second when in active mode + _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, + MPL3115A2::AT_1); + // Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution, + // and 16000 lux range + _lightSensor.setMode(ISL29011::ALS_CONT); + _lightSensor.setResolution(ISL29011::ADC_16BIT); + _lightSensor.setRange(ISL29011::RNG_16000); + + // Set the accelerometer for active mode + _accelerometer.activeMode(); + + // Clear the min-max registers in the Barometric Sensor + _barometricSensor.clearMinMaxRegs(); +} + +void SensorHandler::startSensorThread(void const *p) +{ + SensorHandler *instance = (SensorHandler*)p; + instance->readSensors(); +} + +void SensorHandler::readSensors() +{ + while(1){ + // Test Accelerometer XYZ data ready bit to see if acquisition complete + do { + osDelay(100); // allows other threads to process + result = _accelerometer.getStatus(); + } while ((result & MMA845x::XYZDR) == 0 ); + + // Retrieve accelerometer data + accel_data = _accelerometer.getXYZ(); + + // Trigger a Pressure reading + _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, + MPL3115A2::AT_1); + _barometricSensor.triggerOneShot(); + + // Test barometer device status to see if acquisition is complete + do { + osDelay(100); // allows other threads to process + result = _barometricSensor.getStatus(); + } while ((result & MPL3115A2::PTDR) == 0 ); + + // Retrieve barometric pressure + pressure = _barometricSensor.getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value + + // Trigger a Altitude reading + _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, + MPL3115A2::AT_1); + _barometricSensor.triggerOneShot(); + + // Test barometer device status to see if acquisition is complete + do { + osDelay(100); // allows other threads to process + result = _barometricSensor.getStatus(); + } while ((result & MPL3115A2::PTDR) == 0 ); + + // Retrieve temperature + baro_data = _barometricSensor.getAllData(false); + + // Retrieve light level + lux_data = _lightSensor.getData(); + } +} + +MMA845x_DATA getAcceleration(){ +} + +uint16_t getLight(){ +} + +uint_32t getPressure(){ +} + +MPL3115A2_DATA getTemp(){ +} + diff --git a/libs/SensorHandler/SensorHandler.h b/libs/SensorHandler/SensorHandler.h new file mode 100644 index 0000000..23e31a0 --- /dev/null +++ b/libs/SensorHandler/SensorHandler.h @@ -0,0 +1,38 @@ +/** + * @file SensorHandler.h + * @brief Reads on board sensors... acceleration, pressure, light and temperture. + * @author Leon Lindenfelser + * @version 1.0 + * + */ + +#ifndef SENSORHANDLER_H +#define SENSORHANDLER_H + +#include "mbed.h" +#include "rtos.h" +#include "MTSSerial.h" +#include <string> +#include <vector> +#include <ctime> +#define START_THREAD 1 + +class SensorHandler +{ +public: + MMA845x_DATA getAcceleration(); + uint16_t getLight(); + uint_32t getPressure(); + MPL3115A2_DATA getTemp(); + + +private: + Thread _getSensorThread; + void readSensors(void); + I2C _mDoti2c; + MMA845x _accelerometer; + MPL3115A2 _barometricSensor; + ISL29011 _lightSensor; +} + +#endif //SENSORHANDLER_H |