ECE4180

Dependencies:   mbed PulseSensor mbed-rtos LSM9DS1_Library_cal

Revision:
8:2d43385e7784
Parent:
7:88d71c228407
--- a/main.cpp	Wed Dec 01 00:20:32 2021 +0000
+++ b/main.cpp	Thu Dec 02 00:58:37 2021 +0000
@@ -2,6 +2,7 @@
 #include "rtos.h"
 #include "LSM9DS1.h"
 #include "stepcounter.h"
+#include "PulseSensor.h"
 #define PI 3.14159
 // Earth's magnetic field varies by location. Add or subtract
 // a declination to get a more accurate heading. Calculate
@@ -11,16 +12,39 @@
 LSM9DS1 IMU(p28, p27, 0xD6, 0x3C);
 Timer timer;
 
-//
+/*
+temp
+pulse
+ppm
+step
+*/
+
+// IMU
 filter_avg_t acc_data;
 axis_info_t acc_sample;
 peak_value_t acc_peak;
 slid_reg_t acc_slid;
-uint8_t step_cnt = 0;
-//
+//uint8_t step_cnt = 1;
+
+// skin temp
 AnalogIn skinTemp(p19);
+uint8_t temp_skin = 48;
 
-RawSerial  pc(USBTX, USBRX);
+//Pulse
+char pulse_symbol;
+char pulse_data;
+
+//H_R
+AnalogIn gsr(p17);
+AnalogIn sig(p18);
+ 
+uint8_t hr = 1;
+float gsrValue  = 0;
+float phasic = 0;
+float baseline = 0;
+int on = 1, off = 0;
+
+Serial  pc(USBTX, USBRX);
 RawSerial  dev(p9,p10);
 DigitalOut led1(LED1);
 DigitalOut led4(LED4);
@@ -40,39 +64,15 @@
 char imu_data[24];
 
 
-char c[4];
-void aa(void const *arg){
-    char d1 = '0';
-    while(1){
-        c[1] = d1;
-        d1++;
-        if(d1 >= '9')
-            d1 = '0';
-        Thread::wait(100);
-    }
-}
+
     
