Code supports writing to the SD card as well as working with the Volckens group smartphone apps for the mbed HRM1017

Dependencies:   ADS1115 BLE_API BME280 Calibration CronoDot EEPROM LSM303 MCP40D17 NCP5623BMUTBG SDFileSystem SI1145 STC3100 mbed nRF51822

Fork of UPAS_BLE_and_USB by Volckens Group Sensors

Committer:
joshuasmth04
Date:
Tue Sep 29 02:01:37 2015 +0000
Revision:
91:f838d9a5b596
Parent:
90:26a08de1a0ac
Child:
92:bb36c4bedb8e
Fixed 24hr test conditions, also commented out RTC OSF checks until we figure out why the RTC is losing power when turned off.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
caseyquinn 0:14d46ef4b6cb 1 #include "mbed.h"
caseyquinn 0:14d46ef4b6cb 2 #include "SDFileSystem.h"
caseyquinn 0:14d46ef4b6cb 3 #include "Adafruit_ADS1015.h"
caseyquinn 1:37babeb68ab9 4 #include "MCP40D17.h"
caseyquinn 7:a24d7156bc02 5 #include "STC3100.h"
caseyquinn 10:f9cb61b29340 6 #include "LSM303.h"
caseyquinn 12:8c00a7f5d483 7 #include "BME280.h"
caseyquinn 14:ad550174db8b 8 #include "SI1145.h"
joshuasmth04 40:ef7e1dcb3780 9 #include "NCP5623BMUTBG.h"
joshuasmth04 41:1fb3e0ac6f87 10 #include "CronoDot.h"
caseyquinn 56:c4d6bdd7c3fb 11 #include "EEPROM.h"
joshuasmth04 72:0b36575ab00d 12 #include "US_Menu.h"
joshuasmth04 77:24fbeb2bfe05 13 #include "BLEDevice.h"
joshuasmth04 71:78edbceff4fc 14 #include "BLE_Menu.h"
joshuasmth04 77:24fbeb2bfe05 15 #include "Calibration.h"
joshuasmth04 71:78edbceff4fc 16
joshuasmth04 71:78edbceff4fc 17 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
joshuasmth04 71:78edbceff4fc 18 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
joshuasmth04 71:78edbceff4fc 19 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
caseyquinn 1:37babeb68ab9 20
caseyquinn 1:37babeb68ab9 21 #define SERIAL_BAUD_RATE 9600
joshuasmth04 41:1fb3e0ac6f87 22 #define SCL 20
caseyquinn 7:a24d7156bc02 23 #define SDA 22
caseyquinn 81:480f0310ef9a 24
caseyquinn 0:14d46ef4b6cb 25
joshuasmth04 71:78edbceff4fc 26 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
joshuasmth04 71:78edbceff4fc 27 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
joshuasmth04 71:78edbceff4fc 28
joshuasmth04 71:78edbceff4fc 29 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
joshuasmth04 71:78edbceff4fc 30 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
joshuasmth04 71:78edbceff4fc 31 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
joshuasmth04 71:78edbceff4fc 32 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
joshuasmth04 71:78edbceff4fc 33
joshuasmth04 71:78edbceff4fc 34 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
joshuasmth04 71:78edbceff4fc 35 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
joshuasmth04 71:78edbceff4fc 36 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
joshuasmth04 71:78edbceff4fc 37 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
joshuasmth04 71:78edbceff4fc 38
joshuasmth04 71:78edbceff4fc 39 BLEDevice ble;
joshuasmth04 71:78edbceff4fc 40 BLE_Menu bleMenu(ble, txPayload, rxPayload, uart_base_uuid_rev, uart_base_uuid, uartService);
joshuasmth04 71:78edbceff4fc 41
joshuasmth04 72:0b36575ab00d 42 I2C i2c(p22, p20);
joshuasmth04 72:0b36575ab00d 43 Adafruit_ADS1115 ads(&i2c);
joshuasmth04 72:0b36575ab00d 44 MCP40D17 DigPot(&i2c);
joshuasmth04 72:0b36575ab00d 45 BME280 bmesensor(p22, p20);
joshuasmth04 72:0b36575ab00d 46 STC3100 gasG(p22, p20);
joshuasmth04 72:0b36575ab00d 47 Serial pc(USBTX, USBRX);
joshuasmth04 72:0b36575ab00d 48 DigitalOut blower(p29, 0);
joshuasmth04 72:0b36575ab00d 49 DigitalOut pbKill(p18, 1);
joshuasmth04 72:0b36575ab00d 50 LSM303 movementsensor(p22, p20);
joshuasmth04 72:0b36575ab00d 51 SI1145 lightsensor(p22, p20);
joshuasmth04 72:0b36575ab00d 52 NCP5623BMUTBG RGB_LED(p22, p20);
joshuasmth04 72:0b36575ab00d 53 CronoDot RTC(p22, p20);
joshuasmth04 72:0b36575ab00d 54 EEPROM E2PROM(p22, p20);
joshuasmth04 72:0b36575ab00d 55 US_Menu Menu;
joshuasmth04 77:24fbeb2bfe05 56 DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications
joshuasmth04 77:24fbeb2bfe05 57 Calibration calibrations(1); //Default serial/calibration if there are no values for the selected option
caseyquinn 57:0b554f7aa9a3 58
caseyquinn 57:0b554f7aa9a3 59 Timeout stop; //This is the stop call back object
joshuasmth04 77:24fbeb2bfe05 60 Timeout logg; //This is the logging call back object
caseyquinn 67:300418575137 61
joshuasmth04 77:24fbeb2bfe05 62 uint16_t serial_num = 1; // Default serial/calibration number
joshuasmth04 77:24fbeb2bfe05 63 int RunReady =0;
caseyquinn 70:81f04e69e08a 64
caseyquinn 70:81f04e69e08a 65
caseyquinn 20:ad9883973d86 66 float press;
caseyquinn 20:ad9883973d86 67 float temp;
caseyquinn 20:ad9883973d86 68 float rh;
caseyquinn 4:69bd7e8a994c 69
caseyquinn 20:ad9883973d86 70 int uv;
caseyquinn 20:ad9883973d86 71 int vis;
caseyquinn 20:ad9883973d86 72 int ir;
caseyquinn 8:204c21adf693 73
caseyquinn 67:300418575137 74 float compass;
caseyquinn 10:f9cb61b29340 75 float accel_x;
caseyquinn 10:f9cb61b29340 76 float accel_y;
caseyquinn 10:f9cb61b29340 77 float accel_z;
lionberg 52:80480b2fafba 78 float accel_comp;
caseyquinn 10:f9cb61b29340 79 float mag_x;
caseyquinn 10:f9cb61b29340 80 float mag_y;
caseyquinn 10:f9cb61b29340 81 float mag_z;
caseyquinn 10:f9cb61b29340 82
caseyquinn 0:14d46ef4b6cb 83 int vInReading;
caseyquinn 0:14d46ef4b6cb 84 int vBlowerReading;
caseyquinn 0:14d46ef4b6cb 85 int omronDiff;
lionberg 52:80480b2fafba 86 float omronVolt; //V
caseyquinn 20:ad9883973d86 87 int omronReading;
lionberg 52:80480b2fafba 88 float atmoRho; //g/L
caseyquinn 49:19e828650618 89
caseyquinn 20:ad9883973d86 90 float massflow; //g/min
caseyquinn 20:ad9883973d86 91 float volflow; //L/min
caseyquinn 67:300418575137 92 float volflowSet = 1.0; //L/min
caseyquinn 81:480f0310ef9a 93 int logInerval = 10; //seconds
caseyquinn 57:0b554f7aa9a3 94 double secondsD = 0;
joshuasmth04 41:1fb3e0ac6f87 95 float massflowSet;
caseyquinn 42:fc2f2b9f07ae 96 float deltaVflow = 0.0;
caseyquinn 42:fc2f2b9f07ae 97 float deltaMflow = 0.0;
caseyquinn 47:3146e8c949a9 98 float gainFlow;
lionberg 52:80480b2fafba 99 float sampledVol; //L, total sampled volume
caseyquinn 20:ad9883973d86 100
caseyquinn 55:f24d70f519cd 101 int digital_pot_setpoint; //min = 0x7F, max = 0x00
caseyquinn 55:f24d70f519cd 102 int digital_pot_set;
caseyquinn 55:f24d70f519cd 103 int digital_pot_change;
caseyquinn 55:f24d70f519cd 104 int digitalpotMax = 127;
caseyquinn 55:f24d70f519cd 105 int digitalpotMin = 2;
caseyquinn 55:f24d70f519cd 106
joshuasmth04 59:a9b21b3d9afc 107 int dutyUp;
joshuasmth04 59:a9b21b3d9afc 108 int dutyDown;
joshuasmth04 59:a9b21b3d9afc 109
joshuasmth04 72:0b36575ab00d 110 // variables are only place holders for the US_Menu //
joshuasmth04 72:0b36575ab00d 111 int refreshtime;
joshuasmth04 72:0b36575ab00d 112 float home_lat, home_lon, work_lat, work_lon;
joshuasmth04 77:24fbeb2bfe05 113 //*************************************************//
joshuasmth04 72:0b36575ab00d 114
caseyquinn 67:300418575137 115 //int refresh_Time = 10; // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s)
caseyquinn 42:fc2f2b9f07ae 116
caseyquinn 57:0b554f7aa9a3 117 char device_name[] = "---------------";
caseyquinn 81:480f0310ef9a 118 char filename[] = "/sd/XXXX0000LOG000000000000---------------.txt";
caseyquinn 7:a24d7156bc02 119 SDFileSystem sd(SPIS_PSELMOSI, SPIS_PSELMISO, SPIS_PSELSCK, SPIS_PSELSS, "sd"); // I believe this matches Todd's pinout, let me know if this doesn't work. (p12, p13, p15, p14)
caseyquinn 7:a24d7156bc02 120
caseyquinn 7:a24d7156bc02 121
joshuasmth04 71:78edbceff4fc 122 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
joshuasmth04 71:78edbceff4fc 123 {
joshuasmth04 71:78edbceff4fc 124 bleMenu.disconnectionCallback();
joshuasmth04 71:78edbceff4fc 125 }
joshuasmth04 71:78edbceff4fc 126
joshuasmth04 71:78edbceff4fc 127 void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) // called any time the phone sends a message
joshuasmth04 71:78edbceff4fc 128 {
joshuasmth04 71:78edbceff4fc 129 bleMenu.WrittenHandler(pc, txPayload, txCharacteristic, Handler);
joshuasmth04 71:78edbceff4fc 130 //returns things to the txPayload
joshuasmth04 71:78edbceff4fc 131 }
joshuasmth04 71:78edbceff4fc 132
joshuasmth04 62:edc9632bcc43 133 void check_stop() // this checks if it's time to stop and shutdown
caseyquinn 57:0b554f7aa9a3 134 {
joshuasmth04 86:60c9ec6e48ed 135 //RTC.get_time(); //debug
joshuasmth04 86:60c9ec6e48ed 136 //pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debug
joshuasmth04 85:a95dd5f03818 137 if(RTC.compare(Menu.f_sec, Menu.f_min, Menu.f_hour, Menu.f_day, Menu.f_month, Menu.f_year)) {
caseyquinn 57:0b554f7aa9a3 138 pbKill = 0; // this is were we shut everything down
caseyquinn 57:0b554f7aa9a3 139 }
caseyquinn 57:0b554f7aa9a3 140 stop.detach();
joshuasmth04 77:24fbeb2bfe05 141 stop.attach(&check_stop, 9);
joshuasmth04 62:edc9632bcc43 142 }
caseyquinn 58:7239c2ab2b65 143
joshuasmth04 41:1fb3e0ac6f87 144
caseyquinn 57:0b554f7aa9a3 145 void log_data()
joshuasmth04 59:a9b21b3d9afc 146 {
caseyquinn 70:81f04e69e08a 147 logg.detach();
joshuasmth04 91:f838d9a5b596 148 logg.attach(&log_data, logInerval-0.5); // reading and logging data must take significintly less than 0.5s. This can be increased.
joshuasmth04 59:a9b21b3d9afc 149 RTC.get_time();
caseyquinn 70:81f04e69e08a 150 secondsD = RTC.seconds;
joshuasmth04 77:24fbeb2bfe05 151 while(fmod(secondsD,logInerval)!=0) {
caseyquinn 70:81f04e69e08a 152 RTC.get_time();
caseyquinn 70:81f04e69e08a 153 secondsD = RTC.seconds;
joshuasmth04 77:24fbeb2bfe05 154 }
joshuasmth04 77:24fbeb2bfe05 155
joshuasmth04 59:a9b21b3d9afc 156 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
joshuasmth04 59:a9b21b3d9afc 157 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 59:a9b21b3d9afc 158
joshuasmth04 77:24fbeb2bfe05 159 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 160 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 161 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 162 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 163 } else {
joshuasmth04 77:24fbeb2bfe05 164 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
joshuasmth04 59:a9b21b3d9afc 165 }
joshuasmth04 59:a9b21b3d9afc 166
joshuasmth04 59:a9b21b3d9afc 167 atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
joshuasmth04 59:a9b21b3d9afc 168 volflow = massflow/atmoRho;
joshuasmth04 59:a9b21b3d9afc 169 sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow);
joshuasmth04 59:a9b21b3d9afc 170 deltaVflow = volflow-volflowSet;
joshuasmth04 59:a9b21b3d9afc 171 massflowSet = volflowSet*atmoRho;
joshuasmth04 59:a9b21b3d9afc 172 deltaMflow = massflow-massflowSet;
joshuasmth04 59:a9b21b3d9afc 173
joshuasmth04 59:a9b21b3d9afc 174 if(abs(deltaMflow)>.025) {
joshuasmth04 59:a9b21b3d9afc 175 digital_pot_change = (int)(gainFlow*deltaMflow);
joshuasmth04 59:a9b21b3d9afc 176
joshuasmth04 59:a9b21b3d9afc 177
joshuasmth04 59:a9b21b3d9afc 178 if(abs(digital_pot_change)>=50) {
joshuasmth04 59:a9b21b3d9afc 179 digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow)));
joshuasmth04 59:a9b21b3d9afc 180 RGB_LED.set_led(1,0,0);
joshuasmth04 41:1fb3e0ac6f87 181
joshuasmth04 59:a9b21b3d9afc 182 } else if(digital_pot_change+digital_pot_set>=digitalpotMax&abs(digital_pot_change)<50) {
joshuasmth04 59:a9b21b3d9afc 183 digital_pot_set = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 184 RGB_LED.set_led(1,0,0);
joshuasmth04 59:a9b21b3d9afc 185 } else if(digital_pot_change+digital_pot_set<=digitalpotMin&abs(digital_pot_change)<50) {
joshuasmth04 59:a9b21b3d9afc 186 digital_pot_set = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 187 RGB_LED.set_led(1,0,0);
joshuasmth04 59:a9b21b3d9afc 188 } else {
joshuasmth04 59:a9b21b3d9afc 189 digital_pot_set = (digital_pot_set+ digital_pot_change);
joshuasmth04 59:a9b21b3d9afc 190 RGB_LED.set_led(1,1,0);
joshuasmth04 59:a9b21b3d9afc 191 }
joshuasmth04 59:a9b21b3d9afc 192
joshuasmth04 59:a9b21b3d9afc 193 DigPot.writeRegister(digital_pot_set);
joshuasmth04 59:a9b21b3d9afc 194
joshuasmth04 59:a9b21b3d9afc 195 } else {
joshuasmth04 59:a9b21b3d9afc 196 RGB_LED.set_led(0,1,0);
joshuasmth04 59:a9b21b3d9afc 197 }
joshuasmth04 59:a9b21b3d9afc 198
joshuasmth04 59:a9b21b3d9afc 199 movementsensor.getACCEL();
joshuasmth04 59:a9b21b3d9afc 200 movementsensor.getCOMPASS();
caseyquinn 67:300418575137 201 compass = movementsensor.getCOMPASS_HEADING();
joshuasmth04 59:a9b21b3d9afc 202 accel_x = movementsensor.AccelData.x;
joshuasmth04 59:a9b21b3d9afc 203 accel_y = movementsensor.AccelData.y;
joshuasmth04 59:a9b21b3d9afc 204 accel_z = movementsensor.AccelData.z;
joshuasmth04 59:a9b21b3d9afc 205 accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0;
joshuasmth04 59:a9b21b3d9afc 206 mag_x = movementsensor.MagData.x;
joshuasmth04 59:a9b21b3d9afc 207 mag_y = movementsensor.MagData.y;
joshuasmth04 59:a9b21b3d9afc 208 mag_z = movementsensor.MagData.z;
joshuasmth04 59:a9b21b3d9afc 209
joshuasmth04 59:a9b21b3d9afc 210 vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0
joshuasmth04 59:a9b21b3d9afc 211 vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0
joshuasmth04 59:a9b21b3d9afc 212 omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3
joshuasmth04 59:a9b21b3d9afc 213 press = bmesensor.getPressure();
joshuasmth04 59:a9b21b3d9afc 214 temp = bmesensor.getTemperature()-5.0;
joshuasmth04 59:a9b21b3d9afc 215 rh = bmesensor.getHumidity();
joshuasmth04 59:a9b21b3d9afc 216 uv = lightsensor.getUV();
joshuasmth04 59:a9b21b3d9afc 217 vis = lightsensor.getVIS();
joshuasmth04 59:a9b21b3d9afc 218 ir = lightsensor.getIR();
joshuasmth04 59:a9b21b3d9afc 219
joshuasmth04 59:a9b21b3d9afc 220 FILE *fp = fopen(filename, "a");
caseyquinn 67:300418575137 221 fprintf(fp, "%02d,%02d,%02d,%02d,%02d,%02d,%1.3f,%1.3f,%2.2f,%4.2f,%2.1f,%1.3f,%1.3f,%5.1f,%1.1f,%1.1f,%1.1f,%1.1f,%d,%d,%d,%d,%d,%d,%d,%d,%d,%1.3f,%1.3f,%f\r\n",RTC.year, RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,omronVolt,massflow,temp,press,rh,atmoRho,volflow,sampledVol,accel_x,accel_y,accel_z,accel_comp,uv,omronReading, vInReading, vBlowerReading, omronDiff,gasG.getAmps(), gasG.getVolts(), gasG.getCharge(),digital_pot_set, deltaMflow, deltaVflow, compass);
joshuasmth04 59:a9b21b3d9afc 222 fclose(fp);
joshuasmth04 77:24fbeb2bfe05 223
joshuasmth04 59:a9b21b3d9afc 224 }
caseyquinn 57:0b554f7aa9a3 225
caseyquinn 57:0b554f7aa9a3 226 int main()
caseyquinn 57:0b554f7aa9a3 227 {
joshuasmth04 89:047e8558fd09 228 uint8_t temp_crr;
joshuasmth04 91:f838d9a5b596 229 RGB_LED.set_led(1,1,1);
caseyquinn 57:0b554f7aa9a3 230 // Setup and Initialization
caseyquinn 57:0b554f7aa9a3 231 //---------------------------------------------------------------------------------------------//
joshuasmth04 77:24fbeb2bfe05 232 Menu.read_menu(E2PROM, logInerval,refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Read all data from the EEPROM here
joshuasmth04 85:a95dd5f03818 233 if(Menu.crr == 255) {
joshuasmth04 85:a95dd5f03818 234 Menu.factory_reset(E2PROM, logInerval,refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num);
joshuasmth04 85:a95dd5f03818 235 }
joshuasmth04 71:78edbceff4fc 236 //**************//BLE initialization//**************//
joshuasmth04 78:a465de6cc47e 237 bleMenu.read_menu(E2PROM, logInerval,refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Read all data from the EEPROM here
joshuasmth04 71:78edbceff4fc 238 uint8_t BLE_name[] = {device_name[0], device_name[1], device_name[2], device_name[3], device_name[4], device_name[5], device_name[6], device_name[7], device_name[8]};
joshuasmth04 77:24fbeb2bfe05 239 ble.init();
joshuasmth04 77:24fbeb2bfe05 240 // setup advertising
joshuasmth04 71:78edbceff4fc 241 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
joshuasmth04 71:78edbceff4fc 242 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
joshuasmth04 71:78edbceff4fc 243 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)BLE_name, sizeof(BLE_name) - 1); // ~8 char max!
joshuasmth04 71:78edbceff4fc 244 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
joshuasmth04 77:24fbeb2bfe05 245 // 100ms; in multiples of 0.625ms.
joshuasmth04 71:78edbceff4fc 246 ble.setAdvertisingInterval(160);
joshuasmth04 71:78edbceff4fc 247 ble.addService(uartService);
joshuasmth04 77:24fbeb2bfe05 248 ble.startAdvertising();
joshuasmth04 71:78edbceff4fc 249 ble.onDisconnection(disconnectionCallback); //what happens when disconected
joshuasmth04 71:78edbceff4fc 250 ble.onDataWritten(WrittenHandler); //what happens when the phone sends a message
joshuasmth04 71:78edbceff4fc 251 //**************//BLE initialization//**************//
joshuasmth04 77:24fbeb2bfe05 252
joshuasmth04 77:24fbeb2bfe05 253
joshuasmth04 77:24fbeb2bfe05 254
caseyquinn 57:0b554f7aa9a3 255 //Test for errors
joshuasmth04 71:78edbceff4fc 256 //RunReady = 0; //debug always open the menu
joshuasmth04 76:8130cb6776c6 257 //if(RTC.OSF()) { //Force the menu open if the RTC needs reset.
joshuasmth04 76:8130cb6776c6 258 // RunReady = 0;
joshuasmth04 76:8130cb6776c6 259 //}
joshuasmth04 86:60c9ec6e48ed 260 Reopen:
joshuasmth04 89:047e8558fd09 261 temp_crr = Menu.crr;
joshuasmth04 89:047e8558fd09 262 if((Menu.crr &0x01) != 0x01) { // crr bit 0, should the menu be optional
joshuasmth04 85:a95dd5f03818 263 RGB_LED.set_led(1,1,1);
caseyquinn 82:e01326a63ae9 264 wait(5);
joshuasmth04 85:a95dd5f03818 265 RGB_LED.set_led(1,1,0);
joshuasmth04 80:5487ba127c82 266 Menu.Start(pc, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops, 2, serial_num, calibrations); //Forces you to open the menu
joshuasmth04 80:5487ba127c82 267 bleMenu.open_menu(txPayload, rxCharacteristic, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops ,RunReady, 3, serial_num, calibrations);
joshuasmth04 91:f838d9a5b596 268 } else if(RunReady == 0 ) {
joshuasmth04 71:78edbceff4fc 269 RGB_LED.set_led(0,1,1); // error code/color
caseyquinn 87:c920521283b2 270 //pc.printf("Fix RTC\r\n");
joshuasmth04 91:f838d9a5b596 271 while(RunReady == 0 ) {
joshuasmth04 71:78edbceff4fc 272 RGB_LED.set_led(0,1,1); // error code/color
joshuasmth04 77:24fbeb2bfe05 273 RunReady = Menu.Start(pc, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops, 0.25, serial_num, calibrations); //Forces you to open the menu
joshuasmth04 91:f838d9a5b596 274 RGB_LED.set_led(0,3,3); // error code/color
joshuasmth04 78:a465de6cc47e 275 bleMenu.open_menu(txPayload, rxCharacteristic, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops ,RunReady, 0.25, serial_num, calibrations);
joshuasmth04 71:78edbceff4fc 276 }
joshuasmth04 85:a95dd5f03818 277
joshuasmth04 85:a95dd5f03818 278 if((Menu.crr & 0x08) != 0) { // crr bit 3, power down prepared to sample 24hr when powered on
joshuasmth04 85:a95dd5f03818 279 RunReady = 1;
joshuasmth04 77:24fbeb2bfe05 280 Menu.save_menu(E2PROM, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Save all data to the EEPROM
joshuasmth04 91:f838d9a5b596 281
joshuasmth04 77:24fbeb2bfe05 282 pbKill = 0;
joshuasmth04 77:24fbeb2bfe05 283 }
joshuasmth04 71:78edbceff4fc 284 }
joshuasmth04 86:60c9ec6e48ed 285 /*if(((Menu.crr & 0x02) != 0) || ((Menu.crr & 0x02) != 0)) {
joshuasmth04 86:60c9ec6e48ed 286 RTC.get_time();
joshuasmth04 86:60c9ec6e48ed 287 if(RTC.OSF()) { //Don't proceed if the time needs to be reset
joshuasmth04 86:60c9ec6e48ed 288 RGB_LED.set_led(0,1,1); // error code/color
joshuasmth04 86:60c9ec6e48ed 289 pc.printf("Please fix the Time before exiting!\r\n");
joshuasmth04 86:60c9ec6e48ed 290 RunReady = 0;
joshuasmth04 86:60c9ec6e48ed 291 goto Reopen;
joshuasmth04 86:60c9ec6e48ed 292 }
joshuasmth04 86:60c9ec6e48ed 293 }*/
joshuasmth04 86:60c9ec6e48ed 294 if((Menu.crr & 0x02) != 0) {
joshuasmth04 86:60c9ec6e48ed 295 RTC.get_time();
joshuasmth04 86:60c9ec6e48ed 296 if(RTC.compare(Menu.s_sec, Menu.s_min, Menu.s_hour, Menu.s_day, Menu.s_month, Menu.s_year)) { //Don't proceed if it's already time to start
joshuasmth04 86:60c9ec6e48ed 297 RGB_LED.set_led(0,1,1); // error code/color
joshuasmth04 86:60c9ec6e48ed 298 pc.printf("Please fix the START time before exiting!\r\n");
joshuasmth04 86:60c9ec6e48ed 299 RunReady = 0;
joshuasmth04 86:60c9ec6e48ed 300 goto Reopen;
joshuasmth04 86:60c9ec6e48ed 301 }
joshuasmth04 86:60c9ec6e48ed 302 }
caseyquinn 83:9153d6c3af81 303 // Compare function for 519(UBCUO) mode to have it wait for the start time to start
joshuasmth04 91:f838d9a5b596 304 if(((Menu.crr & 0x04) != 0) && ((Menu.crr & 0x08) == 0)) {
joshuasmth04 85:a95dd5f03818 305 RTC.get_time();
joshuasmth04 85:a95dd5f03818 306 //Need to add in a compare function to check to see if the start time has already passed.
joshuasmth04 85:a95dd5f03818 307 if(RTC.compare(Menu.f_sec, Menu.f_min, Menu.f_hour, Menu.f_day, Menu.f_month, Menu.f_year)) { //Don't proceed if it's already time to stop
joshuasmth04 85:a95dd5f03818 308 RGB_LED.set_led(0,1,1); // error code/color
joshuasmth04 86:60c9ec6e48ed 309 pc.printf("Please fix the STOP time before exiting!\r\n");
joshuasmth04 86:60c9ec6e48ed 310 RunReady = 0;
joshuasmth04 86:60c9ec6e48ed 311 goto Reopen;
joshuasmth04 85:a95dd5f03818 312 }
caseyquinn 83:9153d6c3af81 313 }
joshuasmth04 86:60c9ec6e48ed 314
joshuasmth04 89:047e8558fd09 315 if(bleMenu.crr != temp_crr ) {
joshuasmth04 79:ed555e9081d0 316 Menu.crr = bleMenu.crr; // if this was changed in the UBM move it to the primary USM before saving
joshuasmth04 89:047e8558fd09 317 Menu.save_menu(E2PROM, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Save all data to the EEPROM
joshuasmth04 79:ed555e9081d0 318 }
joshuasmth04 90:26a08de1a0ac 319 //if(Menu.crr != temp_crr) {
joshuasmth04 90:26a08de1a0ac 320 // Menu.save_menu(E2PROM, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Save all data to the EEPROM
joshuasmth04 90:26a08de1a0ac 321 // //RunReady = ?;
joshuasmth04 90:26a08de1a0ac 322 // goto Restart;
joshuasmth04 90:26a08de1a0ac 323 //}
joshuasmth04 89:047e8558fd09 324
joshuasmth04 89:047e8558fd09 325 pc.printf("You're done, you can now disconect the USB cable.\r\n");
caseyquinn 84:85d11d422da3 326 RunReady = 0;
joshuasmth04 77:24fbeb2bfe05 327 Menu.save_menu(E2PROM, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady, serial_num); //Save all data to the EEPROM
joshuasmth04 91:f838d9a5b596 328
caseyquinn 83:9153d6c3af81 329 RGB_LED.set_led(1,1,0);
joshuasmth04 85:a95dd5f03818 330
joshuasmth04 85:a95dd5f03818 331 if((Menu.crr & 0x10) != 0) { //crr bit 4 uses Demo values
caseyquinn 87:c920521283b2 332 //volflowSet = 1.0; //L/min
caseyquinn 87:c920521283b2 333 //logInerval = 10;
caseyquinn 83:9153d6c3af81 334 }
joshuasmth04 85:a95dd5f03818 335
joshuasmth04 85:a95dd5f03818 336 if((Menu.crr & 0x02) != 0) {
joshuasmth04 85:a95dd5f03818 337 while(!RTC.compare(Menu.s_sec, Menu.s_min, Menu.s_hour, Menu.s_day, Menu.s_month, Menu.s_year)) { // this while waits for the start time by looping until the start time
joshuasmth04 85:a95dd5f03818 338 wait(0.5);
joshuasmth04 85:a95dd5f03818 339 RTC.get_time(); //debug
joshuasmth04 91:f838d9a5b596 340 pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debug
joshuasmth04 85:a95dd5f03818 341 //pc.printf("Waiting to start\r\n");
joshuasmth04 85:a95dd5f03818 342 // serial print a count down??
joshuasmth04 85:a95dd5f03818 343 }
caseyquinn 84:85d11d422da3 344 }
joshuasmth04 85:a95dd5f03818 345
joshuasmth04 78:a465de6cc47e 346 calibrations.initialize(serial_num);
joshuasmth04 85:a95dd5f03818 347
joshuasmth04 85:a95dd5f03818 348 if((Menu.crr & 0x04) != 0) { // don't shut off when in demo mode
joshuasmth04 79:ed555e9081d0 349 stop.attach(&check_stop, 60); // check if we should shut down every 5 seconds, starting 60s after the start.
joshuasmth04 79:ed555e9081d0 350 }
joshuasmth04 85:a95dd5f03818 351
caseyquinn 81:480f0310ef9a 352 if(volflowSet<=1.0) {
joshuasmth04 59:a9b21b3d9afc 353 gainFlow = 100;
caseyquinn 81:480f0310ef9a 354 } else if(volflowSet>=2.0) {
joshuasmth04 59:a9b21b3d9afc 355 gainFlow = 25;
joshuasmth04 59:a9b21b3d9afc 356 } else {
caseyquinn 88:9142f21a4715 357 gainFlow = 25;
joshuasmth04 59:a9b21b3d9afc 358 }
joshuasmth04 59:a9b21b3d9afc 359
caseyquinn 57:0b554f7aa9a3 360 RGB_LED.set_led(1,0,0);
caseyquinn 57:0b554f7aa9a3 361 press = bmesensor.getPressure();
caseyquinn 57:0b554f7aa9a3 362 temp = bmesensor.getTemperature();
caseyquinn 57:0b554f7aa9a3 363 rh = bmesensor.getHumidity();
caseyquinn 57:0b554f7aa9a3 364
caseyquinn 57:0b554f7aa9a3 365 atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
caseyquinn 57:0b554f7aa9a3 366 massflowSet = volflowSet*atmoRho;
caseyquinn 57:0b554f7aa9a3 367 //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
joshuasmth04 85:a95dd5f03818 368
caseyquinn 81:480f0310ef9a 369 //--------------------------------------------------------------//
caseyquinn 81:480f0310ef9a 370 // Calibration value checks //
caseyquinn 81:480f0310ef9a 371 //--------------------------------------------------------------//
joshuasmth04 85:a95dd5f03818 372 //while(1){
joshuasmth04 85:a95dd5f03818 373 //pc.printf("%f,%f,%f,%f,%f\r\n",calibrations.DP4, calibrations.DP3, calibrations.DP2, calibrations.DP1, calibrations.DP0);
joshuasmth04 85:a95dd5f03818 374 //pc.printf("%f,%f,%f,%f\r\n",calibrations.omronVMin, calibrations.omronVMax, calibrations.omronMFMin, calibrations.omronMFMax);
joshuasmth04 85:a95dd5f03818 375 //pc.printf("%f,%f,%f,%f,%f\r\n",calibrations.MF4, calibrations.MF3, calibrations.MF2, calibrations.MF1, calibrations.MF0);
joshuasmth04 85:a95dd5f03818 376 //wait(1);
joshuasmth04 85:a95dd5f03818 377 //}
caseyquinn 81:480f0310ef9a 378 //--------------------------------------------------------------//
caseyquinn 81:480f0310ef9a 379 //--------------------------------------------------------------//
joshuasmth04 85:a95dd5f03818 380
joshuasmth04 77:24fbeb2bfe05 381 digital_pot_setpoint = (int)floor(calibrations.DP4*pow(massflowSet,4)+calibrations.DP3*pow(massflowSet,3)+calibrations.DP2*pow(massflowSet,2)+calibrations.DP1*massflowSet+calibrations.DP0); //min = 0x7F, max = 0x00
caseyquinn 81:480f0310ef9a 382 //pc.printf("%d\r\n", digital_pot_setpoint);
joshuasmth04 85:a95dd5f03818 383
joshuasmth04 59:a9b21b3d9afc 384 if(digital_pot_setpoint>=digitalpotMax) {
caseyquinn 57:0b554f7aa9a3 385 digital_pot_setpoint = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 386 } else if(digital_pot_setpoint<=digitalpotMin) {
caseyquinn 57:0b554f7aa9a3 387 digital_pot_setpoint = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 388 }
joshuasmth04 59:a9b21b3d9afc 389
caseyquinn 57:0b554f7aa9a3 390 DigPot.writeRegister(digital_pot_setpoint);
caseyquinn 57:0b554f7aa9a3 391 wait(1);
caseyquinn 57:0b554f7aa9a3 392 blower = 1;
joshuasmth04 85:a95dd5f03818 393 if((Menu.crr &0x08) != 0) {
joshuasmth04 77:24fbeb2bfe05 394 RTC.get_time();
joshuasmth04 85:a95dd5f03818 395 Menu.f_sec = RTC.seconds;
joshuasmth04 85:a95dd5f03818 396 Menu.f_min = RTC.minutes;
joshuasmth04 85:a95dd5f03818 397 Menu.f_hour = RTC.hour;
joshuasmth04 77:24fbeb2bfe05 398 if(RTC.month == 1 | RTC.month == 3 | RTC.month == 5 | RTC.month == 7 | RTC.month == 8 | RTC.month == 10) {
joshuasmth04 77:24fbeb2bfe05 399 if(RTC.date == 31) {
joshuasmth04 85:a95dd5f03818 400 Menu.f_day = 1;
joshuasmth04 85:a95dd5f03818 401 Menu.f_month = RTC.month +1;
joshuasmth04 85:a95dd5f03818 402 Menu.f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 403 } else {
joshuasmth04 85:a95dd5f03818 404 Menu.f_day = RTC.date+1;
joshuasmth04 85:a95dd5f03818 405 Menu.f_month = RTC.month;
joshuasmth04 85:a95dd5f03818 406 Menu.f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 407 }
joshuasmth04 77:24fbeb2bfe05 408 } else if(RTC.month == 4 | RTC.month == 6 | RTC.month == 9 | RTC.month == 11) {
joshuasmth04 77:24fbeb2bfe05 409 if(RTC.date == 30) {
joshuasmth04 85:a95dd5f03818 410 Menu.f_day = 1;
joshuasmth04 85:a95dd5f03818 411 Menu.f_month = RTC.month +1;
joshuasmth04 85:a95dd5f03818 412 Menu.f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 413 } else {
joshuasmth04 85:a95dd5f03818 414 Menu.f_day = RTC.date+1;
joshuasmth04 85:a95dd5f03818 415 Menu.f_month = RTC.month;
joshuasmth04 85:a95dd5f03818 416 Menu.f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 417 }
joshuasmth04 77:24fbeb2bfe05 418 } else if(RTC.month == 2) {
joshuasmth04 77:24fbeb2bfe05 419 if(RTC.year == 16 | RTC.year == 20 | RTC.year == 24| RTC.year == 28) {
joshuasmth04 77:24fbeb2bfe05 420 if(RTC.date == 29) {
joshuasmth04 85:a95dd5f03818 421 Menu.f_day = 1;
joshuasmth04 85:a95dd5f03818 422 Menu.f_month = RTC.month +1;
joshuasmth04 85:a95dd5f03818 423 Menu.f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 424 } else {
joshuasmth04 85:a95dd5f03818 425 Menu.f_day = RTC.date+1;
joshuasmth04 85:a95dd5f03818 426 Menu.f_month = RTC.month;
joshuasmth04 85:a95dd5f03818 427 Menu.f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 428 }
joshuasmth04 77:24fbeb2bfe05 429 } else {
joshuasmth04 77:24fbeb2bfe05 430 if(RTC.date == 28) {
joshuasmth04 85:a95dd5f03818 431 Menu.f_day = 1;
joshuasmth04 85:a95dd5f03818 432 Menu.f_month = RTC.month +1;
joshuasmth04 85:a95dd5f03818 433 Menu.f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 434 } else {
joshuasmth04 85:a95dd5f03818 435 Menu.f_day = RTC.date+1;
joshuasmth04 85:a95dd5f03818 436 Menu.f_month = RTC.month;
joshuasmth04 85:a95dd5f03818 437 Menu.f_year = RTC.year;
caseyquinn 67:300418575137 438 }
caseyquinn 67:300418575137 439 }
joshuasmth04 77:24fbeb2bfe05 440 } else if(RTC.month == 12) {
joshuasmth04 77:24fbeb2bfe05 441 if(RTC.date == 31) {
joshuasmth04 85:a95dd5f03818 442 Menu.f_day = 1;
joshuasmth04 85:a95dd5f03818 443 Menu.f_month = 1;
joshuasmth04 85:a95dd5f03818 444 Menu.f_year = RTC.year+1;
joshuasmth04 77:24fbeb2bfe05 445 } else {
joshuasmth04 85:a95dd5f03818 446 Menu.f_day = RTC.date+1;
joshuasmth04 85:a95dd5f03818 447 Menu.f_month = RTC.month;
joshuasmth04 85:a95dd5f03818 448 Menu.f_year = RTC.year;
joshuasmth04 77:24fbeb2bfe05 449 }
caseyquinn 67:300418575137 450 }
joshuasmth04 77:24fbeb2bfe05 451 }
joshuasmth04 77:24fbeb2bfe05 452
caseyquinn 81:480f0310ef9a 453 sprintf(filename, "/sd/UPAS%04dLOG_%02d-%02d-%02d_%02d=%02d=%02d_%s.txt",serial_num,RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,device_name);
caseyquinn 57:0b554f7aa9a3 454 FILE *fp = fopen(filename, "w");
caseyquinn 57:0b554f7aa9a3 455 fclose(fp);
joshuasmth04 59:a9b21b3d9afc 456 //pc.printf("%d\r\n",digital_pot_setpoint);
joshuasmth04 59:a9b21b3d9afc 457
joshuasmth04 59:a9b21b3d9afc 458 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 459 //Following lines are needed to enter into the initiallization flow control loop
joshuasmth04 59:a9b21b3d9afc 460
caseyquinn 57:0b554f7aa9a3 461 wait(10);
joshuasmth04 59:a9b21b3d9afc 462
caseyquinn 57:0b554f7aa9a3 463 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
caseyquinn 57:0b554f7aa9a3 464 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 77:24fbeb2bfe05 465 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 466 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 467 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 468 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 469 } else {
joshuasmth04 77:24fbeb2bfe05 470 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
joshuasmth04 59:a9b21b3d9afc 471 }
joshuasmth04 59:a9b21b3d9afc 472 deltaMflow = massflow-massflowSet;
joshuasmth04 59:a9b21b3d9afc 473 digital_pot_set = digital_pot_setpoint;
joshuasmth04 59:a9b21b3d9afc 474 wait(5);
joshuasmth04 59:a9b21b3d9afc 475
joshuasmth04 59:a9b21b3d9afc 476 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 477 //Sets the flow withen +-1.5% of the desired flow rate based on mass flow
joshuasmth04 59:a9b21b3d9afc 478
joshuasmth04 59:a9b21b3d9afc 479 while(abs(deltaMflow)>.015) {
joshuasmth04 77:24fbeb2bfe05 480
joshuasmth04 59:a9b21b3d9afc 481 omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V
joshuasmth04 59:a9b21b3d9afc 482 omronVolt = (omronReading*4.096)/(32768*2);
joshuasmth04 59:a9b21b3d9afc 483 //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx
joshuasmth04 77:24fbeb2bfe05 484 if(omronVolt<=calibrations.omronVMin) {
joshuasmth04 77:24fbeb2bfe05 485 massflow = calibrations.omronMFMin;
joshuasmth04 77:24fbeb2bfe05 486 } else if(omronVolt>=calibrations.omronVMax) {
joshuasmth04 77:24fbeb2bfe05 487 massflow = calibrations.omronMFMax;
joshuasmth04 59:a9b21b3d9afc 488 } else {
joshuasmth04 77:24fbeb2bfe05 489 massflow = calibrations.MF4*pow(omronVolt,(float)4)+calibrations.MF3*pow(omronVolt,(float)3)+calibrations.MF2*pow(omronVolt,(float)2)+calibrations.MF1*omronVolt+calibrations.MF0;
joshuasmth04 59:a9b21b3d9afc 490 }
caseyquinn 57:0b554f7aa9a3 491
joshuasmth04 59:a9b21b3d9afc 492 atmoRho = ((press-((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)))*100)/(287.0531*(temp+273.15))+((6.1078*pow((float)10,(float)((7.5*temp)/(237.3+temp))))*(rh/100)*100)/(461.4964*(temp+273.15));
joshuasmth04 59:a9b21b3d9afc 493 volflow = massflow/atmoRho;
joshuasmth04 59:a9b21b3d9afc 494 massflowSet = volflowSet*atmoRho;
joshuasmth04 59:a9b21b3d9afc 495 deltaMflow = massflow-massflowSet;
joshuasmth04 63:66796aef8d68 496 //pc.printf("%f,%f,%f,%f,%d,%u,%x\r\n",volflow,massflow,massflowSet,deltaMflow,digital_pot_set,digital_pot_set,digital_pot_set);
joshuasmth04 85:a95dd5f03818 497 //pc.printf("%d,%d,%d,%d,%d,%d\r\n",Menu.f_sec,Menu.f_min,Menu.f_hour,Menu.f_day,Menu.f_month,Menu.f_year,digital_pot_set);
joshuasmth04 59:a9b21b3d9afc 498 digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow)));
joshuasmth04 59:a9b21b3d9afc 499 if(digital_pot_set>=digitalpotMax) {
joshuasmth04 59:a9b21b3d9afc 500 digital_pot_set = digitalpotMax;
joshuasmth04 59:a9b21b3d9afc 501 } else if(digital_pot_set<=digitalpotMin) {
joshuasmth04 59:a9b21b3d9afc 502 digital_pot_set = digitalpotMin;
joshuasmth04 59:a9b21b3d9afc 503 }
joshuasmth04 59:a9b21b3d9afc 504
joshuasmth04 59:a9b21b3d9afc 505 wait(2);
joshuasmth04 59:a9b21b3d9afc 506 DigPot.writeRegister(digital_pot_set);
joshuasmth04 59:a9b21b3d9afc 507 wait(1);
joshuasmth04 59:a9b21b3d9afc 508
joshuasmth04 59:a9b21b3d9afc 509
joshuasmth04 59:a9b21b3d9afc 510 }
joshuasmth04 59:a9b21b3d9afc 511
joshuasmth04 59:a9b21b3d9afc 512 sampledVol = 0.0;
joshuasmth04 59:a9b21b3d9afc 513 RGB_LED.set_led(0,1,0);
joshuasmth04 77:24fbeb2bfe05 514
caseyquinn 67:300418575137 515
joshuasmth04 59:a9b21b3d9afc 516
joshuasmth04 59:a9b21b3d9afc 517 //** end of initalization **//
caseyquinn 84:85d11d422da3 518
joshuasmth04 59:a9b21b3d9afc 519 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 520 //---------------------------------------------------------------------------------------------//
joshuasmth04 59:a9b21b3d9afc 521 // Main Control Loop
joshuasmth04 59:a9b21b3d9afc 522
joshuasmth04 62:edc9632bcc43 523 logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c
joshuasmth04 59:a9b21b3d9afc 524 while(1) {
joshuasmth04 62:edc9632bcc43 525 //__disable_irq(); // Disable Interrupts
joshuasmth04 62:edc9632bcc43 526 //RTC.get_time();
joshuasmth04 62:edc9632bcc43 527 //__enable_irq(); // Enable Interrupts
joshuasmth04 62:edc9632bcc43 528 //secondsD = (double)RTC.seconds;
joshuasmth04 62:edc9632bcc43 529 //if(fmod(secondsD,logInerval)==0) {
joshuasmth04 77:24fbeb2bfe05 530 //log_data();
joshuasmth04 64:2906b0c48403 531 //}
caseyquinn 0:14d46ef4b6cb 532 }
joshuasmth04 59:a9b21b3d9afc 533
caseyquinn 0:14d46ef4b6cb 534 }
caseyquinn 0:14d46ef4b6cb 535
caseyquinn 0:14d46ef4b6cb 536