Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Adafruit_GFX Adafruit_ST7735 INA219 MODSERIAL MbedJSONValue mbed-rtos mbed
SensorSuite.h
00001 #ifndef SENSORSUITE_H 00002 #define SENSORSUITE_H 00003 00004 #include "INA219.h" 00005 #include "SensorModel.h" 00006 I2C i2c(p9,p10); 00007 00008 /** 00009 00010 This class is going to manage everything to do with sensors 00011 It should be able to receive the sensor data from the current sensors 00012 And then perform some calculatations where necessary 00013 This data is aftewards placed in mailboxes where it can be accessed by 00014 other threads. 00015 00016 **/ 00017 00018 00019 bool alternate = false; 00020 00021 class SensorSuite 00022 { 00023 public: 00024 SensorSuite(); // constructor 00025 void begin(); // launches the sensor suite thread 00026 00027 // functions used to inteface with suite 00028 BatteryModel getBatteryData(); // retrieve information about batteries 00029 SolarModel getSolarData(); // retrieve information about solar panel 00030 ConsumptionModel getConsumptionData(); // retrieve information about consumption 00031 00032 00033 private: 00034 void setupSensors(); // this ensures the sensors are ready to collect data 00035 void readSensorData(); // reads all the data from all sensors 00036 void batteryPercentageEstimate(); // estimates the battery percentage 00037 void suiteOperation(); // function that operated indefinitely 00038 00039 // voltage-current sensors 00040 static INA219 *sensorOne; // sensor 1 00041 static INA219 *sensorTwo; // sensor 2 00042 static INA219 *sensorThree; // sensor 3 00043 static INA219 *sensorFour; // sensor 4 00044 00045 // parameters 00046 static float b1Current; // current from battery one 00047 static float b1Voltage; // voltage from battery one 00048 static float b2Current; // current from battery one 00049 static float b2Voltage; // voltage from battery one 00050 static float spCurrent; // current form solar panel 00051 static float spVoltage; // voltage from solar panel 00052 static float outVoltage; // voltage at output 00053 static float outCurrent; // current at output 00054 00055 static bool batteriesSwitched; // determines which battery is which after switching 00056 00057 // use mutexes - restrict access to sensor data 00058 static Mutex battery_mutex; // restricts access to battery data 00059 static Mutex solar_mutex; // restricts access to solar data 00060 static Mutex consumption_mutex; // restricts access to consumption data 00061 static Mutex general_mutex; // restrics general acess to data during aquisition 00062 00063 00064 00065 00066 }; 00067 00068 SensorSuite::SensorSuite() 00069 { 00070 // determine what mailbox to use: should probably be a pointer 00071 } 00072 00073 void SensorSuite::begin() 00074 { 00075 // intialise the current sensors 00076 setupSensors(); 00077 00078 // start infinite thread to get data 00079 //Thread thread(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL); 00080 //thread.start(callback(this,&SensorSuite::suiteOperation)); 00081 suiteOperation(); 00082 00083 // maybe start infinite thread to receive events 00084 00085 } 00086 00087 void SensorSuite::setupSensors() 00088 { 00089 // dynamically configure the sensors 00090 sensorOne = new INA219(i2c, INA219_ADDR_GV); // for battery one 00091 sensorTwo = new INA219(i2c, INA219_ADDR_VG); // for battery two 00092 sensorThree = new INA219(i2c, INA219_ADDR_GG); // for solar panel 00093 sensorFour = new INA219(i2c, INA219_ADDR_VV); // for power consumption 00094 00095 // querry the position of the batteries 00096 } 00097 00098 void SensorSuite::suiteOperation() 00099 { 00100 // this is the infinitely running thread 00101 00102 while(true) { 00103 00104 // restrict access to battery data 00105 battery_mutex.lock(); 00106 00107 // read values from the batteries 00108 // switching circuit is in default position 00109 if(!batteriesSwitched) { 00110 00111 // read values from the first battery 00112 b1Current = sensorOne->read_current(); 00113 b1Voltage = sensorOne->read_bus_voltage(); 00114 00115 // read values from the second battery 00116 b2Current = sensorTwo->read_current(); 00117 b2Voltage = sensorTwo->read_bus_voltage(); 00118 00119 } else { // switch is in alternate position 00120 00121 // read values from the first battery 00122 b1Current = sensorTwo->read_current(); 00123 b1Voltage = sensorTwo-> read_bus_voltage(); 00124 00125 // read values from the second battery 00126 b2Current = sensorOne->read_current(); 00127 b2Current = sensorOne->read_bus_voltage(); 00128 } 00129 00130 if (b1Voltage < 3.4) 00131 { 00132 // turn on LED 00133 batteryOneLowLED = 1; 00134 00135 } else { 00136 // turn off LED 00137 batteryOneLowLED = 0; 00138 } 00139 00140 if (b2Voltage < 3.4) 00141 { 00142 // turn on LED 00143 batteryTwoLowLED = 1; 00144 } else { 00145 00146 // turn off LED 00147 batteryTwoLowLED = 0; 00148 } 00149 // release access to battery data 00150 battery_mutex.unlock(); 00151 00152 // restrict access to solar data 00153 solar_mutex.lock(); 00154 00155 // read values from the solar panel 00156 spCurrent = sensorThree->read_current(); 00157 spVoltage = sensorThree->read_bus_voltage(); 00158 00159 // release access to solar data 00160 solar_mutex.unlock(); 00161 00162 00163 // restrict access to consumption data 00164 consumption_mutex.lock(); 00165 00166 // read values from the output sensor 00167 outVoltage = sensorFour->read_bus_voltage(); 00168 outCurrent = sensorFour->read_current(); 00169 00170 // release access to consumption data 00171 consumption_mutex.unlock(); 00172 00173 // unlock the general mutex 00174 //general_mutex.unlock(); 00175 00176 Thread::wait(50); // wait ms 00177 00178 } 00179 00180 // read the data points 00181 00182 // load the datapoints into the mailboxes 00183 00184 00185 } 00186 00187 BatteryModel SensorSuite::getBatteryData() 00188 { 00189 // restrict access to battery data to a single thread 00190 battery_mutex.lock(); 00191 00192 // collect data about the batteries 00193 BatteryModel model(b1Current,b1Voltage,b2Current,b2Voltage); 00194 00195 // unlock access to battery data to different threads 00196 battery_mutex.unlock(); 00197 00198 return model; 00199 } 00200 00201 SolarModel SensorSuite::getSolarData() 00202 { 00203 // restrict access to solar data to a single thread 00204 solar_mutex.lock(); 00205 00206 // collect solar data 00207 SolarModel model(spVoltage,spCurrent); 00208 00209 // allow access to solar data to different threads 00210 solar_mutex.unlock(); 00211 00212 return model; 00213 00214 } 00215 00216 ConsumptionModel SensorSuite::getConsumptionData() 00217 { 00218 // restrict access to consumption data to a single thread 00219 consumption_mutex.lock(); 00220 00221 // collect consumption data 00222 ConsumptionModel model(outVoltage, outCurrent); 00223 00224 // allow access to consumption data to different threads 00225 consumption_mutex.unlock(); 00226 00227 return model; 00228 } 00229 00230 // ensure that sensor data is initialised 00231 // voltage-current sensors 00232 INA219* SensorSuite::sensorOne = NULL; // sensor 1 00233 INA219* SensorSuite::sensorTwo = NULL; // sensor 2 00234 INA219* SensorSuite::sensorThree = NULL; // sensor 3 00235 INA219* SensorSuite::sensorFour = NULL; // sensor 4 00236 00237 // parameters 00238 float SensorSuite::b1Current = 0.0; //current from battery one 00239 float SensorSuite::b1Voltage = 0.0; // voltage from battery one 00240 float SensorSuite::b2Current = 0.0; // current from battery one 00241 float SensorSuite::b2Voltage = 0.0; // voltage from battery one 00242 float SensorSuite::spCurrent = 0.0; // current form solar panel 00243 float SensorSuite::spVoltage = 0.0; // voltage from solar panel 00244 float SensorSuite::outVoltage = 0.0; // voltage at output 00245 float SensorSuite::outCurrent = 0.0; // current at output 00246 00247 bool SensorSuite::batteriesSwitched = false; // determines which battery is which after switching 00248 00249 // use mutexes - restrict access to sensor data 00250 Mutex SensorSuite::battery_mutex; // restricts access to battery data 00251 Mutex SensorSuite::solar_mutex; // restricts access to solar data 00252 Mutex SensorSuite::consumption_mutex; // restricts access to consumption data 00253 Mutex SensorSuite::general_mutex; // restrics general acess to data during aquisition 00254 00255 00256 00257 00258 00259 #endif
Generated on Wed Jul 13 2022 02:31:29 by
