Zhihan Zhang / Mbed 2 deprecated 4180_FinalProject

Dependencies:   mbed PulseSensor mbed-rtos LSM9DS1_Library_cal

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "LSM9DS1.h"
00004 #include "PulseSensor.h"
00005 #define PI 3.14159
00006 // Earth's magnetic field varies by location. Add or subtract
00007 // a declination to get a more accurate heading. Calculate
00008 // your's here:
00009 // http://www.ngdc.noaa.gov/geomag-web/#declination
00010 #define DECLINATION -4.94 // Declination (degrees) in Atlanta, GA.
00011 LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
00012 Timer timer;
00013 
00014 /*
00015 Skin temp
00016 Heart beat
00017 Human resistance
00018 Step count
00019 */
00020 
00021 // IMU
00022 //filter_avg_t acc_data;
00023 //axis_info_t acc_sample;
00024 //peak_value_t acc_peak;
00025 //slid_reg_t acc_slid;
00026 uint8_t step = 1;
00027 float threshold = 1.8;
00028 
00029 // skin temp
00030 AnalogIn skinTemp(p19);
00031 uint8_t temp_skin = 25;
00032 
00033 //Heart beat
00034 uint8_t bpm = 70;
00035 
00036 //Human resistance
00037 AnalogIn gsr(p17);
00038 AnalogIn sig(p18);
00039  
00040 uint8_t Human_Resistance = 128;
00041 float gsrValue  = 0;
00042 float phasic = 0;
00043 float baseline = 0;
00044 int on = 1, off = 0;
00045 
00046 Serial  pc(USBTX, USBRX);
00047 RawSerial  dev(p9,p10);
00048 DigitalOut led1(LED1);
00049 DigitalOut led4(LED4);
00050 
00051 struct gyro {
00052     float x;
00053     float y;
00054     float z;
00055 };
00056 
00057 struct acce {
00058     float x;
00059     float y;
00060     float z;
00061 };
00062 
00063 char imu_data[24];
00064     
00065 void sendDataToProcessing(int data)
00066 {
00067     pc.printf("%d\r\n", data);
00068     bpm = data;
00069 }
00070 
00071 
00072 void step_counter(void const *arg) {
00073     
00074     //peak_value_init(&acc_peak);
00075     struct acce ac2;
00076     
00077     float mag;
00078     
00079     timer.start();
00080     
00081     while(1) {
00082         while(!IMU.accelAvailable());
00083         //pc.printf("11\n");
00084         IMU.readAccel();
00085         ac2.x = IMU.calcAccel(IMU.ax);
00086         ac2.y = IMU.calcAccel(IMU.ay);
00087         ac2.z = IMU.calcAccel(IMU.az);
00088         mag = sqrt(pow(ac2.x, 2) + pow(ac2.y, 2) + pow(ac2.z, 2));
00089         
00090         if (mag > threshold && timer.read_ms() > 300) {
00091             //pc.printf("%d \n\r", timer.read_ms());
00092             step += 1;
00093             timer.stop();
00094             timer.reset();
00095             timer.start();
00096         }
00097         Thread::wait(5);
00098     }
00099     
00100             
00101     //while (1) {
00102 //        uint16_t i = 0;
00103 //        float temp = 0;
00104 //
00105 //        for (i = 0; i < FILTER_CNT; i++)
00106 //        {
00107 //            while(!IMU.accelAvailable());
00108 //            //pc.printf("11\n");
00109 //            IMU.readAccel();
00110 //            ac2.x = IMU.calcAccel(IMU.ax);
00111 //            ac2.y = IMU.calcAccel(IMU.ay);
00112 //            ac2.z = IMU.calcAccel(IMU.az);
00113 //            
00114 //            temp = ac2.x * DATA_FACTOR;
00115 //            acc_data.info[i].x = (short)(temp);
00116 //
00117 //            temp = ac2.y * DATA_FACTOR;
00118 //            acc_data.info[i].y = (short)temp;
00119 //
00120 //            temp = ac2.z * DATA_FACTOR;
00121 //            acc_data.info[i].z = (short)temp;
00122 //            
00123 //            Thread::wait(5);
00124 //        }
00125 //
00126 //        filter_calculate(&acc_data, &acc_sample);
00127 //
00128 //        peak_update(&acc_peak, &acc_sample);
00129 //
00130 //        slid_update(&acc_slid, &acc_sample);
00131 //
00132 //        detect_step(&acc_peak, &acc_slid, &acc_sample);
00133 //        
00134 //        timer.stop();
00135 //        if(timer.read_ms() <= 20)
00136 //            Thread::wait(20 - timer.read_ms());
00137 //        //Thread::wait(5);
00138 //        
00139 //    }
00140     
00141 }
00142 
00143 void skin_temp(void const *arg) {
00144      float R1 = 4700*1.4773; //thermistor resistance at 15C
00145     float R2 = 4700; //thermistor resistance at 25C
00146     float R3 = 4700*0.69105; //thermistor resistance at 35C
00147 
00148     float T1 = 288.15; //15C
00149     float T2 = 298.15; //25C
00150     float T3 = 308.15; //35C
00151 
00152     float L1 = log(R1);
00153     float L2 = log(R2);
00154     float L3 = log(R3);
00155     float Y1 = 1/T1;
00156     float Y2 = 1/T2;
00157     float Y3 = 1/T3;
00158 
00159     float g2 = (Y2-Y1)/(L2-L1);
00160     float g3 = (Y3-Y1)/(L3-L1);
00161 
00162     float C = (g3-g2)/(L3-L2)*(1/(L1+L2+L3));
00163     float B = g2 - C*(L1*L1 + L1*L2 + L2*L2);
00164     float A = Y1 - L1*(B + L1*L1*C);
00165     
00166     float Vt;
00167     while(1) {
00168         Vt = skinTemp;
00169         //float R = 9900*(1/Vt - 1); //9900 is the resistance of R1 in voltage divider
00170         float R = 4900 * Vt / (1 - Vt);
00171 
00172         float T = 1/(A + B*log(R) + C*log(R)*log(R)*log(R));
00173         //pc.printf("Vt: %f\n\r", Vt);
00174         //pc.printf("R: %f\n\r", R);
00175         
00176         temp_skin=(uint8_t)(T-273.15); 
00177         //pc.printf("temp: %d\n", temp_skin);
00178         //pc.printf("Skin temp: %f C\n\r", T-273.15);
00179         Thread::wait(100);      
00180     }
00181     
00182 }
00183  
00184 // calculate baseline to compare against
00185 void Get_Baseline(void)
00186 {
00187     double sum = 0;
00188     wait(1);
00189     for(int i=0; i<500; i++) {
00190         gsrValue  = sig;
00191         sum += gsrValue ;
00192         wait(0.005);
00193     }
00194     baseline = sum/500;
00195     //printf("baseline = %f\n\r", baseline);
00196 }
00197  
00198 // main loop, compare against baseline
00199 // sound buzzer if a >5% change happens
00200 void hum_R(void const *arg)
00201 {
00202     float delta;
00203     float Serial_Port_Reading;
00204     int hr;
00205     Get_Baseline();
00206    
00207     while(1) {
00208         gsrValue  = gsr;
00209         phasic = sig;
00210         delta = gsrValue  - phasic;
00211         if(abs(delta) > 0.05) {
00212             gsrValue = gsr;
00213             delta = baseline - gsrValue;
00214         }
00215         hr = 254* (phasic);
00216         //pc.printf("HR!!: %f\n", phasic);
00217         //Human_Resistance = ((1024+2*Serial_Port_Reading)*10000)/(512-Serial_Port_Reading);
00218         Human_Resistance = hr;
00219         Thread::wait(100);
00220     }
00221 }
00222 
00223 //void heartRate(void const *arg) {
00224 //    while (1) {
00225 //        bpm = rand() % 21 + 70;
00226 //        //bpm += (x - 5);
00227 //        Thread::wait(2000);
00228 //    }
00229 //}
00230 
00231 int main()
00232 {
00233     
00234     //pc.baud(9600);
00235     //dev.baud(9600);
00236     
00237     IMU.begin();
00238     if (!IMU.begin()) {
00239         pc.printf("Failed to communicate with LSM9DS1.\n");
00240     }
00241     IMU.calibrate(1);
00242      pc.printf("IMU end\n");
00243     
00244     PulseSensor sensor(p15, sendDataToProcessing);
00245     sensor.start();
00246    
00247     Thread t1(step_counter);
00248     Thread t2(skin_temp);
00249     Thread t3(hum_R);
00250     //Thread t4(heartRate);
00251     
00252     pc.printf("Main Loop\n");
00253     while(1) {
00254         
00255         dev.putc(0xff);
00256         dev.putc(temp_skin);
00257         dev.putc(bpm);
00258         dev.putc(Human_Resistance);
00259         dev.putc(step);
00260         
00261         dev.putc(0xff);
00262         //pc.printf("%d\n\r", rand() % 31 + 60);
00263         Thread::wait(200);
00264         
00265     }
00266 //    struct acce ac2;
00267 //    while(1) {
00268 //        
00269 //        IMU.readAccel();
00270 //        ac2.x = IMU.calcAccel(IMU.ax);
00271 //        ac2.y = IMU.calcAccel(IMU.ay);
00272 //        ac2.z = IMU.calcAccel(IMU.az);
00273 //        pc.printf("%f \n\r", sqrt(pow(ac2.x, 2) + pow(ac2.y, 2) + pow(ac2.z, 2)));
00274 //        wait(0.1);
00275 //    }
00276 }