abc

Dependencies:   mbed-rtos mbed pixy pixy_test

Fork of aUtO_volume_v4 by ese519

Revision:
6:75156f40a227
Parent:
5:06fa76d9d7bd
Child:
7:309840290ee8
--- a/main.cpp	Thu Dec 10 02:42:30 2015 +0000
+++ b/main.cpp	Thu Dec 10 04:53:50 2015 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "Pixy.h"
+#include "rtos.h"
 
 // init board
 Pixy pixy(Pixy::SPI, p11, p12, p13);
@@ -18,14 +19,19 @@
 // global vars
 int num_readings = 0;
 int total_y = 0;
-float result_level = 0; // mL
-float result_hourly = 0; // mL/hr
-float prev_level = 0;
+float result_level = 0.0; // mL
+float result_hourly = 0.0; // mL/hr
+float prev_level = 0.0;
+
+int gotFirstReading = 0;
+int isDraining = 0;
 
 // funcs and threads
 void get_volume(int i);
+void adjustRate();
 void get_hourly(void const *args);
-RtosTimer get_hourly_thread(get_hourly, osTimerPeriodic);
+void check_overflow();
+RtosTimer * rate_thread;
 
 int main() {
     
@@ -40,12 +46,13 @@
     // init pixy
     pixy.setSerialOutput(&pc);
     
-    // start entry pump
-    pin1 = 1;
+    // start with pumps off
+    pin1 = 0;
     pin2 = 0;
     
     // start hourly thread
-    get_hourly_thread.start(10000);
+    rate_thread = new RtosTimer(get_hourly, osTimerPeriodic, (void *)0);
+    rate_thread->start(10000);
 
     while (1) {
        
@@ -59,12 +66,38 @@
                 get_volume(pixy.blocks[j].y);
             }
         }
+        
+        // check for overflow
+        check_overflow();
+        
+        // adjust rate
+        if (!isDraining) adjustRate();
     }
 }
 
-void update_display(void const *args) {
-    result_hourly = (result_level-prev_level)*6
-    prev_level = result_level;
+void adjustRate() {
+
+}
+
+void check_overflow() {
+    if (result_level >= 30.0) { //led1 = 1;
+        pin1 = 0;
+        pin2 = 1;
+        isDraining = 1;
+    }
+    if (result_level < 2.0) {
+        pin2 = 0;
+        pin1 = 1;
+        isDraining = 0;
+    }
+}
+
+void get_hourly(void const *args) {
+    pc.printf("calc'ing hourly...%f %f\r\n", result_level, prev_level);
+    if (!isDraining && gotFirstReading) {
+        result_hourly = (result_level-prev_level)*6.0;
+        prev_level = result_level;
+    }
 }
 
 void get_volume(int y) {
@@ -76,7 +109,12 @@
     // output results
     if (num_readings >= 10) {
         float average_y = (float)total_y/num_readings;
-        float result_level = -0.2642*average_y + 38.453;
+        result_level = -0.2642*average_y + 38.453;
+        
+        if (!gotFirstReading) {
+            gotFirstReading = 1;
+            prev_level = result_level;
+        }
         
         // to pc
         pc.printf("y = %d, num_readings = %d, average = %.2f, mL = %.2f, rate = %.2f\r\n", y, num_readings, average_y, result_level, result_hourly);
@@ -84,7 +122,7 @@
         
         // to bluetooth
         //blue.printf("y = %d, num_readings = %d, average = %.2f, mL = %.2f\r\n", y, num_readings, average_y, result);
-        blue.printf("%.2f\r\n", result);
+        blue.printf("%.2f %.2f\r\n", result_level, result_hourly);
 
         // reset vars
         num_readings = 0;