Factory firmware for the MultiTech Dotbox (MTDOT-BOX) and EVB (MTDOT-EVB) products.
Dependencies: NCP5623B GpsParser ISL29011 libmDot-mbed5 MTS-Serial MMA845x DOGS102 MPL3115A2
SensorHandler.cpp
00001 /* Copyright (c) <2016> <MultiTech Systems>, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 /** 00020 * @file SensorHandler.cpp 00021 * @brief Reads on board sensors... acceleration, pressure, light and temperture. 00022 * @author Leon Lindenfelser 00023 * @version 1.0 00024 * 00025 */ 00026 00027 #include "SensorHandler.h" 00028 00029 SensorHandler::SensorHandler(I2C& i2c) 00030 : _getSensorThread(&SensorHandler::startSensorThread, this) 00031 , _accelerometer(i2c, MMA845x::SA0_VSS) 00032 , _barometricSensor(i2c) 00033 , _lightSensor(i2c) 00034 { 00035 SensorHandler::initSensors(); 00036 _getSensorThread.signal_set(START_THREAD); 00037 return; 00038 } 00039 00040 SensorHandler::~SensorHandler(void) 00041 { 00042 _getSensorThread.terminate(); 00043 } 00044 00045 void SensorHandler::initSensors(){ 00046 // Setup the Accelerometer for 8g range, 14 bit resolution, Noise reduction off, sample rate 1.56 Hz 00047 // normal oversample mode, High pass filter off 00048 _accelerometer.setCommonParameters(MMA845x::RANGE_8g,MMA845x::RES_MAX,MMA845x::LN_OFF, 00049 MMA845x::DR_6_25,MMA845x::OS_NORMAL,MMA845x::HPF_OFF ); 00050 00051 // Setup the Barometric sensor for post processed Ambient pressure, 4 samples per data acquisition. 00052 //and a sample taken every second when in active mode 00053 _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, 00054 MPL3115A2::AT_1); 00055 // Setup the Ambient Light Sensor for continuous Ambient Light Sensing, 16 bit resolution, 00056 // and 16000 lux range 00057 _lightSensor.setMode(ISL29011::ALS_CONT); 00058 _lightSensor.setResolution(ISL29011::ADC_16BIT); 00059 _lightSensor.setRange(ISL29011::RNG_16000); 00060 00061 // Set the accelerometer for active mode 00062 _accelerometer.activeMode(); 00063 00064 // Clear the min-max registers in the Barometric Sensor 00065 _barometricSensor.clearMinMaxRegs(); 00066 } 00067 00068 void SensorHandler::startSensorThread(void const *p) 00069 { 00070 SensorHandler *instance = (SensorHandler*)p; 00071 instance->readSensors(); 00072 } 00073 00074 void SensorHandler::readSensors() 00075 { 00076 uint8_t result; 00077 Timer timer; 00078 timer.start(); 00079 00080 _getSensorThread.signal_wait(START_THREAD); 00081 while(1){ 00082 // Test Accelerometer XYZ data ready bit to see if acquisition complete 00083 timer.reset(); 00084 do { 00085 osDelay(20); // allows other threads to process 00086 result = _accelerometer.getStatus(); 00087 if((result & MMA845x::XYZDR) != 0 ){ 00088 // Retrieve accelerometer data 00089 _mutex.lock(); 00090 _accelerometerData = _accelerometer.getXYZ(); 00091 _mutex.unlock(); 00092 } 00093 } while (((result & MMA845x::XYZDR) == 0 ) && (timer.read_ms() < 1000)); 00094 00095 00096 // Trigger a Pressure reading 00097 _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_BAROMETER, MPL3115A2::OR_16, 00098 MPL3115A2::AT_1); 00099 _barometricSensor.triggerOneShot(); 00100 00101 // Test barometer device status to see if acquisition is complete 00102 timer.reset(); 00103 do { 00104 osDelay(20); // allows other threads to process 00105 result = _barometricSensor.getStatus(); 00106 if((result & MPL3115A2::PTDR) != 0){ 00107 // Retrieve barometric pressure 00108 _mutex.lock(); 00109 _pressure = _barometricSensor.getBaroData() >> 12; // convert 32 bit signed to 20 bit unsigned value 00110 _mutex.unlock(); 00111 } 00112 } while (((result & MPL3115A2::PTDR) == 0) && (timer.read_ms() < 100)); 00113 00114 00115 // Trigger an Altitude reading 00116 _barometricSensor.setParameters(MPL3115A2::DATA_NORMAL, MPL3115A2::DM_ALTIMETER, MPL3115A2::OR_16, 00117 MPL3115A2::AT_1); 00118 _barometricSensor.triggerOneShot(); 00119 00120 // Test barometer device status to see if acquisition is complete 00121 timer.reset(); 00122 do { 00123 osDelay(20); // allows other threads to process 00124 result = _barometricSensor.getStatus(); 00125 if((result & MPL3115A2::PTDR) != 0 ){ 00126 // Retrieve temperature and altitude. 00127 _mutex.lock(); 00128 _barometerData = _barometricSensor.getAllData(false); 00129 _mutex.unlock(); 00130 } 00131 } while (((result & MPL3115A2::PTDR) == 0 ) && (timer.read_ms() < 100)); 00132 00133 // Retrieve light level 00134 _mutex.lock(); 00135 _light = _lightSensor.getData(); 00136 _mutex.unlock(); 00137 osDelay(100); // allows other threads to process 00138 } 00139 } 00140 00141 MMA845x_DATA SensorHandler::getAcceleration(){ 00142 MMA845x_DATA data; 00143 _mutex.lock(); 00144 data = _accelerometerData; 00145 _mutex.unlock(); 00146 return data; 00147 } 00148 00149 float SensorHandler::getLight(){ 00150 float light; 00151 uint16_t whole; 00152 _mutex.lock(); 00153 whole = _light; 00154 _mutex.unlock(); 00155 light = whole * 24 % 100; 00156 light /= 100; 00157 light = light + (whole * 24 / 100); // 16000 lux full scale .24 lux per bit 00158 return light; 00159 } 00160 00161 uint16_t SensorHandler::getLightRaw(){ 00162 uint16_t light; 00163 _mutex.lock(); 00164 light = _light; 00165 _mutex.unlock(); 00166 return light; 00167 } 00168 00169 float SensorHandler::getPressure(){ 00170 float pressure; 00171 uint32_t whole; 00172 _mutex.lock(); 00173 whole = _pressure; 00174 _mutex.unlock(); 00175 pressure = (whole & 3) * .25; 00176 pressure = pressure + (whole >> 2); 00177 return pressure; 00178 } 00179 00180 uint32_t SensorHandler::getPressureRaw(){ 00181 uint32_t pressure; 00182 _mutex.lock(); 00183 pressure = _pressure; 00184 _mutex.unlock(); 00185 return pressure; 00186 } 00187 00188 float SensorHandler::getTemp(Scale scale){ 00189 float temperature; 00190 uint16_t whole; 00191 _mutex.lock(); 00192 whole = _barometerData._temp; 00193 _mutex.unlock(); 00194 temperature = whole & 0x0f; 00195 temperature *= .0625; 00196 temperature += (whole >> 4); 00197 if(scale == FAHRENHEIT){ 00198 temperature = temperature * 1.8 + 32; 00199 } 00200 return temperature; 00201 } 00202 00203 float SensorHandler::getAltitude(){ 00204 float altitude; 00205 uint32_t whole; 00206 _mutex.lock(); 00207 whole = _barometerData._baro; 00208 _mutex.unlock(); 00209 whole /= 4096; 00210 altitude = (whole & 0x0f) * .0625; 00211 whole /= 16; 00212 altitude += whole; 00213 return altitude; 00214 } 00215 00216 MPL3115A2_DATA SensorHandler::getBarometer(){ 00217 MPL3115A2_DATA data; 00218 _mutex.lock(); 00219 data = _barometerData; 00220 _mutex.unlock(); 00221 return data; 00222 } 00223
Generated on Tue Jul 12 2022 17:02:18 by 1.7.2