![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Code for EW301 CTS Lab with state machine and operating point
Dependencies: mbed mbedWSEsbc
main.cpp@2:ee7140e825af, 2021-10-28 (annotated)
- Committer:
- jdawkins
- Date:
- Thu Oct 28 18:16:48 2021 +0000
- Revision:
- 2:ee7140e825af
- Parent:
- 1:d3fac5783cc3
Removed extraneous files
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 | 1:d3fac5783cc3 | 7 | #define STOP 0 |
jdawkins | 1:d3fac5783cc3 | 8 | #define IDLE 1 |
jdawkins | 1:d3fac5783cc3 | 9 | #define RUN 2 |
jdawkins | 0:76487bb2de87 | 10 | |
jdawkins | 0:76487bb2de87 | 11 | DigitalOut myled(LED1); |
jdawkins | 0:76487bb2de87 | 12 | |
jdawkins | 0:76487bb2de87 | 13 | int main() |
jdawkins | 0:76487bb2de87 | 14 | { |
jdawkins | 0:76487bb2de87 | 15 | float log_timer = 0; //initialize variable for log timer |
jdawkins | 0:76487bb2de87 | 16 | bool run_flag = false; |
jdawkins | 0:76487bb2de87 | 17 | bool run_ctrl = false; |
jdawkins | 0:76487bb2de87 | 18 | |
jdawkins | 0:76487bb2de87 | 19 | mbedWSEsbcInit(9600); //Initialize the mbed WSE Project SBC |
jdawkins | 0:76487bb2de87 | 20 | wait(.2); //delay at beginning for voltage settle purposes |
jdawkins | 0:76487bb2de87 | 21 | t.start(); // Set up timer |
jdawkins | 0:76487bb2de87 | 22 | |
jdawkins | 0:76487bb2de87 | 23 | pc.printf("Quanser Twin Tank Control Data Aquisition Program\r\n"); |
jdawkins | 0:76487bb2de87 | 24 | |
jdawkins | 0:76487bb2de87 | 25 | t.reset(); // zero timer |
jdawkins | 0:76487bb2de87 | 26 | float sampT = t.read(); |
jdawkins | 1:d3fac5783cc3 | 27 | float tstop = 600; //initialize stop time |
jdawkins | 0:76487bb2de87 | 28 | float dc = 0; //initialize dc variable |
jdawkins | 1:d3fac5783cc3 | 29 | float q = 0; |
jdawkins | 1:d3fac5783cc3 | 30 | float q0 = 20; |
jdawkins | 0:76487bb2de87 | 31 | float dt = 1/CTRL_FREQ; // set control loop sample time |
jdawkins | 1:d3fac5783cc3 | 32 | float h1 = 0; |
jdawkins | 1:d3fac5783cc3 | 33 | float h2 = 0; |
jdawkins | 1:d3fac5783cc3 | 34 | float freq = 1; |
jdawkins | 1:d3fac5783cc3 | 35 | float T = 10; |
jdawkins | 1:d3fac5783cc3 | 36 | float amp = 5; |
jdawkins | 1:d3fac5783cc3 | 37 | bool freq_resp = false; |
jdawkins | 1:d3fac5783cc3 | 38 | |
jdawkins | 1:d3fac5783cc3 | 39 | bool use_int = false; |
jdawkins | 1:d3fac5783cc3 | 40 | int run_state = 0; |
jdawkins | 0:76487bb2de87 | 41 | |
jdawkins | 0:76487bb2de87 | 42 | while(1) { |
jdawkins | 0:76487bb2de87 | 43 | |
jdawkins | 0:76487bb2de87 | 44 | if(pc.readable()) { |
jdawkins | 0:76487bb2de87 | 45 | char c = pc.getc(); |
jdawkins | 1:d3fac5783cc3 | 46 | |
jdawkins | 0:76487bb2de87 | 47 | if(c == 'H' || c == 'h') { |
jdawkins | 0:76487bb2de87 | 48 | pc.printf("Command List...\r\n"); |
jdawkins | 1:d3fac5783cc3 | 49 | // pc.printf("a - set Amplitude\r\n"); |
jdawkins | 1:d3fac5783cc3 | 50 | pc.printf("f - Run Frequency Resonse\r\n"); |
jdawkins | 1:d3fac5783cc3 | 51 | pc.printf("t - Run Time Response\r\n"); |
jdawkins | 1:d3fac5783cc3 | 52 | pc.printf("i - Return to Operating Point\r\n"); |
jdawkins | 1:d3fac5783cc3 | 53 | // pc.printf("o - Set Operating Point flow\r\n"); |
jdawkins | 1:d3fac5783cc3 | 54 | pc.printf("r - Run Test\r\n"); |
jdawkins | 1:d3fac5783cc3 | 55 | pc.printf("s - Stop Test\r\n"); |
jdawkins | 1:d3fac5783cc3 | 56 | pc.printf("h - display this prompt and exit\r\n"); |
jdawkins | 0:76487bb2de87 | 57 | pc.printf("Enter Command\r\n"); |
jdawkins | 0:76487bb2de87 | 58 | |
jdawkins | 0:76487bb2de87 | 59 | } |
jdawkins | 1:d3fac5783cc3 | 60 | |
jdawkins | 1:d3fac5783cc3 | 61 | /* if(c == 'A' || c == 'a') { |
jdawkins | 1:d3fac5783cc3 | 62 | |
jdawkins | 1:d3fac5783cc3 | 63 | pc.printf("Enter the Amplitude of dQ in (cm^/s)...\r\n"); |
jdawkins | 1:d3fac5783cc3 | 64 | pc.scanf("%f",&); |
jdawkins | 1:d3fac5783cc3 | 65 | pc.printf("Amplitude of delta flow set to %.1f (cm^3/s).\r\n",amp); |
jdawkins | 1:d3fac5783cc3 | 66 | |
jdawkins | 1:d3fac5783cc3 | 67 | }*/ |
jdawkins | 1:d3fac5783cc3 | 68 | |
jdawkins | 1:d3fac5783cc3 | 69 | if(c == 'F' || c == 'f') { |
jdawkins | 1:d3fac5783cc3 | 70 | |
jdawkins | 1:d3fac5783cc3 | 71 | pc.printf("Enter the Period of Oscillation in (s)...\r\n"); |
jdawkins | 1:d3fac5783cc3 | 72 | pc.scanf("%f",&T); |
jdawkins | 1:d3fac5783cc3 | 73 | pc.printf("Period of oscillation set to %.1f seconds.\r\n",T); |
jdawkins | 1:d3fac5783cc3 | 74 | pc.printf("Test Set to Frequency Response, Press R to begin...\r\n",tstop); |
jdawkins | 0:76487bb2de87 | 75 | |
jdawkins | 1:d3fac5783cc3 | 76 | freq_resp = true; |
jdawkins | 0:76487bb2de87 | 77 | } |
jdawkins | 1:d3fac5783cc3 | 78 | /* if(c == 'O' || c == 'o') { |
jdawkins | 0:76487bb2de87 | 79 | |
jdawkins | 1:d3fac5783cc3 | 80 | pc.printf("Enter the Operating Flow Rate Q0 in (cm^/s)...\r\n"); |
jdawkins | 1:d3fac5783cc3 | 81 | pc.scanf("%f",&q0); |
jdawkins | 0:76487bb2de87 | 82 | |
jdawkins | 1:d3fac5783cc3 | 83 | }*/ |
jdawkins | 1:d3fac5783cc3 | 84 | if(c == 'T' || c == 't') { |
jdawkins | 0:76487bb2de87 | 85 | |
jdawkins | 1:d3fac5783cc3 | 86 | pc.printf("Test Set to Time Response, Press R to begin...\r\n",tstop); |
jdawkins | 1:d3fac5783cc3 | 87 | freq_resp = false; |
jdawkins | 1:d3fac5783cc3 | 88 | |
jdawkins | 0:76487bb2de87 | 89 | } |
jdawkins | 1:d3fac5783cc3 | 90 | if(c == 'I' || c =='i') { |
jdawkins | 1:d3fac5783cc3 | 91 | run_state = IDLE; |
jdawkins | 1:d3fac5783cc3 | 92 | t.reset(); |
jdawkins | 1:d3fac5783cc3 | 93 | log_timer = 0; |
jdawkins | 1:d3fac5783cc3 | 94 | pc.printf("Running Returning to OP \r\n"); |
jdawkins | 0:76487bb2de87 | 95 | |
jdawkins | 1:d3fac5783cc3 | 96 | } |
jdawkins | 1:d3fac5783cc3 | 97 | if(c == 'R' || c == 'r') { |
jdawkins | 0:76487bb2de87 | 98 | |
jdawkins | 1:d3fac5783cc3 | 99 | if(freq_resp) { |
jdawkins | 1:d3fac5783cc3 | 100 | pc.printf("Running Test Frequency Response for %.1f seconds...\r\n",tstop); |
jdawkins | 1:d3fac5783cc3 | 101 | } else { |
jdawkins | 1:d3fac5783cc3 | 102 | pc.printf("Running Test Step Response for %.1f seconds...\r\n",tstop); |
jdawkins | 1:d3fac5783cc3 | 103 | } |
jdawkins | 1:d3fac5783cc3 | 104 | run_state = RUN; |
jdawkins | 0:76487bb2de87 | 105 | t.reset(); |
jdawkins | 0:76487bb2de87 | 106 | log_timer = 0; |
jdawkins | 0:76487bb2de87 | 107 | |
jdawkins | 1:d3fac5783cc3 | 108 | } |
jdawkins | 1:d3fac5783cc3 | 109 | if(c == 'S' || c == 's') { |
jdawkins | 1:d3fac5783cc3 | 110 | pc.printf("Test Complete, Returning to OP\r\n"); |
jdawkins | 1:d3fac5783cc3 | 111 | run_state = STOP; |
jdawkins | 1:d3fac5783cc3 | 112 | // t.reset(); |
jdawkins | 1:d3fac5783cc3 | 113 | } |
jdawkins | 1:d3fac5783cc3 | 114 | } // if pc.readable |
jdawkins | 0:76487bb2de87 | 115 | |
jdawkins | 1:d3fac5783cc3 | 116 | // read analog to digital chip for presssure sensors |
jdawkins | 1:d3fac5783cc3 | 117 | float tank2 = read_max1270_volts(0, 1, 1); //chan, range, bipol |
jdawkins | 1:d3fac5783cc3 | 118 | float tank1 = read_max1270_volts(1, 1, 1); |
jdawkins | 0:76487bb2de87 | 119 | |
jdawkins | 1:d3fac5783cc3 | 120 | //Logic to exit loop if tank gets too full |
jdawkins | 1:d3fac5783cc3 | 121 | if(tank1 > 4.1 || tank2 > 4.1) { |
jdawkins | 1:d3fac5783cc3 | 122 | run_state = STOP; |
jdawkins | 1:d3fac5783cc3 | 123 | t.reset(); |
jdawkins | 1:d3fac5783cc3 | 124 | // break; |
jdawkins | 1:d3fac5783cc3 | 125 | } |
jdawkins | 0:76487bb2de87 | 126 | |
jdawkins | 1:d3fac5783cc3 | 127 | //Convert Voltage to Height |
jdawkins | 1:d3fac5783cc3 | 128 | h1 = 6.4*tank1; |
jdawkins | 1:d3fac5783cc3 | 129 | h2 = 6.4*tank2; |
jdawkins | 1:d3fac5783cc3 | 130 | |
jdawkins | 0:76487bb2de87 | 131 | |
jdawkins | 0:76487bb2de87 | 132 | |
jdawkins | 1:d3fac5783cc3 | 133 | switch(run_state) { |
jdawkins | 1:d3fac5783cc3 | 134 | case STOP: { |
jdawkins | 1:d3fac5783cc3 | 135 | mot_control(1, 0.0); |
jdawkins | 1:d3fac5783cc3 | 136 | break; |
jdawkins | 1:d3fac5783cc3 | 137 | } |
jdawkins | 1:d3fac5783cc3 | 138 | case IDLE: { |
jdawkins | 1:d3fac5783cc3 | 139 | |
jdawkins | 1:d3fac5783cc3 | 140 | q = q0; |
jdawkins | 1:d3fac5783cc3 | 141 | dc = 0.0076*q + 0.55; |
jdawkins | 1:d3fac5783cc3 | 142 | if( (t.read()- log_timer) >= (1.0/LOG_FREQ)) { //If log sample time has passed since last write |
jdawkins | 1:d3fac5783cc3 | 143 | pc.printf("%.2f, %.3f, %.3f, %.3f\r", t.read(),h1,h2,q); //write data to scren |
jdawkins | 1:d3fac5783cc3 | 144 | log_timer = t.read(); |
jdawkins | 1:d3fac5783cc3 | 145 | } |
jdawkins | 1:d3fac5783cc3 | 146 | mot_control(1, -dc); |
jdawkins | 1:d3fac5783cc3 | 147 | break; |
jdawkins | 0:76487bb2de87 | 148 | |
jdawkins | 1:d3fac5783cc3 | 149 | } |
jdawkins | 1:d3fac5783cc3 | 150 | case RUN: { |
jdawkins | 1:d3fac5783cc3 | 151 | |
jdawkins | 1:d3fac5783cc3 | 152 | if(t.read() > tstop) { |
jdawkins | 1:d3fac5783cc3 | 153 | run_state= IDLE; |
jdawkins | 1:d3fac5783cc3 | 154 | } |
jdawkins | 1:d3fac5783cc3 | 155 | if(freq_resp) { |
jdawkins | 0:76487bb2de87 | 156 | |
jdawkins | 1:d3fac5783cc3 | 157 | q = q0 + amp*sin((2*3.14159/T)*t.read()); |
jdawkins | 1:d3fac5783cc3 | 158 | dc = 0.0076*q + 0.55; |
jdawkins | 1:d3fac5783cc3 | 159 | } else { |
jdawkins | 1:d3fac5783cc3 | 160 | q = q0 + amp; |
jdawkins | 1:d3fac5783cc3 | 161 | dc = 0.0076*q + 0.55; |
jdawkins | 1:d3fac5783cc3 | 162 | } |
jdawkins | 1:d3fac5783cc3 | 163 | // Saturate dc command to send no more than maximum value |
jdawkins | 1:d3fac5783cc3 | 164 | if(dc>1.0) { |
jdawkins | 1:d3fac5783cc3 | 165 | dc =1.0; |
jdawkins | 1:d3fac5783cc3 | 166 | } |
jdawkins | 1:d3fac5783cc3 | 167 | if(dc<-1.0) { |
jdawkins | 1:d3fac5783cc3 | 168 | dc = -1.0; |
jdawkins | 1:d3fac5783cc3 | 169 | } |
jdawkins | 0:76487bb2de87 | 170 | |
jdawkins | 1:d3fac5783cc3 | 171 | if( (t.read()- log_timer) >= (1.0/LOG_FREQ)) { //If log sample time has passed since last write |
jdawkins | 1:d3fac5783cc3 | 172 | pc.printf("%.2f, %.3f, %.3f, %.3f\r\n", t.read(),h1,h2,q); //write data to scren |
jdawkins | 1:d3fac5783cc3 | 173 | log_timer = t.read(); |
jdawkins | 1:d3fac5783cc3 | 174 | } |
jdawkins | 0:76487bb2de87 | 175 | |
jdawkins | 1:d3fac5783cc3 | 176 | mot_control(1, -dc); // negative value required due to polarity of pump motor |
jdawkins | 1:d3fac5783cc3 | 177 | break; |
jdawkins | 0:76487bb2de87 | 178 | |
jdawkins | 1:d3fac5783cc3 | 179 | } |
jdawkins | 1:d3fac5783cc3 | 180 | default: { |
jdawkins | 1:d3fac5783cc3 | 181 | mot_control(1, 0.0); |
jdawkins | 1:d3fac5783cc3 | 182 | break; |
jdawkins | 1:d3fac5783cc3 | 183 | } |
jdawkins | 1:d3fac5783cc3 | 184 | |
jdawkins | 1:d3fac5783cc3 | 185 | } |
jdawkins | 1:d3fac5783cc3 | 186 | |
jdawkins | 1:d3fac5783cc3 | 187 | led3=!led3; //Flash Led to Indicate program is running |
jdawkins | 1:d3fac5783cc3 | 188 | wait(dt); //Wait for sample time of loop frequency |
jdawkins | 1:d3fac5783cc3 | 189 | |
jdawkins | 0:76487bb2de87 | 190 | }//while |
jdawkins | 0:76487bb2de87 | 191 | }//main |