MTDOT-BOX-EVB-Factory-Firmware
Dependencies: NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2
SensorHandler/SensorHandler.cpp@1:71125aa00e33, 2016-02-04 (annotated)
- Committer:
- Mike Fiore
- Date:
- Thu Feb 04 12:36:36 2016 -0600
- Revision:
- 1:71125aa00e33
- Child:
- 12:05435282f899
add rest of source - version 2.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
1:71125aa00e33 | 1 | /* Copyright (c) <2016> <MultiTech Systems>, MIT License |
Mike Fiore |
1:71125aa00e33 | 2 | * |
Mike Fiore |
1:71125aa00e33 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Mike Fiore |
1:71125aa00e33 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
Mike Fiore |
1:71125aa00e33 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
Mike Fiore |
1:71125aa00e33 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
Mike Fiore |
1:71125aa00e33 | 7 | * furnished to do so, subject to the following conditions: |
Mike Fiore |
1:71125aa00e33 | 8 | * |
Mike Fiore |
1:71125aa00e33 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Mike Fiore |
1:71125aa00e33 | 10 | * substantial portions of the Software. |
Mike Fiore |
1:71125aa00e33 | 11 | * |
Mike Fiore |
1:71125aa00e33 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Mike Fiore |
1:71125aa00e33 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Mike Fiore |
1:71125aa00e33 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Mike Fiore |
1:71125aa00e33 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Mike Fiore |
1:71125aa00e33 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Mike Fiore |
1:71125aa00e33 | 17 | */ |
Mike Fiore |
1:71125aa00e33 | 18 | |
Mike Fiore |
1:71125aa00e33 | 19 | /** |
Mike Fiore |
1:71125aa00e33 | 20 | * @file SensorHandler.cpp |
Mike Fiore |
1:71125aa00e33 | 21 | * @brief Reads on board sensors... acceleration, pressure, light and temperture. |
Mike Fiore |
1:71125aa00e33 | 22 | * @author Leon Lindenfelser |
Mike Fiore |
1:71125aa00e33 | 23 | * @version 1.0 |
Mike Fiore |
1:71125aa00e33 | 24 | * |
Mike Fiore |
1:71125aa00e33 | 25 | */ |
Mike Fiore |
1:71125aa00e33 | 26 | |
Mike Fiore |
1:71125aa00e33 | 27 | #include "SensorHandler.h" |
Mike Fiore |
1:71125aa00e33 | 28 | |
Mike Fiore |
1:71125aa00e33 | 29 | SensorHandler::SensorHandler() |
Mike Fiore |
1:71125aa00e33 | 30 | : _getSensorThread(&SensorHandler::startSensorThread, this), |
Mike Fiore |
1:71125aa00e33 | 31 | _mDoti2c(PC_9,PA_8), |
Mike Fiore |
1:71125aa00e33 | 32 | _accelerometer(_mDoti2c,MMA845x::SA0_VSS), |
Mike Fiore |
1:71125aa00e33 | 33 | _barometricSensor(_mDoti2c), |
Mike Fiore |
1:71125aa00e33 | 34 | _lightSensor(_mDoti2c) |
Mike Fiore |
1:71125aa00e33 | 35 | { |
Mike Fiore |
1:71125aa00e33 | 36 | SensorHandler::initSensors(); |
Mike Fiore |
1:71125aa00e33 | 37 | _getSensorThread.signal_set(START_THREAD); |
Mike Fiore |
1:71125aa00e33 | 38 | return; |
Mike Fiore |
1:71125aa00e33 | 39 | } |
Mike Fiore |
1:71125aa00e33 | 40 | |
Mike Fiore |
1:71125aa00e33 | 41 | SensorHandler::~SensorHandler(void) |
Mike Fiore |
1:71125aa00e33 | 42 | { |
Mike Fiore |
1:71125aa00e33 | 43 | _getSensorThread.terminate(); |
Mike Fiore |
1:71125aa00e33 | 44 | } |
Mike Fiore |
1:71125aa00e33 | 45 | |
Mike Fiore |
1:71125aa00e33 | 46 | void SensorHandler::initSensors(){ |
Mike Fiore |
1:71125aa00e33 | 47 | // Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz |
Mike Fiore |
1:71125aa00e33 | 48 | // normal oversample mode, High pass filter off |
Mike Fiore |
1:71125aa00e33 | 49 | _accelerometer.setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, |
Mike Fiore |
1:71125aa00e33 | 50 | MMA845x::DR_6_25,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); |
Mike Fiore |
1:71125aa00e33 | 51 | |
Mike Fiore |
1:71125aa00e33 | 52 | // Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. |
Mike Fiore |
1:71125aa00e33 | 53 | //and a sample taken every second when in active mode |
Mike Fiore |
1:71125aa00e33 | 54 | _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
Mike Fiore |
1:71125aa00e33 | 55 | MPL3115A2::AT_1); |
Mike Fiore |
1:71125aa00e33 | 56 | // Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution, |
Mike Fiore |
1:71125aa00e33 | 57 | // and 16000 lux range |
Mike Fiore |
1:71125aa00e33 | 58 | _lightSensor.setMode(ISL29011::ALS_CONT); |
Mike Fiore |
1:71125aa00e33 | 59 | _lightSensor.setResolution(ISL29011::ADC_16BIT); |
Mike Fiore |
1:71125aa00e33 | 60 | _lightSensor.setRange(ISL29011::RNG_16000); |
Mike Fiore |
1:71125aa00e33 | 61 | |
Mike Fiore |
1:71125aa00e33 | 62 | // Set the accelerometer for active mode |
Mike Fiore |
1:71125aa00e33 | 63 | _accelerometer.activeMode(); |
Mike Fiore |
1:71125aa00e33 | 64 | |
Mike Fiore |
1:71125aa00e33 | 65 | // Clear the min-max registers in the Barometric Sensor |
Mike Fiore |
1:71125aa00e33 | 66 | _barometricSensor.clearMinMaxRegs(); |
Mike Fiore |
1:71125aa00e33 | 67 | } |
Mike Fiore |
1:71125aa00e33 | 68 | |
Mike Fiore |
1:71125aa00e33 | 69 | void SensorHandler::startSensorThread(void const *p) |
Mike Fiore |
1:71125aa00e33 | 70 | { |
Mike Fiore |
1:71125aa00e33 | 71 | SensorHandler *instance = (SensorHandler*)p; |
Mike Fiore |
1:71125aa00e33 | 72 | instance->readSensors(); |
Mike Fiore |
1:71125aa00e33 | 73 | } |
Mike Fiore |
1:71125aa00e33 | 74 | |
Mike Fiore |
1:71125aa00e33 | 75 | void SensorHandler::readSensors() |
Mike Fiore |
1:71125aa00e33 | 76 | { |
Mike Fiore |
1:71125aa00e33 | 77 | uint8_t result; |
Mike Fiore |
1:71125aa00e33 | 78 | Timer timer; |
Mike Fiore |
1:71125aa00e33 | 79 | timer.start(); |
Mike Fiore |
1:71125aa00e33 | 80 | |
Mike Fiore |
1:71125aa00e33 | 81 | _getSensorThread.signal_wait(START_THREAD); |
Mike Fiore |
1:71125aa00e33 | 82 | while(1){ |
Mike Fiore |
1:71125aa00e33 | 83 | // Test Accelerometer XYZ data ready bit to see if acquisition complete |
Mike Fiore |
1:71125aa00e33 | 84 | timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 85 | do { |
Mike Fiore |
1:71125aa00e33 | 86 | osDelay(20); // allows other threads to process |
Mike Fiore |
1:71125aa00e33 | 87 | result = _accelerometer.getStatus(); |
Mike Fiore |
1:71125aa00e33 | 88 | if((result & MMA845x::XYZDR) != 0 ){ |
Mike Fiore |
1:71125aa00e33 | 89 | // Retrieve accelerometer data |
Mike Fiore |
1:71125aa00e33 | 90 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 91 | _accelerometerData = _accelerometer.getXYZ(); |
Mike Fiore |
1:71125aa00e33 | 92 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 93 | } |
Mike Fiore |
1:71125aa00e33 | 94 | } while (((result & MMA845x::XYZDR) == 0 ) && (timer.read_ms() < 1000)); |
Mike Fiore |
1:71125aa00e33 | 95 | |
Mike Fiore |
1:71125aa00e33 | 96 | |
Mike Fiore |
1:71125aa00e33 | 97 | // Trigger a Pressure reading |
Mike Fiore |
1:71125aa00e33 | 98 | _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
Mike Fiore |
1:71125aa00e33 | 99 | MPL3115A2::AT_1); |
Mike Fiore |
1:71125aa00e33 | 100 | _barometricSensor.triggerOneShot(); |
Mike Fiore |
1:71125aa00e33 | 101 | |
Mike Fiore |
1:71125aa00e33 | 102 | // Test barometer device status to see if acquisition is complete |
Mike Fiore |
1:71125aa00e33 | 103 | timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 104 | do { |
Mike Fiore |
1:71125aa00e33 | 105 | osDelay(20); // allows other threads to process |
Mike Fiore |
1:71125aa00e33 | 106 | result = _barometricSensor.getStatus(); |
Mike Fiore |
1:71125aa00e33 | 107 | if((result & MPL3115A2::PTDR) != 0){ |
Mike Fiore |
1:71125aa00e33 | 108 | // Retrieve barometric pressure |
Mike Fiore |
1:71125aa00e33 | 109 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 110 | _pressure = _barometricSensor.getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value |
Mike Fiore |
1:71125aa00e33 | 111 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 112 | } |
Mike Fiore |
1:71125aa00e33 | 113 | } while (((result & MPL3115A2::PTDR) == 0) && (timer.read_ms() < 100)); |
Mike Fiore |
1:71125aa00e33 | 114 | |
Mike Fiore |
1:71125aa00e33 | 115 | |
Mike Fiore |
1:71125aa00e33 | 116 | // Trigger an Altitude reading |
Mike Fiore |
1:71125aa00e33 | 117 | _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, |
Mike Fiore |
1:71125aa00e33 | 118 | MPL3115A2::AT_1); |
Mike Fiore |
1:71125aa00e33 | 119 | _barometricSensor.triggerOneShot(); |
Mike Fiore |
1:71125aa00e33 | 120 | |
Mike Fiore |
1:71125aa00e33 | 121 | // Test barometer device status to see if acquisition is complete |
Mike Fiore |
1:71125aa00e33 | 122 | timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 123 | do { |
Mike Fiore |
1:71125aa00e33 | 124 | osDelay(20); // allows other threads to process |
Mike Fiore |
1:71125aa00e33 | 125 | result = _barometricSensor.getStatus(); |
Mike Fiore |
1:71125aa00e33 | 126 | if((result & MPL3115A2::PTDR) != 0 ){ |
Mike Fiore |
1:71125aa00e33 | 127 | // Retrieve temperature and altitude. |
Mike Fiore |
1:71125aa00e33 | 128 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 129 | _barometerData = _barometricSensor.getAllData(false); |
Mike Fiore |
1:71125aa00e33 | 130 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 131 | } |
Mike Fiore |
1:71125aa00e33 | 132 | } while (((result & MPL3115A2::PTDR) == 0 ) && (timer.read_ms() < 100)); |
Mike Fiore |
1:71125aa00e33 | 133 | |
Mike Fiore |
1:71125aa00e33 | 134 | // Retrieve light level |
Mike Fiore |
1:71125aa00e33 | 135 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 136 | _light = _lightSensor.getData(); |
Mike Fiore |
1:71125aa00e33 | 137 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 138 | osDelay(100); // allows other threads to process |
Mike Fiore |
1:71125aa00e33 | 139 | } |
Mike Fiore |
1:71125aa00e33 | 140 | } |
Mike Fiore |
1:71125aa00e33 | 141 | |
Mike Fiore |
1:71125aa00e33 | 142 | MMA845x_DATA SensorHandler::getAcceleration(){ |
Mike Fiore |
1:71125aa00e33 | 143 | MMA845x_DATA data; |
Mike Fiore |
1:71125aa00e33 | 144 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 145 | data = _accelerometerData; |
Mike Fiore |
1:71125aa00e33 | 146 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 147 | return data; |
Mike Fiore |
1:71125aa00e33 | 148 | } |
Mike Fiore |
1:71125aa00e33 | 149 | |
Mike Fiore |
1:71125aa00e33 | 150 | float SensorHandler::getLight(){ |
Mike Fiore |
1:71125aa00e33 | 151 | float light; |
Mike Fiore |
1:71125aa00e33 | 152 | uint16_t whole; |
Mike Fiore |
1:71125aa00e33 | 153 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 154 | whole = _light; |
Mike Fiore |
1:71125aa00e33 | 155 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 156 | light = whole * 24 % 100; |
Mike Fiore |
1:71125aa00e33 | 157 | light /= 100; |
Mike Fiore |
1:71125aa00e33 | 158 | light = light + (whole * 24 / 100); // 16000 lux full scale .24 lux per bit |
Mike Fiore |
1:71125aa00e33 | 159 | return light; |
Mike Fiore |
1:71125aa00e33 | 160 | } |
Mike Fiore |
1:71125aa00e33 | 161 | |
Mike Fiore |
1:71125aa00e33 | 162 | uint16_t SensorHandler::getLightRaw(){ |
Mike Fiore |
1:71125aa00e33 | 163 | uint16_t light; |
Mike Fiore |
1:71125aa00e33 | 164 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 165 | light = _light; |
Mike Fiore |
1:71125aa00e33 | 166 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 167 | return light; |
Mike Fiore |
1:71125aa00e33 | 168 | } |
Mike Fiore |
1:71125aa00e33 | 169 | |
Mike Fiore |
1:71125aa00e33 | 170 | float SensorHandler::getPressure(){ |
Mike Fiore |
1:71125aa00e33 | 171 | float pressure; |
Mike Fiore |
1:71125aa00e33 | 172 | uint32_t whole; |
Mike Fiore |
1:71125aa00e33 | 173 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 174 | whole = _pressure; |
Mike Fiore |
1:71125aa00e33 | 175 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 176 | pressure = (whole & 3) * .25; |
Mike Fiore |
1:71125aa00e33 | 177 | pressure = pressure + (whole >> 2); |
Mike Fiore |
1:71125aa00e33 | 178 | return pressure; |
Mike Fiore |
1:71125aa00e33 | 179 | } |
Mike Fiore |
1:71125aa00e33 | 180 | |
Mike Fiore |
1:71125aa00e33 | 181 | uint32_t SensorHandler::getPressureRaw(){ |
Mike Fiore |
1:71125aa00e33 | 182 | uint32_t pressure; |
Mike Fiore |
1:71125aa00e33 | 183 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 184 | pressure = _pressure; |
Mike Fiore |
1:71125aa00e33 | 185 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 186 | return pressure; |
Mike Fiore |
1:71125aa00e33 | 187 | } |
Mike Fiore |
1:71125aa00e33 | 188 | |
Mike Fiore |
1:71125aa00e33 | 189 | float SensorHandler::getTemp(Scale scale){ |
Mike Fiore |
1:71125aa00e33 | 190 | float temperature; |
Mike Fiore |
1:71125aa00e33 | 191 | uint16_t whole; |
Mike Fiore |
1:71125aa00e33 | 192 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 193 | whole = _barometerData._temp; |
Mike Fiore |
1:71125aa00e33 | 194 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 195 | temperature = whole & 0x0f; |
Mike Fiore |
1:71125aa00e33 | 196 | temperature *= .0625; |
Mike Fiore |
1:71125aa00e33 | 197 | temperature += (whole >> 4); |
Mike Fiore |
1:71125aa00e33 | 198 | if(scale == FAHRENHEIT){ |
Mike Fiore |
1:71125aa00e33 | 199 | temperature = temperature * 1.8 + 32; |
Mike Fiore |
1:71125aa00e33 | 200 | } |
Mike Fiore |
1:71125aa00e33 | 201 | return temperature; |
Mike Fiore |
1:71125aa00e33 | 202 | } |
Mike Fiore |
1:71125aa00e33 | 203 | |
Mike Fiore |
1:71125aa00e33 | 204 | float SensorHandler::getAltitude(){ |
Mike Fiore |
1:71125aa00e33 | 205 | float altitude; |
Mike Fiore |
1:71125aa00e33 | 206 | uint32_t whole; |
Mike Fiore |
1:71125aa00e33 | 207 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 208 | whole = _barometerData._baro; |
Mike Fiore |
1:71125aa00e33 | 209 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 210 | whole /= 4096; |
Mike Fiore |
1:71125aa00e33 | 211 | altitude = (whole & 0x0f) * .0625; |
Mike Fiore |
1:71125aa00e33 | 212 | whole /= 16; |
Mike Fiore |
1:71125aa00e33 | 213 | altitude += whole; |
Mike Fiore |
1:71125aa00e33 | 214 | return altitude; |
Mike Fiore |
1:71125aa00e33 | 215 | } |
Mike Fiore |
1:71125aa00e33 | 216 | |
Mike Fiore |
1:71125aa00e33 | 217 | MPL3115A2_DATA SensorHandler::getBarometer(){ |
Mike Fiore |
1:71125aa00e33 | 218 | MPL3115A2_DATA data; |
Mike Fiore |
1:71125aa00e33 | 219 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 220 | data = _barometerData; |
Mike Fiore |
1:71125aa00e33 | 221 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 222 | return data; |
Mike Fiore |
1:71125aa00e33 | 223 | } |
Mike Fiore |
1:71125aa00e33 | 224 |