The final program for the #include AIR robot

Dependencies:   Biquad HIDScope QEI angleandposition controlandadjust mbed

Fork of includeair by BioRobotics

Committer:
Gerth
Date:
Wed Oct 14 13:00:50 2015 +0000
Revision:
6:37c94a5e205f
Parent:
5:8ac5d0651e4d
Child:
7:7fbb2c028778
Added switch 3 to change to calibration mode (using the buttons to set arms to 0 degrees;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Gerth 0:dd66fff537d7 1 #include "mbed.h"
Gerth 1:917c07a4f3ec 2 #include "QEI.h"
Gerth 1:917c07a4f3ec 3 #include "HIDScope.h"
Gerth 1:917c07a4f3ec 4 #include "Biquad.h"
Gerth 1:917c07a4f3ec 5 #include "controlandadjust.h"
Gerth 1:917c07a4f3ec 6
Gerth 1:917c07a4f3ec 7 //info out
Gerth 1:917c07a4f3ec 8 HIDScope scope(4);
Gerth 1:917c07a4f3ec 9 Ticker scope_ticker;
Gerth 3:48438eea184e 10 const double scope_frequency=500;
Gerth 1:917c07a4f3ec 11 Serial pc(USBTX,USBRX);
Gerth 1:917c07a4f3ec 12
Gerth 1:917c07a4f3ec 13 ////////////////ENCODERS
Gerth 4:bf7765b0f612 14 QEI encoder1(D12,D13,NC,32);/// maybe use Encoder in stead of QEI, because Encoder had setposition
Gerth 1:917c07a4f3ec 15 QEI encoder2(D10,D11,NC,32);
Gerth 1:917c07a4f3ec 16
Gerth 6:37c94a5e205f 17 /////////////////////////////////CALIBRATION (MODE)
Gerth 6:37c94a5e205f 18 int modecounter=0;
Gerth 6:37c94a5e205f 19 DigitalIn changemodebutton(PTA4);
Gerth 6:37c94a5e205f 20
Gerth 1:917c07a4f3ec 21 //////////////////////////////////CONTROLLER
Gerth 1:917c07a4f3ec 22 controlandadjust mycontroller; // make a controller
Gerth 1:917c07a4f3ec 23 //controller constants
Gerth 1:917c07a4f3ec 24 float Kp=0.5;
Gerth 1:917c07a4f3ec 25 float Ki=0.01;
Gerth 1:917c07a4f3ec 26 float Kd=0.001;
Gerth 1:917c07a4f3ec 27 Ticker control_ticker;
Gerth 3:48438eea184e 28 const double control_frequency=25;
Gerth 1:917c07a4f3ec 29
Gerth 1:917c07a4f3ec 30 double error1_int=0;// storage variables for the errors
Gerth 1:917c07a4f3ec 31 double error2_int=0;
Gerth 1:917c07a4f3ec 32 double error1_prev=0;
Gerth 1:917c07a4f3ec 33 double error2_prev=0;
Gerth 1:917c07a4f3ec 34
Gerth 1:917c07a4f3ec 35 InterruptIn valuechangebutton(PTC6);//button to change controller constants
Gerth 1:917c07a4f3ec 36
Gerth 1:917c07a4f3ec 37 //safetyandthreshold
Gerth 3:48438eea184e 38 AnalogIn safety_pot(A3);//pot 2, used for the safety cutoff value for the pwm
Gerth 3:48438eea184e 39 AnalogIn threshold_pot(A2);//pot1, used to adjust threshold if signal differs per person
Gerth 1:917c07a4f3ec 40
Gerth 1:917c07a4f3ec 41 Ticker safetyandthreshold_ticker; // ticker to read potmeters
Gerth 3:48438eea184e 42 const double safetyandthreshold_frequency=1; // frequency for the ticker
Gerth 3:48438eea184e 43
Gerth 4:bf7765b0f612 44 float threshold_value=1;//initial threshold value
Gerth 1:917c07a4f3ec 45
Gerth 1:917c07a4f3ec 46 ////////////////////////////////FILTER
Gerth 1:917c07a4f3ec 47 #include "filtervalues.h"
Gerth 1:917c07a4f3ec 48 Ticker filter_ticker;
Gerth 3:48438eea184e 49 const double filter_frequency=500;
Gerth 1:917c07a4f3ec 50 Biquad myfilter1;
Gerth 1:917c07a4f3ec 51 Biquad myfilter2;
Gerth 1:917c07a4f3ec 52
Gerth 1:917c07a4f3ec 53 AnalogIn emg1_input(A0);
Gerth 1:917c07a4f3ec 54 AnalogIn emg2_input(A1);
Gerth 1:917c07a4f3ec 55
Gerth 1:917c07a4f3ec 56 double filteredsignal1=0;
Gerth 1:917c07a4f3ec 57 double filteredsignal2=0;
Gerth 5:8ac5d0651e4d 58 float filter_extragain=1;
Gerth 1:917c07a4f3ec 59
Gerth 3:48438eea184e 60 /////////////////READSIGNAL
Gerth 3:48438eea184e 61 Ticker readsignal_ticker;
Gerth 3:48438eea184e 62 const double readsignal_frequency=25;
Gerth 3:48438eea184e 63
Gerth 4:bf7765b0f612 64 DigitalOut led1(D8);
Gerth 4:bf7765b0f612 65 DigitalOut led2(D9);
Gerth 4:bf7765b0f612 66
Gerth 4:bf7765b0f612 67 //////////////////////////////// POSITION AND ANGLE SHIZZLE
Gerth 4:bf7765b0f612 68 float desired_position=0;
Gerth 4:bf7765b0f612 69 float desired_angle[]= {0,0};
Gerth 3:48438eea184e 70
Gerth 1:917c07a4f3ec 71 //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS
Gerth 1:917c07a4f3ec 72 volatile bool scopedata_go=false,
Gerth 1:917c07a4f3ec 73 control_go=false,
Gerth 1:917c07a4f3ec 74 filter_go=false,
Gerth 3:48438eea184e 75 safetyandthreshold_go=false,
Gerth 6:37c94a5e205f 76 readsignal_go=false,
Gerth 6:37c94a5e205f 77 switchedmode=false;
Gerth 1:917c07a4f3ec 78
Gerth 1:917c07a4f3ec 79 void scopedata_activate()
Gerth 1:917c07a4f3ec 80 {
Gerth 1:917c07a4f3ec 81 scopedata_go=true;
Gerth 1:917c07a4f3ec 82 }
Gerth 1:917c07a4f3ec 83 void control_activate()
Gerth 1:917c07a4f3ec 84 {
Gerth 1:917c07a4f3ec 85 control_go=true;
Gerth 1:917c07a4f3ec 86 }
Gerth 1:917c07a4f3ec 87 void filter_activate()
Gerth 1:917c07a4f3ec 88 {
Gerth 1:917c07a4f3ec 89 filter_go=true;
Gerth 1:917c07a4f3ec 90 }
Gerth 1:917c07a4f3ec 91 void safetyandthreshold_activate()
Gerth 1:917c07a4f3ec 92 {
Gerth 1:917c07a4f3ec 93 safetyandthreshold_go=true;
Gerth 1:917c07a4f3ec 94 }
Gerth 3:48438eea184e 95 void readsignal_activate()
Gerth 3:48438eea184e 96 {
Gerth 3:48438eea184e 97 readsignal_go=true;
Gerth 3:48438eea184e 98 }
Gerth 0:dd66fff537d7 99
Gerth 1:917c07a4f3ec 100
Gerth 1:917c07a4f3ec 101 ////////////////////////FUNCTIONS
Gerth 1:917c07a4f3ec 102 //gather data and send to scope
Gerth 1:917c07a4f3ec 103 void scopedata()
Gerth 1:917c07a4f3ec 104 {
Gerth 3:48438eea184e 105 scope.set(0,emg1_input.read());
Gerth 3:48438eea184e 106 scope.set(1,filteredsignal1);
Gerth 3:48438eea184e 107 scope.send();
Gerth 4:bf7765b0f612 108 }
Gerth 1:917c07a4f3ec 109 //read potmeters and adjust the safetyfactor and threshold
Gerth 1:917c07a4f3ec 110 void safetyandthreshold()
Gerth 1:917c07a4f3ec 111 {
Gerth 3:48438eea184e 112 mycontroller.cutoff((ceil (10*safety_pot.read()) )/10); // adjust the safetyfactor value between 0 and 1 rounded to 1 decimal
Gerth 3:48438eea184e 113 threshold_value=((ceil (10*threshold_pot.read()) )/10); // adjust the threshold value between 0 and 1 rounded to 1 decimal
Gerth 1:917c07a4f3ec 114 }
Gerth 1:917c07a4f3ec 115 /////filter
Gerth 1:917c07a4f3ec 116 void filtereverything()
Gerth 1:917c07a4f3ec 117 {
Gerth 1:917c07a4f3ec 118 //filter_timer.reset();
Gerth 1:917c07a4f3ec 119 // filter_timer.start();
Gerth 1:917c07a4f3ec 120 //pass1 so f1
Gerth 2:c7707856d137 121 double pass1_emg1 = myfilter1.filter(emg1_input.read(), v1_f1_emg1 , v2_f1_emg1 , a1_f1 , a2_f1 , b0_f1 , b1_f1 , b2_f1);
Gerth 2:c7707856d137 122 double pass1_emg2 = myfilter2.filter(emg2_input.read(), v1_f1_emg2 , v2_f1_emg2 , a1_f1 , a2_f1 , b0_f1 , b1_f1 , b2_f1);
Gerth 1:917c07a4f3ec 123
Gerth 1:917c07a4f3ec 124 //pass2 so f2
Gerth 2:c7707856d137 125 double pass2_emg1 = myfilter1.filter(pass1_emg1, v1_f2_emg1 , v2_f2_emg1 , a1_f2 , a2_f2 , b0_f2 , b1_f2 , b2_f2);
Gerth 2:c7707856d137 126 double pass2_emg2 = myfilter2.filter(pass1_emg2, v1_f2_emg2 , v2_f2_emg2 , a1_f2 , a2_f2 , b0_f2 , b1_f2 , b2_f2);
Gerth 1:917c07a4f3ec 127
Gerth 1:917c07a4f3ec 128 //pass3 so f3
Gerth 2:c7707856d137 129 double pass3_emg1 = myfilter1.filter(pass2_emg1, v1_f3_emg1 , v2_f3_emg1 , a1_f3 , a2_f3 , b0_f3 , b1_f3 , b2_f3);
Gerth 2:c7707856d137 130 double pass3_emg2 = myfilter2.filter(pass2_emg2, v1_f3_emg2 , v2_f3_emg2 , a1_f3 , a2_f3 , b0_f3 , b1_f3 , b2_f3);
Gerth 1:917c07a4f3ec 131
Gerth 1:917c07a4f3ec 132 //pass4 so f4
Gerth 2:c7707856d137 133 double pass4_emg1 = myfilter1.filter(pass3_emg1, v1_f4_emg1 , v2_f4_emg1 , a1_f4 , a2_f4 , b0_f4 , b1_f4 , b2_f4);
Gerth 2:c7707856d137 134 double pass4_emg2 = myfilter2.filter(pass3_emg2, v1_f4_emg2 , v2_f4_emg2 , a1_f4 , a2_f4 , b0_f4 , b1_f4 , b2_f4);
Gerth 1:917c07a4f3ec 135
Gerth 1:917c07a4f3ec 136 //pass5 so f5
Gerth 2:c7707856d137 137 double pass5_emg1 = myfilter1.filter(pass4_emg1, v1_f5_emg1 , v2_f5_emg1 , a1_f5 , a2_f5 , b0_f5 , b1_f5 , b2_f5);
Gerth 2:c7707856d137 138 double pass5_emg2 = myfilter2.filter(pass4_emg2, v1_f5_emg2 , v2_f5_emg2 , a1_f5 , a2_f5 , b0_f5 , b1_f5 , b2_f5);
Gerth 1:917c07a4f3ec 139
Gerth 1:917c07a4f3ec 140 ///// take absolute value
Gerth 2:c7707856d137 141 double pass5_emg1_abs=(fabs(pass5_emg1));
Gerth 2:c7707856d137 142 double pass5_emg2_abs=(fabs(pass5_emg2));
Gerth 1:917c07a4f3ec 143
Gerth 1:917c07a4f3ec 144 //pass6 so f6
Gerth 2:c7707856d137 145 double pass6_emg1 = myfilter1.filter(pass5_emg1_abs, v1_f6_emg1 , v2_f6_emg1 , a1_f6 , a2_f6 , b0_f6 , b1_f6 , b2_f6);
Gerth 2:c7707856d137 146 double pass6_emg2 = myfilter2.filter(pass5_emg2_abs, v1_f6_emg2 , v2_f6_emg2 , a1_f6 , a2_f6 , b0_f6 , b1_f6 , b2_f6);
Gerth 1:917c07a4f3ec 147
Gerth 1:917c07a4f3ec 148
Gerth 1:917c07a4f3ec 149 //pass7 so f7
Gerth 2:c7707856d137 150 double pass7_emg1 = myfilter1.filter(pass6_emg1, v1_f7_emg1 , v2_f7_emg1 , a1_f7 , a2_f7 , b0_f7 , b1_f7 , b2_f7);
Gerth 2:c7707856d137 151 double pass7_emg2 = myfilter2.filter(pass6_emg2, v1_f7_emg2 , v2_f7_emg2 , a1_f7 , a2_f7 , b0_f7 , b1_f7 , b2_f7);
Gerth 1:917c07a4f3ec 152
Gerth 2:c7707856d137 153 filteredsignal1=(pass7_emg1*9e11*filter_extragain);
Gerth 2:c7707856d137 154 filteredsignal2=(pass7_emg2*9e11*filter_extragain);
Gerth 1:917c07a4f3ec 155
Gerth 1:917c07a4f3ec 156 //filter_timer.stop();
Gerth 1:917c07a4f3ec 157 }
Gerth 1:917c07a4f3ec 158
Gerth 3:48438eea184e 159 void control()
Gerth 3:48438eea184e 160 {
Gerth 1:917c07a4f3ec 161 ///call desired controller here
Gerth 3:48438eea184e 162 }
Gerth 1:917c07a4f3ec 163
Gerth 1:917c07a4f3ec 164 //adjust controller values when sw2 is pressed
Gerth 1:917c07a4f3ec 165 void valuechange()
Gerth 1:917c07a4f3ec 166 {
Gerth 3:48438eea184e 167 mycontroller.STOP();
Gerth 5:8ac5d0651e4d 168 pc.printf("KP is now %f, enter new value\n",Kp);
Gerth 1:917c07a4f3ec 169 pc.scanf("%f", &Kp);
Gerth 1:917c07a4f3ec 170
Gerth 1:917c07a4f3ec 171 pc.printf("KI is now %f, enter new value\n",Ki);
Gerth 1:917c07a4f3ec 172 pc.scanf("%f", &Ki);
Gerth 1:917c07a4f3ec 173
Gerth 1:917c07a4f3ec 174 pc.printf("KD is now %f, enter new value\n",Kd);
Gerth 5:8ac5d0651e4d 175 pc.scanf("%f", &Kd);
Gerth 3:48438eea184e 176
Gerth 3:48438eea184e 177 pc.printf("Extra gain is now %f, enter new value\n",filter_extragain);
Gerth 5:8ac5d0651e4d 178 pc.scanf("%f", &filter_extragain);
Gerth 1:917c07a4f3ec 179 }
Gerth 3:48438eea184e 180 void readsignal()
Gerth 3:48438eea184e 181 {
Gerth 4:bf7765b0f612 182 if (filteredsignal1>=threshold_value && filteredsignal2>=threshold_value) {
Gerth 4:bf7765b0f612 183 led1=led2=1;
Gerth 4:bf7765b0f612 184 } else if (filteredsignal1>=threshold_value && filteredsignal2<=threshold_value) {
Gerth 4:bf7765b0f612 185 led1=1;
Gerth 4:bf7765b0f612 186 led2=0;
Gerth 4:bf7765b0f612 187 } else if (filteredsignal1<=threshold_value && filteredsignal2>=threshold_value) {
Gerth 4:bf7765b0f612 188 led1=0;
Gerth 4:bf7765b0f612 189 led2=1;
Gerth 3:48438eea184e 190 } else {
Gerth 4:bf7765b0f612 191 led1=led2=0;
Gerth 3:48438eea184e 192 }
Gerth 4:bf7765b0f612 193
Gerth 3:48438eea184e 194 }
Gerth 3:48438eea184e 195
Gerth 6:37c94a5e205f 196 void changemode()
Gerth 6:37c94a5e205f 197 {
Gerth 6:37c94a5e205f 198 switchedmode=true;
Gerth 6:37c94a5e205f 199 modecounter++;
Gerth 6:37c94a5e205f 200 if (modecounter==3) {
Gerth 6:37c94a5e205f 201 modecounter=0;
Gerth 6:37c94a5e205f 202 } else {
Gerth 6:37c94a5e205f 203 modecounter=modecounter;
Gerth 6:37c94a5e205f 204 }
Gerth 6:37c94a5e205f 205 wait(1);
Gerth 6:37c94a5e205f 206 }
Gerth 0:dd66fff537d7 207
Gerth 0:dd66fff537d7 208 int main()
Gerth 0:dd66fff537d7 209 {
Gerth 1:917c07a4f3ec 210 //tickers
Gerth 1:917c07a4f3ec 211 safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency);
Gerth 1:917c07a4f3ec 212 filter_ticker.attach(&filter_activate,1.0/filter_frequency);
Gerth 1:917c07a4f3ec 213 control_ticker.attach(&control_activate,1.0/control_frequency);
Gerth 1:917c07a4f3ec 214 scope_ticker.attach(&scopedata_activate,1.0/scope_frequency);
Gerth 3:48438eea184e 215 readsignal_ticker.attach(&readsignal_activate, 1.0/readsignal_frequency);
Gerth 1:917c07a4f3ec 216
Gerth 1:917c07a4f3ec 217 while(1) {
Gerth 6:37c94a5e205f 218 if (changemodebutton==0) {
Gerth 6:37c94a5e205f 219 changemode();
Gerth 1:917c07a4f3ec 220 }
Gerth 6:37c94a5e205f 221 if(modecounter==0) {//normal running mode
Gerth 6:37c94a5e205f 222 if (switchedmode==true) {
Gerth 6:37c94a5e205f 223 encoder1.reset();
Gerth 6:37c94a5e205f 224 encoder2.reset();
Gerth 6:37c94a5e205f 225 pc.printf("Program running\n");//
Gerth 6:37c94a5e205f 226 switchedmode=false;
Gerth 6:37c94a5e205f 227 }
Gerth 6:37c94a5e205f 228 if (scopedata_go==true) {
Gerth 6:37c94a5e205f 229 scopedata();
Gerth 6:37c94a5e205f 230 scopedata_go=false;
Gerth 6:37c94a5e205f 231 }
Gerth 6:37c94a5e205f 232 if (filter_go==true) {
Gerth 6:37c94a5e205f 233 filtereverything();
Gerth 6:37c94a5e205f 234 filter_go=false;
Gerth 6:37c94a5e205f 235 }
Gerth 6:37c94a5e205f 236 if (safetyandthreshold_go==true) {
Gerth 6:37c94a5e205f 237 safetyandthreshold();
Gerth 6:37c94a5e205f 238 safetyandthreshold_go=false;
Gerth 6:37c94a5e205f 239 }
Gerth 6:37c94a5e205f 240 if (control_go==true) {
Gerth 6:37c94a5e205f 241 control();
Gerth 6:37c94a5e205f 242 control_go=false;
Gerth 6:37c94a5e205f 243 }
Gerth 6:37c94a5e205f 244 if (readsignal_go==true) {
Gerth 6:37c94a5e205f 245 readsignal();
Gerth 6:37c94a5e205f 246 readsignal_go=false;
Gerth 6:37c94a5e205f 247 }
Gerth 6:37c94a5e205f 248 valuechangebutton.fall(&valuechange);
Gerth 1:917c07a4f3ec 249 }
Gerth 6:37c94a5e205f 250 if (modecounter==1) {
Gerth 6:37c94a5e205f 251 if(switchedmode==true) {
Gerth 6:37c94a5e205f 252 pc.printf("Calibration mode! Use buttons to move rigth arm to 0 degrees\n");
Gerth 6:37c94a5e205f 253 switchedmode=false;
Gerth 6:37c94a5e205f 254 }
Gerth 6:37c94a5e205f 255
Gerth 1:917c07a4f3ec 256 }
Gerth 6:37c94a5e205f 257
Gerth 6:37c94a5e205f 258 if (modecounter==2) {
Gerth 6:37c94a5e205f 259 if(switchedmode==true) {
Gerth 6:37c94a5e205f 260 pc.printf("Calibration mode! Use buttons to move left arm to 0 degrees\n");
Gerth 6:37c94a5e205f 261 switchedmode=false;
Gerth 6:37c94a5e205f 262 }
Gerth 6:37c94a5e205f 263
Gerth 3:48438eea184e 264 }
Gerth 0:dd66fff537d7 265 }
Gerth 0:dd66fff537d7 266 }