Oud verslag voor Biquad.

Dependencies:   Biquad HIDScope QEI angleandposition controlandadjust mbed

Fork of includeair by Jasper Gerth

Committer:
Gerth
Date:
Mon Oct 19 15:06:03 2015 +0000
Revision:
18:1c3254a32fd1
Parent:
17:72d3522165ac
Child:
19:6f22b5687587
works now with calculated angles. only the shooting has to be calculated;

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 16:63320b8f79c2 6 #include "angleandposition.h"
Gerth 1:917c07a4f3ec 7
Gerth 1:917c07a4f3ec 8 //info out
Gerth 10:9e96d14d7034 9 HIDScope scope(6);
Gerth 1:917c07a4f3ec 10 Ticker scope_ticker;
Gerth 3:48438eea184e 11 const double scope_frequency=500;
Gerth 1:917c07a4f3ec 12 Serial pc(USBTX,USBRX);
Gerth 1:917c07a4f3ec 13
Gerth 16:63320b8f79c2 14 DigitalOut ledred(LED_RED);
Gerth 16:63320b8f79c2 15 DigitalOut ledgreen(LED_GREEN);
Gerth 16:63320b8f79c2 16 DigitalOut ledblue(LED_BLUE);
Gerth 14:4c4f45a1dd23 17
Gerth 1:917c07a4f3ec 18 ////////////////ENCODERS
Gerth 9:4ee354663560 19 const float cpr_sensor=32;
Gerth 9:4ee354663560 20 const float cpr_shaft=cpr_sensor*131;
Gerth 17:72d3522165ac 21 QEI encoder1(D13,D12,NC,cpr_sensor);/// maybe use Encoder in stead of QEI, because Encoder had setposition
Gerth 9:4ee354663560 22 QEI encoder2(D10,D11,NC,cpr_sensor);
Gerth 9:4ee354663560 23 const double PIE=3.14159265359;
Gerth 9:4ee354663560 24 const float counttorad=((2*PIE)/cpr_shaft);
Gerth 1:917c07a4f3ec 25
Gerth 6:37c94a5e205f 26 /////////////////////////////////CALIBRATION (MODE)
Gerth 8:54a7da09ccad 27 int modecounter=1;
Gerth 6:37c94a5e205f 28 DigitalIn changemodebutton(PTA4);
Gerth 6:37c94a5e205f 29
Gerth 7:7fbb2c028778 30 Ticker readbuttoncalibrate_ticker;
Gerth 7:7fbb2c028778 31 const double readbuttoncalibrate_frequency=10;
Gerth 7:7fbb2c028778 32
Gerth 14:4c4f45a1dd23 33 Ticker ledblink_ticker;
Gerth 14:4c4f45a1dd23 34 const double ledblink_frequency=4;
Gerth 14:4c4f45a1dd23 35
Gerth 9:4ee354663560 36 const double radpersec_calibrate=0.1*PIE;
Gerth 8:54a7da09ccad 37
Gerth 8:54a7da09ccad 38 DigitalIn buttonR(D2);
Gerth 8:54a7da09ccad 39 DigitalIn buttonL(D3);
Gerth 8:54a7da09ccad 40
Gerth 1:917c07a4f3ec 41 //////////////////////////////////CONTROLLER
Gerth 1:917c07a4f3ec 42 controlandadjust mycontroller; // make a controller
Gerth 1:917c07a4f3ec 43 //controller constants
Gerth 1:917c07a4f3ec 44 float Kp=0.5;
Gerth 9:4ee354663560 45 float Ki=0.01;
Gerth 1:917c07a4f3ec 46 float Kd=0.001;
Gerth 1:917c07a4f3ec 47 Ticker control_ticker;
Gerth 9:4ee354663560 48 const double control_frequency=25;
Gerth 7:7fbb2c028778 49 const double Ts_control=1.0/control_frequency;
Gerth 1:917c07a4f3ec 50
Gerth 7:7fbb2c028778 51 float error1_int=0;// storage variables for the errors
Gerth 7:7fbb2c028778 52 float error2_int=0;
Gerth 7:7fbb2c028778 53 float error1_prev=0;
Gerth 7:7fbb2c028778 54 float error2_prev=0;
Gerth 1:917c07a4f3ec 55
Gerth 1:917c07a4f3ec 56 InterruptIn valuechangebutton(PTC6);//button to change controller constants
Gerth 1:917c07a4f3ec 57
Gerth 1:917c07a4f3ec 58 //safetyandthreshold
Gerth 3:48438eea184e 59 AnalogIn safety_pot(A3);//pot 2, used for the safety cutoff value for the pwm
Gerth 3:48438eea184e 60 AnalogIn threshold_pot(A2);//pot1, used to adjust threshold if signal differs per person
Gerth 1:917c07a4f3ec 61
Gerth 1:917c07a4f3ec 62 Ticker safetyandthreshold_ticker; // ticker to read potmeters
Gerth 3:48438eea184e 63 const double safetyandthreshold_frequency=1; // frequency for the ticker
Gerth 3:48438eea184e 64
Gerth 4:bf7765b0f612 65 float threshold_value=1;//initial threshold value
Gerth 1:917c07a4f3ec 66
Gerth 1:917c07a4f3ec 67 ////////////////////////////////FILTER
Gerth 1:917c07a4f3ec 68 #include "filtervalues.h"
Gerth 1:917c07a4f3ec 69 Ticker filter_ticker;
Gerth 3:48438eea184e 70 const double filter_frequency=500;
Gerth 1:917c07a4f3ec 71 Biquad myfilter1;
Gerth 1:917c07a4f3ec 72 Biquad myfilter2;
Gerth 1:917c07a4f3ec 73
Gerth 1:917c07a4f3ec 74 AnalogIn emg1_input(A0);
Gerth 1:917c07a4f3ec 75 AnalogIn emg2_input(A1);
Gerth 1:917c07a4f3ec 76
Gerth 1:917c07a4f3ec 77 double filteredsignal1=0;
Gerth 1:917c07a4f3ec 78 double filteredsignal2=0;
Gerth 5:8ac5d0651e4d 79 float filter_extragain=1;
Gerth 1:917c07a4f3ec 80
Gerth 3:48438eea184e 81 /////////////////READSIGNAL
Gerth 3:48438eea184e 82 Ticker readsignal_ticker;
Gerth 3:48438eea184e 83 const double readsignal_frequency=25;
Gerth 3:48438eea184e 84
Gerth 11:c10651055871 85 DigitalOut led1(PTC12);
Gerth 4:bf7765b0f612 86 DigitalOut led2(D9);
Gerth 4:bf7765b0f612 87
Gerth 4:bf7765b0f612 88 //////////////////////////////// POSITION AND ANGLE SHIZZLE
Gerth 4:bf7765b0f612 89 float desired_position=0;
Gerth 4:bf7765b0f612 90 float desired_angle[]= {0,0};
Gerth 18:1c3254a32fd1 91 float mm_per_sec_emg=0.050;// move the pod 50 mm per sec if muscle is flexed
Gerth 18:1c3254a32fd1 92 float fieldwidth=0.473;
Gerth 18:1c3254a32fd1 93 float safetymarginfield=0.075; //adjustable, tweak for maximum but safe range
Gerth 11:c10651055871 94 float maxdisplacement=((fieldwidth/2)-safetymarginfield); //so the pod doesn't hit the edges of the playfield
Gerth 11:c10651055871 95 float rad_per_sec_emg=0.25*PIE;// THIS ONE IS NOT NESSECARY FOR ACTUAL PROGRAM
Gerth 3:48438eea184e 96
Gerth 16:63320b8f79c2 97 angleandposition anglepos;
Gerth 18:1c3254a32fd1 98 float y_start=0.255;
Gerth 18:1c3254a32fd1 99 float y_punch=0.473;
Gerth 18:1c3254a32fd1 100
Gerth 1:917c07a4f3ec 101 //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS
Gerth 1:917c07a4f3ec 102 volatile bool scopedata_go=false,
Gerth 1:917c07a4f3ec 103 control_go=false,
Gerth 1:917c07a4f3ec 104 filter_go=false,
Gerth 3:48438eea184e 105 safetyandthreshold_go=false,
Gerth 6:37c94a5e205f 106 readsignal_go=false,
Gerth 8:54a7da09ccad 107 switchedmode=true,
Gerth 14:4c4f45a1dd23 108 readbuttoncalibrate_go=false,
Gerth 14:4c4f45a1dd23 109 ledblink_go=false;
Gerth 1:917c07a4f3ec 110
Gerth 1:917c07a4f3ec 111 void scopedata_activate()
Gerth 1:917c07a4f3ec 112 {
Gerth 1:917c07a4f3ec 113 scopedata_go=true;
Gerth 1:917c07a4f3ec 114 }
Gerth 1:917c07a4f3ec 115 void control_activate()
Gerth 1:917c07a4f3ec 116 {
Gerth 1:917c07a4f3ec 117 control_go=true;
Gerth 1:917c07a4f3ec 118 }
Gerth 1:917c07a4f3ec 119 void filter_activate()
Gerth 1:917c07a4f3ec 120 {
Gerth 1:917c07a4f3ec 121 filter_go=true;
Gerth 1:917c07a4f3ec 122 }
Gerth 1:917c07a4f3ec 123 void safetyandthreshold_activate()
Gerth 1:917c07a4f3ec 124 {
Gerth 1:917c07a4f3ec 125 safetyandthreshold_go=true;
Gerth 1:917c07a4f3ec 126 }
Gerth 3:48438eea184e 127 void readsignal_activate()
Gerth 3:48438eea184e 128 {
Gerth 3:48438eea184e 129 readsignal_go=true;
Gerth 3:48438eea184e 130 }
Gerth 7:7fbb2c028778 131 void readbuttoncalibrate_activate()
Gerth 7:7fbb2c028778 132 {
Gerth 7:7fbb2c028778 133 readbuttoncalibrate_go=true;
Gerth 7:7fbb2c028778 134 }
Gerth 14:4c4f45a1dd23 135 void ledblink_activate()
Gerth 14:4c4f45a1dd23 136 {
Gerth 14:4c4f45a1dd23 137 ledblink_go=true;
Gerth 14:4c4f45a1dd23 138 }
Gerth 1:917c07a4f3ec 139
Gerth 1:917c07a4f3ec 140 ////////////////////////FUNCTIONS
Gerth 1:917c07a4f3ec 141 //gather data and send to scope
Gerth 1:917c07a4f3ec 142 void scopedata()
Gerth 1:917c07a4f3ec 143 {
Gerth 17:72d3522165ac 144 scope.set(0,desired_position);
Gerth 17:72d3522165ac 145 scope.set(1,desired_angle[0]);
Gerth 17:72d3522165ac 146 scope.set(2,desired_angle[1]);
Gerth 3:48438eea184e 147 scope.send();
Gerth 4:bf7765b0f612 148 }
Gerth 1:917c07a4f3ec 149 //read potmeters and adjust the safetyfactor and threshold
Gerth 1:917c07a4f3ec 150 void safetyandthreshold()
Gerth 1:917c07a4f3ec 151 {
Gerth 3:48438eea184e 152 mycontroller.cutoff((ceil (10*safety_pot.read()) )/10); // adjust the safetyfactor value between 0 and 1 rounded to 1 decimal
Gerth 3:48438eea184e 153 threshold_value=((ceil (10*threshold_pot.read()) )/10); // adjust the threshold value between 0 and 1 rounded to 1 decimal
Gerth 1:917c07a4f3ec 154 }
Gerth 1:917c07a4f3ec 155 /////filter
Gerth 1:917c07a4f3ec 156 void filtereverything()
Gerth 1:917c07a4f3ec 157 {
Gerth 1:917c07a4f3ec 158 //filter_timer.reset();
Gerth 1:917c07a4f3ec 159 // filter_timer.start();
Gerth 1:917c07a4f3ec 160 //pass1 so f1
Gerth 2:c7707856d137 161 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 162 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 163
Gerth 1:917c07a4f3ec 164 //pass2 so f2
Gerth 2:c7707856d137 165 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 166 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 167
Gerth 1:917c07a4f3ec 168 //pass3 so f3
Gerth 2:c7707856d137 169 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 170 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 171
Gerth 1:917c07a4f3ec 172 //pass4 so f4
Gerth 2:c7707856d137 173 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 174 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 175
Gerth 1:917c07a4f3ec 176 //pass5 so f5
Gerth 2:c7707856d137 177 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 178 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 179
Gerth 1:917c07a4f3ec 180 ///// take absolute value
Gerth 2:c7707856d137 181 double pass5_emg1_abs=(fabs(pass5_emg1));
Gerth 2:c7707856d137 182 double pass5_emg2_abs=(fabs(pass5_emg2));
Gerth 1:917c07a4f3ec 183
Gerth 1:917c07a4f3ec 184 //pass6 so f6
Gerth 2:c7707856d137 185 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 186 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 187
Gerth 1:917c07a4f3ec 188
Gerth 1:917c07a4f3ec 189 //pass7 so f7
Gerth 2:c7707856d137 190 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 191 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 192
Gerth 2:c7707856d137 193 filteredsignal1=(pass7_emg1*9e11*filter_extragain);
Gerth 2:c7707856d137 194 filteredsignal2=(pass7_emg2*9e11*filter_extragain);
Gerth 1:917c07a4f3ec 195
Gerth 1:917c07a4f3ec 196 //filter_timer.stop();
Gerth 1:917c07a4f3ec 197 }
Gerth 1:917c07a4f3ec 198
Gerth 7:7fbb2c028778 199
Gerth 1:917c07a4f3ec 200
Gerth 1:917c07a4f3ec 201 //adjust controller values when sw2 is pressed
Gerth 1:917c07a4f3ec 202 void valuechange()
Gerth 1:917c07a4f3ec 203 {
Gerth 3:48438eea184e 204 mycontroller.STOP();
Gerth 5:8ac5d0651e4d 205 pc.printf("KP is now %f, enter new value\n",Kp);
Gerth 1:917c07a4f3ec 206 pc.scanf("%f", &Kp);
Gerth 1:917c07a4f3ec 207
Gerth 1:917c07a4f3ec 208 pc.printf("KI is now %f, enter new value\n",Ki);
Gerth 1:917c07a4f3ec 209 pc.scanf("%f", &Ki);
Gerth 1:917c07a4f3ec 210
Gerth 1:917c07a4f3ec 211 pc.printf("KD is now %f, enter new value\n",Kd);
Gerth 5:8ac5d0651e4d 212 pc.scanf("%f", &Kd);
Gerth 3:48438eea184e 213
Gerth 3:48438eea184e 214 pc.printf("Extra gain is now %f, enter new value\n",filter_extragain);
Gerth 5:8ac5d0651e4d 215 pc.scanf("%f", &filter_extragain);
Gerth 1:917c07a4f3ec 216 }
Gerth 11:c10651055871 217
Gerth 11:c10651055871 218 const float schiethoek=0.35*PIE;
Gerth 11:c10651055871 219 const float schiettijd=0.5;
Gerth 11:c10651055871 220 void shoot() // THIS NEEDS ADJUSTMEND
Gerth 14:4c4f45a1dd23 221 {
Gerth 11:c10651055871 222 pc.printf("SHOOT\n");
Gerth 11:c10651055871 223 //hoeken groter maken
Gerth 11:c10651055871 224 desired_angle[0]-=schiethoek;
Gerth 11:c10651055871 225 desired_angle[1]+=schiethoek;
Gerth 11:c10651055871 226
Gerth 11:c10651055871 227 Timer schiettimer;
Gerth 11:c10651055871 228 schiettimer.reset();
Gerth 11:c10651055871 229 schiettimer.start();
Gerth 11:c10651055871 230 float pass=0;
Gerth 11:c10651055871 231 while(schiettimer.read()<=schiettijd) {
Gerth 11:c10651055871 232 // errors berekenen en naar de controller passen
Gerth 14:4c4f45a1dd23 233 float error1=(desired_angle[0]-counttorad*encoder1.getPulses());
Gerth 14:4c4f45a1dd23 234 float error2=(desired_angle[1]-counttorad*encoder2.getPulses());
Gerth 11:c10651055871 235 mycontroller.PI(error1, error2, Kp, Ki, Ts_control, error1_int, error2_int);
Gerth 11:c10651055871 236 scopedata();
Gerth 11:c10651055871 237 wait (Ts_control-(schiettimer.read()-Ts_control*pass)); // even wachten anders wordt de while loop te snel doorlopen en gaan de motoren wak
Gerth 14:4c4f45a1dd23 238 ledblue=!ledblue;
Gerth 11:c10651055871 239 pass++;
Gerth 11:c10651055871 240 }
Gerth 11:c10651055871 241 schiettimer.stop();
Gerth 16:63320b8f79c2 242 ledblue=1;
Gerth 11:c10651055871 243 //terug na schieten
Gerth 11:c10651055871 244 desired_angle[0]+=schiethoek;
Gerth 11:c10651055871 245 desired_angle[1]-=schiethoek;
Gerth 11:c10651055871 246 }
Gerth 11:c10651055871 247
Gerth 16:63320b8f79c2 248 ////////////////////////////////////////////////////READ EMG AND MOVE DESIRED POSITION
Gerth 3:48438eea184e 249 void readsignal()
Gerth 3:48438eea184e 250 {
Gerth 11:c10651055871 251 //check if pod has to shoot
Gerth 4:bf7765b0f612 252 if (filteredsignal1>=threshold_value && filteredsignal2>=threshold_value) {
Gerth 4:bf7765b0f612 253 led1=led2=1;
Gerth 11:c10651055871 254 shoot();
Gerth 14:4c4f45a1dd23 255 // check if pod has to move to the right
Gerth 4:bf7765b0f612 256 } else if (filteredsignal1>=threshold_value && filteredsignal2<=threshold_value) {
Gerth 4:bf7765b0f612 257 led1=1;
Gerth 4:bf7765b0f612 258 led2=0;
Gerth 16:63320b8f79c2 259 desired_position += (mm_per_sec_emg/readsignal_frequency);// move desiredposition right
Gerth 11:c10651055871 260 if (desired_position>=maxdisplacement) {//check if the pod doesnt move too far and hit the edge
Gerth 11:c10651055871 261 desired_position=maxdisplacement;
Gerth 11:c10651055871 262 } else {
Gerth 11:c10651055871 263 desired_position=desired_position;
Gerth 11:c10651055871 264 }
Gerth 14:4c4f45a1dd23 265 // check if pod has to move to the left
Gerth 4:bf7765b0f612 266 } else if (filteredsignal1<=threshold_value && filteredsignal2>=threshold_value) {
Gerth 4:bf7765b0f612 267 led1=0;
Gerth 4:bf7765b0f612 268 led2=1;
Gerth 16:63320b8f79c2 269 desired_position -= (mm_per_sec_emg/readsignal_frequency);//move desiredposition left
Gerth 11:c10651055871 270 if (desired_position<=(-1*maxdisplacement)) {//check if the pod doesnt move too far and hit the edge
Gerth 14:4c4f45a1dd23 271 desired_position=(-1*maxdisplacement);
Gerth 11:c10651055871 272 } else {
Gerth 11:c10651055871 273 desired_position=desired_position;
Gerth 11:c10651055871 274 }
Gerth 3:48438eea184e 275 } else {
Gerth 4:bf7765b0f612 276 led1=led2=0;
Gerth 3:48438eea184e 277 }
Gerth 16:63320b8f79c2 278 }
Gerth 16:63320b8f79c2 279 ///////////////////////////////////////////////READ BUTTON AND MOVE DESIRED POSITION
Gerth 17:72d3522165ac 280 void readsignalbutton()
Gerth 16:63320b8f79c2 281 {
Gerth 17:72d3522165ac 282 int buttonr=buttonR.read();
Gerth 17:72d3522165ac 283 int buttonl=buttonL.read();
Gerth 16:63320b8f79c2 284 //check if pod has to shoot
Gerth 17:72d3522165ac 285 if (buttonr==0 && buttonl==0) {
Gerth 16:63320b8f79c2 286 led1=led2=1;
Gerth 16:63320b8f79c2 287 shoot();
Gerth 16:63320b8f79c2 288 // check if pod has to move to the right
Gerth 17:72d3522165ac 289 } else if (buttonr==0 && buttonl==1) {
Gerth 16:63320b8f79c2 290 led1=1;
Gerth 16:63320b8f79c2 291 led2=0;
Gerth 16:63320b8f79c2 292 desired_position += (mm_per_sec_emg/readsignal_frequency);// move desiredposition right
Gerth 17:72d3522165ac 293 if (desired_position>=maxdisplacement) {//check if the pod doesnt move too far and hit the edge
Gerth 16:63320b8f79c2 294 desired_position=maxdisplacement;
Gerth 16:63320b8f79c2 295 } else {
Gerth 16:63320b8f79c2 296 desired_position=desired_position;
Gerth 16:63320b8f79c2 297 }
Gerth 17:72d3522165ac 298 // check if pod has to move to the left
Gerth 17:72d3522165ac 299 } else if (buttonr==1 && buttonl==0) {
Gerth 16:63320b8f79c2 300 led1=0;
Gerth 16:63320b8f79c2 301 led2=1;
Gerth 16:63320b8f79c2 302 desired_position -= (mm_per_sec_emg/readsignal_frequency);//move desiredposition left
Gerth 17:72d3522165ac 303 if (desired_position<=(-1*maxdisplacement)) {//check if the pod doesnt move too far and hit the edge
Gerth 16:63320b8f79c2 304 desired_position=(-1*maxdisplacement);
Gerth 16:63320b8f79c2 305 } else {
Gerth 16:63320b8f79c2 306 desired_position=desired_position;
Gerth 16:63320b8f79c2 307 }
Gerth 16:63320b8f79c2 308 } else {
Gerth 16:63320b8f79c2 309 led1=led2=0;
Gerth 16:63320b8f79c2 310 }
Gerth 3:48438eea184e 311 }
Gerth 3:48438eea184e 312
Gerth 6:37c94a5e205f 313 void changemode()
Gerth 6:37c94a5e205f 314 {
Gerth 10:9e96d14d7034 315 mycontroller.STOP();
Gerth 6:37c94a5e205f 316 switchedmode=true;
Gerth 6:37c94a5e205f 317 modecounter++;
Gerth 15:17de575b7385 318 if (modecounter==4) {
Gerth 6:37c94a5e205f 319 modecounter=0;
Gerth 6:37c94a5e205f 320 } else {
Gerth 6:37c94a5e205f 321 modecounter=modecounter;
Gerth 6:37c94a5e205f 322 }
Gerth 6:37c94a5e205f 323 wait(1);
Gerth 6:37c94a5e205f 324 }
Gerth 0:dd66fff537d7 325
Gerth 16:63320b8f79c2 326 ///////////////////////////////////////////////////MAIN
Gerth 7:7fbb2c028778 327
Gerth 0:dd66fff537d7 328 int main()
Gerth 0:dd66fff537d7 329 {
Gerth 1:917c07a4f3ec 330 //tickers
Gerth 1:917c07a4f3ec 331 safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency);
Gerth 1:917c07a4f3ec 332 filter_ticker.attach(&filter_activate,1.0/filter_frequency);
Gerth 1:917c07a4f3ec 333 control_ticker.attach(&control_activate,1.0/control_frequency);
Gerth 1:917c07a4f3ec 334 scope_ticker.attach(&scopedata_activate,1.0/scope_frequency);
Gerth 3:48438eea184e 335 readsignal_ticker.attach(&readsignal_activate, 1.0/readsignal_frequency);
Gerth 7:7fbb2c028778 336 readbuttoncalibrate_ticker.attach(&readbuttoncalibrate_activate, 1.0/readbuttoncalibrate_frequency);
Gerth 14:4c4f45a1dd23 337 ledblink_ticker.attach(&ledblink_activate, 1.0/ledblink_frequency);
Gerth 14:4c4f45a1dd23 338
Gerth 1:917c07a4f3ec 339 while(1) {
Gerth 6:37c94a5e205f 340 if (changemodebutton==0) {
Gerth 6:37c94a5e205f 341 changemode();
Gerth 1:917c07a4f3ec 342 }
Gerth 9:4ee354663560 343 if (scopedata_go==true) {
Gerth 9:4ee354663560 344 scopedata();
Gerth 9:4ee354663560 345 scopedata_go=false;
Gerth 9:4ee354663560 346 }
Gerth 9:4ee354663560 347 if (safetyandthreshold_go==true) {
Gerth 9:4ee354663560 348 safetyandthreshold();
Gerth 9:4ee354663560 349 safetyandthreshold_go=false;
Gerth 9:4ee354663560 350 }
Gerth 7:7fbb2c028778 351 ///////////////////////////////////////////NORMAL RUNNING MODE
Gerth 7:7fbb2c028778 352 if(modecounter==0) {
Gerth 6:37c94a5e205f 353 if (switchedmode==true) {
Gerth 6:37c94a5e205f 354 encoder1.reset();
Gerth 6:37c94a5e205f 355 encoder2.reset();
Gerth 6:37c94a5e205f 356 pc.printf("Program running\n");//
Gerth 16:63320b8f79c2 357 ledgreen=0;
Gerth 16:63320b8f79c2 358 led1=led2=ledred=ledblue=1;
Gerth 6:37c94a5e205f 359 switchedmode=false;
Gerth 6:37c94a5e205f 360 }
Gerth 9:4ee354663560 361
Gerth 6:37c94a5e205f 362 if (filter_go==true) {
Gerth 6:37c94a5e205f 363 filtereverything();
Gerth 6:37c94a5e205f 364 filter_go=false;
Gerth 6:37c94a5e205f 365 }
Gerth 17:72d3522165ac 366 if (readsignal_go==true) {
Gerth 17:72d3522165ac 367 readsignal();
Gerth 17:72d3522165ac 368 readsignal_go=false;
Gerth 17:72d3522165ac 369 }
Gerth 6:37c94a5e205f 370 if (control_go==true) {
Gerth 16:63320b8f79c2 371 desired_angle[0]=anglepos.positiontoangle1(desired_position,y_start);
Gerth 16:63320b8f79c2 372 desired_angle[1]=anglepos.positiontoangle2(desired_position,y_start);
Gerth 16:63320b8f79c2 373
Gerth 11:c10651055871 374 float error1=(desired_angle[0]-counttorad*encoder1.getPulses());
Gerth 11:c10651055871 375 float error2=(desired_angle[1]-counttorad*encoder2.getPulses());
Gerth 7:7fbb2c028778 376 mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int);
Gerth 6:37c94a5e205f 377 control_go=false;
Gerth 6:37c94a5e205f 378 }
Gerth 17:72d3522165ac 379
Gerth 6:37c94a5e205f 380 valuechangebutton.fall(&valuechange);
Gerth 1:917c07a4f3ec 381 }
Gerth 7:7fbb2c028778 382 ////////////////////////////////////////////////////CALIBRATE RIGHT ARM
Gerth 6:37c94a5e205f 383 if (modecounter==1) {
Gerth 6:37c94a5e205f 384 if(switchedmode==true) {
Gerth 6:37c94a5e205f 385 pc.printf("Calibration mode! Use buttons to move rigth arm to 0 degrees\n");
Gerth 16:63320b8f79c2 386 led1=led2=ledred=0;
Gerth 16:63320b8f79c2 387 ledgreen=ledblue=1;
Gerth 6:37c94a5e205f 388 switchedmode=false;
Gerth 14:4c4f45a1dd23 389 }
Gerth 14:4c4f45a1dd23 390 if (ledblink_go==true) {
Gerth 14:4c4f45a1dd23 391 led1=!led1;
Gerth 16:63320b8f79c2 392 ledblink_go=false;
Gerth 6:37c94a5e205f 393 }
Gerth 7:7fbb2c028778 394 if (control_go==true) {
Gerth 11:c10651055871 395 float error1=(desired_angle[0]-counttorad*encoder1.getPulses());
Gerth 9:4ee354663560 396 float error2=0;// this is the error you want to use
Gerth 7:7fbb2c028778 397 mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int);
Gerth 7:7fbb2c028778 398 control_go=false;
Gerth 7:7fbb2c028778 399 }
Gerth 9:4ee354663560 400 if (readbuttoncalibrate_go==true) {
Gerth 9:4ee354663560 401 if (buttonR.read()==0 && buttonL.read()==1) {
Gerth 9:4ee354663560 402 desired_angle[0] += (radpersec_calibrate/readbuttoncalibrate_frequency);
Gerth 9:4ee354663560 403 readbuttoncalibrate_go=false;
Gerth 9:4ee354663560 404 }
Gerth 9:4ee354663560 405 if (buttonR.read()==1 && buttonL.read()==0) {
Gerth 9:4ee354663560 406 desired_angle[0] -= (radpersec_calibrate/readbuttoncalibrate_frequency);
Gerth 9:4ee354663560 407 readbuttoncalibrate_go=false;
Gerth 9:4ee354663560 408 }
Gerth 9:4ee354663560 409 }
Gerth 8:54a7da09ccad 410 }
Gerth 8:54a7da09ccad 411 ////////////////////////////////////////////CALIBRATE LEFT ARM
Gerth 8:54a7da09ccad 412 if (modecounter==2) {
Gerth 8:54a7da09ccad 413 if(switchedmode==true) {
Gerth 8:54a7da09ccad 414 pc.printf("Calibration mode! Use buttons to move left arm to 0 degrees\n");
Gerth 16:63320b8f79c2 415 led1=led2=ledred=0;
Gerth 16:63320b8f79c2 416 ledgreen=ledblue=1;
Gerth 8:54a7da09ccad 417 switchedmode=false;
Gerth 8:54a7da09ccad 418 }
Gerth 14:4c4f45a1dd23 419 if (ledblink_go==true) {
Gerth 14:4c4f45a1dd23 420 led2=!led2;
Gerth 16:63320b8f79c2 421 ledblink_go=false;
Gerth 14:4c4f45a1dd23 422 }
Gerth 8:54a7da09ccad 423 if (control_go==true) {
Gerth 8:54a7da09ccad 424 float error1=0;
Gerth 8:54a7da09ccad 425 float error2=(desired_angle[1]-counttorad*encoder2.getPulses());// this is the error you want to use
Gerth 8:54a7da09ccad 426 mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int);
Gerth 8:54a7da09ccad 427 control_go=false;
Gerth 8:54a7da09ccad 428 }
Gerth 8:54a7da09ccad 429 if (readbuttoncalibrate_go==true) {
Gerth 8:54a7da09ccad 430 if (buttonR.read()==0 && buttonL.read()==1) {
Gerth 8:54a7da09ccad 431 desired_angle[1] += (radpersec_calibrate/readbuttoncalibrate_frequency);
Gerth 8:54a7da09ccad 432 readbuttoncalibrate_go=false;
Gerth 7:7fbb2c028778 433 }
Gerth 8:54a7da09ccad 434 if (buttonR.read()==1 && buttonL.read()==0) {
Gerth 8:54a7da09ccad 435 desired_angle[1] -= (radpersec_calibrate/readbuttoncalibrate_frequency);
Gerth 8:54a7da09ccad 436 readbuttoncalibrate_go=false;
Gerth 7:7fbb2c028778 437 }
Gerth 6:37c94a5e205f 438 }
Gerth 3:48438eea184e 439 }
Gerth 16:63320b8f79c2 440 ///////////////////////////////BUTTONCONTROLMODE
Gerth 15:17de575b7385 441 if (modecounter==3) {
Gerth 16:63320b8f79c2 442 if (switchedmode==true) {
Gerth 16:63320b8f79c2 443 led1=led2=0;
Gerth 16:63320b8f79c2 444 ledred=ledblue=1;
Gerth 16:63320b8f79c2 445 switchedmode=false;
Gerth 16:63320b8f79c2 446 }
Gerth 16:63320b8f79c2 447 if (ledblink_go==true) {
Gerth 15:17de575b7385 448 ledgreen=!ledgreen;
Gerth 16:63320b8f79c2 449 ledblink_go=false;
Gerth 15:17de575b7385 450 }
Gerth 16:63320b8f79c2 451 if (readsignal_go==true) {
Gerth 17:72d3522165ac 452 readsignalbutton();
Gerth 17:72d3522165ac 453 readsignal_go=false;
Gerth 17:72d3522165ac 454 }
Gerth 17:72d3522165ac 455 if (control_go==true) {
Gerth 17:72d3522165ac 456 desired_angle[0]=anglepos.positiontoangle1(desired_position,y_start);
Gerth 17:72d3522165ac 457 desired_angle[1]=anglepos.positiontoangle2(desired_position,y_start);
Gerth 17:72d3522165ac 458
Gerth 17:72d3522165ac 459 float error1=(desired_angle[0]-counttorad*encoder1.getPulses());
Gerth 17:72d3522165ac 460 float error2=(desired_angle[1]-counttorad*encoder2.getPulses());
Gerth 17:72d3522165ac 461 mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int);
Gerth 17:72d3522165ac 462 control_go=false;
Gerth 16:63320b8f79c2 463 }
Gerth 16:63320b8f79c2 464 }
Gerth 15:17de575b7385 465
Gerth 0:dd66fff537d7 466 }
Gerth 8:54a7da09ccad 467 }