-void bb(void const *arg){
-    char d2 = 'a';
-    while(1){
-        c[2] = d2;
-        d2++;
-        if(d2 >= 'z')
-            d2 = 'a';
-        Thread::wait(100);
-    }
+void sendDataToProcessing(char symbol, int data)
+{
+    pc.printf("%c%d\r\n", symbol, data);
+    pulse_symbol = symbol;
+    pulse_data = data;
 }
 
-void out(void const *arg){
-    while(1){
-        //int i = 1;
-        //dev.putc(c[1]);
-        //dev.putc(c[2]);
-        //dev.putc(10);
-        //pc.printf("step: %d\n", step_cnt);
-        Thread::wait(100);
-    }
-}
 
 void store_imu_data(float d, char* sp) {
     int id = *(int*)&d;
@@ -113,17 +113,20 @@
     
     peak_value_init(&acc_peak);
     struct acce ac2;
+    //pc.printf("enter loop!!\n");
 
     while (1)
     {
-        timer.reset();
-        timer.start();
+        //pc.printf("00\n");
+        //timer.reset();
+        //timer.start();
         uint16_t i = 0;
         float temp = 0;
 
         for (i = 0; i < FILTER_CNT; i++)
         {
             while(!IMU.accelAvailable());
+            //pc.printf("11\n");
             IMU.readAccel();
             ac2.x = IMU.calcAccel(IMU.ax);
             ac2.y = IMU.calcAccel(IMU.ay);
@@ -152,18 +155,20 @@
         timer.stop();
         if(timer.read_ms() <= 20)
             Thread::wait(20 - timer.read_ms());
+        //Thread::wait(5);
+        
     }
     
 }
 
 void skin_temp(void const *arg) {
-     float R1 = 12457; //thermistor resistance at 20C
-    float R2 = 8052.1; //thermistor resistance at 30C
-    float R3 = 5334.0; //thermistor resistance at 40C
-    
-    float T1 = 293.15; //20C
-    float T2 = 303.15; //30C
-    float T3 = 313.15; //40C
+     float R1 = 4700*1.4773; //thermistor resistance at 15C
+    float R2 = 4700; //thermistor resistance at 25C
+    float R3 = 4700*0.69105; //thermistor resistance at 35C
+
+    float T1 = 288.15; //15C
+    float T2 = 298.15; //25C
+    float T3 = 308.15; //35C
 
     float L1 = log(R1);
     float L2 = log(R2);
@@ -182,47 +187,110 @@
     float Vt;
     while(1) {
         Vt = skinTemp;
-        //myled = skinTemp;
-        float R = 9900*(1/Vt - 1); //9900 is the resistance of R1 in voltage divider
+        //float R = 9900*(1/Vt - 1); //9900 is the resistance of R1 in voltage divider
+        float R = 4900 * Vt / (1 - Vt);
 
         float T = 1/(A + B*log(R) + C*log(R)*log(R)*log(R));
-        //pc.printf("Vt: %f\n\r", 1 - Vt);
+        //pc.printf("Vt: %f\n\r", Vt);
         //pc.printf("R: %f\n\r", R);
-              
-        //pc.printf("Skin temp: %f\n\r", T-273.15);
-        Thread::wait(1000);      
+        
+           temp_skin=(uint8_t)(T-273.15); 
+           //pc.printf("temp: %d\n", temp_skin);
+        //pc.printf("Skin temp: %f C\n\r", T-273.15);
+        Thread::wait(100);      
     }
     
 }
 
 
+
+
+ 
+// calculate baseline to compare against
+void Get_Baseline(void)
+{
+    double sum = 0;
+    wait(1);
+    for(int i=0; i<500; i++) {
+        gsrValue  = sig;
+        sum += gsrValue ;
+        wait(0.005);
+    }
+    baseline = sum/500;
+    //printf("baseline = %f\n\r", baseline);
+}
+ 
+// main loop, compare against baseline
+// sound buzzer if a >5% change happens
+void hum_R(void const *arg)
+{
+    float delta;
+    float Serial_Port_Reading;
+    int Human_Resistance;
+    Get_Baseline();
+   
+    while(1) {
+        gsrValue  = gsr;
+        phasic = sig;
+        delta = gsrValue  - phasic;
+        if(abs(delta) > 0.05) {
+            gsrValue = gsr;
+            delta = baseline - gsrValue;
+        }
+        Human_Resistance = 254* (phasic);
+        //pc.printf("HR!!: %f\n", phasic);
+        //Human_Resistance = ((1024+2*Serial_Port_Reading)*10000)/(512-Serial_Port_Reading);
+        //printf("%f\n\r", delta);  // print for phasic
+        //printf("%d\n\r", Human_Resistance);   // print for gsrValue 
+        hr = Human_Resistance;
+        Thread::wait(100);
+    }
+}
+
+
+
 int main()
 {
     
-    pc.baud(9600);
-    dev.baud(9600);
+    //pc.baud(9600);
+    //dev.baud(9600);
     
     IMU.begin();
     if (!IMU.begin()) {
         pc.printf("Failed to communicate with LSM9DS1.\n");
     }
     IMU.calibrate(1);
+     pc.printf("IMU end\n");
+    
+    PulseSensor sensor(p15, sendDataToProcessing);
+     //sensor.start();
 
    
     Thread t1(step_counter);
     Thread t2(skin_temp);
-    //Thread t2(bb);
-    //Thread t3(out);
-
+    Thread t3(hum_R);
+        char aaa = 1;
+        char bbb = 2;
+        char ccc = 11;
+        char ddd = 4;
     
+    pc.printf("Main Loop\n");
     while(1) {
+       
         
         dev.putc(0xff);
-        dev.putc('a');
-        dev.putc('b');
-        dev.putc('\n')
+        //pc.putc(temp_skin);
+        dev.putc(temp_skin);
+        dev.putc(ccc);
+        //dev.putc(pulse_symbol);
+        
+        dev.putc(hr);
+        dev.putc(get_step());
+        
         dev.putc(0xff);
-        Thread::wait(10000);
+        //pc.putc('b');
+        //pc.printf("step_cnt: %d\n",get_step());
+        Thread::wait(200);
         
     }
 }