Fitbit code using RTOS

Dependencies:   mbed PulseSensor2 SCP1000 mbed-rtos 4DGL-uLCD-SE LSM9DS1_Library_cal PinDetect FatFileSystemCpp GP-20U7

Files at this revision

API Documentation at this revision

Comitter:
dyu2021
Date:
Wed Apr 22 15:18:28 2020 +0000
Parent:
1:3b016acc5c55
Child:
3:f3e1ee4aa5ec
Commit message:
Added rtos fitbit;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Apr 21 22:56:01 2020 +0000
+++ b/main.cpp	Wed Apr 22 15:18:28 2020 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include "rtos.h"
 #include "LSM9DS1.h"
 #include "SCP1000.h"
 #include "PulseSensor.h"
@@ -35,6 +36,9 @@
 int flights = 0;
 float distance = 0.0;
 float calories = 0;
+int oldSteps = 0;
+const int stepGoal = 100;
+float stride_length = 0.0;
 
 unsigned long pressure;
 float latitude = 0;
@@ -56,6 +60,14 @@
 int screen = 1;
 int oldScreen = 1;
 bool setup_state = true;
+
+Thread thread1;
+Thread thread2;
+Thread thread3;
+Thread thread4;
+Thread thread5;
+Mutex lcd_mtx;
+Mutex gps_mtx;
  
 // when the pushbotton is pressed the run flag is set to false and the main 
 // function while loop exits so that the data file can be closed 
