4180 weather balloon logging and cutdown system

Dependencies:   GPS MPL3115A2 SDFileSystem mbed-rtos mbed

Fork of WeatherBalloon4180 by Chad Miller

Files at this revision

API Documentation at this revision

Comitter:
cmiller86
Date:
Tue Nov 17 07:16:58 2015 +0000
Parent:
2:21e4b9092bb2
Child:
4:ebf8c354c758
Commit message:
Multithreaded

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Nov 17 06:59:00 2015 +0000
+++ b/main.cpp	Tue Nov 17 07:16:58 2015 +0000
@@ -4,6 +4,8 @@
 #include "GPS.h"
 #include "SDFileSystem.h"
 
+#define PC_DEBUG
+
 Serial pc(USBTX, USBRX);
 SDFileSystem sd(p5, p6, p7, p8, "sd");
 GPS gps(p9, p10);
@@ -17,42 +19,43 @@
 DigitalOut led1(LED1);
 DigitalOut relay(p8);
 
-Timer t;
-
-bool attempted = false;
-bool cutdown = false;
+bool attempted = false, cutdown = false;
+float tempF1, tempF2, tempF3;
 
 FILE *sdout;
+Timer t;
+Mutex log_mutex;
 
 void init()
 {
     t.start();
+    
+    led1  = 0;
     relay = 0;
     
     mkdir("/sd/weather_balloon", 0777);
     sdout = fopen("/sd/weather_balloon/log.txt", "w");
 }
 
-int main()
+void update_gps()
 {
-    float tempC1, tempF1, tempC2, tempF2, tempC3, tempF3;
-    
-    while(1)
+    while(true)
     {
-        pc.printf("----- %f -----\n\r", t.read());
-        fprintf(sdout, "----- %f -----\n\r", t.read());
-        
-        if(t.read() >= 20)
-            cutdown = true;
-                
         gps.sample();
         
-        pc.printf("Long = %f\n\rLati = %f\n\r", gps.longitude, gps.latitude);
-        fprintf(sdout, "Long = %f\n\rLati = %f\n\r", gps.longitude, gps.latitude);
-        
         if(!gps.longitude)
             led1 = 1;
         
+        Thread::wait(250);
+    }
+}
+
+void update_temperature()
+{
+    float tempC1, tempC2, tempC3;
+    
+    while(true)
+    {
         tempC1 = ((temp1 * 3.3) - 0.600) * 100.0;
         tempC2 = ((temp2 * 3.3) - 0.600) * 100.0;
         tempC3 = ((temp3 * 3.3) - 0.600) * 100.0;
@@ -60,35 +63,68 @@
         tempF2 = (9.0 * tempC2) / 5.0 + 32;
         tempF3 = (9.0 * tempC3) / 5.0 + 32;
         
-        pc.printf("Temp1 = %f\n\rTemp2 = %f\n\rTemp3 = %f\n\r", tempF1, tempF2, tempF3);
+        Thread::wait(250);
+    }
+}
+
+void write_to_log()
+{
+    while(true)
+    {
+        log_mutex.lock();
+        
+        fprintf(sdout, "----- %f -----\n\r", t.read());
+        fprintf(sdout, "Long = %f\n\rLati = %f\n\r", gps.longitude, gps.latitude);
         fprintf(sdout, "Temp1 = %f\n\rTemp2 = %f\n\rTemp3 = %f\n\r", tempF1, tempF2, tempF3);
+        fprintf(sdout, dtmf ? "DTMF = True\n\r" : "DTMF = False\n\r");
         
-        if(dtmf)
-        {
-            pc.printf("DTMF = True\n\r");
-            fprintf(sdout, "DTMF = True\n\r");
-            
+        #ifdef PC_DEBUG
+        pc.printf("----- %f -----\n\r", t.read());
+        pc.printf("Long = %f\n\rLati = %f\n\r", gps.longitude, gps.latitude);
+        pc.printf("Temp1 = %f\n\rTemp2 = %f\n\rTemp3 = %f\n\r", tempF1, tempF2, tempF3);
+        pc.printf(dtmf ? "DTMF = True\n\r" : "DTMF = False\n\r");
+        #endif
+        
+        log_mutex.unlock();
+        
+        Thread::wait(1000);
+    }
+}
+
+void check_cutdown()
+{
+    while(true)
+    {
+        if(t.read() >= 20 || dtmf)
             cutdown = true;
-        }
-        else
-        {
-            pc.printf("DTMF = False\n\r");
-            fprintf(sdout, "DTMF = False\n\r");
-        }
 
         if(cutdown && !attempted)
         {
+            log_mutex.lock();
             pc.printf("Cutdown Started = %f\n\r", t.read());
             fprintf(sdout, "Cutdown Started = %f\n\r", t.read());
+            log_mutex.unlock();
             
             relay = 1;
-            wait(20);
+            Thread::wait(200000);
             relay = 0;
             
+            log_mutex.lock();
             pc.printf("Cutdown Ended = %f\n\r", t.read());
             fprintf(sdout, "Cutdown Ended = %f\n\r", t.read());
+            log_mutex.unlock();
             
             attempted = true;
         }
+        
+        Thread::wait(100);
     }
 }
+
+int main()
+{
+    Thread gps_thread(update_gps);
+    Thread temperature_thread(update_temperature);
+    Thread log_thread(write_to_log);
+    Thread cutdown_thread(check_cutdown);
+}