MTDOT-BOX-EVB-Factory-Firmware
Dependencies: NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2
SensorHandler/SensorHandler.cpp@16:e76cec0eec43, 2019-03-14 (annotated)
- Committer:
- jenkins@jenkinsdm1
- Date:
- Thu Mar 14 08:23:20 2019 -0500
- Revision:
- 16:e76cec0eec43
- Parent:
- 12:05435282f899
dotbox-firmware revision 3.1.2
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 | |
jenkins@jenkinsdm1 | 12:05435282f899 | 29 | SensorHandler::SensorHandler(I2C& i2c) |
jenkins@jenkinsdm1 | 12:05435282f899 | 30 | : _getSensorThread(&SensorHandler::startSensorThread, this) |
jenkins@jenkinsdm1 | 12:05435282f899 | 31 | , _accelerometer(i2c, MMA845x::SA0_VSS) |
jenkins@jenkinsdm1 | 12:05435282f899 | 32 | , _barometricSensor(i2c) |
jenkins@jenkinsdm1 | 12:05435282f899 | 33 | , _lightSensor(i2c) |
Mike Fiore |
1:71125aa00e33 | 34 | { |
Mike Fiore |
1:71125aa00e33 | 35 | SensorHandler::initSensors(); |
Mike Fiore |
1:71125aa00e33 | 36 | _getSensorThread.signal_set(START_THREAD); |
Mike Fiore |
1:71125aa00e33 | 37 | return; |
Mike Fiore |
1:71125aa00e33 | 38 | } |
Mike Fiore |
1:71125aa00e33 | 39 | |
Mike Fiore |
1:71125aa00e33 | 40 | SensorHandler::~SensorHandler(void) |
Mike Fiore |
1:71125aa00e33 | 41 | { |
Mike Fiore |
1:71125aa00e33 | 42 | _getSensorThread.terminate(); |
Mike Fiore |
1:71125aa00e33 | 43 | } |
Mike Fiore |
1:71125aa00e33 | 44 | |
Mike Fiore |
1:71125aa00e33 | 45 | void SensorHandler::initSensors(){ |
Mike Fiore |
1:71125aa00e33 | 46 | // Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz |
Mike Fiore |
1:71125aa00e33 | 47 | // normal oversample mode, High pass filter off |
Mike Fiore |
1:71125aa00e33 | 48 | _accelerometer.setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, |
Mike Fiore |
1:71125aa00e33 | 49 | MMA845x::DR_6_25,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); |
Mike Fiore |
1:71125aa00e33 | 50 | |
Mike Fiore |
1:71125aa00e33 | 51 | // Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. |
Mike Fiore |
1:71125aa00e33 | 52 | //and a sample taken every second when in active mode |
Mike Fiore |
1:71125aa00e33 | 53 | _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
Mike Fiore |
1:71125aa00e33 | 54 | MPL3115A2::AT_1); |
Mike Fiore |
1:71125aa00e33 | 55 | // Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution, |
Mike Fiore |
1:71125aa00e33 | 56 | // and 16000 lux range |
Mike Fiore |
1:71125aa00e33 | 57 | _lightSensor.setMode(ISL29011::ALS_CONT); |
Mike Fiore |
1:71125aa00e33 | 58 | _lightSensor.setResolution(ISL29011::ADC_16BIT); |
Mike Fiore |
1:71125aa00e33 | 59 | _lightSensor.setRange(ISL29011::RNG_16000); |
Mike Fiore |
1:71125aa00e33 | 60 | |
Mike Fiore |
1:71125aa00e33 | 61 | // Set the accelerometer for active mode |
Mike Fiore |
1:71125aa00e33 | 62 | _accelerometer.activeMode(); |
Mike Fiore |
1:71125aa00e33 | 63 | |
Mike Fiore |
1:71125aa00e33 | 64 | // Clear the min-max registers in the Barometric Sensor |
jenkins@jenkinsdm1 | 12:05435282f899 | 65 | _barometricSensor.clearMinMaxRegs(); |
Mike Fiore |
1:71125aa00e33 | 66 | } |
Mike Fiore |
1:71125aa00e33 | 67 | |
Mike Fiore |
1:71125aa00e33 | 68 | void SensorHandler::startSensorThread(void const *p) |
Mike Fiore |
1:71125aa00e33 | 69 | { |
Mike Fiore |
1:71125aa00e33 | 70 | SensorHandler *instance = (SensorHandler*)p; |
Mike Fiore |
1:71125aa00e33 | 71 | instance->readSensors(); |
Mike Fiore |
1:71125aa00e33 | 72 | } |
Mike Fiore |
1:71125aa00e33 | 73 | |
Mike Fiore |
1:71125aa00e33 | 74 | void SensorHandler::readSensors() |
Mike Fiore |
1:71125aa00e33 | 75 | { |
Mike Fiore |
1:71125aa00e33 | 76 | uint8_t result; |
Mike Fiore |
1:71125aa00e33 | 77 | Timer timer; |
Mike Fiore |
1:71125aa00e33 | 78 | timer.start(); |
Mike Fiore |
1:71125aa00e33 | 79 | |
Mike Fiore |
1:71125aa00e33 | 80 | _getSensorThread.signal_wait(START_THREAD); |
Mike Fiore |
1:71125aa00e33 | 81 | while(1){ |
Mike Fiore |
1:71125aa00e33 | 82 | // Test Accelerometer XYZ data ready bit to see if acquisition complete |
Mike Fiore |
1:71125aa00e33 | 83 | timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 84 | do { |
Mike Fiore |
1:71125aa00e33 | 85 | osDelay(20); // allows other threads to process |
Mike Fiore |
1:71125aa00e33 | 86 | result = _accelerometer.getStatus(); |
Mike Fiore |
1:71125aa00e33 | 87 | if((result & MMA845x::XYZDR) != 0 ){ |
Mike Fiore |
1:71125aa00e33 | 88 | // Retrieve accelerometer data |
Mike Fiore |
1:71125aa00e33 | 89 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 90 | _accelerometerData = _accelerometer.getXYZ(); |
Mike Fiore |
1:71125aa00e33 | 91 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 92 | } |
Mike Fiore |
1:71125aa00e33 | 93 | } while (((result & MMA845x::XYZDR) == 0 ) && (timer.read_ms() < 1000)); |
Mike Fiore |
1:71125aa00e33 | 94 | |
Mike Fiore |
1:71125aa00e33 | 95 | |
Mike Fiore |
1:71125aa00e33 | 96 | // Trigger a Pressure reading |
Mike Fiore |
1:71125aa00e33 | 97 | _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, |
Mike Fiore |
1:71125aa00e33 | 98 | MPL3115A2::AT_1); |
Mike Fiore |
1:71125aa00e33 | 99 | _barometricSensor.triggerOneShot(); |
Mike Fiore |
1:71125aa00e33 | 100 | |
Mike Fiore |
1:71125aa00e33 | 101 | // Test barometer device status to see if acquisition is complete |
Mike Fiore |
1:71125aa00e33 | 102 | timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 103 | do { |
Mike Fiore |
1:71125aa00e33 | 104 | osDelay(20); // allows other threads to process |
Mike Fiore |
1:71125aa00e33 | 105 | result = _barometricSensor.getStatus(); |
Mike Fiore |
1:71125aa00e33 | 106 | if((result & MPL3115A2::PTDR) != 0){ |
Mike Fiore |
1:71125aa00e33 | 107 | // Retrieve barometric pressure |
Mike Fiore |
1:71125aa00e33 | 108 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 109 | _pressure = _barometricSensor.getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value |
Mike Fiore |
1:71125aa00e33 | 110 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 111 | } |
Mike Fiore |
1:71125aa00e33 | 112 | } while (((result & MPL3115A2::PTDR) == 0) && (timer.read_ms() < 100)); |
Mike Fiore |
1:71125aa00e33 | 113 | |
Mike Fiore |
1:71125aa00e33 | 114 | |
Mike Fiore |
1:71125aa00e33 | 115 | // Trigger an Altitude reading |
Mike Fiore |
1:71125aa00e33 | 116 | _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, |
Mike Fiore |
1:71125aa00e33 | 117 | MPL3115A2::AT_1); |
Mike Fiore |
1:71125aa00e33 | 118 | _barometricSensor.triggerOneShot(); |
Mike Fiore |
1:71125aa00e33 | 119 | |
Mike Fiore |
1:71125aa00e33 | 120 | // Test barometer device status to see if acquisition is complete |
Mike Fiore |
1:71125aa00e33 | 121 | timer.reset(); |
Mike Fiore |
1:71125aa00e33 | 122 | do { |
Mike Fiore |
1:71125aa00e33 | 123 | osDelay(20); // allows other threads to process |
Mike Fiore |
1:71125aa00e33 | 124 | result = _barometricSensor.getStatus(); |
Mike Fiore |
1:71125aa00e33 | 125 | if((result & MPL3115A2::PTDR) != 0 ){ |
Mike Fiore |
1:71125aa00e33 | 126 | // Retrieve temperature and altitude. |
Mike Fiore |
1:71125aa00e33 | 127 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 128 | _barometerData = _barometricSensor.getAllData(false); |
Mike Fiore |
1:71125aa00e33 | 129 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 130 | } |
Mike Fiore |
1:71125aa00e33 | 131 | } while (((result & MPL3115A2::PTDR) == 0 ) && (timer.read_ms() < 100)); |
jenkins@jenkinsdm1 | 12:05435282f899 | 132 | |
Mike Fiore |
1:71125aa00e33 | 133 | // Retrieve light level |
Mike Fiore |
1:71125aa00e33 | 134 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 135 | _light = _lightSensor.getData(); |
Mike Fiore |
1:71125aa00e33 | 136 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 137 | osDelay(100); // allows other threads to process |
Mike Fiore |
1:71125aa00e33 | 138 | } |
Mike Fiore |
1:71125aa00e33 | 139 | } |
Mike Fiore |
1:71125aa00e33 | 140 | |
Mike Fiore |
1:71125aa00e33 | 141 | MMA845x_DATA SensorHandler::getAcceleration(){ |
Mike Fiore |
1:71125aa00e33 | 142 | MMA845x_DATA data; |
Mike Fiore |
1:71125aa00e33 | 143 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 144 | data = _accelerometerData; |
Mike Fiore |
1:71125aa00e33 | 145 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 146 | return data; |
Mike Fiore |
1:71125aa00e33 | 147 | } |
Mike Fiore |
1:71125aa00e33 | 148 | |
Mike Fiore |
1:71125aa00e33 | 149 | float SensorHandler::getLight(){ |
Mike Fiore |
1:71125aa00e33 | 150 | float light; |
Mike Fiore |
1:71125aa00e33 | 151 | uint16_t whole; |
Mike Fiore |
1:71125aa00e33 | 152 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 153 | whole = _light; |
Mike Fiore |
1:71125aa00e33 | 154 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 155 | light = whole * 24 % 100; |
Mike Fiore |
1:71125aa00e33 | 156 | light /= 100; |
Mike Fiore |
1:71125aa00e33 | 157 | light = light + (whole * 24 / 100); // 16000 lux full scale .24 lux per bit |
Mike Fiore |
1:71125aa00e33 | 158 | return light; |
Mike Fiore |
1:71125aa00e33 | 159 | } |
Mike Fiore |
1:71125aa00e33 | 160 | |
Mike Fiore |
1:71125aa00e33 | 161 | uint16_t SensorHandler::getLightRaw(){ |
Mike Fiore |
1:71125aa00e33 | 162 | uint16_t light; |
Mike Fiore |
1:71125aa00e33 | 163 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 164 | light = _light; |
Mike Fiore |
1:71125aa00e33 | 165 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 166 | return light; |
Mike Fiore |
1:71125aa00e33 | 167 | } |
Mike Fiore |
1:71125aa00e33 | 168 | |
Mike Fiore |
1:71125aa00e33 | 169 | float SensorHandler::getPressure(){ |
Mike Fiore |
1:71125aa00e33 | 170 | float pressure; |
Mike Fiore |
1:71125aa00e33 | 171 | uint32_t whole; |
Mike Fiore |
1:71125aa00e33 | 172 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 173 | whole = _pressure; |
Mike Fiore |
1:71125aa00e33 | 174 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 175 | pressure = (whole & 3) * .25; |
Mike Fiore |
1:71125aa00e33 | 176 | pressure = pressure + (whole >> 2); |
Mike Fiore |
1:71125aa00e33 | 177 | return pressure; |
Mike Fiore |
1:71125aa00e33 | 178 | } |
Mike Fiore |
1:71125aa00e33 | 179 | |
Mike Fiore |
1:71125aa00e33 | 180 | uint32_t SensorHandler::getPressureRaw(){ |
Mike Fiore |
1:71125aa00e33 | 181 | uint32_t pressure; |
Mike Fiore |
1:71125aa00e33 | 182 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 183 | pressure = _pressure; |
Mike Fiore |
1:71125aa00e33 | 184 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 185 | return pressure; |
Mike Fiore |
1:71125aa00e33 | 186 | } |
Mike Fiore |
1:71125aa00e33 | 187 | |
Mike Fiore |
1:71125aa00e33 | 188 | float SensorHandler::getTemp(Scale scale){ |
Mike Fiore |
1:71125aa00e33 | 189 | float temperature; |
Mike Fiore |
1:71125aa00e33 | 190 | uint16_t whole; |
Mike Fiore |
1:71125aa00e33 | 191 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 192 | whole = _barometerData._temp; |
Mike Fiore |
1:71125aa00e33 | 193 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 194 | temperature = whole & 0x0f; |
Mike Fiore |
1:71125aa00e33 | 195 | temperature *= .0625; |
Mike Fiore |
1:71125aa00e33 | 196 | temperature += (whole >> 4); |
Mike Fiore |
1:71125aa00e33 | 197 | if(scale == FAHRENHEIT){ |
Mike Fiore |
1:71125aa00e33 | 198 | temperature = temperature * 1.8 + 32; |
Mike Fiore |
1:71125aa00e33 | 199 | } |
Mike Fiore |
1:71125aa00e33 | 200 | return temperature; |
Mike Fiore |
1:71125aa00e33 | 201 | } |
Mike Fiore |
1:71125aa00e33 | 202 | |
Mike Fiore |
1:71125aa00e33 | 203 | float SensorHandler::getAltitude(){ |
Mike Fiore |
1:71125aa00e33 | 204 | float altitude; |
Mike Fiore |
1:71125aa00e33 | 205 | uint32_t whole; |
Mike Fiore |
1:71125aa00e33 | 206 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 207 | whole = _barometerData._baro; |
Mike Fiore |
1:71125aa00e33 | 208 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 209 | whole /= 4096; |
Mike Fiore |
1:71125aa00e33 | 210 | altitude = (whole & 0x0f) * .0625; |
Mike Fiore |
1:71125aa00e33 | 211 | whole /= 16; |
Mike Fiore |
1:71125aa00e33 | 212 | altitude += whole; |
Mike Fiore |
1:71125aa00e33 | 213 | return altitude; |
Mike Fiore |
1:71125aa00e33 | 214 | } |
Mike Fiore |
1:71125aa00e33 | 215 | |
Mike Fiore |
1:71125aa00e33 | 216 | MPL3115A2_DATA SensorHandler::getBarometer(){ |
Mike Fiore |
1:71125aa00e33 | 217 | MPL3115A2_DATA data; |
Mike Fiore |
1:71125aa00e33 | 218 | _mutex.lock(); |
Mike Fiore |
1:71125aa00e33 | 219 | data = _barometerData; |
Mike Fiore |
1:71125aa00e33 | 220 | _mutex.unlock(); |
Mike Fiore |
1:71125aa00e33 | 221 | return data; |
Mike Fiore |
1:71125aa00e33 | 222 | } |
Mike Fiore |
1:71125aa00e33 | 223 |