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
- Committer:
- joshuasmth04
- Date:
- 2015-09-01
- Revision:
- 76:8130cb6776c6
- Parent:
- 75:52e3d2280040
- Child:
- 77:24fbeb2bfe05
File content as of revision 76:8130cb6776c6:
#include "mbed.h" #include "SDFileSystem.h" #include "Adafruit_ADS1015.h" #include "MCP40D17.h" #include "STC3100.h" #include "LSM303.h" #include "BME280.h" #include "SI1145.h" #include "NCP5623BMUTBG.h" #include "CronoDot.h" #include "EEPROM.h" #include "US_Menu.h" #include "BLEDevice.h" #include "BLE_Menu.h" #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */ #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */ #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */ #define SERIAL_BAUD_RATE 9600 #define SCL 20 #define SDA 22 //#define Crono 0xD0 //D0 for the chronoDot uint8_t txPayload[TXRX_BUF_LEN] = {0,}; uint8_t rxPayload[TXRX_BUF_LEN] = {0,}; static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic}; GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); BLEDevice ble; BLE_Menu bleMenu(ble, txPayload, rxPayload, uart_base_uuid_rev, uart_base_uuid, uartService); I2C i2c(p22, p20); Adafruit_ADS1115 ads(&i2c); MCP40D17 DigPot(&i2c); BME280 bmesensor(p22, p20); STC3100 gasG(p22, p20); Serial pc(USBTX, USBRX); DigitalOut blower(p29, 0); DigitalOut pbKill(p18, 1); LSM303 movementsensor(p22, p20); SI1145 lightsensor(p22, p20); NCP5623BMUTBG RGB_LED(p22, p20); CronoDot RTC(p22, p20); EEPROM E2PROM(p22, p20); US_Menu Menu; //DigitalOut GPS_EN(p4,0); //pin 4 is used to enable and disable the GPS, in order to recive serial communications Timeout stop; //This is the stop call back object Timeout logg; //This is the logging call back object int RunReady =0; //UPAS0012 CALIBRATION TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx' //mass flow sensor output signal (x) vs. mass flow (y) //y = -0.6154x4 + 3.7873x3 - 7.2564x2 + 7.0202x - 1.9413 float MF4 = -0.6154; float MF3 = 3.7873; float MF2 = -7.2564; float MF1 = 7.0202; float MF0 = -1.9413; //Mass flow sensor polynomial deviation limits float omronVMin = 0.425; //V float omronVMax = 2.005; //V float omronMFMin = 0.002; //g/L float omronMFMax = 3.544; //g/L //DIGITAL POTENTIOSTAT dig-pot vs m_dot POLYNOMIAL TRANSFER FUNCTION COEFFICIENTS FROM 'UPAS v2 OSU-calibration primary flow data.xlsx' //y = 5.3839x4 - 51.627x3 + 191.09x2 - 345.9x + 277.63 float DP4 = 5.3839; float DP3 = -51.627; float DP2 = 191.09; float DP1 = -345.9; float DP0 = 277.63; float press; float temp; float rh; int uv; int vis; int ir; float compass; float accel_x; float accel_y; float accel_z; float accel_comp; float mag_x; float mag_y; float mag_z; int vInReading; int vBlowerReading; int omronDiff; float omronVolt; //V int omronReading; float atmoRho; //g/L float massflow; //g/min float volflow; //L/min float volflowSet = 1.0; //L/min int logInerval = 10; double secondsD = 0; float massflowSet; float deltaVflow = 0.0; float deltaMflow = 0.0; float gainFlow; float sampledVol; //L, total sampled volume int digital_pot_setpoint; //min = 0x7F, max = 0x00 int digital_pot_set; int digital_pot_change; int digitalpotMax = 127; int digitalpotMin = 2; int dutyUp; int dutyDown; uint8_t f_sec, f_min, f_hour, f_date, f_month, f_year; // variables are only place holders for the US_Menu // int refreshtime; float home_lat, home_lon, work_lat, work_lon; //*************************************************// //int refresh_Time = 10; // refresh time in s, note calling read_GPS()(or similar) will still take how ever long it needs(hopefully < 1s) char device_name[] = "---------------"; char filename[] = "/sd/UPAS0012LOG000000000000---------------.txt"; 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) void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { bleMenu.disconnectionCallback(); } void WrittenHandler(const GattCharacteristicWriteCBParams *Handler) // called any time the phone sends a message { bleMenu.WrittenHandler(pc, txPayload, txCharacteristic, Handler); //returns things to the txPayload } void check_stop() // this checks if it's time to stop and shutdown { //RTC.get_time(); //debug //pc.printf("%02d:%02d:%02d on %d/%d/%d) \r\n",RTC.hour, RTC.minutes, RTC.seconds, RTC.month, RTC.date, RTC.year);//debig if(RTC.compare(f_sec, f_min, f_hour, f_date, f_month, f_year)) { pbKill = 0; // this is were we shut everything down } stop.detach(); stop.attach(&check_stop, 9); } void log_data() { logg.detach(); logg.attach(&log_data, logInerval); RTC.get_time(); secondsD = RTC.seconds; while(fmod(secondsD,logInerval)!=0){ RTC.get_time(); secondsD = RTC.seconds; } omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V omronVolt = (omronReading*4.096)/(32768*2); if(omronVolt<=omronVMin) { massflow = omronMFMin; } else if(omronVolt>=omronVMax) { massflow = omronMFMax; } else { massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; } 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)); volflow = massflow/atmoRho; sampledVol = sampledVol + ((((float)logInerval)/60.0)*volflow); deltaVflow = volflow-volflowSet; massflowSet = volflowSet*atmoRho; deltaMflow = massflow-massflowSet; if(abs(deltaMflow)>.025) { digital_pot_change = (int)(gainFlow*deltaMflow); if(abs(digital_pot_change)>=50) { digital_pot_set = (int)(digital_pot_set+(int)((10.0*deltaMflow))); RGB_LED.set_led(1,0,0); } else if(digital_pot_change+digital_pot_set>=digitalpotMax&abs(digital_pot_change)<50) { digital_pot_set = digitalpotMax; RGB_LED.set_led(1,0,0); } else if(digital_pot_change+digital_pot_set<=digitalpotMin&abs(digital_pot_change)<50) { digital_pot_set = digitalpotMin; RGB_LED.set_led(1,0,0); } else { digital_pot_set = (digital_pot_set+ digital_pot_change); RGB_LED.set_led(1,1,0); } DigPot.writeRegister(digital_pot_set); } else { RGB_LED.set_led(0,1,0); } movementsensor.getACCEL(); movementsensor.getCOMPASS(); compass = movementsensor.getCOMPASS_HEADING(); accel_x = movementsensor.AccelData.x; accel_y = movementsensor.AccelData.y; accel_z = movementsensor.AccelData.z; accel_comp = pow(accel_x,(float)2)+pow(accel_y,(float)2)+pow(accel_z,(float)2)-1.0; mag_x = movementsensor.MagData.x; mag_y = movementsensor.MagData.y; mag_z = movementsensor.MagData.z; vInReading = ads.readADC_SingleEnded(1, 0xD583); // read channel 0 vBlowerReading = ads.readADC_SingleEnded(2, 0xE783); // read channel 0 omronDiff = ads.readADC_Differential(0x8583); // differential channel 2-3 press = bmesensor.getPressure(); temp = bmesensor.getTemperature()-5.0; rh = bmesensor.getHumidity(); uv = lightsensor.getUV(); vis = lightsensor.getVIS(); ir = lightsensor.getIR(); //Mount the filesystem //sd.mount(); FILE *fp = fopen(filename, "a"); 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); fclose(fp); //Unmount the filesystem //sd.unmount(); //wait(1.2); } int main() { // Setup and Initialization //---------------------------------------------------------------------------------------------// Menu.read_menu(E2PROM, logInerval,refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady); //Read all data from the EEPROM here //**************//BLE initialization//**************// //bleMenu.read_settings(E2PROM, device_name); 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]}; ble.init(); // setup advertising ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,(const uint8_t *)BLE_name, sizeof(BLE_name) - 1); // ~8 char max! ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid)); // 100ms; in multiples of 0.625ms. ble.setAdvertisingInterval(160); ble.addService(uartService); ble.startAdvertising(); ble.onDisconnection(disconnectionCallback); //what happens when disconected ble.onDataWritten(WrittenHandler); //what happens when the phone sends a message //**************//BLE initialization//**************// //Test for errors //RunReady = 0; //debug always open the menu //if(RTC.OSF()) { //Force the menu open if the RTC needs reset. // RunReady = 0; //} if(RunReady == 0){ RGB_LED.set_led(0,1,1); // error code/color pc.printf("Change Name\r\n"); while(RunReady == 0){ RGB_LED.set_led(0,1,1); // error code/color RunReady = Menu.Start(pc, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, 513, 0.25); //Forces you to open the menu RGB_LED.set_led(0,10,10); // error code/color bleMenu.open_menu(txPayload, rxCharacteristic, E2PROM, RTC, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, 9,RunReady, 0.25); } //RunReady = 1; Menu.save_menu(E2PROM, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady); //Save all data to the EEPROM pbKill = 0; } RunReady = 0; Menu.save_menu(E2PROM, logInerval, refreshtime, volflowSet, device_name, dutyUp, dutyDown, home_lat, home_lon, work_lat, work_lon, RunReady); //Save all data to the EEPROM stop.attach(&check_stop, 60); // check if we should shut down every 5 seconds, starting 60s after the start. if(volflowSet==1.0) { gainFlow = 100; } else if(volflowSet==2.0) { gainFlow = 25; } else { gainFlow = 25; } RGB_LED.set_led(1,0,0); press = bmesensor.getPressure(); temp = bmesensor.getTemperature(); rh = bmesensor.getHumidity(); 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)); massflowSet = volflowSet*atmoRho; //Digtal pot tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx digital_pot_setpoint = (int)floor(DP4*pow(massflowSet,4)+DP3*pow(massflowSet,3)+DP2*pow(massflowSet,2)+DP1*massflowSet+DP0); //min = 0x7F, max = 0x00 if(digital_pot_setpoint>=digitalpotMax) { digital_pot_setpoint = digitalpotMax; } else if(digital_pot_setpoint<=digitalpotMin) { digital_pot_setpoint = digitalpotMin; } DigPot.writeRegister(digital_pot_setpoint); wait(1); blower = 1; RTC.get_time(); f_sec = RTC.seconds; f_min = RTC.minutes; f_hour = RTC.hour; if(RTC.month == 1 | RTC.month == 3 | RTC.month == 5 | RTC.month == 7 | RTC.month == 8 | RTC.month == 10){ if(RTC.date == 31){ f_date = 1; f_month = RTC.month +1; f_year = RTC.year; } else{ f_date = RTC.date+1; f_month = RTC.month; f_year = RTC.year; } } else if(RTC.month == 4 | RTC.month == 6 | RTC.month == 9 | RTC.month == 11){ if(RTC.date == 30){ f_date = 1; f_month = RTC.month +1; f_year = RTC.year; } else{ f_date = RTC.date+1; f_month = RTC.month; f_year = RTC.year; } } else if(RTC.month == 2){ if(RTC.year == 16 | RTC.year == 20 | RTC.year == 24| RTC.year == 28){ if(RTC.date == 29){ f_date = 1; f_month = RTC.month +1; f_year = RTC.year; } else{ f_date = RTC.date+1; f_month = RTC.month; f_year = RTC.year; } } else{ if(RTC.date == 28){ f_date = 1; f_month = RTC.month +1; f_year = RTC.year; } else{ f_date = RTC.date+1; f_month = RTC.month; f_year = RTC.year; } } } else if(RTC.month == 12){ if(RTC.date == 31){ f_date = 1; f_month = 1; f_year = RTC.year+1; } else{ f_date = RTC.date+1; f_month = RTC.month; f_year = RTC.year; } } sprintf(filename, "/sd/UPAS0012LOG_%02d-%02d-%02d_%02d=%02d=%02d_%s.txt",RTC.year,RTC.month,RTC.date,RTC.hour,RTC.minutes,RTC.seconds,device_name); FILE *fp = fopen(filename, "w"); fclose(fp); //pc.printf("%d\r\n",digital_pot_setpoint); //---------------------------------------------------------------------------------------------// //Following lines are needed to enter into the initiallization flow control loop wait(10); omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V omronVolt = (omronReading*4.096)/(32768*2); if(omronVolt<=omronVMin) { massflow = omronMFMin; } else if(omronVolt>=omronVMax) { massflow = omronMFMax; } else { massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; } deltaMflow = massflow-massflowSet; digital_pot_set = digital_pot_setpoint; wait(5); //---------------------------------------------------------------------------------------------// //Sets the flow withen +-1.5% of the desired flow rate based on mass flow while(abs(deltaMflow)>.015) { omronReading = ads.readADC_SingleEnded(0, 0xC583); // read channel 0 PGA = 2 : Full Scale Range = 2.048V omronVolt = (omronReading*4.096)/(32768*2); //Mass Flow tf from file: UPAS v2 OSU-PrimaryFlowData FullSet 2015-05-29 CQ mods.xlsx if(omronVolt<=omronVMin) { massflow = omronMFMin; } else if(omronVolt>=omronVMax) { massflow = omronMFMax; } else { massflow = MF4*pow(omronVolt,(float)4)+MF3*pow(omronVolt,(float)3)+MF2*pow(omronVolt,(float)2)+MF1*omronVolt+MF0; } 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)); volflow = massflow/atmoRho; massflowSet = volflowSet*atmoRho; deltaMflow = massflow-massflowSet; //pc.printf("%f,%f,%f,%f,%d,%u,%x\r\n",volflow,massflow,massflowSet,deltaMflow,digital_pot_set,digital_pot_set,digital_pot_set); pc.printf("%d,%d,%d,%d,%d,%d\r\n",f_sec,f_min,f_hour,f_date,f_month,f_year,digital_pot_set); digital_pot_set = (int)(digital_pot_set+(int)((gainFlow*deltaMflow))); if(digital_pot_set>=digitalpotMax) { digital_pot_set = digitalpotMax; } else if(digital_pot_set<=digitalpotMin) { digital_pot_set = digitalpotMin; } wait(2); DigPot.writeRegister(digital_pot_set); wait(1); } sampledVol = 0.0; RGB_LED.set_led(0,1,0); //** end of initalization **// //---------------------------------------------------------------------------------------------// //---------------------------------------------------------------------------------------------// //---------------------------------------------------------------------------------------------// // Main Control Loop logg.attach(&log_data, logInerval); // uses callbacks or block Interrupts for anything that uses i2c while(1) { //__disable_irq(); // Disable Interrupts //RTC.get_time(); //__enable_irq(); // Enable Interrupts //secondsD = (double)RTC.seconds; //if(fmod(secondsD,logInerval)==0) { //log_data(); //} } }