diff options
author | Leon Lindenfelser <llindenfelser@multitech.com> | 2015-12-09 08:27:10 -0600 |
---|---|---|
committer | Leon Lindenfelser <llindenfelser@multitech.com> | 2015-12-09 08:27:10 -0600 |
commit | c29f4bf9b0b628df0749cf668bd27894d678785b (patch) | |
tree | ab848bdf62c205b963140739ed9c8915382f74c4 | |
parent | 5a74150c78737daf2764570835b59b45141f3775 (diff) | |
parent | 61c66f590786b6ff3af5564a86166350d7c4aa31 (diff) | |
download | mtdot-box-evb-factory-firmware-c29f4bf9b0b628df0749cf668bd27894d678785b.tar.gz mtdot-box-evb-factory-firmware-c29f4bf9b0b628df0749cf668bd27894d678785b.tar.bz2 mtdot-box-evb-factory-firmware-c29f4bf9b0b628df0749cf668bd27894d678785b.zip |
Merge branch 'sensors'
-rw-r--r-- | Mode/Mode.cpp | 45 | ||||
-rw-r--r-- | Mode/Mode.h | 14 | ||||
-rw-r--r-- | SensorHandler/SensorHandler.cpp | 194 | ||||
-rw-r--r-- | SensorHandler/SensorHandler.h | 53 |
4 files changed, 302 insertions, 4 deletions
diff --git a/Mode/Mode.cpp b/Mode/Mode.cpp index 70a3f49..a97b226 100644 --- a/Mode/Mode.cpp +++ b/Mode/Mode.cpp @@ -2,6 +2,15 @@ #include "MTSLog.h" /* + * 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 { @@ -25,7 +34,10 @@ Mode::Mode(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, G _power(2), _next_tx(0), _send_data(false), - _gps_available(_gps->gpsDetected()) + _gps_available(_gps->gpsDetected()), + _gpsUART(PA_2, PA_3), + _mdot_gps(&_gpsUART), + _mdot_sensors() {} Mode::~Mode() {} @@ -143,6 +155,16 @@ void Mode::updateData(DataItem& data, DataType type, bool status) { data.power = _power; } +void Mode::updateSensors(SensorItem& data) { + data.accel_data = _mdot_sensors.getAcceleration(); + data.baro_data = _mdot_sensors.getBarometer(); + data.lux_data_raw = _mdot_sensors.getLightRaw(); + data.pressure_raw = _mdot_sensors.getPressureRaw(); + data.light = _mdot_sensors.getLight(); + data.pressure = _mdot_sensors.getPressure(); + data.altitude = _mdot_sensors.getAltitude(); +} + uint32_t Mode::getIndex(DataType type) { uint32_t index = 0; mDot::mdot_file file; @@ -273,6 +295,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_raw; // 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_raw; // 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/Mode/Mode.h b/Mode/Mode.h index 61687f1..ca8433a 100644 --- a/Mode/Mode.h +++ b/Mode/Mode.h @@ -10,6 +10,7 @@ #include "MMA845x.h" #include "MPL3115A2.h" #include "FileName.h" +#include "SensorHandler.h" class Mode { public: @@ -36,9 +37,12 @@ class Mode { typedef struct { MMA845x_DATA accel_data; MPL3115A2_DATA baro_data; - uint16_t lux_data; - uint32_t pressure; - } SensorItem; + uint16_t lux_data_raw; + uint32_t pressure_raw; + float light; + float pressure; + float altitude; + } SensorItem; Mode(DOGS102* lcd, ButtonHandler* buttons, mDot* dot, LoRaHandler* lora, GPSPARSER* gps); ~Mode(); @@ -49,6 +53,7 @@ class Mode { bool deleteDataFile(); bool appendDataFile(const DataItem& data); void updateData(DataItem& data, DataType type, bool status); + void updateSensors(SensorItem& data); uint32_t getIndex(DataType type); std::vector<uint8_t> formatSurveyData(DataItem& data); @@ -72,6 +77,9 @@ class Mode { uint8_t _state; bool _send_data; bool _gps_available; + mts::MTSSerial _gpsUART; + GPSPARSER _mdot_gps; + SensorHandler _mdot_sensors; }; #endif diff --git a/SensorHandler/SensorHandler.cpp b/SensorHandler/SensorHandler.cpp new file mode 100644 index 0000000..89f7d2d --- /dev/null +++ b/SensorHandler/SensorHandler.cpp @@ -0,0 +1,194 @@ +/** + * @file SensorHandler.cpp + * @brief Reads on board sensors... acceleration, pressure, light and temperture. + * @author Leon Lindenfelser + * @version 1.0 + * + */ + +#include "SensorHandler.h" + +SensorHandler::SensorHandler() + : _getSensorThread(&SensorHandler::startSensorThread, this), + _mDoti2c(PC_9,PA_8), + _accelerometer(_mDoti2c,MMA845x::SA0_VSS), + _barometricSensor(_mDoti2c), + _lightSensor(_mDoti2c) +{ + SensorHandler::initSensors(); + _getSensorThread.signal_set(START_THREAD); + return; +} + +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() +{ + uint8_t result; + _getSensorThread.signal_wait(START_THREAD); + 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 + _mutex.lock(); + _accelerometerData = _accelerometer.getXYZ(); + _mutex.unlock(); + + // 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 + _mutex.lock(); + _pressure = _barometricSensor.getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value + _mutex.unlock(); + + // Trigger an 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 and altitude. + _mutex.lock(); + _barometerData = _barometricSensor.getAllData(false); + _mutex.unlock(); + + // Retrieve light level + _mutex.lock(); + _light = _lightSensor.getData(); + _mutex.unlock(); + } +} + +MMA845x_DATA SensorHandler::getAcceleration(){ + MMA845x_DATA data; + _mutex.lock(); + data = _accelerometerData; + _mutex.unlock(); + return data; +} + +float SensorHandler::getLight(){ + float light; + uint16_t whole; + _mutex.lock(); + whole = _light; + _mutex.unlock(); + light = whole * 24 % 100; + light /= 100; + light = light + (whole * 24 / 100); // 16000 lux full scale .24 lux per bit + return light; +} + +uint16_t SensorHandler::getLightRaw(){ + uint16_t light; + _mutex.lock(); + light = _light; + _mutex.unlock(); + return light; +} + +float SensorHandler::getPressure(){ + float pressure; + uint32_t whole; + _mutex.lock(); + whole = _pressure; + _mutex.unlock(); + pressure = (whole & 3) * .25; + pressure = pressure + (whole >> 2); + return pressure; +} + +uint32_t SensorHandler::getPressureRaw(){ + uint32_t pressure; + _mutex.lock(); + pressure = _pressure; + _mutex.unlock(); + return pressure; +} + +float SensorHandler::getTemp(Scale scale){ + float temperature; + uint16_t whole; + _mutex.lock(); + whole = _barometerData._temp; + _mutex.unlock(); + temperature = whole & 0x0f; + temperature *= .0625; + temperature += (whole >> 4); + if(scale == FAHRENHEIT){ + temperature = temperature * 1.8 + 32; + } + return temperature; +} + +float SensorHandler::getAltitude(){ + float altitude; + uint32_t whole; + _mutex.lock(); + whole = _barometerData._baro; + _mutex.unlock(); + whole /= 4096; + altitude = (whole & 0x0f) * .0625; + whole /= 16; + altitude += whole; + return altitude; +} + +MPL3115A2_DATA SensorHandler::getBarometer(){ + MPL3115A2_DATA data; + _mutex.lock(); + data = _barometerData; + _mutex.unlock(); + return data; +} + diff --git a/SensorHandler/SensorHandler.h b/SensorHandler/SensorHandler.h new file mode 100644 index 0000000..01ccf77 --- /dev/null +++ b/SensorHandler/SensorHandler.h @@ -0,0 +1,53 @@ +/** + * @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 "MMA845x.h" +#include "MPL3115A2.h" +#include "ISL29011.h" +#include "DOGS102.h" +#include "rtos.h" +#include <string> +#include <vector> +#define START_THREAD 1 + +class SensorHandler +{ +public: + enum Scale{CELSIUS, FAHRENHEIT}; + SensorHandler(void); + ~SensorHandler(void); + MMA845x_DATA getAcceleration(void); + float getLight(void); + uint16_t getLightRaw(void); + float getPressure(void); + uint32_t getPressureRaw(void); + float getTemp(Scale); + float getAltitude(void); + MPL3115A2_DATA getBarometer(void); + +private: + Thread _getSensorThread; + static void startSensorThread (void const *p); + void readSensors(void); + void initSensors(void); + I2C _mDoti2c; + MMA845x _accelerometer; + MPL3115A2 _barometricSensor; + ISL29011 _lightSensor; + MMA845x_DATA _accelerometerData; + MPL3115A2_DATA _barometerData; + uint16_t _light; + uint32_t _pressure; + Mutex _mutex; +}; + +#endif //SENSORHANDLER_H |