Oud verslag voor Biquad.

Dependencies:   Biquad HIDScope QEI angleandposition controlandadjust mbed

Fork of includeair by Jasper Gerth

Committer:
Gerth
Date:
Mon Oct 19 14:21:46 2015 +0000
Revision:
17:72d3522165ac
Parent:
16:63320b8f79c2
Child:
18:1c3254a32fd1
buttoncontrol mode now working at rigth speed;

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 17:72d3522165ac 91 float mm_per_sec_emg=50;// move the pod 50 mm per sec if muscle is flexed
Gerth 11:c10651055871 92 float fieldwidth=473;
Gerth 11:c10651055871 93 float safetymarginfield=75; //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 16:63320b8f79c2 98 float y_start=255;
Gerth 16:63320b8f79c2 99 float y_punch=473;
Gerth 1:917c07a4f3ec 100 //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS
Gerth 1:917c07a4f3ec 101 volatile bool scopedata_go=false,
Gerth 1:917c07a4f3ec 102 control_go=false,
Gerth 1:917c07a4f3ec 103 filter_go=false,
Gerth 3:48438eea184e 104 safetyandthreshold_go=false,
Gerth 6:37c94a5e205f 105 readsignal_go=false,
Gerth 8:54a7da09ccad 106 switchedmode=true,
Gerth 14:4c4f45a1dd23 107 readbuttoncalibrate_go=false,
Gerth 14:4c4f45a1dd23 108 ledblink_go=false;
Gerth 1:917c07a4f3ec 109
Gerth 1:917c07a4f3ec 110 void scopedata_activate()
Gerth 1:917c07a4f3ec 111 {
Gerth 1:917c07a4f3ec 112 scopedata_go=true;
Gerth 1:917c07a4f3ec 113 }
Gerth 1:917c07a4f3ec 114 void control_activate()
Gerth 1:917c07a4f3ec 115 {
Gerth 1:917c07a4f3ec 116 control_go=true;
Gerth 1:917c07a4f3ec 117 }
Gerth 1:917c07a4f3ec 118 void filter_activate()
Gerth 1:917c07a4f3ec 119 {
Gerth 1:917c07a4f3ec 120 filter_go=true;
Gerth 1:917c07a4f3ec 121 }
Gerth 1:917c07a4f3ec 122 void safetyandthreshold_activate()
Gerth 1:917c07a4f3ec 123 {
Gerth 1:917c07a4f3ec 124 safetyandthreshold_go=true;
Gerth 1:917c07a4f3ec 125 }
Gerth 3:48438eea184e 126 void readsignal_activate()
Gerth 3:48438eea184e 127 {
Gerth 3:48438eea184e 128 readsignal_go=true;
Gerth 3:48438eea184e 129 }
Gerth 7:7fbb2c028778 130 void readbuttoncalibrate_activate()
Gerth 7:7fbb2c028778 131 {
Gerth 7:7fbb2c028778 132 readbuttoncalibrate_go=true;
Gerth 7:7fbb2c028778 133 }
Gerth 14:4c4f45a1dd23 134 void ledblink_activate()
Gerth 14:4c4f45a1dd23 135 {
Gerth 14:4c4f45a1dd23 136 ledblink_go=true;
Gerth 14:4c4f45a1dd23 137 }
Gerth 1:917c07a4f3ec 138
Gerth 1:917c07a4f3ec 139 ////////////////////////FUNCTIONS
Gerth 1:917c07a4f3ec 140 //gather data and send to scope
Gerth 1:917c07a4f3ec 141 void scopedata()
Gerth 1:917c07a4f3ec 142 {
Gerth 17:72d3522165ac 143 scope.set(0,desired_position);
Gerth 17:72d3522165ac 144 scope.set(1,desired_angle[0]);
Gerth 17:72d3522165ac 145 scope.set(2,desired_angle[1]);
Gerth 3:48438eea184e 146 scope.send();
Gerth 4:bf7765b0f612 147 }
Gerth 1:917c07a4f3ec 148 //read potmeters and adjust the safetyfactor and threshold
Gerth 1:917c07a4f3ec 149 void safetyandthreshold()
Gerth 1:917c07a4f3ec 150 {
Gerth 3:48438eea184e 151 mycontroller.cutoff((ceil (10*safety_pot.read()) )/10); // adjust the safetyfactor value between 0 and 1 rounded to 1 decimal
Gerth 3:48438eea184e 152 threshold_value=((ceil (10*threshold_pot.read()) )/10); // adjust the threshold value between 0 and 1 rounded to 1 decimal
Gerth 1:917c07a4f3ec 153 }
Gerth 1:917c07a4f3ec 154 /////filter
Gerth 1:917c07a4f3ec 155 void filtereverything()
Gerth 1:917c07a4f3ec 156 {
Gerth 1:917c07a4f3ec 157 //filter_timer.reset();
Gerth 1:917c07a4f3ec 158 // filter_timer.start();
Gerth 1:917c07a4f3ec 159 //pass1 so f1
Gerth 2:c7707856d137 160 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 161 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 162
Gerth 1:917c07a4f3ec 163 //pass2 so f2
Gerth 2:c7707856d137 164 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 165 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 166
Gerth 1:917c07a4f3ec 167 //pass3 so f3
Gerth 2:c7707856d137 168 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 169 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 170
Gerth 1:917c07a4f3ec 171 //pass4 so f4
Gerth 2:c7707856d137 172 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 173 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 174
Gerth 1:917c07a4f3ec 175 //pass5 so f5
Gerth 2:c7707856d137 176 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 177 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 178
Gerth 1:917c07a4f3ec 179 ///// take absolute value
Gerth 2:c7707856d137 180 double pass5_emg1_abs=(fabs(pass5_emg1));
Gerth 2:c7707856d137 181 double pass5_emg2_abs=(fabs(pass5_emg2));
Gerth 1:917c07a4f3ec 182
Gerth 1:917c07a4f3ec 183 //pass6 so f6
Gerth 2:c7707856d137 184 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 185 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 186
Gerth 1:917c07a4f3ec 187
Gerth 1:917c07a4f3ec 188 //pass7 so f7
Gerth 2:c7707856d137 189 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 190 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 191
Gerth 2:c7707856d137 192 filteredsignal1=(pass7_emg1*9e11*filter_extragain);
Gerth 2:c7707856d137 193 filteredsignal2=(pass7_emg2*9e11*filter_extragain);
Gerth 1:917c07a4f3ec 194
Gerth 1:917c07a4f3ec 195 //filter_timer.stop();
Gerth 1:917c07a4f3ec 196 }
Gerth 1:917c07a4f3ec 197
Gerth 7:7fbb2c028778 198
Gerth 1:917c07a4f3ec 199
Gerth 1:917c07a4f3ec 200 //adjust controller values when sw2 is pressed
Gerth 1:917c07a4f3ec 201 void valuechange()
Gerth 1:917c07a4f3ec 202 {
Gerth 3:48438eea184e 203 mycontroller.STOP();
Gerth 5:8ac5d0651e4d 204 pc.printf("KP is now %f, enter new value\n",Kp);
Gerth 1:917c07a4f3ec 205 pc.scanf("%f", &Kp);
Gerth 1:917c07a4f3ec 206
Gerth 1:917c07a4f3ec 207 pc.printf("KI is now %f, enter new value\n",Ki);
Gerth 1:917c07a4f3ec 208 pc.scanf("%f", &Ki);
Gerth 1:917c07a4f3ec 209
Gerth 1:917c07a4f3ec 210 pc.printf("KD is now %f, enter new value\n",Kd);
Gerth 5:8ac5d0651e4d 211 pc.scanf("%f", &Kd);
Gerth 3:48438eea184e 212
Gerth 3:48438eea184e 213 pc.printf("Extra gain is now %f, enter new value\n",filter_extragain);
Gerth 5:8ac5d0651e4d 214 pc.scanf("%f", &filter_extragain);
Gerth 1:917c07a4f3ec 215 }
Gerth 11:c10651055871 216
Gerth 11:c10651055871 217 const float schiethoek=0.35*PIE;
Gerth 11:c10651055871 218 const float schiettijd=0.5;
Gerth 11:c10651055871 219 void shoot() // THIS NEEDS ADJUSTMEND
Gerth 14:4c4f45a1dd23 220 {
Gerth 11:c10651055871 221 pc.printf("SHOOT\n");
Gerth 11:c10651055871 222 //hoeken groter maken
Gerth 11:c10651055871 223 desired_angle[0]-=schiethoek;
Gerth 11:c10651055871 224 desired_angle[1]+=schiethoek;
Gerth 11:c10651055871 225
Gerth 11:c10651055871 226 Timer schiettimer;
Gerth 11:c10651055871 227 schiettimer.reset();
Gerth 11:c10651055871 228 schiettimer.start();
Gerth 11:c10651055871 229 float pass=0;
Gerth 11:c10651055871 230 while(schiettimer.read()<=schiettijd) {
Gerth 11:c10651055871 231 // errors berekenen en naar de controller passen
Gerth 14:4c4f45a1dd23 232 float error1=(desired_angle[0]-counttorad*encoder1.getPulses());
Gerth 14:4c4f45a1dd23 233 float error2=(desired_angle[1]-counttorad*encoder2.getPulses());
Gerth 11:c10651055871 234 mycontroller.PI(error1, error2, Kp, Ki, Ts_control, error1_int, error2_int);
Gerth 11:c10651055871 235 scopedata();
Gerth 11:c10651055871 236 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 237 ledblue=!ledblue;
Gerth 11:c10651055871 238 pass++;
Gerth 11:c10651055871 239 }
Gerth 11:c10651055871 240 schiettimer.stop();
Gerth 16:63320b8f79c2 241 ledblue=1;
Gerth 11:c10651055871 242 //terug na schieten
Gerth 11:c10651055871 243 desired_angle[0]+=schiethoek;
Gerth 11:c10651055871 244 desired_angle[1]-=schiethoek;
Gerth 11:c10651055871 245 }
Gerth 11:c10651055871 246
Gerth 16:63320b8f79c2 247 ////////////////////////////////////////////////////READ EMG AND MOVE DESIRED POSITION
Gerth 3:48438eea184e 248 void readsignal()
Gerth 3:48438eea184e 249 {
Gerth 11:c10651055871 250 //check if pod has to shoot
Gerth 4:bf7765b0f612 251 if (filteredsignal1>=threshold_value && filteredsignal2>=threshold_value) {
Gerth 4:bf7765b0f612 252 led1=led2=1;
Gerth 11:c10651055871 253 shoot();
Gerth 14:4c4f45a1dd23 254 // check if pod has to move to the right
Gerth 4:bf7765b0f612 255 } else if (filteredsignal1>=threshold_value && filteredsignal2<=threshold_value) {
Gerth 4:bf7765b0f612 256 led1=1;
Gerth 4:bf7765b0f612 257 led2=0;
Gerth 16:63320b8f79c2 258 desired_position += (mm_per_sec_emg/readsignal_frequency);// move desiredposition right
Gerth 11:c10651055871 259 if (desired_position>=maxdisplacement) {//check if the pod doesnt move too far and hit the edge
Gerth 11:c10651055871 260 desired_position=maxdisplacement;
Gerth 11:c10651055871 261 } else {
Gerth 11:c10651055871 262 desired_position=desired_position;
Gerth 11:c10651055871 263 }
Gerth 14:4c4f45a1dd23 264 // check if pod has to move to the left
Gerth 4:bf7765b0f612 265 } else if (filteredsignal1<=threshold_value && filteredsignal2>=threshold_value) {
Gerth 4:bf7765b0f612 266 led1=0;
Gerth 4:bf7765b0f612 267 led2=1;
Gerth 16:63320b8f79c2 268 desired_position -= (mm_per_sec_emg/readsignal_frequency);//move desiredposition left
Gerth 11:c10651055871 269 if (desired_position<=(-1*maxdisplacement)) {//check if the pod doesnt move too far and hit the edge
Gerth 14:4c4f45a1dd23 270 desired_position=(-1*maxdisplacement);
Gerth 11:c10651055871 271 } else {
Gerth 11:c10651055871 272 desired_position=desired_position;
Gerth 11:c10651055871 273 }
Gerth 3:48438eea184e 274 } else {
Gerth 4:bf7765b0f612 275 led1=led2=0;
Gerth 3:48438eea184e 276 }
Gerth 16:63320b8f79c2 277 }
Gerth 16:63320b8f79c2 278 ///////////////////////////////////////////////READ BUTTON AND MOVE DESIRED POSITION
Gerth 17:72d3522165ac 279 void readsignalbutton()
Gerth 16:63320b8f79c2 280 {
Gerth 17:72d3522165ac 281 int buttonr=buttonR.read();
Gerth 17:72d3522165ac 282 int buttonl=buttonL.read();
Gerth 16:63320b8f79c2 283 //check if pod has to shoot
Gerth 17:72d3522165ac 284 if (buttonr==0 && buttonl==0) {
Gerth 16:63320b8f79c2 285 led1=led2=1;
Gerth 16:63320b8f79c2 286 shoot();
Gerth 16:63320b8f79c2 287 // check if pod has to move to the right
Gerth 17:72d3522165ac 288 } else if (buttonr==0 && buttonl==1) {
Gerth 16:63320b8f79c2 289 led1=1;
Gerth 16:63320b8f79c2 290 led2=0;
Gerth 16:63320b8f79c2 291 desired_position += (mm_per_sec_emg/readsignal_frequency);// move desiredposition right
Gerth 17:72d3522165ac 292 if (desired_position>=maxdisplacement) {//check if the pod doesnt move too far and hit the edge
Gerth 16:63320b8f79c2 293 desired_position=maxdisplacement;
Gerth 16:63320b8f79c2 294 } else {
Gerth 16:63320b8f79c2 295 desired_position=desired_position;
Gerth 16:63320b8f79c2 296 }
Gerth 17:72d3522165ac 297 // check if pod has to move to the left
Gerth 17:72d3522165ac 298 } else if (buttonr==1 && buttonl==0) {
Gerth 16:63320b8f79c2 299 led1=0;
Gerth 16:63320b8f79c2 300 led2=1;
Gerth 16:63320b8f79c2 301 desired_position -= (mm_per_sec_emg/readsignal_frequency);//move desiredposition left
Gerth 17:72d3522165ac 302 if (desired_position<=(-1*maxdisplacement)) {//check if the pod doesnt move too far and hit the edge
Gerth 16:63320b8f79c2 303 desired_position=(-1*maxdisplacement);
Gerth 16:63320b8f79c2 304 } else {
Gerth 16:63320b8f79c2 305 desired_position=desired_position;
Gerth 16:63320b8f79c2 306 }
Gerth 16:63320b8f79c2 307 } else {
Gerth 16:63320b8f79c2 308 led1=led2=0;
Gerth 16:63320b8f79c2 309 }
Gerth 3:48438eea184e 310 }
Gerth 3:48438eea184e 311
Gerth 6:37c94a5e205f 312 void changemode()
Gerth 6:37c94a5e205f 313 {
Gerth 10:9e96d14d7034 314 mycontroller.STOP();
Gerth 6:37c94a5e205f 315 switchedmode=true;
Gerth 6:37c94a5e205f 316 modecounter++;
Gerth 15:17de575b7385 317 if (modecounter==4) {
Gerth 6:37c94a5e205f 318 modecounter=0;
Gerth 6:37c94a5e205f 319 } else {
Gerth 6:37c94a5e205f 320 modecounter=modecounter;
Gerth 6:37c94a5e205f 321 }
Gerth 6:37c94a5e205f 322 wait(1);
Gerth 6:37c94a5e205f 323 }
Gerth 0:dd66fff537d7 324
Gerth 16:63320b8f79c2 325 ///////////////////////////////////////////////////MAIN
Gerth 7:7fbb2c028778 326
Gerth 0:dd66fff537d7 327 int main()
Gerth 0:dd66fff537d7 328 {
Gerth 1:917c07a4f3ec 329 //tickers
Gerth 1:917c07a4f3ec 330 safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency);
Gerth 1:917c07a4f3ec 331 filter_ticker.attach(&filter_activate,1.0/filter_frequency);
Gerth 1:917c07a4f3ec 332 control_ticker.attach(&control_activate,1.0/control_frequency);
Gerth 1:917c07a4f3ec 333 scope_ticker.attach(&scopedata_activate,1.0/scope_frequency);
Gerth 3:48438eea184e 334 readsignal_ticker.attach(&readsignal_activate, 1.0/readsignal_frequency);
Gerth 7:7fbb2c028778 335 readbuttoncalibrate_ticker.attach(&readbuttoncalibrate_activate, 1.0/readbuttoncalibrate_frequency);
Gerth 14:4c4f45a1dd23 336 ledblink_ticker.attach(&ledblink_activate, 1.0/ledblink_frequency);
Gerth 14:4c4f45a1dd23 337
Gerth 1:917c07a4f3ec 338 while(1) {
Gerth 6:37c94a5e205f 339 if (changemodebutton==0) {
Gerth 6:37c94a5e205f 340 changemode();
Gerth 1:917c07a4f3ec 341 }
Gerth 9:4ee354663560 342 if (scopedata_go==true) {
Gerth 9:4ee354663560 343 scopedata();
Gerth 9:4ee354663560 344 scopedata_go=false;
Gerth 9:4ee354663560 345 }
Gerth 9:4ee354663560 346 if (safetyandthreshold_go==true) {
Gerth 9:4ee354663560 347 safetyandthreshold();
Gerth 9:4ee354663560 348 safetyandthreshold_go=false;
Gerth 9:4ee354663560 349 }
Gerth 7:7fbb2c028778 350 ///////////////////////////////////////////NORMAL RUNNING MODE
Gerth 7:7fbb2c028778 351 if(modecounter==0) {
Gerth 6:37c94a5e205f 352 if (switchedmode==true) {
Gerth 6:37c94a5e205f 353 encoder1.reset();
Gerth 6:37c94a5e205f 354 encoder2.reset();
Gerth 6:37c94a5e205f 355 pc.printf("Program running\n");//
Gerth 16:63320b8f79c2 356 ledgreen=0;
Gerth 16:63320b8f79c2 357 led1=led2=ledred=ledblue=1;
Gerth 6:37c94a5e205f 358 switchedmode=false;
Gerth 6:37c94a5e205f 359 }
Gerth 9:4ee354663560 360
Gerth 6:37c94a5e205f 361 if (filter_go==true) {
Gerth 6:37c94a5e205f 362 filtereverything();
Gerth 6:37c94a5e205f 363 filter_go=false;
Gerth 6:37c94a5e205f 364 }
Gerth 17:72d3522165ac 365 if (readsignal_go==true) {
Gerth 17:72d3522165ac 366 readsignal();
Gerth 17:72d3522165ac 367 readsignal_go=false;
Gerth 17:72d3522165ac 368 }
Gerth 6:37c94a5e205f 369 if (control_go==true) {
Gerth 16:63320b8f79c2 370 desired_angle[0]=anglepos.positiontoangle1(desired_position,y_start);
Gerth 16:63320b8f79c2 371 desired_angle[1]=anglepos.positiontoangle2(desired_position,y_start);
Gerth 16:63320b8f79c2 372
Gerth 11:c10651055871 373 float error1=(desired_angle[0]-counttorad*encoder1.getPulses());
Gerth 11:c10651055871 374 float error2=(desired_angle[1]-counttorad*encoder2.getPulses());
Gerth 7:7fbb2c028778 375 mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int);
Gerth 6:37c94a5e205f 376 control_go=false;
Gerth 6:37c94a5e205f 377 }
Gerth 17:72d3522165ac 378
Gerth 6:37c94a5e205f 379 valuechangebutton.fall(&valuechange);
Gerth 1:917c07a4f3ec 380 }
Gerth 7:7fbb2c028778 381 ////////////////////////////////////////////////////CALIBRATE RIGHT ARM
Gerth 6:37c94a5e205f 382 if (modecounter==1) {
Gerth 6:37c94a5e205f 383 if(switchedmode==true) {
Gerth 6:37c94a5e205f 384 pc.printf("Calibration mode! Use buttons to move rigth arm to 0 degrees\n");
Gerth 16:63320b8f79c2 385 led1=led2=ledred=0;
Gerth 16:63320b8f79c2 386 ledgreen=ledblue=1;
Gerth 6:37c94a5e205f 387 switchedmode=false;
Gerth 14:4c4f45a1dd23 388 }
Gerth 14:4c4f45a1dd23 389 if (ledblink_go==true) {
Gerth 14:4c4f45a1dd23 390 led1=!led1;
Gerth 16:63320b8f79c2 391 ledblink_go=false;
Gerth 6:37c94a5e205f 392 }
Gerth 7:7fbb2c028778 393 if (control_go==true) {
Gerth 11:c10651055871 394 float error1=(desired_angle[0]-counttorad*encoder1.getPulses());
Gerth 9:4ee354663560 395 float error2=0;// this is the error you want to use
Gerth 7:7fbb2c028778 396 mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int);
Gerth 7:7fbb2c028778 397 control_go=false;
Gerth 7:7fbb2c028778 398 }
Gerth 9:4ee354663560 399 if (readbuttoncalibrate_go==true) {
Gerth 9:4ee354663560 400 if (buttonR.read()==0 && buttonL.read()==1) {
Gerth 9:4ee354663560 401 desired_angle[0] += (radpersec_calibrate/readbuttoncalibrate_frequency);
Gerth 9:4ee354663560 402 readbuttoncalibrate_go=false;
Gerth 9:4ee354663560 403 }
Gerth 9:4ee354663560 404 if (buttonR.read()==1 && buttonL.read()==0) {
Gerth 9:4ee354663560 405 desired_angle[0] -= (radpersec_calibrate/readbuttoncalibrate_frequency);
Gerth 9:4ee354663560 406 readbuttoncalibrate_go=false;
Gerth 9:4ee354663560 407 }
Gerth 9:4ee354663560 408 }
Gerth 8:54a7da09ccad 409 }
Gerth 8:54a7da09ccad 410 ////////////////////////////////////////////CALIBRATE LEFT ARM
Gerth 8:54a7da09ccad 411 if (modecounter==2) {
Gerth 8:54a7da09ccad 412 if(switchedmode==true) {
Gerth 8:54a7da09ccad 413 pc.printf("Calibration mode! Use buttons to move left arm to 0 degrees\n");
Gerth 16:63320b8f79c2 414 led1=led2=ledred=0;
Gerth 16:63320b8f79c2 415 ledgreen=ledblue=1;
Gerth 8:54a7da09ccad 416 switchedmode=false;
Gerth 8:54a7da09ccad 417 }
Gerth 14:4c4f45a1dd23 418 if (ledblink_go==true) {
Gerth 14:4c4f45a1dd23 419 led2=!led2;
Gerth 16:63320b8f79c2 420 ledblink_go=false;
Gerth 14:4c4f45a1dd23 421 }
Gerth 8:54a7da09ccad 422 if (control_go==true) {
Gerth 8:54a7da09ccad 423 float error1=0;
Gerth 8:54a7da09ccad 424 float error2=(desired_angle[1]-counttorad*encoder2.getPulses());// this is the error you want to use
Gerth 8:54a7da09ccad 425 mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int);
Gerth 8:54a7da09ccad 426 control_go=false;
Gerth 8:54a7da09ccad 427 }
Gerth 8:54a7da09ccad 428 if (readbuttoncalibrate_go==true) {
Gerth 8:54a7da09ccad 429 if (buttonR.read()==0 && buttonL.read()==1) {
Gerth 8:54a7da09ccad 430 desired_angle[1] += (radpersec_calibrate/readbuttoncalibrate_frequency);
Gerth 8:54a7da09ccad 431 readbuttoncalibrate_go=false;
Gerth 7:7fbb2c028778 432 }
Gerth 8:54a7da09ccad 433 if (buttonR.read()==1 && buttonL.read()==0) {
Gerth 8:54a7da09ccad 434 desired_angle[1] -= (radpersec_calibrate/readbuttoncalibrate_frequency);
Gerth 8:54a7da09ccad 435 readbuttoncalibrate_go=false;
Gerth 7:7fbb2c028778 436 }
Gerth 6:37c94a5e205f 437 }
Gerth 3:48438eea184e 438 }
Gerth 16:63320b8f79c2 439 ///////////////////////////////BUTTONCONTROLMODE
Gerth 15:17de575b7385 440 if (modecounter==3) {
Gerth 16:63320b8f79c2 441 if (switchedmode==true) {
Gerth 16:63320b8f79c2 442 led1=led2=0;
Gerth 16:63320b8f79c2 443 ledred=ledblue=1;
Gerth 16:63320b8f79c2 444 switchedmode=false;
Gerth 16:63320b8f79c2 445 }
Gerth 16:63320b8f79c2 446 if (ledblink_go==true) {
Gerth 15:17de575b7385 447 ledgreen=!ledgreen;
Gerth 16:63320b8f79c2 448 ledblink_go=false;
Gerth 15:17de575b7385 449 }
Gerth 16:63320b8f79c2 450 if (readsignal_go==true) {
Gerth 17:72d3522165ac 451 readsignalbutton();
Gerth 17:72d3522165ac 452 readsignal_go=false;
Gerth 17:72d3522165ac 453 }
Gerth 17:72d3522165ac 454 if (control_go==true) {
Gerth 17:72d3522165ac 455 desired_angle[0]=anglepos.positiontoangle1(desired_position,y_start);
Gerth 17:72d3522165ac 456 desired_angle[1]=anglepos.positiontoangle2(desired_position,y_start);
Gerth 17:72d3522165ac 457
Gerth 17:72d3522165ac 458 float error1=(desired_angle[0]-counttorad*encoder1.getPulses());
Gerth 17:72d3522165ac 459 float error2=(desired_angle[1]-counttorad*encoder2.getPulses());
Gerth 17:72d3522165ac 460 mycontroller.PI(error1,error2,Kp,Ki,Ts_control,error1_int,error2_int);
Gerth 17:72d3522165ac 461 control_go=false;
Gerth 16:63320b8f79c2 462 }
Gerth 16:63320b8f79c2 463 }
Gerth 15:17de575b7385 464
Gerth 0:dd66fff537d7 465 }
Gerth 8:54a7da09ccad 466 }