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
main.cpp@85:a95dd5f03818, 2015-09-18 (annotated)
- Committer:
- joshuasmth04
- Date:
- Fri Sep 18 16:00:46 2015 +0000
- Revision:
- 85:a95dd5f03818
- Parent:
- 84:85d11d422da3
- Child:
- 86:60c9ec6e48ed
A lot of stuff, see the onenote page.;
Who changed what in which revision?
User | Revision | Line number | New 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 | 85:a95dd5f03818 | 135 | RTC.get_time(); //debug |
joshuasmth04 | 85:a95dd5f03818 | 136 | pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debig |
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(); |
caseyquinn | 70:81f04e69e08a | 148 | logg.attach(&log_data, logInerval); |
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 | { |
caseyquinn | 57:0b554f7aa9a3 | 228 | |
caseyquinn | 57:0b554f7aa9a3 | 229 | // Setup and Initialization |
caseyquinn | 57:0b554f7aa9a3 | 230 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 77:24fbeb2bfe05 | 231 | 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 | 232 | if(Menu.crr == 255) { |
joshuasmth04 | 85:a95dd5f03818 | 233 | 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 | 234 | } |
joshuasmth04 | 71:78edbceff4fc | 235 | //**************//BLE initialization//**************// |
joshuasmth04 | 78:a465de6cc47e | 236 | 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 | 237 | 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 | 238 | ble.init(); |
joshuasmth04 | 77:24fbeb2bfe05 | 239 | // setup advertising |
joshuasmth04 | 71:78edbceff4fc | 240 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
joshuasmth04 | 71:78edbceff4fc | 241 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
joshuasmth04 | 71:78edbceff4fc | 242 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)BLE_name, sizeof(BLE_name) - 1); // ~8 char max! |
joshuasmth04 | 71:78edbceff4fc | 243 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); |
joshuasmth04 | 77:24fbeb2bfe05 | 244 | // 100ms; in multiples of 0.625ms. |
joshuasmth04 | 71:78edbceff4fc | 245 | ble.setAdvertisingInterval(160); |
joshuasmth04 | 71:78edbceff4fc | 246 | ble.addService(uartService); |
joshuasmth04 | 77:24fbeb2bfe05 | 247 | ble.startAdvertising(); |
joshuasmth04 | 71:78edbceff4fc | 248 | ble.onDisconnection(disconnectionCallback); //what happens when disconected |
joshuasmth04 | 71:78edbceff4fc | 249 | ble.onDataWritten(WrittenHandler); //what happens when the phone sends a message |
joshuasmth04 | 71:78edbceff4fc | 250 | //**************//BLE initialization//**************// |
joshuasmth04 | 77:24fbeb2bfe05 | 251 | |
joshuasmth04 | 77:24fbeb2bfe05 | 252 | |
joshuasmth04 | 77:24fbeb2bfe05 | 253 | |
caseyquinn | 57:0b554f7aa9a3 | 254 | //Test for errors |
joshuasmth04 | 71:78edbceff4fc | 255 | //RunReady = 0; //debug always open the menu |
joshuasmth04 | 76:8130cb6776c6 | 256 | //if(RTC.OSF()) { //Force the menu open if the RTC needs reset. |
joshuasmth04 | 76:8130cb6776c6 | 257 | // RunReady = 0; |
joshuasmth04 | 76:8130cb6776c6 | 258 | //} |
joshuasmth04 | 85:a95dd5f03818 | 259 | |
joshuasmth04 | 79:ed555e9081d0 | 260 | uint8_t temp_crr = Menu.crr; |
joshuasmth04 | 85:a95dd5f03818 | 261 | if((Menu.crr &0x01) == 0) { // crr bit 0, should the menu be optional |
joshuasmth04 | 85:a95dd5f03818 | 262 | RGB_LED.set_led(1,1,1); |
caseyquinn | 82:e01326a63ae9 | 263 | wait(5); |
joshuasmth04 | 85:a95dd5f03818 | 264 | RGB_LED.set_led(1,1,0); |
joshuasmth04 | 80:5487ba127c82 | 265 | 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 | 266 | 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 | 85:a95dd5f03818 | 267 | } else if(RunReady == 0 || RTC.OSF()) { |
joshuasmth04 | 71:78edbceff4fc | 268 | RGB_LED.set_led(0,1,1); // error code/color |
caseyquinn | 83:9153d6c3af81 | 269 | //pc.printf("Change Name\r\n"); |
caseyquinn | 83:9153d6c3af81 | 270 | while(RunReady == 0 || RTC.OSF()) { |
joshuasmth04 | 71:78edbceff4fc | 271 | RGB_LED.set_led(0,1,1); // error code/color |
joshuasmth04 | 77:24fbeb2bfe05 | 272 | 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 | 71:78edbceff4fc | 273 | RGB_LED.set_led(0,10,10); // error code/color |
joshuasmth04 | 78:a465de6cc47e | 274 | 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 | 275 | } |
joshuasmth04 | 85:a95dd5f03818 | 276 | |
joshuasmth04 | 85:a95dd5f03818 | 277 | if((Menu.crr & 0x08) != 0) { // crr bit 3, power down prepared to sample 24hr when powered on |
joshuasmth04 | 85:a95dd5f03818 | 278 | RunReady = 1; |
joshuasmth04 | 77:24fbeb2bfe05 | 279 | 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 | 77:24fbeb2bfe05 | 280 | pbKill = 0; |
joshuasmth04 | 77:24fbeb2bfe05 | 281 | } |
joshuasmth04 | 71:78edbceff4fc | 282 | } |
joshuasmth04 | 77:24fbeb2bfe05 | 283 | |
caseyquinn | 83:9153d6c3af81 | 284 | // Compare function for 519(UBCUO) mode to have it wait for the start time to start |
joshuasmth04 | 85:a95dd5f03818 | 285 | if((Menu.crr & 0x04) == 1) { |
joshuasmth04 | 85:a95dd5f03818 | 286 | RTC.get_time(); |
joshuasmth04 | 85:a95dd5f03818 | 287 | //Need to add in a compare function to check to see if the start time has already passed. |
joshuasmth04 | 85:a95dd5f03818 | 288 | 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 | 289 | RGB_LED.set_led(0,1,1); // error code/color |
joshuasmth04 | 85:a95dd5f03818 | 290 | pc.printf("Please fix the START or STOP times before exiting!\r\n"); |
joshuasmth04 | 85:a95dd5f03818 | 291 | Menu.Start(pc, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, Menu.menu_ops, 0, serial_num, calibrations); //Forces you to open the menu |
joshuasmth04 | 85:a95dd5f03818 | 292 | 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 | 85:a95dd5f03818 | 293 | } |
joshuasmth04 | 85:a95dd5f03818 | 294 | pc.printf("You're done, you can now disconect the USB cable.\r\n"); |
caseyquinn | 83:9153d6c3af81 | 295 | } |
caseyquinn | 83:9153d6c3af81 | 296 | |
joshuasmth04 | 85:a95dd5f03818 | 297 | if(bleMenu.crr != temp_crr) { |
joshuasmth04 | 79:ed555e9081d0 | 298 | Menu.crr = bleMenu.crr; // if this was changed in the UBM move it to the primary USM before saving |
joshuasmth04 | 79:ed555e9081d0 | 299 | } |
caseyquinn | 84:85d11d422da3 | 300 | RunReady = 0; |
joshuasmth04 | 77:24fbeb2bfe05 | 301 | 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 |
caseyquinn | 83:9153d6c3af81 | 302 | RGB_LED.set_led(1,1,0); |
joshuasmth04 | 85:a95dd5f03818 | 303 | |
joshuasmth04 | 85:a95dd5f03818 | 304 | if((Menu.crr & 0x10) != 0) { //crr bit 4 uses Demo values |
joshuasmth04 | 85:a95dd5f03818 | 305 | volflowSet = 1.0; //L/min |
joshuasmth04 | 85:a95dd5f03818 | 306 | logInerval = 10; |
caseyquinn | 83:9153d6c3af81 | 307 | } |
joshuasmth04 | 85:a95dd5f03818 | 308 | |
joshuasmth04 | 85:a95dd5f03818 | 309 | if((Menu.crr & 0x02) != 0) { |
joshuasmth04 | 85:a95dd5f03818 | 310 | 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 | 311 | wait(0.5); |
joshuasmth04 | 85:a95dd5f03818 | 312 | RTC.get_time(); //debug |
joshuasmth04 | 85:a95dd5f03818 | 313 | pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debig |
joshuasmth04 | 85:a95dd5f03818 | 314 | //pc.printf("Waiting to start\r\n"); |
joshuasmth04 | 85:a95dd5f03818 | 315 | // serial print a count down?? |
joshuasmth04 | 85:a95dd5f03818 | 316 | } |
caseyquinn | 84:85d11d422da3 | 317 | } |
joshuasmth04 | 85:a95dd5f03818 | 318 | |
joshuasmth04 | 78:a465de6cc47e | 319 | calibrations.initialize(serial_num); |
joshuasmth04 | 85:a95dd5f03818 | 320 | |
joshuasmth04 | 85:a95dd5f03818 | 321 | if((Menu.crr & 0x04) != 0) { // don't shut off when in demo mode |
joshuasmth04 | 79:ed555e9081d0 | 322 | stop.attach(&check_stop, 60); // check if we should shut down every 5 seconds, starting 60s after the start. |
joshuasmth04 | 79:ed555e9081d0 | 323 | } |
joshuasmth04 | 85:a95dd5f03818 | 324 | |
joshuasmth04 | 85:a95dd5f03818 | 325 | |
caseyquinn | 57:0b554f7aa9a3 | 326 | |
caseyquinn | 81:480f0310ef9a | 327 | if(volflowSet<=1.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 328 | gainFlow = 100; |
caseyquinn | 81:480f0310ef9a | 329 | } else if(volflowSet>=2.0) { |
joshuasmth04 | 59:a9b21b3d9afc | 330 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 331 | } else { |
joshuasmth04 | 59:a9b21b3d9afc | 332 | gainFlow = 25; |
joshuasmth04 | 59:a9b21b3d9afc | 333 | } |
joshuasmth04 | 59:a9b21b3d9afc | 334 | |
caseyquinn | 57:0b554f7aa9a3 | 335 | RGB_LED.set_led(1,0,0); |
caseyquinn | 57:0b554f7aa9a3 | 336 | press = bmesensor.getPressure(); |
caseyquinn | 57:0b554f7aa9a3 | 337 | temp = bmesensor.getTemperature(); |
caseyquinn | 57:0b554f7aa9a3 | 338 | rh = bmesensor.getHumidity(); |
caseyquinn | 57:0b554f7aa9a3 | 339 | |
caseyquinn | 57:0b554f7aa9a3 | 340 | 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 | 341 | massflowSet = volflowSet*atmoRho; |
caseyquinn | 57:0b554f7aa9a3 | 342 | //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 85:a95dd5f03818 | 343 | |
caseyquinn | 81:480f0310ef9a | 344 | //--------------------------------------------------------------// |
caseyquinn | 81:480f0310ef9a | 345 | // Calibration value checks // |
caseyquinn | 81:480f0310ef9a | 346 | //--------------------------------------------------------------// |
joshuasmth04 | 85:a95dd5f03818 | 347 | //while(1){ |
joshuasmth04 | 85:a95dd5f03818 | 348 | //pc.printf("%f,%f,%f,%f,%f\r\n",calibrations.DP4, calibrations.DP3, calibrations.DP2, calibrations.DP1, calibrations.DP0); |
joshuasmth04 | 85:a95dd5f03818 | 349 | //pc.printf("%f,%f,%f,%f\r\n",calibrations.omronVMin, calibrations.omronVMax, calibrations.omronMFMin, calibrations.omronMFMax); |
joshuasmth04 | 85:a95dd5f03818 | 350 | //pc.printf("%f,%f,%f,%f,%f\r\n",calibrations.MF4, calibrations.MF3, calibrations.MF2, calibrations.MF1, calibrations.MF0); |
joshuasmth04 | 85:a95dd5f03818 | 351 | //wait(1); |
joshuasmth04 | 85:a95dd5f03818 | 352 | //} |
caseyquinn | 81:480f0310ef9a | 353 | //--------------------------------------------------------------// |
caseyquinn | 81:480f0310ef9a | 354 | //--------------------------------------------------------------// |
joshuasmth04 | 85:a95dd5f03818 | 355 | |
joshuasmth04 | 77:24fbeb2bfe05 | 356 | 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 | 357 | //pc.printf("%d\r\n", digital_pot_setpoint); |
joshuasmth04 | 85:a95dd5f03818 | 358 | |
joshuasmth04 | 59:a9b21b3d9afc | 359 | if(digital_pot_setpoint>=digitalpotMax) { |
caseyquinn | 57:0b554f7aa9a3 | 360 | digital_pot_setpoint = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 361 | } else if(digital_pot_setpoint<=digitalpotMin) { |
caseyquinn | 57:0b554f7aa9a3 | 362 | digital_pot_setpoint = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 363 | } |
joshuasmth04 | 59:a9b21b3d9afc | 364 | |
caseyquinn | 57:0b554f7aa9a3 | 365 | DigPot.writeRegister(digital_pot_setpoint); |
caseyquinn | 57:0b554f7aa9a3 | 366 | wait(1); |
caseyquinn | 57:0b554f7aa9a3 | 367 | blower = 1; |
joshuasmth04 | 85:a95dd5f03818 | 368 | if((Menu.crr &0x08) != 0) { |
joshuasmth04 | 77:24fbeb2bfe05 | 369 | RTC.get_time(); |
joshuasmth04 | 85:a95dd5f03818 | 370 | Menu.f_sec = RTC.seconds; |
joshuasmth04 | 85:a95dd5f03818 | 371 | Menu.f_min = RTC.minutes; |
joshuasmth04 | 85:a95dd5f03818 | 372 | Menu.f_hour = RTC.hour; |
joshuasmth04 | 77:24fbeb2bfe05 | 373 | if(RTC.month == 1 | RTC.month == 3 | RTC.month == 5 | RTC.month == 7 | RTC.month == 8 | RTC.month == 10) { |
joshuasmth04 | 77:24fbeb2bfe05 | 374 | if(RTC.date == 31) { |
joshuasmth04 | 85:a95dd5f03818 | 375 | Menu.f_day = 1; |
joshuasmth04 | 85:a95dd5f03818 | 376 | Menu.f_month = RTC.month +1; |
joshuasmth04 | 85:a95dd5f03818 | 377 | Menu.f_year = RTC.year; |
joshuasmth04 | 77:24fbeb2bfe05 | 378 | } else { |
joshuasmth04 | 85:a95dd5f03818 | 379 | Menu.f_day = RTC.date+1; |
joshuasmth04 | 85:a95dd5f03818 | 380 | Menu.f_month = RTC.month; |
joshuasmth04 | 85:a95dd5f03818 | 381 | Menu.f_year = RTC.year; |
joshuasmth04 | 77:24fbeb2bfe05 | 382 | } |
joshuasmth04 | 77:24fbeb2bfe05 | 383 | } else if(RTC.month == 4 | RTC.month == 6 | RTC.month == 9 | RTC.month == 11) { |
joshuasmth04 | 77:24fbeb2bfe05 | 384 | if(RTC.date == 30) { |
joshuasmth04 | 85:a95dd5f03818 | 385 | Menu.f_day = 1; |
joshuasmth04 | 85:a95dd5f03818 | 386 | Menu.f_month = RTC.month +1; |
joshuasmth04 | 85:a95dd5f03818 | 387 | Menu.f_year = RTC.year; |
joshuasmth04 | 77:24fbeb2bfe05 | 388 | } else { |
joshuasmth04 | 85:a95dd5f03818 | 389 | Menu.f_day = RTC.date+1; |
joshuasmth04 | 85:a95dd5f03818 | 390 | Menu.f_month = RTC.month; |
joshuasmth04 | 85:a95dd5f03818 | 391 | Menu.f_year = RTC.year; |
joshuasmth04 | 77:24fbeb2bfe05 | 392 | } |
joshuasmth04 | 77:24fbeb2bfe05 | 393 | } else if(RTC.month == 2) { |
joshuasmth04 | 77:24fbeb2bfe05 | 394 | if(RTC.year == 16 | RTC.year == 20 | RTC.year == 24| RTC.year == 28) { |
joshuasmth04 | 77:24fbeb2bfe05 | 395 | if(RTC.date == 29) { |
joshuasmth04 | 85:a95dd5f03818 | 396 | Menu.f_day = 1; |
joshuasmth04 | 85:a95dd5f03818 | 397 | Menu.f_month = RTC.month +1; |
joshuasmth04 | 85:a95dd5f03818 | 398 | Menu.f_year = RTC.year; |
joshuasmth04 | 77:24fbeb2bfe05 | 399 | } else { |
joshuasmth04 | 85:a95dd5f03818 | 400 | Menu.f_day = RTC.date+1; |
joshuasmth04 | 85:a95dd5f03818 | 401 | Menu.f_month = RTC.month; |
joshuasmth04 | 85:a95dd5f03818 | 402 | Menu.f_year = RTC.year; |
joshuasmth04 | 77:24fbeb2bfe05 | 403 | } |
joshuasmth04 | 77:24fbeb2bfe05 | 404 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 405 | if(RTC.date == 28) { |
joshuasmth04 | 85:a95dd5f03818 | 406 | Menu.f_day = 1; |
joshuasmth04 | 85:a95dd5f03818 | 407 | Menu.f_month = RTC.month +1; |
joshuasmth04 | 85:a95dd5f03818 | 408 | Menu.f_year = RTC.year; |
joshuasmth04 | 77:24fbeb2bfe05 | 409 | } else { |
joshuasmth04 | 85:a95dd5f03818 | 410 | Menu.f_day = RTC.date+1; |
joshuasmth04 | 85:a95dd5f03818 | 411 | Menu.f_month = RTC.month; |
joshuasmth04 | 85:a95dd5f03818 | 412 | Menu.f_year = RTC.year; |
caseyquinn | 67:300418575137 | 413 | } |
caseyquinn | 67:300418575137 | 414 | } |
joshuasmth04 | 77:24fbeb2bfe05 | 415 | } else if(RTC.month == 12) { |
joshuasmth04 | 77:24fbeb2bfe05 | 416 | if(RTC.date == 31) { |
joshuasmth04 | 85:a95dd5f03818 | 417 | Menu.f_day = 1; |
joshuasmth04 | 85:a95dd5f03818 | 418 | Menu.f_month = 1; |
joshuasmth04 | 85:a95dd5f03818 | 419 | Menu.f_year = RTC.year+1; |
joshuasmth04 | 77:24fbeb2bfe05 | 420 | } else { |
joshuasmth04 | 85:a95dd5f03818 | 421 | Menu.f_day = RTC.date+1; |
joshuasmth04 | 85:a95dd5f03818 | 422 | Menu.f_month = RTC.month; |
joshuasmth04 | 85:a95dd5f03818 | 423 | Menu.f_year = RTC.year; |
joshuasmth04 | 77:24fbeb2bfe05 | 424 | } |
caseyquinn | 67:300418575137 | 425 | } |
joshuasmth04 | 77:24fbeb2bfe05 | 426 | } |
joshuasmth04 | 77:24fbeb2bfe05 | 427 | |
caseyquinn | 81:480f0310ef9a | 428 | 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 | 429 | FILE *fp = fopen(filename, "w"); |
caseyquinn | 57:0b554f7aa9a3 | 430 | fclose(fp); |
joshuasmth04 | 59:a9b21b3d9afc | 431 | //pc.printf("%d\r\n",digital_pot_setpoint); |
joshuasmth04 | 59:a9b21b3d9afc | 432 | |
joshuasmth04 | 59:a9b21b3d9afc | 433 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 434 | //Following lines are needed to enter into the initiallization flow control loop |
joshuasmth04 | 59:a9b21b3d9afc | 435 | |
caseyquinn | 57:0b554f7aa9a3 | 436 | wait(10); |
joshuasmth04 | 59:a9b21b3d9afc | 437 | |
caseyquinn | 57:0b554f7aa9a3 | 438 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
caseyquinn | 57:0b554f7aa9a3 | 439 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 77:24fbeb2bfe05 | 440 | if(omronVolt<=calibrations.omronVMin) { |
joshuasmth04 | 77:24fbeb2bfe05 | 441 | massflow = calibrations.omronMFMin; |
joshuasmth04 | 77:24fbeb2bfe05 | 442 | } else if(omronVolt>=calibrations.omronVMax) { |
joshuasmth04 | 77:24fbeb2bfe05 | 443 | massflow = calibrations.omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 444 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 445 | 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 | 446 | } |
joshuasmth04 | 59:a9b21b3d9afc | 447 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 59:a9b21b3d9afc | 448 | digital_pot_set = digital_pot_setpoint; |
joshuasmth04 | 59:a9b21b3d9afc | 449 | wait(5); |
joshuasmth04 | 59:a9b21b3d9afc | 450 | |
joshuasmth04 | 59:a9b21b3d9afc | 451 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 452 | //Sets the flow withen +-1.5% of the desired flow rate based on mass flow |
joshuasmth04 | 59:a9b21b3d9afc | 453 | |
joshuasmth04 | 59:a9b21b3d9afc | 454 | while(abs(deltaMflow)>.015) { |
joshuasmth04 | 77:24fbeb2bfe05 | 455 | |
joshuasmth04 | 59:a9b21b3d9afc | 456 | omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V |
joshuasmth04 | 59:a9b21b3d9afc | 457 | omronVolt = (omronReading*4.096)/(32768*2); |
joshuasmth04 | 59:a9b21b3d9afc | 458 | //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx |
joshuasmth04 | 77:24fbeb2bfe05 | 459 | if(omronVolt<=calibrations.omronVMin) { |
joshuasmth04 | 77:24fbeb2bfe05 | 460 | massflow = calibrations.omronMFMin; |
joshuasmth04 | 77:24fbeb2bfe05 | 461 | } else if(omronVolt>=calibrations.omronVMax) { |
joshuasmth04 | 77:24fbeb2bfe05 | 462 | massflow = calibrations.omronMFMax; |
joshuasmth04 | 59:a9b21b3d9afc | 463 | } else { |
joshuasmth04 | 77:24fbeb2bfe05 | 464 | 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 | 465 | } |
caseyquinn | 57:0b554f7aa9a3 | 466 | |
joshuasmth04 | 59:a9b21b3d9afc | 467 | 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 | 468 | volflow = massflow/atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 469 | massflowSet = volflowSet*atmoRho; |
joshuasmth04 | 59:a9b21b3d9afc | 470 | deltaMflow = massflow-massflowSet; |
joshuasmth04 | 63:66796aef8d68 | 471 | //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 | 472 | //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 | 473 | digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow))); |
joshuasmth04 | 59:a9b21b3d9afc | 474 | if(digital_pot_set>=digitalpotMax) { |
joshuasmth04 | 59:a9b21b3d9afc | 475 | digital_pot_set = digitalpotMax; |
joshuasmth04 | 59:a9b21b3d9afc | 476 | } else if(digital_pot_set<=digitalpotMin) { |
joshuasmth04 | 59:a9b21b3d9afc | 477 | digital_pot_set = digitalpotMin; |
joshuasmth04 | 59:a9b21b3d9afc | 478 | } |
joshuasmth04 | 59:a9b21b3d9afc | 479 | |
joshuasmth04 | 59:a9b21b3d9afc | 480 | wait(2); |
joshuasmth04 | 59:a9b21b3d9afc | 481 | DigPot.writeRegister(digital_pot_set); |
joshuasmth04 | 59:a9b21b3d9afc | 482 | wait(1); |
joshuasmth04 | 59:a9b21b3d9afc | 483 | |
joshuasmth04 | 59:a9b21b3d9afc | 484 | |
joshuasmth04 | 59:a9b21b3d9afc | 485 | } |
joshuasmth04 | 59:a9b21b3d9afc | 486 | |
joshuasmth04 | 59:a9b21b3d9afc | 487 | sampledVol = 0.0; |
joshuasmth04 | 59:a9b21b3d9afc | 488 | RGB_LED.set_led(0,1,0); |
joshuasmth04 | 77:24fbeb2bfe05 | 489 | |
caseyquinn | 67:300418575137 | 490 | |
joshuasmth04 | 59:a9b21b3d9afc | 491 | |
joshuasmth04 | 59:a9b21b3d9afc | 492 | //** end of initalization **// |
caseyquinn | 84:85d11d422da3 | 493 | |
joshuasmth04 | 59:a9b21b3d9afc | 494 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 495 | //---------------------------------------------------------------------------------------------// |
joshuasmth04 | 59:a9b21b3d9afc | 496 | // Main Control Loop |
joshuasmth04 | 59:a9b21b3d9afc | 497 | |
joshuasmth04 | 62:edc9632bcc43 | 498 | logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c |
joshuasmth04 | 59:a9b21b3d9afc | 499 | while(1) { |
joshuasmth04 | 62:edc9632bcc43 | 500 | //__disable_irq(); // Disable Interrupts |
joshuasmth04 | 62:edc9632bcc43 | 501 | //RTC.get_time(); |
joshuasmth04 | 62:edc9632bcc43 | 502 | //__enable_irq(); // Enable Interrupts |
joshuasmth04 | 62:edc9632bcc43 | 503 | //secondsD = (double)RTC.seconds; |
joshuasmth04 | 62:edc9632bcc43 | 504 | //if(fmod(secondsD,logInerval)==0) { |
joshuasmth04 | 77:24fbeb2bfe05 | 505 | //log_data(); |
joshuasmth04 | 64:2906b0c48403 | 506 | //} |
caseyquinn | 0:14d46ef4b6cb | 507 | } |
joshuasmth04 | 59:a9b21b3d9afc | 508 | |
caseyquinn | 0:14d46ef4b6cb | 509 | } |
caseyquinn | 0:14d46ef4b6cb | 510 | |
caseyquinn | 0:14d46ef4b6cb | 511 |