@@ -99,202 +111,254 @@
  
 //Display the time on the top
 void display_time() {
-    uLCD.locate(1, 1);
-    uLCD.color(WHITE);
-    uLCD.text_width(2);
-    uLCD.text_height(3);
-    time_t seconds = time(NULL);
-    char timeBuffer[32];
-    strftime(timeBuffer, 32, "%I:%M %p\r\n", localtime(&seconds));
-    uLCD.printf("%s", timeBuffer);
+    while(1) {
+        lcd_mtx.lock();
+        uLCD.locate(1, 1);
+        uLCD.color(WHITE);
+        uLCD.text_width(2);
+        uLCD.text_height(3);
+        time_t seconds = time(NULL);
+        char timeBuffer[32];
+        strftime(timeBuffer, 32, "%I:%M %p\r\n", localtime(&seconds));
+        uLCD.printf("%s", timeBuffer);
+        lcd_mtx.unlock();
+        Thread::wait(700);
+    }
 }
 
 void setup_screen(void) {
-    if (oldScreen != screen) {
-        uLCD.filled_rectangle(0,0, 128, 128, BLACK);
-        oldScreen++;
-    }
-    switch(screen) {
-        case 1:
-            //Gender
-            uLCD.locate(2, 1);
-            uLCD.text_width(2);
-            uLCD.text_height(2);
-            uLCD.puts("Gender");
-            uLCD.text_width(3);
-            uLCD.text_height(3);
-            uLCD.locate(1, 3);
-            uLCD.putc('M');
-            uLCD.locate(4, 3);
-            uLCD.putc('F');
-            if(pot.read() > 0.5) {
-                gender = 0;
-                uLCD.rectangle(13, 60, 48, 100, BLACK);
-                uLCD.rectangle(75, 60, 110, 100, GREEN);
-            }else {
-                gender = 1;
-                uLCD.rectangle(75, 60, 110, 100, BLACK);
-                uLCD.rectangle(13, 60, 48, 100, GREEN);
-            }
-            break;
-        case 2: 
-            //Weight
-            uLCD.color(WHITE);
-            uLCD.locate(9, 14);
-            uLCD.text_width(1);
-            uLCD.text_height(1);
-            uLCD.puts("lbs");
-            uLCD.locate(2, 1);
-            uLCD.text_width(2);
-            uLCD.text_height(2);
-            uLCD.puts("Weight");
-            weight = 0.45 * (90 + pot.read() * 210);
-            char weight_string[3];
-            if(weight < 100) {
-                sprintf(weight_string, " %d", weight);
-            }else {
-                sprintf(weight_string, "%d", weight);
-            }
-            uLCD.text_width(3);
-            uLCD.text_height(3);
-            uLCD.locate(2, 3);
-            uLCD.color(GREEN);
-            uLCD.puts(weight_string);
-            uLCD.line(35, 100, 110, 100, WHITE);
-            break;
-        case 3:
-            //Age 
-            uLCD.color(WHITE);
-            uLCD.locate(3, 1);
-            uLCD.text_width(2);
-            uLCD.text_height(2);
-            uLCD.puts("Age");
-            age = (int) (10 + pot.read() * 89);
-            char age_string[2];
-            sprintf(age_string, "%d", age);
-            uLCD.text_width(3);
-            uLCD.text_height(3);
-            uLCD.locate(2, 3);
-            uLCD.color(GREEN);
-            uLCD.puts(age_string);
-            uLCD.line(40, 100, 90, 100, WHITE);
-            break;
+    while(1) {
+        lcd_mtx.lock();
+        if (oldScreen != screen) {
+            uLCD.filled_rectangle(0,0, 128, 128, BLACK);
+            oldScreen++;
+        }
+        switch(screen) {
+            case 1:
+                //Gender
+                uLCD.locate(2, 1);
+                uLCD.text_width(2);
+                uLCD.text_height(2);
+                uLCD.puts("Gender");
+                uLCD.text_width(3);
+                uLCD.text_height(3);
+                uLCD.locate(1, 3);
+                uLCD.putc('M');
+                uLCD.locate(4, 3);
+                uLCD.putc('F');
+                if(pot.read() > 0.5) {
+                    gender = 0;
+                    uLCD.rectangle(13, 60, 48, 100, BLACK);
+                    uLCD.rectangle(75, 60, 110, 100, GREEN);
+                } else {
+                    gender = 1;
+                    uLCD.rectangle(75, 60, 110, 100, BLACK);
+                    uLCD.rectangle(13, 60, 48, 100, GREEN);
+                }
+                break;
+            case 2:
+                //Weight
+                uLCD.color(WHITE);
+                uLCD.locate(9, 14);
+                uLCD.text_width(1);
+                uLCD.text_height(1);
+                uLCD.puts("lbs");
+                uLCD.locate(2, 1);
+                uLCD.text_width(2);
+                uLCD.text_height(2);
+                uLCD.puts("Weight");
+                weight = 0.45 * (90 + pot.read() * 210);
+                char weight_string[3];
+                if(weight < 100) {
+                    sprintf(weight_string, " %d", weight);
+                } else {
+                    sprintf(weight_string, "%d", weight);
+                }
+                uLCD.text_width(3);
+                uLCD.text_height(3);
+                uLCD.locate(2, 3);
+                uLCD.color(GREEN);
+                uLCD.puts(weight_string);
+                uLCD.line(35, 100, 110, 100, WHITE);
+                break;
+            case 3:
+                //Age
+                uLCD.color(WHITE);
+                uLCD.locate(3, 1);
+                uLCD.text_width(2);
+                uLCD.text_height(2);
+                uLCD.puts("Age");
+                age = (int) (10 + pot.read() * 89);
+                char age_string[2];
+                sprintf(age_string, "%d", age);
+                uLCD.text_width(3);
+                uLCD.text_height(3);
+                uLCD.locate(2, 3);
+                uLCD.color(GREEN);
+                uLCD.puts(age_string);
+                uLCD.line(40, 100, 90, 100, WHITE);
+                break;
+        }
+        lcd_mtx.unlock();
+        Thread::wait(100);
     }
 }
  
 void update_screen(void) {
-    read_pot();
-    if (oldMode != mode) {
-        uLCD.filled_rectangle(0,0, 128, 128, BLACK);
-    }
-    // print the information to the LCD display
-    switch(mode) {
-        case 1:
-            //Step count
-            uLCD.media_init();
-            uLCD.set_sector_address(0x0000, 0x0005);
-            uLCD.display_image(50, 45);
-            uLCD.filled_rectangle(10, 110, 118, 115, BLACK);
-            uLCD.locate(3, 11);
-            uLCD.text_height(1);
-            uLCD.text_width(1);
-            uLCD.color(WHITE);
-            uLCD.printf("%4d steps",steps);
-            //uLCD.filled_rectangle(10, 110, 118, 115, WHITE);
-            break;
-        case 2:
-            // Heart rate
-            uLCD.media_init();
-            uLCD.set_sector_address(0x0000, 0x000A);
-            uLCD.display_image(50, 45);
-            uLCD.locate(5, 11);
-            uLCD.text_height(1);
-            uLCD.text_width(1);
-            uLCD.color(WHITE);
-            uLCD.printf("%3d BPM", bpm);
-            break;
-        case 3:
-            //Distance
-            uLCD.media_init();
-            uLCD.set_sector_address(0x0000, 0x000F);
-            uLCD.display_image(50, 45);
-            uLCD.locate(6, 11);
-            uLCD.text_height(1);
-            uLCD.text_width(1);
-            uLCD.color(WHITE);
-            uLCD.printf("%4.2f MI", distance);
-            break;
-        case 4:
-            //Calories
-            uLCD.media_init();
-            uLCD.set_sector_address(0x0000, 0x0000);
-            uLCD.display_image(50, 45);
-            uLCD.locate(4, 11);
-            uLCD.text_height(1);
-            uLCD.text_width(1);
-            uLCD.color(WHITE);
-            uLCD.printf("%4d cal", (int)calories);
-            break;
-        case 5:
-            //Floors
-            uLCD.media_init();
-            uLCD.set_sector_address(0x0000, 0x0014);
-            uLCD.display_image(50, 45);
-            uLCD.locate(4, 11);
-            uLCD.text_height(1);
-            uLCD.text_width(1);
-            uLCD.color(WHITE);
-            uLCD.printf("%2d floors", flights);
-            break;
+    while(1) {
+        read_pot();
+        lcd_mtx.lock();
+        if (oldMode != mode) {
+            uLCD.filled_rectangle(0,0, 128, 128, BLACK);
+        }
+        // print the information to the LCD display
+        switch(mode) {
+            case 1:
+                //Step count
+                //uLCD.media_init();
+                //uLCD.set_sector_address(0x0000, 0x0005);
+                //uLCD.display_image(50, 45);
+                uLCD.filled_rectangle(10, 110, 118, 115, BLACK);
+                uLCD.locate(3, 11);
+                uLCD.text_height(1);
+                uLCD.text_width(1);
+                uLCD.color(WHITE);
+                uLCD.printf("%4d steps",steps);
+                uLCD.filled_rectangle(10, 110, 10 + int(steps * (110/stepGoal)), 115, WHITE);
+                break;
+            case 2:
+                // Heart rate
+                //uLCD.media_init();
+                //uLCD.set_sector_address(0x0000, 0x000A);
+                //uLCD.display_image(50, 45);
+                uLCD.locate(5, 11);
+                uLCD.text_height(1);
+                uLCD.text_width(1);
+                uLCD.color(WHITE);
+                uLCD.printf("%3d BPM", bpm);
+                break;
+            case 3:
+                //Distance
+                //uLCD.media_init();
+                //uLCD.set_sector_address(0x0000, 0x000F);
+                //uLCD.display_image(50, 45);
+                uLCD.locate(6, 11);
+                uLCD.text_height(1);
+                uLCD.text_width(1);
+                uLCD.color(WHITE);
+                uLCD.printf("%4.2f ft", distance);
+                break;
+            case 4:
+                //Calories
+                //uLCD.media_init();
+                //uLCD.set_sector_address(0x0000, 0x0000);
+                //uLCD.display_image(50, 45);
+                uLCD.locate(4, 11);
+                uLCD.text_height(1);
+                uLCD.text_width(1);
+                uLCD.color(WHITE);
+                uLCD.printf("%4d cal", (int)calories);
+                break;
+            case 5:
+                //Floors
+                //uLCD.media_init();
+                //uLCD.set_sector_address(0x0000, 0x0014);
+                //uLCD.display_image(50, 45);
+                uLCD.locate(4, 11);
+                uLCD.text_height(1);
+                uLCD.text_width(1);
+                uLCD.color(WHITE);
+                uLCD.printf("%2d floors", flights);
+                break;
+        }
+        lcd_mtx.unlock();
+        Thread::wait(100);
     }
 }
 
 void readHR(){
-    bpm = PPG.get_BPM();
-    calories = calories + (.0083)*.239*(gender*(-55.0969+.6309*bpm+.1988*weight+.2017*age)+(1-gender)*(-20.4022+.4472*bpm-.1263*weight+.074*age));;
+    while(1) {
+        bpm = PPG.get_BPM();
+        //calories = calories + (.0083)*.239*(gender*(-55.0969+.6309*bpm+.1988*weight+.2017*age)+(1-gender)*(-20.4022+.4472*bpm-.1263*weight+.074*age));
+        calories = calories + (.0083)*0.239*(gender*(-55.0969+.6309*bpm+.1988*0.453592*weight
+                                             +.2017*age)+(1-gender)*(-20.4022+.4472*bpm-.1263*0.453592*weight+.074*age));
+        //converted weight from lbs to kilograms
+
+        //Alternate way to calculate distance (likely more accurate)
+        //distance = distance + (steps - oldSteps)* stride_length;
+        //oldSteps = steps;
+        Thread::wait(500);
+    }
 }
 
-void readBarometer(){
-    pressure = scp1000.readPressure();
-    if(count >= 0) count--;
-    unsigned long dif;
-    if(pressure < p_buff[0]) {
-        dif = p_buff[0] - pressure;
-    }else {
-        dif = 0;
+void readBarometer()
+{
+    while(1) {
+        pressure = scp1000.readPressure();
+        if(count >= 0) count--;
+        unsigned long dif;
+        if(pressure < p_buff[0]) {
+            dif = p_buff[0] - pressure;
+        } else {
+            dif = 0;
+        }
+        if(pressure != 0 && p_buff[0] != 0 && dif > 40 && dif < 60 && count < 0) {
+            flights++;
+            count = 2;
+        }
+        p_buff[0] = p_buff[1];
+        p_buff[1] = p_buff[2];
+        p_buff[2] = p_buff[3];
+        p_buff[3] = pressure;
+        Thread::wait(2000);
     }
-    if(pressure != 0 && p_buff[0] != 0 && dif > 40 && dif < 60 && count < 0) {
-        flights++;
-        count = 2;
-    }
-    p_buff[0] = p_buff[1];
-    p_buff[1] = p_buff[2];
-    p_buff[2] = p_buff[3];
-    p_buff[3] = pressure; 
 }
         
 void readGPS(){
-    if(gps.connected()) {
-        if(gps.sample()) {
-            if(gps.ns == 'S') {
-                longitude = gps.longitude*PI/180;
-            }else {
-                longitude = -gps.longitude*PI/180;
+    while(1) {
+        gps_mtx.lock();
+        if(gps.connected()) {
+            if(gps.sample()) {
+                if(gps.ns == 'S') {
+                    longitude = gps.longitude*PI/180;
+                } else {
+                    longitude = -gps.longitude*PI/180;
+                }
+                if(gps.ew == 'W') {
+                    latitude = gps.latitude*PI/180;
+                } else {
+                    latitude = -gps.latitude*PI/180;
+                }
+                if(latitude != 0 && longitude != 0 && old_lat != 0 && old_lon != 0) {
+                    distance = distance + (3963*acosf(sinf(old_lat)*sinf(latitude)+cosf(old_lat)*cosf(latitude)*cosf(longitude-old_lon)));
+                }
+                old_lat = latitude;
+                old_lon = longitude;
+                //pc.printf("%f, %f, %f\r\n", latitude, longitude, distance);
             }
-            if(gps.ew == 'W') {
-                latitude = gps.latitude*PI/180;
-            }else {
-                latitude = -gps.latitude*PI/180;
-            }
-            if(latitude != 0 && longitude != 0 && old_lat != 0 && old_lon != 0) {
-                distance = distance + (3963*acosf(sinf(old_lat)*sinf(latitude)+cosf(old_lat)*cosf(latitude)*cosf(longitude-old_lon)));
-            }
-            old_lat = latitude;
-            old_lon = longitude;
-            //pc.printf("%f, %f, %f\r\n", latitude, longitude, distance);
         }
+        gps_mtx.unlock();
+        Thread::wait(10000);
     }
 }
+
+/*
+void saveData() {
+    // Save the data to the usb flash drive and print to the terminal
+    FILE *fp = fopen( "/msc/data.txt", "a");
+    if(fp == NULL) {
+        error("Could not open file for write\n");
+    }
+    time_t seconds = time(NULL);
+    char hour[2];
+    char date[32];
+    strftime(hour, 2, "%H", localtime(&seconds));
+    strftime(date, 32, "%D", localtime(&seconds));
+    fprintf(fp, "%c-%c\t%d\t%d\t%f\%f\n\r", hour, date, steps, flights, calories, distance);
+    pc.printf("%c-%c\t%d\t%d\t%f\%f\n\r", hour, date, steps, flights, calories, distance);
+    fclose(fp);
+}
+*/
     
 int main() {
     //Set RTC time
@@ -309,20 +373,24 @@
     uLCD.baudrate(3000000);
     uLCD.background_color(BLACK);
     uLCD.cls();
-    setup.attach(&setup_screen, 0.3);
+    thread1.start(setup_screen);
+    //setup.attach(&setup_screen, 0.3);
     
     while(setup_state) {
-        pc.printf("%d", screen);
+        pc.printf("%d\r\n", screen);
     }
+    thread1.terminate();
     
     // Off button
     pb.attach_deasserted(&button);
     
     // set up the display
-    setup.detach();
+    //setup.detach();
     uLCD.cls();
-    display.attach(&update_screen, 0.5);
-    LCD_clock.attach(&display_time, 0.7);
+    thread1.start(update_screen);
+    thread2.start(display_time);
+    //display.attach(&update_screen, 0.5);
+    //LCD_clock.attach(&display_time, 0.7);
     
     // LED indicates whether or not data is being collected
     one = 0;
@@ -347,16 +415,17 @@
     if(fp == NULL) {
         error("Could not open file for write\n");
     }
-    fprintf(fp, "Sample Number, Pressure (Pa), Acceleration Magnitude (Gs), Heart Rate (bpm), Latitude (degrees), Longitude (degrees)\n");
-    
+    //fprintf(fp, "Sample Number, Pressure (Pa), Acceleration Magnitude (Gs), Heart Rate (bpm), Latitude (degrees), Longitude (degrees)\n");
     
-    Barometer.attach(&readBarometer, 2);
-    GPS.attach(&readGPS, 10);
-    HR.attach(&readHR, 0.5);
+    thread3.start(readBarometer);
+    thread4.start(readGPS);
+    thread5.start(readHR);
+    //Barometer.attach(&readBarometer, 2);
+    //GPS.attach(&readGPS, 10);
+    //HR.attach(&readHR, 0.5);
 
     while(run) {
         // Read Sensors
-        //bpm = PPG.get_BPM();
         IMU.readAccel();
         ax = IMU.calcAccel(IMU.ax);
         ay = IMU.calcAccel(IMU.ay);
@@ -382,12 +451,12 @@
         avg_buffer[1] = avg;
         
         // Save the data to the usb flash drive and print to the terminal
-        fprintf(fp, "%d, %lu, %f, %d, %f, %f\r\n", sample_num, pressure, avg, bpm, latitude, longitude); 
+        //fprintf(fp, "%d, %lu, %f, %d, %f, %f\r\n", sample_num, pressure, avg, bpm, latitude, longitude); 
         //pc.printf("%d, %lu, %f, %d, %f, %f\r\n", sample_num, pressure, avg, bpm, latitude, longitude); 
         sample_num++;
         one = !one;
         // Sampling rate of ~200 Hz
-        wait(0.2);
+        Thread::wait(200);
     }
     fclose(fp);
     one = 0;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Apr 22 15:18:28 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed-rtos/#58563e6cba1e