![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Code required for data acquisition of couple tanks system with WSE sbc
Dependencies: mbed mbedWSEsbc
main.cpp@0:e42d1cc6513e, 2016-08-29 (annotated)
- Committer:
- jdawkins
- Date:
- Mon Aug 29 13:49:17 2016 +0000
- Revision:
- 0:e42d1cc6513e
- Child:
- 1:a87d98b19444
Initial commit of updated quanser coupled tank system data acquisition program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jdawkins | 0:e42d1cc6513e | 1 | #include "mbed.h" |
jdawkins | 0:e42d1cc6513e | 2 | #include "mbedWSEsbc.h" |
jdawkins | 0:e42d1cc6513e | 3 | |
jdawkins | 0:e42d1cc6513e | 4 | #define PI 3.14159 |
jdawkins | 0:e42d1cc6513e | 5 | #define LOG_FREQ 1.0 |
jdawkins | 0:e42d1cc6513e | 6 | #define CTRL_FREQ 10.0 |
jdawkins | 0:e42d1cc6513e | 7 | |
jdawkins | 0:e42d1cc6513e | 8 | float tstop,pwm,log_timer,log_start,loop_time; |
jdawkins | 0:e42d1cc6513e | 9 | float duty_cycle; |
jdawkins | 0:e42d1cc6513e | 10 | bool run_flag; |
jdawkins | 0:e42d1cc6513e | 11 | double dt; |
jdawkins | 0:e42d1cc6513e | 12 | |
jdawkins | 0:e42d1cc6513e | 13 | bool readAndParse(Serial *port); |
jdawkins | 0:e42d1cc6513e | 14 | int main() |
jdawkins | 0:e42d1cc6513e | 15 | { |
jdawkins | 0:e42d1cc6513e | 16 | run_flag = false; |
jdawkins | 0:e42d1cc6513e | 17 | log_timer = 0; //initialize variable for log timer |
jdawkins | 0:e42d1cc6513e | 18 | log_start = 0; |
jdawkins | 0:e42d1cc6513e | 19 | loop_time = 0; |
jdawkins | 0:e42d1cc6513e | 20 | tstop = 120; |
jdawkins | 0:e42d1cc6513e | 21 | pwm = 0.7; |
jdawkins | 0:e42d1cc6513e | 22 | led1 = led2 = led3 = led4 = 0; |
jdawkins | 0:e42d1cc6513e | 23 | dt = (1/CTRL_FREQ); |
jdawkins | 0:e42d1cc6513e | 24 | mbedWSEsbcInit(115200); //Initialize the mbed WSE Project SBC |
jdawkins | 0:e42d1cc6513e | 25 | wait(.2); //delay at beginning for voltage settle purposes |
jdawkins | 0:e42d1cc6513e | 26 | t.start(); // Set up timer |
jdawkins | 0:e42d1cc6513e | 27 | pc.printf("Quanser Twin Tank Control Data Aquisition Program\r\n"); |
jdawkins | 0:e42d1cc6513e | 28 | |
jdawkins | 0:e42d1cc6513e | 29 | t.reset(); // zero timer |
jdawkins | 0:e42d1cc6513e | 30 | |
jdawkins | 0:e42d1cc6513e | 31 | while(1) { |
jdawkins | 0:e42d1cc6513e | 32 | loop_time = t.read(); |
jdawkins | 0:e42d1cc6513e | 33 | readAndParse(&pc); |
jdawkins | 0:e42d1cc6513e | 34 | //Emergency Stop check |
jdawkins | 0:e42d1cc6513e | 35 | /*if(pc.readable()) { //if any key is hit turn of motor and break while loop |
jdawkins | 0:e42d1cc6513e | 36 | mot_control(1, 0.0); |
jdawkins | 0:e42d1cc6513e | 37 | break; |
jdawkins | 0:e42d1cc6513e | 38 | }*/ |
jdawkins | 0:e42d1cc6513e | 39 | // read analog to digital chip for presssure sensors |
jdawkins | 0:e42d1cc6513e | 40 | if(run_flag) { |
jdawkins | 0:e42d1cc6513e | 41 | float tank2 = read_max1270_volts(0, 1, 1); //chan, range, bipol |
jdawkins | 0:e42d1cc6513e | 42 | float tank1 = read_max1270_volts(1, 1, 1); |
jdawkins | 0:e42d1cc6513e | 43 | |
jdawkins | 0:e42d1cc6513e | 44 | //Logic to exit loop if tank gets too full |
jdawkins | 0:e42d1cc6513e | 45 | if(tank1 > 4.1 || tank2 > 4.1) { |
jdawkins | 0:e42d1cc6513e | 46 | pc.printf("%.2f, %.3f, %.3f, %.3f\r\n", t.read(),tank1,tank2,pwm); |
jdawkins | 0:e42d1cc6513e | 47 | run_flag = false; |
jdawkins | 0:e42d1cc6513e | 48 | break; |
jdawkins | 0:e42d1cc6513e | 49 | } |
jdawkins | 0:e42d1cc6513e | 50 | |
jdawkins | 0:e42d1cc6513e | 51 | |
jdawkins | 0:e42d1cc6513e | 52 | // Saturate PWM command to send no more than maximum value |
jdawkins | 0:e42d1cc6513e | 53 | if(pwm>1.0) { |
jdawkins | 0:e42d1cc6513e | 54 | pwm =1.0; |
jdawkins | 0:e42d1cc6513e | 55 | } |
jdawkins | 0:e42d1cc6513e | 56 | if(pwm<-1.0) { |
jdawkins | 0:e42d1cc6513e | 57 | pwm = -1.0; |
jdawkins | 0:e42d1cc6513e | 58 | } |
jdawkins | 0:e42d1cc6513e | 59 | |
jdawkins | 0:e42d1cc6513e | 60 | //Set the new output. |
jdawkins | 0:e42d1cc6513e | 61 | mot_control(1, -pwm); // negative value required due to polarity of pump motor |
jdawkins | 0:e42d1cc6513e | 62 | |
jdawkins | 0:e42d1cc6513e | 63 | float log_dt = t.read()- log_timer; |
jdawkins | 0:e42d1cc6513e | 64 | float time = t.read() -log_start; |
jdawkins | 0:e42d1cc6513e | 65 | if( log_dt >= (1.0/LOG_FREQ)) { //If log sample time has passed since last write |
jdawkins | 0:e42d1cc6513e | 66 | log_timer = t.read(); |
jdawkins | 0:e42d1cc6513e | 67 | pc.printf("%.2f,%.3f,%.3f,%.3f\r\n",time,tank1,tank2,pwm); //write data to screen |
jdawkins | 0:e42d1cc6513e | 68 | // xbee.printf("%.2f,%.3f,%.3f,%.3f\r\n",time,tank1,tank2,pwm); //write data to screen |
jdawkins | 0:e42d1cc6513e | 69 | |
jdawkins | 0:e42d1cc6513e | 70 | } |
jdawkins | 0:e42d1cc6513e | 71 | |
jdawkins | 0:e42d1cc6513e | 72 | led1 = !led1; |
jdawkins | 0:e42d1cc6513e | 73 | led2 = !led2; |
jdawkins | 0:e42d1cc6513e | 74 | led3 = !led3; |
jdawkins | 0:e42d1cc6513e | 75 | led4 = !led4; |
jdawkins | 0:e42d1cc6513e | 76 | // float loop_dur = (t.read()-loop_time); |
jdawkins | 0:e42d1cc6513e | 77 | // printf("Loop Time %f %f %f\r\n", t.read()-loop_time, dt, loop_dur); |
jdawkins | 0:e42d1cc6513e | 78 | wait(dt); //Wait for sample time of loop frequency |
jdawkins | 0:e42d1cc6513e | 79 | |
jdawkins | 0:e42d1cc6513e | 80 | } //if run flag |
jdawkins | 0:e42d1cc6513e | 81 | else { // Slow flash LED When system is not running |
jdawkins | 0:e42d1cc6513e | 82 | led1 = !led1; |
jdawkins | 0:e42d1cc6513e | 83 | led2 = !led2; |
jdawkins | 0:e42d1cc6513e | 84 | led3 = !led3; |
jdawkins | 0:e42d1cc6513e | 85 | led4 = !led4; |
jdawkins | 0:e42d1cc6513e | 86 | mot_control(1, 0); // negative value required due to polarity of pump motor |
jdawkins | 0:e42d1cc6513e | 87 | wait(0.5); |
jdawkins | 0:e42d1cc6513e | 88 | } |
jdawkins | 0:e42d1cc6513e | 89 | }//while |
jdawkins | 0:e42d1cc6513e | 90 | }//main |
jdawkins | 0:e42d1cc6513e | 91 | |
jdawkins | 0:e42d1cc6513e | 92 | bool readAndParse(Serial *port) |
jdawkins | 0:e42d1cc6513e | 93 | { |
jdawkins | 0:e42d1cc6513e | 94 | |
jdawkins | 0:e42d1cc6513e | 95 | char msg[64]; |
jdawkins | 0:e42d1cc6513e | 96 | int i = 0; |
jdawkins | 0:e42d1cc6513e | 97 | |
jdawkins | 0:e42d1cc6513e | 98 | if(port->readable()) { |
jdawkins | 0:e42d1cc6513e | 99 | char c = port->getc(); |
jdawkins | 0:e42d1cc6513e | 100 | if(c=='$') { |
jdawkins | 0:e42d1cc6513e | 101 | msg[i] = c; |
jdawkins | 0:e42d1cc6513e | 102 | i++; |
jdawkins | 0:e42d1cc6513e | 103 | while(1) { |
jdawkins | 0:e42d1cc6513e | 104 | msg[i] = port->getc(); |
jdawkins | 0:e42d1cc6513e | 105 | if(msg[i]=='\n' || msg[i]=='\r') { |
jdawkins | 0:e42d1cc6513e | 106 | break; |
jdawkins | 0:e42d1cc6513e | 107 | } |
jdawkins | 0:e42d1cc6513e | 108 | i++; |
jdawkins | 0:e42d1cc6513e | 109 | } |
jdawkins | 0:e42d1cc6513e | 110 | } |
jdawkins | 0:e42d1cc6513e | 111 | } |
jdawkins | 0:e42d1cc6513e | 112 | |
jdawkins | 0:e42d1cc6513e | 113 | if(!strncmp(msg, "$RUN", 4)) { |
jdawkins | 0:e42d1cc6513e | 114 | run_flag = true; |
jdawkins | 0:e42d1cc6513e | 115 | log_timer = 0; |
jdawkins | 0:e42d1cc6513e | 116 | log_start = t.read(); |
jdawkins | 0:e42d1cc6513e | 117 | return true; |
jdawkins | 0:e42d1cc6513e | 118 | } else if(!strncmp(msg, "$STOP", 5)) { |
jdawkins | 0:e42d1cc6513e | 119 | mot_control(1, 0.0); // Turn off Pump once test is finished |
jdawkins | 0:e42d1cc6513e | 120 | run_flag = false; |
jdawkins | 0:e42d1cc6513e | 121 | return true; |
jdawkins | 0:e42d1cc6513e | 122 | |
jdawkins | 0:e42d1cc6513e | 123 | } else if(!strncmp(msg, "$PWM", 4)) { |
jdawkins | 0:e42d1cc6513e | 124 | sscanf(msg,"$PWM,%f\n",&pwm); |
jdawkins | 0:e42d1cc6513e | 125 | return true; |
jdawkins | 0:e42d1cc6513e | 126 | } else { |
jdawkins | 0:e42d1cc6513e | 127 | return false; |
jdawkins | 0:e42d1cc6513e | 128 | } |
jdawkins | 0:e42d1cc6513e | 129 | |
jdawkins | 0:e42d1cc6513e | 130 | } |
jdawkins | 0:e42d1cc6513e | 131 |