EMG and motor script together, Not fully working yet,

Dependencies:   Encoder QEI biquadFilter mbed

Committer:
Joost38H
Date:
Wed Oct 25 13:32:29 2017 +0000
Revision:
2:2c4ee76dc830
Parent:
1:6aac013b0ba3
Child:
3:59b504840b95
hij doet nog niet wat ik wil

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Joost38H 0:eb16ed402ffa 1 #include "mbed.h"
Joost38H 0:eb16ed402ffa 2 #include "math.h"
Joost38H 0:eb16ed402ffa 3 #include "encoder.h"
Joost38H 0:eb16ed402ffa 4 #include "QEI.h"
Joost38H 0:eb16ed402ffa 5 #include "BiQuad.h"
Joost38H 0:eb16ed402ffa 6
Joost38H 0:eb16ed402ffa 7 Serial pc(USBTX, USBRX);
Joost38H 0:eb16ed402ffa 8
Joost38H 0:eb16ed402ffa 9 //Defining all in- and outputs
Joost38H 0:eb16ed402ffa 10 //EMG input
Joost38H 0:eb16ed402ffa 11 AnalogIn emgBR( A0 ); //Right Biceps
Joost38H 0:eb16ed402ffa 12 AnalogIn emgBL( A1 ); //Left Biceps
Joost38H 0:eb16ed402ffa 13
Joost38H 0:eb16ed402ffa 14 //Output motor 1 and reading Encoder motor 1
Joost38H 0:eb16ed402ffa 15 DigitalOut motor1DirectionPin(D4);
Joost38H 0:eb16ed402ffa 16 PwmOut motor1MagnitudePin(D5);
Joost38H 0:eb16ed402ffa 17 QEI Encoder1(D12,D13,NC,32);
Joost38H 0:eb16ed402ffa 18
Joost38H 0:eb16ed402ffa 19
Joost38H 0:eb16ed402ffa 20 //Output motor 2 and reading Encoder motor 2
Joost38H 0:eb16ed402ffa 21 DigitalOut motor2DirectionPin(D7);
Joost38H 0:eb16ed402ffa 22 PwmOut motor2MagnitudePin(D6);
Joost38H 0:eb16ed402ffa 23 QEI Encoder2(D10,D11,NC,32);
Joost38H 0:eb16ed402ffa 24
Joost38H 0:eb16ed402ffa 25 //LED output, needed for feedback
Joost38H 0:eb16ed402ffa 26 DigitalOut led_R(LED_RED);
Joost38H 0:eb16ed402ffa 27 DigitalOut led_G(LED_GREEN);
Joost38H 0:eb16ed402ffa 28 DigitalOut led_B(LED_BLUE);
Joost38H 0:eb16ed402ffa 29
Joost38H 1:6aac013b0ba3 30 //Setting Tickers for sampling EMG and determing if the threshold is met
Joost38H 1:6aac013b0ba3 31 Ticker sample_timer;
Joost38H 1:6aac013b0ba3 32 Ticker threshold_timer;
Joost38H 1:6aac013b0ba3 33
Joost38H 0:eb16ed402ffa 34 InterruptIn button(SW2); // Wordt uiteindelijk vervangen door EMG
Joost38H 0:eb16ed402ffa 35
Joost38H 0:eb16ed402ffa 36 Timer t;
Joost38H 0:eb16ed402ffa 37 double speedfactor; // = 0.01; snelheid in, zonder potmeter gebruik <- waarom is dit zo?
Joost38H 0:eb16ed402ffa 38
Joost38H 0:eb16ed402ffa 39 // Getting the counts from the Encoder
Joost38H 0:eb16ed402ffa 40 int counts1 = Encoder1.getPulses();
Joost38H 0:eb16ed402ffa 41 int counts2 = Encoder2.getPulses();
Joost38H 0:eb16ed402ffa 42
Joost38H 0:eb16ed402ffa 43 // Defining variables delta (the difference between position and desired position) <- Is dit zo?
Joost38H 0:eb16ed402ffa 44 int delta1;
Joost38H 0:eb16ed402ffa 45 int delta2;
Joost38H 0:eb16ed402ffa 46
Joost38H 1:6aac013b0ba3 47 /* Defining all the different BiQuad filters, which contain a Notch filter,
Joost38H 1:6aac013b0ba3 48 High-pass filter and Low-pass filter. The Notch filter cancels all frequencies
Joost38H 1:6aac013b0ba3 49 between 49 and 51 Hz, the High-pass filter cancels all frequencies below 20 Hz
Joost38H 1:6aac013b0ba3 50 and the Low-pass filter cancels out all frequencies below 4 Hz */
Joost38H 1:6aac013b0ba3 51
Joost38H 1:6aac013b0ba3 52 /* Defining all the normalized values of b and a in the Notch filter for the
Joost38H 1:6aac013b0ba3 53 creation of the Notch BiQuad */
Joost38H 1:6aac013b0ba3 54
Joost38H 1:6aac013b0ba3 55 BiQuad bqNotch1( 0.9876, -1.5981, 0.9876, -1.5981, 0.9752 );
Joost38H 1:6aac013b0ba3 56 BiQuad bqNotch2( 0.9876, -1.5981, 0.9876, -1.5981, 0.9752 );
Joost38H 1:6aac013b0ba3 57
Joost38H 1:6aac013b0ba3 58 /* Defining all the normalized values of b and a in the High-pass filter for the
Joost38H 1:6aac013b0ba3 59 creation of the High-pass BiQuad */
Joost38H 1:6aac013b0ba3 60
Joost38H 1:6aac013b0ba3 61 BiQuad bqHigh1( 0.8371, -1.6742, 0.8371, -1.6475, 0.7009 );
Joost38H 1:6aac013b0ba3 62 BiQuad bqHigh2( 0.8371, -1.6742, 0.8371, -1.6475, 0.7009 );
Joost38H 1:6aac013b0ba3 63
Joost38H 1:6aac013b0ba3 64 /* Defining all the normalized values of b and a in the Low-pass filter for the
Joost38H 1:6aac013b0ba3 65 creation of the Low-pass BiQuad */
Joost38H 1:6aac013b0ba3 66
Joost38H 1:6aac013b0ba3 67 BiQuad bqLow1( 6.0985e-4, 0.0012, 6.0985e-4, -1.9289, 0.9314 );
Joost38H 1:6aac013b0ba3 68 BiQuad bqLow2( 6.0985e-4, 0.0012, 6.0985e-4, -1.9289, 0.9314 );
Joost38H 1:6aac013b0ba3 69
Joost38H 1:6aac013b0ba3 70 // Creating a variable needed for the creation of the BiQuadChain
Joost38H 1:6aac013b0ba3 71 BiQuadChain bqChain1;
Joost38H 1:6aac013b0ba3 72 BiQuadChain bqChain2;
Joost38H 1:6aac013b0ba3 73
Joost38H 1:6aac013b0ba3 74
Joost38H 1:6aac013b0ba3 75 //Declaring all doubles needed in the filtering process
Joost38H 1:6aac013b0ba3 76 double emgBRfiltered; //Right biceps Notch+High pass filter
Joost38H 1:6aac013b0ba3 77 double emgBRrectified; //Right biceps rectified
Joost38H 1:6aac013b0ba3 78 double emgBRcomplete; //Right biceps low-pass filter, filtering complete
Joost38H 1:6aac013b0ba3 79
Joost38H 1:6aac013b0ba3 80 double emgBLfiltered; //Left biceps Notch+High pass filter
Joost38H 1:6aac013b0ba3 81 double emgBLrectified; //Left biceps rectified
Joost38H 1:6aac013b0ba3 82 double emgBLcomplete; //Left biceps low-pass filter, filtering complete
Joost38H 1:6aac013b0ba3 83
Joost38H 1:6aac013b0ba3 84 /* Declaring counters and variables needed to get threshold of both muscles.
Joost38H 1:6aac013b0ba3 85 This is neeeded for the calibration process*/
Joost38H 1:6aac013b0ba3 86 int countBR = 0;
Joost38H 1:6aac013b0ba3 87 int countBL = 0;
Joost38H 1:6aac013b0ba3 88
Joost38H 1:6aac013b0ba3 89 double numsamples = 500;
Joost38H 1:6aac013b0ba3 90 double emgBRsum = 0;
Joost38H 1:6aac013b0ba3 91 double emgBRmeanMVC;
Joost38H 1:6aac013b0ba3 92 double thresholdBR;
Joost38H 1:6aac013b0ba3 93
Joost38H 1:6aac013b0ba3 94 double emgBLsum = 0;
Joost38H 1:6aac013b0ba3 95 double emgBLmeanMVC;
Joost38H 1:6aac013b0ba3 96 double thresholdBL;
Joost38H 1:6aac013b0ba3 97
Joost38H 1:6aac013b0ba3 98 // EMG functions
Joost38H 1:6aac013b0ba3 99 void EMG_sample()
Joost38H 1:6aac013b0ba3 100 {
Joost38H 1:6aac013b0ba3 101 //Filtering steps for the Right Biceps EMG
Joost38H 1:6aac013b0ba3 102 emgBRfiltered = bqChain1.step( emgBR.read() ); //Notch+High-pass
Joost38H 1:6aac013b0ba3 103 emgBRrectified = fabs(emgBRfiltered); //Rectification
Joost38H 1:6aac013b0ba3 104 emgBRcomplete = bqLow1.step(emgBRrectified); //Low-pass
Joost38H 1:6aac013b0ba3 105
Joost38H 1:6aac013b0ba3 106
Joost38H 2:2c4ee76dc830 107 /*Getting threshold value for Right Biceps, a value of 20% of
Joost38H 1:6aac013b0ba3 108 Maximum Voluntary Contraction is chosen as threshold value */
Joost38H 1:6aac013b0ba3 109 if (countBR < numsamples) {
Joost38H 1:6aac013b0ba3 110 emgBRsum = emgBRsum + emgBRcomplete;
Joost38H 1:6aac013b0ba3 111 countBR++;
Joost38H 2:2c4ee76dc830 112 led_R = 0;
Joost38H 2:2c4ee76dc830 113 led_B = 0;
Joost38H 2:2c4ee76dc830 114 led_G = 1;
Joost38H 1:6aac013b0ba3 115 }
Joost38H 1:6aac013b0ba3 116
Joost38H 1:6aac013b0ba3 117 emgBRmeanMVC = emgBRsum / numsamples;
Joost38H 1:6aac013b0ba3 118
Joost38H 2:2c4ee76dc830 119 thresholdBR = emgBRmeanMVC * 0.25;
Joost38H 2:2c4ee76dc830 120
Joost38H 1:6aac013b0ba3 121 //Filtering steps for the Left Biceps EMG
Joost38H 1:6aac013b0ba3 122 emgBLfiltered = bqChain2.step( emgBL.read() ); //Notch+High-pass
Joost38H 1:6aac013b0ba3 123 emgBLrectified = fabs( emgBLfiltered ); //Rectification
Joost38H 1:6aac013b0ba3 124 emgBLcomplete = bqLow2.step( emgBLrectified ); //Low-pass
Joost38H 1:6aac013b0ba3 125
Joost38H 2:2c4ee76dc830 126
Joost38H 2:2c4ee76dc830 127 /*Getting threshold value for Left Biceps, a value of 20% of
Joost38H 1:6aac013b0ba3 128 Maximum Voluntary Contraction is chosen as threshold value */
Joost38H 1:6aac013b0ba3 129 if (countBL < numsamples) {
Joost38H 1:6aac013b0ba3 130 emgBLsum = emgBLsum + emgBLcomplete;
Joost38H 1:6aac013b0ba3 131 countBL++;
Joost38H 1:6aac013b0ba3 132 }
Joost38H 1:6aac013b0ba3 133
Joost38H 1:6aac013b0ba3 134 emgBLmeanMVC = emgBLsum / numsamples;
Joost38H 1:6aac013b0ba3 135
Joost38H 2:2c4ee76dc830 136 thresholdBL = emgBLmeanMVC * 0.25;
Joost38H 2:2c4ee76dc830 137
Joost38H 2:2c4ee76dc830 138 //pc.printf("ThresholdBR = %0.3f, ThresholdBL = %0.3f \n", thresholdBR,thresholdBL);
Joost38H 1:6aac013b0ba3 139 }
Joost38H 1:6aac013b0ba3 140
Joost38H 1:6aac013b0ba3 141 // Function to make the BiQuadChain for the Notch and High pass filter for both filters
Joost38H 1:6aac013b0ba3 142 void getbqChain()
Joost38H 1:6aac013b0ba3 143 {
Joost38H 1:6aac013b0ba3 144 bqChain1.add(&bqNotch1).add(&bqHigh1);
Joost38H 1:6aac013b0ba3 145 bqChain2.add(&bqNotch2).add(&bqHigh2);
Joost38H 1:6aac013b0ba3 146 }
Joost38H 1:6aac013b0ba3 147
Joost38H 0:eb16ed402ffa 148 // Initial input value for X
Joost38H 0:eb16ed402ffa 149 int Xin=0; //<- Hoe zit het met deze als we de robot daadwerkelijk gebruiken
Joost38H 0:eb16ed402ffa 150 double huidigetijdX;
Joost38H 0:eb16ed402ffa 151
Joost38H 0:eb16ed402ffa 152 // Feedback system for counting values of X
Joost38H 0:eb16ed402ffa 153 void ledtX(){
Joost38H 0:eb16ed402ffa 154 t.reset();
Joost38H 0:eb16ed402ffa 155 Xin++;
Joost38H 0:eb16ed402ffa 156 pc.printf("Xin is %i\n",Xin);
Joost38H 0:eb16ed402ffa 157 led_G=0;
Joost38H 0:eb16ed402ffa 158 led_R=1;
Joost38H 2:2c4ee76dc830 159 wait(0.2);
Joost38H 0:eb16ed402ffa 160 led_G=1;
Joost38H 0:eb16ed402ffa 161 led_R=0;
Joost38H 2:2c4ee76dc830 162 wait(0.5);
Joost38H 0:eb16ed402ffa 163 }
Joost38H 0:eb16ed402ffa 164
Joost38H 0:eb16ed402ffa 165
Joost38H 0:eb16ed402ffa 166 // Couting system for values of X
Joost38H 0:eb16ed402ffa 167 int tellerX(){
Joost38H 0:eb16ed402ffa 168 led_G=1;
Joost38H 0:eb16ed402ffa 169 led_B=1;
Joost38H 2:2c4ee76dc830 170 led_R=0;
Joost38H 0:eb16ed402ffa 171 while(true){
Joost38H 1:6aac013b0ba3 172 //button.fall(ledtX); //This has to be replaced by EMG
Joost38H 1:6aac013b0ba3 173 if (emgBRcomplete > thresholdBR){
Joost38H 0:eb16ed402ffa 174 ledtX(); // dit is wat je uiteindelijk wil dat er staat
Joost38H 1:6aac013b0ba3 175 }
Joost38H 0:eb16ed402ffa 176 t.start();
Joost38H 0:eb16ed402ffa 177 huidigetijdX=t.read();
Joost38H 0:eb16ed402ffa 178 if (huidigetijdX>2){
Joost38H 0:eb16ed402ffa 179 led_R=1; //Go to the next program (couting values for Y)
Joost38H 2:2c4ee76dc830 180 if (emgBRcomplete > thresholdBR){
Joost38H 2:2c4ee76dc830 181 0; // dit is wat je uiteindelijk wil dat er staat
Joost38H 2:2c4ee76dc830 182 }
Joost38H 0:eb16ed402ffa 183 return 0;
Joost38H 0:eb16ed402ffa 184 }
Joost38H 0:eb16ed402ffa 185 }
Joost38H 0:eb16ed402ffa 186 }
Joost38H 0:eb16ed402ffa 187
Joost38H 0:eb16ed402ffa 188 // Initial values needed for Y (see comments at X function)
Joost38H 0:eb16ed402ffa 189 int Yin=0;
Joost38H 0:eb16ed402ffa 190 double huidigetijdY;
Joost38H 0:eb16ed402ffa 191
Joost38H 0:eb16ed402ffa 192 //Feedback system for couting values of Y
Joost38H 0:eb16ed402ffa 193 void ledtY(){
Joost38H 0:eb16ed402ffa 194 t.reset();
Joost38H 0:eb16ed402ffa 195 Yin++;
Joost38H 0:eb16ed402ffa 196 pc.printf("Yin is %i\n",Yin);
Joost38H 0:eb16ed402ffa 197 led_G=0;
Joost38H 0:eb16ed402ffa 198 led_B=1;
Joost38H 2:2c4ee76dc830 199 wait(0.2);
Joost38H 0:eb16ed402ffa 200 led_G=1;
Joost38H 0:eb16ed402ffa 201 led_B=0;
Joost38H 2:2c4ee76dc830 202 wait(0.5);
Joost38H 0:eb16ed402ffa 203 }
Joost38H 0:eb16ed402ffa 204
Joost38H 0:eb16ed402ffa 205 // Couting system for values of Y
Joost38H 0:eb16ed402ffa 206 int tellerY(){
Joost38H 0:eb16ed402ffa 207 t.reset();
Joost38H 0:eb16ed402ffa 208 led_G=1;
Joost38H 0:eb16ed402ffa 209 led_B=0;
Joost38H 2:2c4ee76dc830 210 led_R=1;
Joost38H 0:eb16ed402ffa 211 while(true){
Joost38H 1:6aac013b0ba3 212 //button.fall(ledtY); //See comments at X
Joost38H 1:6aac013b0ba3 213 if (emgBRcomplete > thresholdBR){
Joost38H 1:6aac013b0ba3 214 ledtY(); // dit is wat je uiteindelijk wil dat er staat
Joost38H 1:6aac013b0ba3 215 }
Joost38H 0:eb16ed402ffa 216 t.start();
Joost38H 0:eb16ed402ffa 217 huidigetijdY=t.read();
Joost38H 0:eb16ed402ffa 218 if (huidigetijdY>2){
Joost38H 0:eb16ed402ffa 219 led_B=1;
Joost38H 2:2c4ee76dc830 220 if (emgBRcomplete > thresholdBR){
Joost38H 2:2c4ee76dc830 221 0; // dit is wat je uiteindelijk wil dat er staat
Joost38H 2:2c4ee76dc830 222 }
Joost38H 2:2c4ee76dc830 223
Joost38H 2:2c4ee76dc830 224 //button.fall(0); // Wat is deze?
Joost38H 0:eb16ed402ffa 225 return 0; // ga door naar het volgende programma
Joost38H 0:eb16ed402ffa 226 }
Joost38H 0:eb16ed402ffa 227 }
Joost38H 0:eb16ed402ffa 228 }
Joost38H 0:eb16ed402ffa 229
Joost38H 0:eb16ed402ffa 230 bool B = false;
Joost38H 0:eb16ed402ffa 231
Joost38H 0:eb16ed402ffa 232 // Declaring all variables needed for calculating rope lengths,
Joost38H 0:eb16ed402ffa 233 double Pox = 0;
Joost38H 0:eb16ed402ffa 234 double Poy = 0;
Joost38H 0:eb16ed402ffa 235 double Pbx = 0;
Joost38H 0:eb16ed402ffa 236 double Pby = 887;
Joost38H 0:eb16ed402ffa 237 double Prx = 768;
Joost38H 0:eb16ed402ffa 238 double Pry = 443;
Joost38H 0:eb16ed402ffa 239 double Pex=121;
Joost38H 0:eb16ed402ffa 240 double Pey=308;
Joost38H 0:eb16ed402ffa 241 double diamtrklosje=20;
Joost38H 0:eb16ed402ffa 242 double pi=3.14159265359;
Joost38H 0:eb16ed402ffa 243 double omtrekklosje=diamtrklosje*pi;
Joost38H 0:eb16ed402ffa 244 double Lou;
Joost38H 0:eb16ed402ffa 245 double Lbu;
Joost38H 0:eb16ed402ffa 246 double Lru;
Joost38H 0:eb16ed402ffa 247 double dLod;
Joost38H 0:eb16ed402ffa 248 double dLbd;
Joost38H 0:eb16ed402ffa 249 double dLrd;
Joost38H 0:eb16ed402ffa 250
Joost38H 0:eb16ed402ffa 251 // Declaring variables needed for calculating motor counts
Joost38H 0:eb16ed402ffa 252 double roto;
Joost38H 0:eb16ed402ffa 253 double rotb;
Joost38H 0:eb16ed402ffa 254 double rotr;
Joost38H 0:eb16ed402ffa 255 double rotzo;
Joost38H 0:eb16ed402ffa 256 double rotzb;
Joost38H 0:eb16ed402ffa 257 double rotzr;
Joost38H 0:eb16ed402ffa 258 double counto;
Joost38H 0:eb16ed402ffa 259 double countb;
Joost38H 0:eb16ed402ffa 260 double countr;
Joost38H 0:eb16ed402ffa 261 double countzo;
Joost38H 0:eb16ed402ffa 262 double countzb;
Joost38H 0:eb16ed402ffa 263 double countzr;
Joost38H 0:eb16ed402ffa 264
Joost38H 0:eb16ed402ffa 265 // Declaring variables neeeded for calculating motor movements to get to a certain point <- klopt dit?
Joost38H 0:eb16ed402ffa 266 double Psx;
Joost38H 0:eb16ed402ffa 267 double Psy;
Joost38H 0:eb16ed402ffa 268 double Vex;
Joost38H 0:eb16ed402ffa 269 double Vey;
Joost38H 0:eb16ed402ffa 270 double Kz=0.7; // nadersnelheid instellen
Joost38H 0:eb16ed402ffa 271 double modVe;
Joost38H 0:eb16ed402ffa 272 double Vmax=20;
Joost38H 0:eb16ed402ffa 273 double Pstx;
Joost38H 0:eb16ed402ffa 274 double Psty;
Joost38H 0:eb16ed402ffa 275 double T=0.02;//seconds
Joost38H 0:eb16ed402ffa 276
Joost38H 0:eb16ed402ffa 277
Joost38H 0:eb16ed402ffa 278 //Deel om motor(en) aan te sturen--------------------------------------------
Joost38H 0:eb16ed402ffa 279
Joost38H 0:eb16ed402ffa 280 void calcdelta1() {
Joost38H 0:eb16ed402ffa 281 delta1 = (counto - Encoder1.getPulses());
Joost38H 0:eb16ed402ffa 282 }
Joost38H 0:eb16ed402ffa 283
Joost38H 0:eb16ed402ffa 284 void calcdelta2() {
Joost38H 2:2c4ee76dc830 285 delta2 = (countb - Encoder2.getPulses()); // <------- de reden dat de delta negatief is (jitse)
Joost38H 0:eb16ed402ffa 286 }
Joost38H 0:eb16ed402ffa 287
Joost38H 0:eb16ed402ffa 288 double referenceVelocity1;
Joost38H 0:eb16ed402ffa 289 double motorValue1;
Joost38H 0:eb16ed402ffa 290
Joost38H 0:eb16ed402ffa 291 double referenceVelocity2;
Joost38H 0:eb16ed402ffa 292 double motorValue2;
Joost38H 0:eb16ed402ffa 293
Joost38H 0:eb16ed402ffa 294 Ticker controlmotor1; // één ticker van maken?
Joost38H 0:eb16ed402ffa 295 Ticker calculatedelta1;
Joost38H 0:eb16ed402ffa 296 Ticker printdata1; //aparte ticker om print pc aan te kunnen spreken zonder get te worden van hoeveelheid geprinte waardes
Joost38H 0:eb16ed402ffa 297
Joost38H 0:eb16ed402ffa 298 Ticker controlmotor2; // één ticker van maken?
Joost38H 0:eb16ed402ffa 299 Ticker calculatedelta2;
Joost38H 0:eb16ed402ffa 300 Ticker printdata2; //aparte ticker om print pc aan te kunnen spreken zonder get te worden van hoeveelheid geprinte waardes
Joost38H 0:eb16ed402ffa 301
Joost38H 0:eb16ed402ffa 302 double GetReferenceVelocity1()
Joost38H 0:eb16ed402ffa 303 {
Joost38H 0:eb16ed402ffa 304 // Returns reference velocity in rad/s. Positive value means clockwise rotation.
Joost38H 0:eb16ed402ffa 305 double maxVelocity1=Vex*25+Vey*25; // max 8.4 in rad/s of course!
Joost38H 0:eb16ed402ffa 306 referenceVelocity1 = (-1)*speedfactor * maxVelocity1;
Joost38H 0:eb16ed402ffa 307
Joost38H 0:eb16ed402ffa 308 if (Encoder1.getPulses() < (counto+1))
Joost38H 0:eb16ed402ffa 309 { speedfactor = 0.1;
Joost38H 0:eb16ed402ffa 310 }
Joost38H 0:eb16ed402ffa 311 else if (Encoder1.getPulses() > (counto-1))
Joost38H 0:eb16ed402ffa 312 { speedfactor = -0.1;
Joost38H 0:eb16ed402ffa 313 }
Joost38H 0:eb16ed402ffa 314 else
Joost38H 0:eb16ed402ffa 315 { speedfactor = 0;
Joost38H 0:eb16ed402ffa 316 }
Joost38H 0:eb16ed402ffa 317
Joost38H 0:eb16ed402ffa 318 return referenceVelocity1;
Joost38H 0:eb16ed402ffa 319 }
Joost38H 0:eb16ed402ffa 320
Joost38H 0:eb16ed402ffa 321 double GetReferenceVelocity2()
Joost38H 0:eb16ed402ffa 322 {
Joost38H 0:eb16ed402ffa 323 // Returns reference velocity in rad/s. Positive value means clockwise rotation.
Joost38H 0:eb16ed402ffa 324 double maxVelocity2=Vex*25+Vey*25; // max 8.4 in rad/s of course!
Joost38H 0:eb16ed402ffa 325 referenceVelocity2 = (-1)*speedfactor * maxVelocity2;
Joost38H 0:eb16ed402ffa 326
Joost38H 0:eb16ed402ffa 327 if (Encoder2.getPulses() < (counto+1))
Joost38H 0:eb16ed402ffa 328 { speedfactor = 0.1;
Joost38H 0:eb16ed402ffa 329 }
Joost38H 0:eb16ed402ffa 330 else if (Encoder2.getPulses() > (counto-1))
Joost38H 0:eb16ed402ffa 331 { speedfactor = -0.1;
Joost38H 0:eb16ed402ffa 332 }
Joost38H 0:eb16ed402ffa 333 else
Joost38H 0:eb16ed402ffa 334 { speedfactor = 0;
Joost38H 0:eb16ed402ffa 335 }
Joost38H 0:eb16ed402ffa 336
Joost38H 0:eb16ed402ffa 337 return referenceVelocity2;
Joost38H 0:eb16ed402ffa 338 }
Joost38H 0:eb16ed402ffa 339
Joost38H 0:eb16ed402ffa 340 void SetMotor1(double motorValue1)
Joost38H 0:eb16ed402ffa 341 {
Joost38H 0:eb16ed402ffa 342 // Given -1<=motorValue<=1, this sets the PWM and direction bits for motor 1. Positive value makes
Joost38H 0:eb16ed402ffa 343 // motor rotating clockwise. motorValues outside range are truncated to within range
Joost38H 0:eb16ed402ffa 344 if (motorValue1 >=0) motor1DirectionPin=1;
Joost38H 0:eb16ed402ffa 345 else motor1DirectionPin=0;
Joost38H 0:eb16ed402ffa 346 if (fabs(motorValue1)>1) motor1MagnitudePin = 1;
Joost38H 0:eb16ed402ffa 347 else motor1MagnitudePin = fabs(motorValue1);
Joost38H 0:eb16ed402ffa 348
Joost38H 0:eb16ed402ffa 349 }
Joost38H 0:eb16ed402ffa 350
Joost38H 0:eb16ed402ffa 351 void SetMotor2(double motorValue2)
Joost38H 0:eb16ed402ffa 352 {
Joost38H 0:eb16ed402ffa 353 // Given -1<=motorValue<=1, this sets the PWM and direction bits for motor 1. Positive value makes
Joost38H 0:eb16ed402ffa 354 // motor rotating clockwise. motorValues outside range are truncated to within range
Joost38H 0:eb16ed402ffa 355 if (motorValue2 >=0) motor2DirectionPin=1;
Joost38H 0:eb16ed402ffa 356 else motor2DirectionPin=0;
Joost38H 0:eb16ed402ffa 357 if (fabs(motorValue2)>1) motor2MagnitudePin = 1;
Joost38H 0:eb16ed402ffa 358 else motor2MagnitudePin = fabs(motorValue2);
Joost38H 0:eb16ed402ffa 359
Joost38H 0:eb16ed402ffa 360 }
Joost38H 0:eb16ed402ffa 361
Joost38H 0:eb16ed402ffa 362 double FeedForwardControl1(double referenceVelocity1)
Joost38H 0:eb16ed402ffa 363 {
Joost38H 0:eb16ed402ffa 364 // very simple linear feed-forward control
Joost38H 0:eb16ed402ffa 365 const double MotorGain=8.4; // unit: (rad/s) / PWM, max 8.4
Joost38H 0:eb16ed402ffa 366 double motorValue1 = referenceVelocity1 / MotorGain;
Joost38H 0:eb16ed402ffa 367 return motorValue1;
Joost38H 0:eb16ed402ffa 368 }
Joost38H 0:eb16ed402ffa 369
Joost38H 0:eb16ed402ffa 370 double FeedForwardControl2(double referenceVelocity2)
Joost38H 0:eb16ed402ffa 371 {
Joost38H 0:eb16ed402ffa 372 // very simple linear feed-forward control
Joost38H 0:eb16ed402ffa 373 const double MotorGain=8.4; // unit: (rad/s) / PWM, max 8.4
Joost38H 0:eb16ed402ffa 374 double motorValue2 = referenceVelocity2 / MotorGain;
Joost38H 0:eb16ed402ffa 375 return motorValue2;
Joost38H 0:eb16ed402ffa 376 }
Joost38H 0:eb16ed402ffa 377
Joost38H 0:eb16ed402ffa 378 void MeasureAndControl1()
Joost38H 0:eb16ed402ffa 379 {
Joost38H 0:eb16ed402ffa 380 // This function measures the potmeter position, extracts a reference velocity from it,
Joost38H 0:eb16ed402ffa 381 // and controls the motor with a simple FeedForward controller. Call this from a Ticker.
Joost38H 0:eb16ed402ffa 382 double referenceVelocity1 = GetReferenceVelocity1();
Joost38H 0:eb16ed402ffa 383 double motorValue1 = FeedForwardControl1(referenceVelocity1);
Joost38H 0:eb16ed402ffa 384 SetMotor1(motorValue1);
Joost38H 0:eb16ed402ffa 385 }
Joost38H 0:eb16ed402ffa 386
Joost38H 0:eb16ed402ffa 387 void MeasureAndControl2()
Joost38H 0:eb16ed402ffa 388 {
Joost38H 0:eb16ed402ffa 389 // This function measures the potmeter position, extracts a reference velocity from it,
Joost38H 0:eb16ed402ffa 390 // and controls the motor with a simple FeedForward controller. Call this from a Ticker.
Joost38H 0:eb16ed402ffa 391 double referenceVelocity2 = GetReferenceVelocity2();
Joost38H 0:eb16ed402ffa 392 double motorValue2 = FeedForwardControl2(referenceVelocity2);
Joost38H 0:eb16ed402ffa 393 SetMotor2(motorValue2);
Joost38H 0:eb16ed402ffa 394 }
Joost38H 0:eb16ed402ffa 395
Joost38H 0:eb16ed402ffa 396 void readdata1()
Joost38H 0:eb16ed402ffa 397 { //pc.printf("CurrentState = %i \r\n",Encoder.getCurrentState());
Joost38H 0:eb16ed402ffa 398 pc.printf("Pulses_M1 = %i \r\n",Encoder1.getPulses());
Joost38H 0:eb16ed402ffa 399 //pc.printf("Revolutions = %i \r\n",Encoder.getRevolutions());
Joost38H 0:eb16ed402ffa 400 pc.printf("Delta_M1 = %i \r\n",delta1);
Joost38H 0:eb16ed402ffa 401 }
Joost38H 0:eb16ed402ffa 402
Joost38H 0:eb16ed402ffa 403 void readdata2()
Joost38H 0:eb16ed402ffa 404 { //pc.printf("CurrentState = %i \r\n",Encoder.getCurrentState());
Joost38H 0:eb16ed402ffa 405 pc.printf("Pulses_M2 = %i \r\n",Encoder2.getPulses());
Joost38H 0:eb16ed402ffa 406 //pc.printf("Revolutions = %i \r\n",Encoder.getRevolutions());
Joost38H 0:eb16ed402ffa 407 pc.printf("Delta_M2 = %i \r\n",delta2);
Joost38H 0:eb16ed402ffa 408 }
Joost38H 0:eb16ed402ffa 409
Joost38H 0:eb16ed402ffa 410 // einde deel motor------------------------------------------------------------------------------------
Joost38H 0:eb16ed402ffa 411
Joost38H 0:eb16ed402ffa 412 Ticker loop;
Joost38H 0:eb16ed402ffa 413
Joost38H 0:eb16ed402ffa 414 /*Calculates ropelengths that are needed to get to new positions, based on the
Joost38H 0:eb16ed402ffa 415 set coordinates and the position of the poles */
Joost38H 0:eb16ed402ffa 416 double touwlengtes(){
Joost38H 0:eb16ed402ffa 417 Lou=sqrt(pow((Pstx-Pox),2)+pow((Psty-Poy),2));
Joost38H 0:eb16ed402ffa 418 Lbu=sqrt(pow((Pstx-Pbx),2)+pow((Psty-Pby),2));
Joost38H 0:eb16ed402ffa 419 Lru=sqrt(pow((Pstx-Prx),2)+pow((Psty-Pry),2));
Joost38H 0:eb16ed402ffa 420 return 0;
Joost38H 0:eb16ed402ffa 421 }
Joost38H 0:eb16ed402ffa 422
Joost38H 0:eb16ed402ffa 423 /* Calculates rotations (and associated counts) of the motor to get to the
Joost38H 0:eb16ed402ffa 424 desired new position*/
Joost38H 0:eb16ed402ffa 425 double turns(){
Joost38H 0:eb16ed402ffa 426
Joost38H 0:eb16ed402ffa 427 roto=Lou/omtrekklosje;
Joost38H 0:eb16ed402ffa 428 rotb=Lbu/omtrekklosje;
Joost38H 0:eb16ed402ffa 429 rotr=Lru/omtrekklosje;
Joost38H 0:eb16ed402ffa 430 counto=roto*4200;
Joost38H 0:eb16ed402ffa 431 //counto = (int)(counto + 0.5); // omzetten van rotaties naar counts
Joost38H 0:eb16ed402ffa 432 countb=rotb*4200;
Joost38H 0:eb16ed402ffa 433 //countb = (int)(countb + 0.5);
Joost38H 0:eb16ed402ffa 434 countr=rotr*4200;
Joost38H 0:eb16ed402ffa 435 //countr = (int)(countr + 0.5);
Joost38H 0:eb16ed402ffa 436 return 0;
Joost38H 0:eb16ed402ffa 437 }
Joost38H 0:eb16ed402ffa 438
Joost38H 0:eb16ed402ffa 439 // Waar komen Pstx en Psty vandaan en waar staan ze voor? En is dit maar voor een paal?
Joost38H 0:eb16ed402ffa 440 double Pst(){
Joost38H 0:eb16ed402ffa 441 Pstx=Pex+Vex*T;
Joost38H 0:eb16ed402ffa 442 Psty=Pey+Vey*T;
Joost38H 0:eb16ed402ffa 443 touwlengtes();
Joost38H 0:eb16ed402ffa 444 Pex=Pstx;
Joost38H 0:eb16ed402ffa 445 Pey=Psty;
Joost38H 0:eb16ed402ffa 446 //pc.printf("een stappie verder\n\r x=%.2f\n\r y=%.2f\n\r",Pstx,Psty);
Joost38H 0:eb16ed402ffa 447 //pc.printf("met lengtes:\n\r Lo=%.2f Lb=%.2f Lr=%.2f\n\r",Lou,Lbu,Lru);
Joost38H 0:eb16ed402ffa 448 turns();
Joost38H 0:eb16ed402ffa 449 //pc.printf("rotatie per motor:\n\r o=%.2f b=%.2f r=%.2f\n\r",roto,rotb,rotr);
Joost38H 0:eb16ed402ffa 450 pc.printf("counts per motor:\n\r o=%.2f b=%.2f r=%.2f\n\r",counto,countb,countr);
Joost38H 0:eb16ed402ffa 451 /*float R;
Joost38H 0:eb16ed402ffa 452 R=Vex/Vey; // met dit stukje kan je zien dat de verhouding tussen Vex en Vey constant is en de end efector dus een rechte lijn maakt
Joost38H 0:eb16ed402ffa 453 pc.printf("\n\r R=%f",R);*/
Joost38H 0:eb16ed402ffa 454 return 0;
Joost38H 0:eb16ed402ffa 455 }
Joost38H 0:eb16ed402ffa 456
Joost38H 0:eb16ed402ffa 457 //Calculating desired end position based on the EMG input <- Waarom maar voor een paal?
Joost38H 0:eb16ed402ffa 458 double Ps(){
Joost38H 0:eb16ed402ffa 459 Psx=(Xin)*30+121;
Joost38H 0:eb16ed402ffa 460 Psy=(Yin)*30+308;
Joost38H 0:eb16ed402ffa 461 //pc.printf("x=%.2f \n\r y=%.2f \n\r",Psx,Psy);
Joost38H 0:eb16ed402ffa 462 return 0;
Joost38H 0:eb16ed402ffa 463 }
Joost38H 0:eb16ed402ffa 464
Joost38H 0:eb16ed402ffa 465 // Rekent dit de snelheid uit waarmee de motoren bewegen?
Joost38H 0:eb16ed402ffa 466 void Ve(){
Joost38H 0:eb16ed402ffa 467 Vex=Kz*(Psx-Pex);
Joost38H 0:eb16ed402ffa 468 Vey=Kz*(Psy-Pey);
Joost38H 0:eb16ed402ffa 469 modVe=sqrt(pow(Vex,2)+pow(Vey,2));
Joost38H 0:eb16ed402ffa 470 if(modVe>Vmax){
Joost38H 0:eb16ed402ffa 471 Vex=(Vex/modVe)*Vmax;
Joost38H 0:eb16ed402ffa 472 Vey=(Vey/modVe)*Vmax;
Joost38H 0:eb16ed402ffa 473 }
Joost38H 0:eb16ed402ffa 474 Pst();
Joost38H 0:eb16ed402ffa 475 pc.printf("Vex=%.2f \r\n Vey=%.2f \r\n",Vex,Vey);
Joost38H 0:eb16ed402ffa 476 if((abs(Vex)<0.01f)&&(abs(Vey)<0.01f)){
Joost38H 0:eb16ed402ffa 477 B=true;
Joost38H 0:eb16ed402ffa 478 loop.detach();
Joost38H 0:eb16ed402ffa 479 }
Joost38H 0:eb16ed402ffa 480 }
Joost38H 0:eb16ed402ffa 481
Joost38H 0:eb16ed402ffa 482 // Calculating the desired position, so that the motors can go here
Joost38H 0:eb16ed402ffa 483 int calculator(){
Joost38H 0:eb16ed402ffa 484 Ps();
Joost38H 0:eb16ed402ffa 485 loop.attach(&Ve,0.02);
Joost38H 0:eb16ed402ffa 486 return 0;
Joost38H 0:eb16ed402ffa 487 }
Joost38H 0:eb16ed402ffa 488
Joost38H 0:eb16ed402ffa 489 // Function which makes it possible to lower the end-effector to pick up a piece
Joost38H 0:eb16ed402ffa 490 void zakker(){
Joost38H 0:eb16ed402ffa 491 while(1){
Joost38H 0:eb16ed402ffa 492 wait(1);
Joost38H 0:eb16ed402ffa 493 if(B==true){ //misschien moet je hier als voorwaarden een delta is 1 zetten // hierdoor wacht dit programma totdat de beweging klaar is
Joost38H 0:eb16ed402ffa 494 dLod=sqrt(pow(Lou,2)+pow(397.85,2))-Lou; //dit is wat je motoren moeten doen om te zakken
Joost38H 0:eb16ed402ffa 495 dLbd=sqrt(pow(Lbu,2)+pow(397.85,2))-Lbu;
Joost38H 0:eb16ed402ffa 496 dLrd=sqrt(pow(Lru,2)+pow(397.85,2))-Lru;
Joost38H 0:eb16ed402ffa 497 rotzo=dLod/omtrekklosje;
Joost38H 0:eb16ed402ffa 498 rotzb=dLbd/omtrekklosje;
Joost38H 0:eb16ed402ffa 499 rotzr=dLrd/omtrekklosje;
Joost38H 0:eb16ed402ffa 500 countzo=rotzo*4200;
Joost38H 0:eb16ed402ffa 501 countzb=rotzb*4200;
Joost38H 0:eb16ed402ffa 502 countzr=rotzr*4200;
Joost38H 0:eb16ed402ffa 503
Joost38H 0:eb16ed402ffa 504 pc.printf("o=%.2fb=%.2fr=%.2f",countzo,countzb,countzr); // hier moet komen te staan hoe het zakken gaat
Joost38H 0:eb16ed402ffa 505 }
Joost38H 0:eb16ed402ffa 506 }
Joost38H 0:eb16ed402ffa 507 }
Joost38H 0:eb16ed402ffa 508
Joost38H 0:eb16ed402ffa 509 int main()
Joost38H 0:eb16ed402ffa 510 {
Joost38H 0:eb16ed402ffa 511 pc.baud(115200);
Joost38H 1:6aac013b0ba3 512 getbqChain();
Joost38H 2:2c4ee76dc830 513 while(true){
Joost38H 2:2c4ee76dc830 514 sample_timer.attach(&EMG_sample, 0.002);
Joost38H 2:2c4ee76dc830 515 wait(2.5f);
Joost38H 2:2c4ee76dc830 516 tellerX();
Joost38H 2:2c4ee76dc830 517 tellerY();
Joost38H 2:2c4ee76dc830 518 calculator();
Joost38H 2:2c4ee76dc830 519 controlmotor1.attach(&MeasureAndControl1, 0.01);
Joost38H 2:2c4ee76dc830 520 calculatedelta1.attach(&calcdelta1, 0.01);
Joost38H 2:2c4ee76dc830 521 printdata1.attach(&readdata1, 0.5);
Joost38H 2:2c4ee76dc830 522 controlmotor2.attach(&MeasureAndControl2, 0.01);
Joost38H 2:2c4ee76dc830 523 calculatedelta2.attach(&calcdelta2, 0.01);
Joost38H 2:2c4ee76dc830 524 printdata2.attach(&readdata2, 0.5);
Joost38H 2:2c4ee76dc830 525 //zakker();
Joost38H 2:2c4ee76dc830 526 wait(5.0f);
Joost38H 2:2c4ee76dc830 527 }
Joost38H 0:eb16ed402ffa 528 return 0;
Joost38H 0:eb16ed402ffa 529 }