USNA WRC EW306 / Mbed 2 deprecated cts_matlab_interface

Dependencies:   mbed mbedWSEsbc

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mbedWSEsbc.h"
00003 
00004 #define PI 3.14159
00005 #define LOG_FREQ 5.0
00006 #define CTRL_FREQ 100.0
00007 
00008 
00009 DigitalOut myled(LED1);
00010 DigitalOut rx_LED(LED2);
00011 DigitalOut log_LED(LED3);
00012 DigitalOut run_LED(LED4);
00013 
00014 float pwm;
00015 bool run_flag;
00016 float dt;
00017 char buf[64];
00018 Ticker control_tick;
00019 Ticker log_tick;
00020 float tank1,tank2;
00021 
00022 int parseMessage(char * msg)
00023 {
00024     if(!strncmp(msg, "$CMD", 4)) {
00025         float arg1;
00026 
00027         sscanf(msg,"$CMD,%f\n",&arg1);
00028         pwm = arg1;
00029         //  rx_LED = ! rx_LED;// toggle led to indicate position message receipt
00030         run_flag = true;
00031 
00032         return 1;
00033     }   //end of $CMD
00034 
00035     if(!strncmp(msg, "$STP", 4)) {
00036         rx_LED = ! rx_LED;// toggle led to indicate position message receipt
00037         run_flag = false;
00038         pwm = 0;
00039         return 1;
00040     }   //end of $STP
00041 
00042     return 0;
00043 }
00044 
00045 void controlLoop(){
00046     
00047     tank2 = read_max1270_volts(0, 1, 1);   //chan, range, bipol
00048     tank1 = read_max1270_volts(1, 1, 1);
00049     //Logic to exit loop if tank gets too full
00050     if(tank1 > 4.1 || tank2 > 4.1) {
00051         run_flag = false;
00052     }    
00053 
00054     if(run_flag) {
00055         mot_control(1, -pwm); // negative value required due do polarity of pump motor
00056 
00057     } else {
00058         mot_control(1,0);
00059     }    
00060     myled = !myled;
00061 }
00062 
00063 void logLoop(){
00064     
00065     pc.printf("$SEN,%.3f,%.3f,%.3f\r\n", tank1,tank2,pwm); //write data to scren
00066   
00067     log_LED = !log_LED;
00068 }
00069 
00070 int main()
00071 {
00072     pc.printf("Quanser Twin Tank Control Data Aquisition Program\r\n");
00073 
00074     float log_timer = 0;  //initialize variable for log timer
00075     run_flag = true;
00076     myled = 1;
00077     pwm = 0;
00078     //  pc.attach(&rxCallback);
00079     mbedWSEsbcInit(9600);  //Initialize the mbed WSE Project SBC
00080     wait(.2);   //delay at beginning for voltage settle purposes
00081     t.start();  // Set up timer
00082     Timer tc;
00083     tc.start();
00084     tc.reset();
00085     
00086     control_tick.attach(&controlLoop, 0.1);
00087     log_tick.attach(&logLoop,1.0);
00088 
00089     t.reset(); // zero timer
00090     dt = 1/(CTRL_FREQ);
00091 
00092     while(1) {
00093 
00094         if(pc.readable()) {
00095             int i=0;
00096             char c = pc.getc();
00097             // pc.printf("%s",c);
00098             if(c=='$') {
00099                 buf[i] = c;
00100                 i++;
00101                 while(1) {
00102                     buf[i] = pc.getc();
00103                     if(buf[i]=='\n') {
00104                         break;
00105                     }
00106                     i++;
00107                 }
00108             }
00109 
00110             parseMessage(buf);
00111 
00112         } // If PC Readable
00113 
00114 
00115         //if run flag
00116         wait(dt);   //Wait for sample time of loop frequency
00117     }//while
00118 }//main