ese519 / Mbed 2 deprecated aUtO_volume_v5

Dependencies:   mbed-rtos mbed pixy pixy_test

Fork of aUtO_volume_v4 by ese519

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Pixy.h"
00003 #include "rtos.h"
00004 
00005 // init board
00006 Pixy pixy(Pixy::SPI, p11, p12, p13);
00007 Serial pc(USBTX, USBRX);
00008 DigitalOut pin1(p24);
00009 DigitalOut pin2(p25);
00010 
00011 #if   defined(TARGET_LPC1768)
00012 Serial blue(p9, p10);          // TX, RX
00013 //Serial blue(p13, p14);         // TX, RX
00014 #elif defined(TARGET_LPC4330_M4)
00015 Serial blue(P6_4, P6_5);         // UART0_TX, UART0_RX
00016 //Serial blue(P2_3, P2_4);         // UART3_TX, UART3_RX
00017 #endif
00018 
00019 // global vars
00020 int num_readings = 0;
00021 int total_y = 0;
00022 int numOverflows = 0;
00023 float total_UO = 0.0;
00024 float result_level = 0.0; // mL
00025 float result_hourly = 0.0; // mL/hr
00026 float prev_level = 0.0;
00027 
00028 int gotFirstReading = 0;
00029 int isDraining = 0;
00030 int gotFirstHourly = 0;
00031 
00032 // funcs and threads
00033 void get_volume(int i);
00034 void adjustRate();
00035 void get_hourly(void const *args);
00036 void check_overflow();
00037 RtosTimer * rate_thread;
00038 
00039 int main() {
00040     
00041     // init bluetooth
00042     blue.baud(9600);
00043     
00044     // init pc
00045     pc.baud(9600);
00046     pc.printf("Bluetooth Start\r\n");
00047     pc.printf("ready\n\r");
00048     
00049     // init pixy
00050     pixy.setSerialOutput(&pc);
00051     
00052     // start with pumps off
00053     pin1 = 0;
00054     pin2 = 0;
00055     
00056     // start hourly thread
00057     rate_thread = new RtosTimer(get_hourly, osTimerPeriodic, (void *)0);
00058     rate_thread->start(15000);
00059 
00060     while (1) {
00061        
00062         // get pixy data
00063         uint16_t blocks;
00064         blocks = pixy.getBlocks();
00065         
00066         // store data
00067         if (blocks) {
00068             for (int j = 0; j < blocks; j++) {
00069                 get_volume(pixy.blocks[j].y);
00070             }
00071         }
00072         
00073         // check for overflow
00074         check_overflow();
00075         
00076         // adjust rate
00077         //if (!isDraining) adjustRate();
00078     }
00079 }
00080 
00081 void adjustRate() {
00082     
00083     if (gotFirstHourly) {
00084         if (result_hourly < 60.0) {
00085             
00086         }
00087         else if (result_hourly < 120.0) {
00088         
00089         }
00090         else {
00091             
00092         }
00093     }
00094 }
00095 
00096 void check_overflow() {
00097     if (result_level >= 30.0 && !isDraining) { //led1 = 1;
00098         pin1 = 0;
00099         pin2 = 1;
00100         isDraining = 1;
00101         numOverflows++;
00102     }
00103     if (result_level < 4.0) {
00104         pin2 = 0;
00105         pin1 = 1;
00106         isDraining = 0;
00107     }
00108 }
00109 
00110 void get_hourly(void const *args) {
00111     pc.printf("calc'ing hourly...%f %f\r\n", result_level, prev_level);
00112     if (!isDraining && gotFirstReading) {
00113         float temp = (result_level-prev_level)*4.0;
00114         if (temp >= 0) {
00115             result_hourly = temp;
00116             prev_level = result_level;
00117             gotFirstHourly = 1;
00118         }
00119     }
00120     if (isDraining) gotFirstReading = 0;
00121 }
00122 
00123 void get_volume(int y) {
00124     
00125     // update data
00126     total_y += y;
00127     num_readings++;
00128     
00129     // output results
00130     if (num_readings >= 10) {
00131         float average_y = (float)total_y/num_readings;
00132         result_level = -0.2642*average_y + 38.453;
00133         
00134         if (!gotFirstReading && !isDraining) {
00135             gotFirstReading = 1;
00136             prev_level = result_level;
00137         }
00138         
00139         // to pc
00140         pc.printf("y = %d, num_readings = %d, average = %.2f, mL = %.2f, rate = %.2f\r\n", y, num_readings, average_y, result_level, result_hourly);
00141         pc.printf("%.2f %.2f\r\n", result_level, result_hourly);
00142         
00143         // to bluetooth
00144         if (!isDraining) total_UO = result_level+(float)numOverflows*26.0;
00145         blue.printf("%.2f %.2f\r\n", total_UO, result_hourly);
00146 
00147         // reset vars
00148         num_readings = 0;
00149         total_y = 0;
00150     }
00151 }