EMG and motor script together, Not fully working yet,

Dependencies:   Encoder QEI biquadFilter mbed

Committer:
Joost38H
Date:
Thu Oct 26 09:55:24 2017 +0000
Revision:
3:59b504840b95
Parent:
2:2c4ee76dc830
Child:
4:fddab1c875a9
tellen pieken werkt op goede manier

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