Skeleton code for ES306 coupled Tanks experiment.
Dependencies: mbed mbedWSEsbc
main.cpp@0:76487bb2de87, 2017-03-07 (annotated)
- Committer:
- jdawkins
- Date:
- Tue Mar 07 14:44:39 2017 +0000
- Revision:
- 0:76487bb2de87
Initial Commit for ES306 Coupled Tank Control Experiment skeleton program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jdawkins | 0:76487bb2de87 | 1 | #include "mbed.h" |
jdawkins | 0:76487bb2de87 | 2 | #include "mbedWSEsbc.h" |
jdawkins | 0:76487bb2de87 | 3 | |
jdawkins | 0:76487bb2de87 | 4 | #define PI 3.14159 |
jdawkins | 0:76487bb2de87 | 5 | #define LOG_FREQ 2.0 |
jdawkins | 0:76487bb2de87 | 6 | #define CTRL_FREQ 200.0 |
jdawkins | 0:76487bb2de87 | 7 | |
jdawkins | 0:76487bb2de87 | 8 | |
jdawkins | 0:76487bb2de87 | 9 | DigitalOut myled(LED1); |
jdawkins | 0:76487bb2de87 | 10 | |
jdawkins | 0:76487bb2de87 | 11 | int main() |
jdawkins | 0:76487bb2de87 | 12 | { |
jdawkins | 0:76487bb2de87 | 13 | float log_timer = 0; //initialize variable for log timer |
jdawkins | 0:76487bb2de87 | 14 | bool run_flag = false; |
jdawkins | 0:76487bb2de87 | 15 | bool run_ctrl = false; |
jdawkins | 0:76487bb2de87 | 16 | |
jdawkins | 0:76487bb2de87 | 17 | mbedWSEsbcInit(9600); //Initialize the mbed WSE Project SBC |
jdawkins | 0:76487bb2de87 | 18 | wait(.2); //delay at beginning for voltage settle purposes |
jdawkins | 0:76487bb2de87 | 19 | t.start(); // Set up timer |
jdawkins | 0:76487bb2de87 | 20 | |
jdawkins | 0:76487bb2de87 | 21 | pc.printf("Quanser Twin Tank Control Data Aquisition Program\r\n"); |
jdawkins | 0:76487bb2de87 | 22 | |
jdawkins | 0:76487bb2de87 | 23 | t.reset(); // zero timer |
jdawkins | 0:76487bb2de87 | 24 | float sampT = t.read(); |
jdawkins | 0:76487bb2de87 | 25 | float tstop = 180; //initialize stop time |
jdawkins | 0:76487bb2de87 | 26 | float dc = 0; //initialize dc variable |
jdawkins | 0:76487bb2de87 | 27 | float dt = 1/CTRL_FREQ; // set control loop sample time |
jdawkins | 0:76487bb2de87 | 28 | |
jdawkins | 0:76487bb2de87 | 29 | while(1) { |
jdawkins | 0:76487bb2de87 | 30 | |
jdawkins | 0:76487bb2de87 | 31 | if(pc.readable()) { |
jdawkins | 0:76487bb2de87 | 32 | char c = pc.getc(); |
jdawkins | 0:76487bb2de87 | 33 | if(c == 'H' || c == 'h') { |
jdawkins | 0:76487bb2de87 | 34 | pc.printf("Command List...\r\n"); |
jdawkins | 0:76487bb2de87 | 35 | pc.printf("c - run closed loop controller\r\n"); |
jdawkins | 0:76487bb2de87 | 36 | pc.printf("o - run open loop control\r\n"); |
jdawkins | 0:76487bb2de87 | 37 | pc.printf("h - display this prompt and exit\r\n"); |
jdawkins | 0:76487bb2de87 | 38 | pc.printf("Enter Command\r\n"); |
jdawkins | 0:76487bb2de87 | 39 | |
jdawkins | 0:76487bb2de87 | 40 | } |
jdawkins | 0:76487bb2de87 | 41 | if(c == 'C' || c == 'c') { |
jdawkins | 0:76487bb2de87 | 42 | |
jdawkins | 0:76487bb2de87 | 43 | pc.printf("Enter test duration in seconds...\r\n"); |
jdawkins | 0:76487bb2de87 | 44 | pc.scanf("%f",&tstop); |
jdawkins | 0:76487bb2de87 | 45 | pc.printf("Test duration set to %.1f seconds.\r\n",tstop); |
jdawkins | 0:76487bb2de87 | 46 | |
jdawkins | 0:76487bb2de87 | 47 | pc.printf("Running Closed Loop Control for %.1f seconds, press any key to stop test...\r\n",tstop); |
jdawkins | 0:76487bb2de87 | 48 | run_flag = true; |
jdawkins | 0:76487bb2de87 | 49 | run_ctrl = true; |
jdawkins | 0:76487bb2de87 | 50 | } |
jdawkins | 0:76487bb2de87 | 51 | |
jdawkins | 0:76487bb2de87 | 52 | if(c == 'O' || c == 'o') { |
jdawkins | 0:76487bb2de87 | 53 | |
jdawkins | 0:76487bb2de87 | 54 | pc.printf("Enter desired duty cycle for test as a decimal between 0.00-1.00 \r\n",tstop); |
jdawkins | 0:76487bb2de87 | 55 | pc.scanf("%f",&dc); |
jdawkins | 0:76487bb2de87 | 56 | pc.printf("Duty Cycle is set to %.2f \r\n",dc); |
jdawkins | 0:76487bb2de87 | 57 | |
jdawkins | 0:76487bb2de87 | 58 | pc.printf("Enter test duration in seconds...\r\n"); |
jdawkins | 0:76487bb2de87 | 59 | pc.scanf("%f",&tstop); |
jdawkins | 0:76487bb2de87 | 60 | pc.printf("Test duration set to %.1f seconds.\r\n",tstop); |
jdawkins | 0:76487bb2de87 | 61 | |
jdawkins | 0:76487bb2de87 | 62 | pc.printf("Running test for %.1f seconds, press any key to stop test...\r\n",tstop); |
jdawkins | 0:76487bb2de87 | 63 | run_flag = true; |
jdawkins | 0:76487bb2de87 | 64 | run_ctrl = false; |
jdawkins | 0:76487bb2de87 | 65 | } |
jdawkins | 0:76487bb2de87 | 66 | |
jdawkins | 0:76487bb2de87 | 67 | |
jdawkins | 0:76487bb2de87 | 68 | |
jdawkins | 0:76487bb2de87 | 69 | if(run_flag) { |
jdawkins | 0:76487bb2de87 | 70 | |
jdawkins | 0:76487bb2de87 | 71 | //Prime the pump for a few seconds |
jdawkins | 0:76487bb2de87 | 72 | mot_control(1,-1.0); |
jdawkins | 0:76487bb2de87 | 73 | wait(1); |
jdawkins | 0:76487bb2de87 | 74 | t.reset(); |
jdawkins | 0:76487bb2de87 | 75 | log_timer = 0; |
jdawkins | 0:76487bb2de87 | 76 | while(t.read() < tstop) { |
jdawkins | 0:76487bb2de87 | 77 | |
jdawkins | 0:76487bb2de87 | 78 | //Emergency Stop check |
jdawkins | 0:76487bb2de87 | 79 | if(pc.readable()) { //if any key is hit turn of motor and break while loop |
jdawkins | 0:76487bb2de87 | 80 | mot_control(1, 0.0); |
jdawkins | 0:76487bb2de87 | 81 | break; |
jdawkins | 0:76487bb2de87 | 82 | } |
jdawkins | 0:76487bb2de87 | 83 | |
jdawkins | 0:76487bb2de87 | 84 | // read analog to digital chip for presssure sensors |
jdawkins | 0:76487bb2de87 | 85 | float tank2 = read_max1270_volts(0, 1, 1); //chan, range, bipol |
jdawkins | 0:76487bb2de87 | 86 | float tank1 = read_max1270_volts(1, 1, 1); |
jdawkins | 0:76487bb2de87 | 87 | |
jdawkins | 0:76487bb2de87 | 88 | //Logic to exit loop if tank gets too full |
jdawkins | 0:76487bb2de87 | 89 | if(tank1 > 4.1 || tank2 > 4.1) { |
jdawkins | 0:76487bb2de87 | 90 | pc.printf("%.2f, %.3f, %.3f, %.3f\r\n", t.read(),tank1,tank2,dc); |
jdawkins | 0:76487bb2de87 | 91 | break; |
jdawkins | 0:76487bb2de87 | 92 | } |
jdawkins | 0:76487bb2de87 | 93 | |
jdawkins | 0:76487bb2de87 | 94 | //Convert Voltage to Height |
jdawkins | 0:76487bb2de87 | 95 | |
jdawkins | 0:76487bb2de87 | 96 | |
jdawkins | 0:76487bb2de87 | 97 | if(run_ctrl) { //If controller is active |
jdawkins | 0:76487bb2de87 | 98 | |
jdawkins | 0:76487bb2de87 | 99 | |
jdawkins | 0:76487bb2de87 | 100 | // Control Law Goes Here |
jdawkins | 0:76487bb2de87 | 101 | |
jdawkins | 0:76487bb2de87 | 102 | |
jdawkins | 0:76487bb2de87 | 103 | //Convert flow rate to duty cycle; |
jdawkins | 0:76487bb2de87 | 104 | |
jdawkins | 0:76487bb2de87 | 105 | }else{ // if open loop test |
jdawkins | 0:76487bb2de87 | 106 | |
jdawkins | 0:76487bb2de87 | 107 | } |
jdawkins | 0:76487bb2de87 | 108 | |
jdawkins | 0:76487bb2de87 | 109 | |
jdawkins | 0:76487bb2de87 | 110 | // Saturate dc command to send no more than maximum value |
jdawkins | 0:76487bb2de87 | 111 | if(dc>1.0) { |
jdawkins | 0:76487bb2de87 | 112 | dc =1.0; |
jdawkins | 0:76487bb2de87 | 113 | } |
jdawkins | 0:76487bb2de87 | 114 | if(dc<-1.0) { |
jdawkins | 0:76487bb2de87 | 115 | dc = -1.0; |
jdawkins | 0:76487bb2de87 | 116 | } |
jdawkins | 0:76487bb2de87 | 117 | |
jdawkins | 0:76487bb2de87 | 118 | //Set the new output. |
jdawkins | 0:76487bb2de87 | 119 | |
jdawkins | 0:76487bb2de87 | 120 | mot_control(1, -dc); // negative value required due to polarity of pump motor |
jdawkins | 0:76487bb2de87 | 121 | |
jdawkins | 0:76487bb2de87 | 122 | |
jdawkins | 0:76487bb2de87 | 123 | if( (t.read()- log_timer) >= (1.0/LOG_FREQ)) { //If log sample time has passed since last write |
jdawkins | 0:76487bb2de87 | 124 | pc.printf("%.2f, %.3f, %.3f, %.3f\r\n", t.read(),tank1,tank2,dc); //write data to scren |
jdawkins | 0:76487bb2de87 | 125 | log_timer = t.read(); |
jdawkins | 0:76487bb2de87 | 126 | } |
jdawkins | 0:76487bb2de87 | 127 | |
jdawkins | 0:76487bb2de87 | 128 | led3=!led3; //Flash Led to Indicate program is running |
jdawkins | 0:76487bb2de87 | 129 | wait(dt); //Wait for sample time of loop frequency |
jdawkins | 0:76487bb2de87 | 130 | } // while t < tstop |
jdawkins | 0:76487bb2de87 | 131 | mot_control(1, 0.0); // Turn off Pump once test is finished |
jdawkins | 0:76487bb2de87 | 132 | run_flag = false; |
jdawkins | 0:76487bb2de87 | 133 | run_ctrl = false; |
jdawkins | 0:76487bb2de87 | 134 | pc.printf("\r\nEnter Command\r\n"); |
jdawkins | 0:76487bb2de87 | 135 | } //if run flag |
jdawkins | 0:76487bb2de87 | 136 | } // if pc.readable |
jdawkins | 0:76487bb2de87 | 137 | }//while |
jdawkins | 0:76487bb2de87 | 138 | }//main |