EMG and motor script together, Not fully working yet,.

Dependencies:   Encoder MODSERIAL QEI biquadFilter mbed

Fork of Code_MotorEMG by Joost Herijgers

Committer:
Joost38H
Date:
Wed Nov 01 12:59:02 2017 +0000
Revision:
18:00379f0ecc7f
Parent:
17:0acc8d4b142c
Child:
19:6567ed67d6ee
WIEEEEHOEEEEE, motor 1 werkt als een tiet;

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 10:952377fbbbfe 3 //#include "encoder.h"
Joost38H 0:eb16ed402ffa 4 #include "QEI.h"
Joost38H 0:eb16ed402ffa 5 #include "BiQuad.h"
Joost38H 18:00379f0ecc7f 6 #include "MODSERIAL.h"
Joost38H 5:81d3b53087c0 7
Joost38H 18:00379f0ecc7f 8 MODSERIAL pc(USBTX, USBRX);
Joost38H 5:81d3b53087c0 9
Joost38H 0:eb16ed402ffa 10 //Defining all in- and outputs
Joost38H 0:eb16ed402ffa 11 //EMG input
Joost38H 0:eb16ed402ffa 12 AnalogIn emgBR( A0 ); //Right Biceps
Joost38H 0:eb16ed402ffa 13 AnalogIn emgBL( A1 ); //Left Biceps
Joost38H 5:81d3b53087c0 14
Joost38H 0:eb16ed402ffa 15 //Output motor 1 and reading Encoder motor 1
Joost38H 0:eb16ed402ffa 16 DigitalOut motor1DirectionPin(D4);
Joost38H 0:eb16ed402ffa 17 PwmOut motor1MagnitudePin(D5);
Joost38H 0:eb16ed402ffa 18 QEI Encoder1(D12,D13,NC,32);
Joost38H 5:81d3b53087c0 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 5:81d3b53087c0 25 //Output motor 3 and reading Encoder motor 3
Joost38H 5:81d3b53087c0 26 DigitalOut motor3DirectionPin(D8);
Joost38H 5:81d3b53087c0 27 PwmOut motor3MagnitudePin(D9);
Joost38H 5:81d3b53087c0 28 QEI Encoder3(D2,D3,NC,32);
Joost38H 5:81d3b53087c0 29
Joost38H 0:eb16ed402ffa 30 //LED output, needed for feedback
Joost38H 0:eb16ed402ffa 31 DigitalOut led_R(LED_RED);
Joost38H 0:eb16ed402ffa 32 DigitalOut led_G(LED_GREEN);
Joost38H 0:eb16ed402ffa 33 DigitalOut led_B(LED_BLUE);
Joost38H 5:81d3b53087c0 34
Joost38H 1:6aac013b0ba3 35 //Setting Tickers for sampling EMG and determing if the threshold is met
Joost38H 5:81d3b53087c0 36 Ticker sample_timer;
Joost38H 5:81d3b53087c0 37 Ticker threshold_timerR;
Joost38H 5:81d3b53087c0 38 Ticker threshold_timerL;
Joost38H 5:81d3b53087c0 39
Joost38H 5:81d3b53087c0 40 Timer t_thresholdR;
Joost38H 5:81d3b53087c0 41 Timer t_thresholdL;
Joost38H 5:81d3b53087c0 42
Joost38H 10:952377fbbbfe 43 float currentTimeTR;
Joost38H 10:952377fbbbfe 44 float currentTimeTL;
Joost38H 5:81d3b53087c0 45
Joost38H 0:eb16ed402ffa 46 InterruptIn button(SW2); // Wordt uiteindelijk vervangen door EMG
Joost38H 5:81d3b53087c0 47
Joost38H 0:eb16ed402ffa 48 Timer t;
Joost38H 10:952377fbbbfe 49 float speedfactor; // = 0.01; snelheid in, zonder potmeter gebruik <- waarom is dit zo?
Joost38H 5:81d3b53087c0 50
Joost38H 0:eb16ed402ffa 51 // Defining variables delta (the difference between position and desired position) <- Is dit zo?
Joost38H 0:eb16ed402ffa 52 int delta1;
Joost38H 0:eb16ed402ffa 53 int delta2;
Joost38H 5:81d3b53087c0 54 int delta3;
Joost38H 0:eb16ed402ffa 55
Joost38H 5:81d3b53087c0 56 // Boolean needed to know if new input coordinates have to be given
Joost38H 5:81d3b53087c0 57 bool Move_done = false;
Joost38H 14:291f9a29bd74 58 bool Input_done = true;
Joost38H 5:81d3b53087c0 59
Joost38H 1:6aac013b0ba3 60 /* Defining all the different BiQuad filters, which contain a Notch filter,
Joost38H 1:6aac013b0ba3 61 High-pass filter and Low-pass filter. The Notch filter cancels all frequencies
Joost38H 1:6aac013b0ba3 62 between 49 and 51 Hz, the High-pass filter cancels all frequencies below 20 Hz
Joost38H 5:81d3b53087c0 63 and the Low-pass filter cancels out all frequencies below 4 Hz. The filters are
Joost38H 5:81d3b53087c0 64 declared four times, so that they can be used for sampling of right and left
Joost38H 5:81d3b53087c0 65 biceps, during measurements and calibration. */
Joost38H 5:81d3b53087c0 66
Joost38H 1:6aac013b0ba3 67 /* Defining all the normalized values of b and a in the Notch filter for the
Joost38H 1:6aac013b0ba3 68 creation of the Notch BiQuad */
Joost38H 5:81d3b53087c0 69
Joost38H 5:81d3b53087c0 70 BiQuad bqNotch1( 0.9876, -1.5981, 0.9876, -1.5981, 0.9752 );
Joost38H 5:81d3b53087c0 71 BiQuad bqNotch2( 0.9876, -1.5981, 0.9876, -1.5981, 0.9752 );
Joost38H 5:81d3b53087c0 72
Joost38H 5:81d3b53087c0 73 BiQuad bqNotchTR( 0.9876, -1.5981, 0.9876, -1.5981, 0.9752 );
Joost38H 5:81d3b53087c0 74 BiQuad bqNotchTL( 0.9876, -1.5981, 0.9876, -1.5981, 0.9752 );
Joost38H 5:81d3b53087c0 75
Joost38H 1:6aac013b0ba3 76 /* Defining all the normalized values of b and a in the High-pass filter for the
Joost38H 1:6aac013b0ba3 77 creation of the High-pass BiQuad */
Joost38H 5:81d3b53087c0 78
Joost38H 5:81d3b53087c0 79 BiQuad bqHigh1( 0.8371, -1.6742, 0.8371, -1.6475, 0.7009 );
Joost38H 5:81d3b53087c0 80 BiQuad bqHigh2( 0.8371, -1.6742, 0.8371, -1.6475, 0.7009 );
Joost38H 5:81d3b53087c0 81
Joost38H 5:81d3b53087c0 82 BiQuad bqHighTR( 0.8371, -1.6742, 0.8371, -1.6475, 0.7009 );
Joost38H 5:81d3b53087c0 83 BiQuad bqHighTL( 0.8371, -1.6742, 0.8371, -1.6475, 0.7009 );
Joost38H 5:81d3b53087c0 84
Joost38H 1:6aac013b0ba3 85 /* Defining all the normalized values of b and a in the Low-pass filter for the
Joost38H 1:6aac013b0ba3 86 creation of the Low-pass BiQuad */
Joost38H 5:81d3b53087c0 87
Joost38H 5:81d3b53087c0 88 BiQuad bqLow1( 6.0985e-4, 0.0012, 6.0985e-4, -1.9289, 0.9314 );
Joost38H 5:81d3b53087c0 89 BiQuad bqLow2( 6.0985e-4, 0.0012, 6.0985e-4, -1.9289, 0.9314 );
Joost38H 5:81d3b53087c0 90
Joost38H 5:81d3b53087c0 91 BiQuad bqLowTR( 6.0985e-4, 0.0012, 6.0985e-4, -1.9289, 0.9314 );
Joost38H 5:81d3b53087c0 92 BiQuad bqLowTL( 6.0985e-4, 0.0012, 6.0985e-4, -1.9289, 0.9314 );
Joost38H 5:81d3b53087c0 93
Joost38H 1:6aac013b0ba3 94 // Creating a variable needed for the creation of the BiQuadChain
Joost38H 5:81d3b53087c0 95 BiQuadChain bqChain1;
Joost38H 5:81d3b53087c0 96 BiQuadChain bqChain2;
Joost38H 5:81d3b53087c0 97
Joost38H 5:81d3b53087c0 98 BiQuadChain bqChainTR;
Joost38H 5:81d3b53087c0 99 BiQuadChain bqChainTL;
Joost38H 5:81d3b53087c0 100
Joost38H 10:952377fbbbfe 101 //Declaring all floats needed in the filtering process
Joost38H 10:952377fbbbfe 102 float emgBRfiltered; //Right biceps Notch+High pass filter
Joost38H 10:952377fbbbfe 103 float emgBRrectified; //Right biceps rectified
Joost38H 10:952377fbbbfe 104 float emgBRcomplete; //Right biceps low-pass filter, filtering complete
Joost38H 5:81d3b53087c0 105
Joost38H 10:952377fbbbfe 106 float emgBLfiltered; //Left biceps Notch+High pass filter
Joost38H 10:952377fbbbfe 107 float emgBLrectified; //Left biceps rectified
Joost38H 10:952377fbbbfe 108 float emgBLcomplete; //Left biceps low-pass filter, filtering complete
Joost38H 1:6aac013b0ba3 109
Joost38H 5:81d3b53087c0 110 // Declaring all variables needed for getting the Threshold value
Joost38H 10:952377fbbbfe 111 float numsamples = 500;
Joost38H 10:952377fbbbfe 112 float emgBRsum = 0;
Joost38H 10:952377fbbbfe 113 float emgBRmeanMVC;
Joost38H 10:952377fbbbfe 114 float thresholdBR;
Joost38H 5:81d3b53087c0 115
Joost38H 10:952377fbbbfe 116 float emgBLsum = 0;
Joost38H 10:952377fbbbfe 117 float emgBLmeanMVC;
Joost38H 10:952377fbbbfe 118 float thresholdBL;
Joost38H 5:81d3b53087c0 119
Joost38H 5:81d3b53087c0 120 /* Function to sample the EMG of the Right Biceps and get a Threshold value
Joost38H 5:81d3b53087c0 121 from it, which can be used throughout the process */
Joost38H 5:81d3b53087c0 122
Joost38H 4:fddab1c875a9 123 void Threshold_samplingBR() {
Joost38H 4:fddab1c875a9 124 t_thresholdR.start();
Joost38H 4:fddab1c875a9 125 currentTimeTR = t_thresholdR.read();
Joost38H 4:fddab1c875a9 126
Joost38H 4:fddab1c875a9 127 if (currentTimeTR <= 1) {
Joost38H 4:fddab1c875a9 128
Joost38H 4:fddab1c875a9 129 emgBRfiltered = bqChainTR.step( emgBR.read() ); //Notch+High-pass
Joost38H 4:fddab1c875a9 130 emgBRrectified = fabs(emgBRfiltered); //Rectification
Joost38H 4:fddab1c875a9 131 emgBRcomplete = bqLowTR.step(emgBRrectified); //Low-pass
Joost38H 4:fddab1c875a9 132
Joost38H 4:fddab1c875a9 133 emgBRsum = emgBRsum + emgBRcomplete;
Joost38H 4:fddab1c875a9 134 }
Joost38H 4:fddab1c875a9 135 emgBRmeanMVC = emgBRsum/numsamples;
Joost38H 4:fddab1c875a9 136 thresholdBR = emgBRmeanMVC * 0.20;
Joost38H 5:81d3b53087c0 137
Joost38H 4:fddab1c875a9 138 //pc.printf("ThresholdBR = %f \n", thresholdBR);
Joost38H 4:fddab1c875a9 139 }
Joost38H 5:81d3b53087c0 140 /* Function to sample the EMG of the Left Biceps and get a Threshold value
Joost38H 5:81d3b53087c0 141 from it, which can be used throughout the process */
Joost38H 5:81d3b53087c0 142
Joost38H 4:fddab1c875a9 143 void Threshold_samplingBL() {
Joost38H 4:fddab1c875a9 144 t_thresholdL.start();
Joost38H 4:fddab1c875a9 145 currentTimeTL = t_thresholdL.read();
Joost38H 4:fddab1c875a9 146
Joost38H 4:fddab1c875a9 147 if (currentTimeTL <= 1) {
Joost38H 4:fddab1c875a9 148
Joost38H 4:fddab1c875a9 149 emgBLfiltered = bqChain2.step( emgBL.read() ); //Notch+High-pass
Joost38H 4:fddab1c875a9 150 emgBLrectified = fabs( emgBLfiltered ); //Rectification
Joost38H 4:fddab1c875a9 151 emgBLcomplete = bqLow2.step( emgBLrectified ); //Low-pass
Joost38H 4:fddab1c875a9 152
Joost38H 4:fddab1c875a9 153 emgBLsum = emgBLsum + emgBLcomplete;
Joost38H 4:fddab1c875a9 154 }
Joost38H 4:fddab1c875a9 155
Joost38H 4:fddab1c875a9 156 emgBLmeanMVC = emgBLsum/numsamples;
Joost38H 4:fddab1c875a9 157 thresholdBL = emgBLmeanMVC * 0.20;
Joost38H 5:81d3b53087c0 158
Joost38H 4:fddab1c875a9 159 }
Joost38H 5:81d3b53087c0 160
Joost38H 5:81d3b53087c0 161 // EMG sampling and filtering
Joost38H 4:fddab1c875a9 162
Joost38H 1:6aac013b0ba3 163 void EMG_sample()
Joost38H 1:6aac013b0ba3 164 {
Joost38H 1:6aac013b0ba3 165 //Filtering steps for the Right Biceps EMG
Joost38H 1:6aac013b0ba3 166 emgBRfiltered = bqChain1.step( emgBR.read() ); //Notch+High-pass
Joost38H 1:6aac013b0ba3 167 emgBRrectified = fabs(emgBRfiltered); //Rectification
Joost38H 1:6aac013b0ba3 168 emgBRcomplete = bqLow1.step(emgBRrectified); //Low-pass
Joost38H 5:81d3b53087c0 169
Joost38H 1:6aac013b0ba3 170 //Filtering steps for the Left Biceps EMG
Joost38H 1:6aac013b0ba3 171 emgBLfiltered = bqChain2.step( emgBL.read() ); //Notch+High-pass
Joost38H 1:6aac013b0ba3 172 emgBLrectified = fabs( emgBLfiltered ); //Rectification
Joost38H 1:6aac013b0ba3 173 emgBLcomplete = bqLow2.step( emgBLrectified ); //Low-pass
Joost38H 5:81d3b53087c0 174
Joost38H 1:6aac013b0ba3 175 }
Joost38H 5:81d3b53087c0 176 // Function to make the BiQuadChain for the Notch and High pass filter for all three filters
Joost38H 1:6aac013b0ba3 177 void getbqChain()
Joost38H 1:6aac013b0ba3 178 {
Joost38H 5:81d3b53087c0 179 bqChain1.add(&bqNotch1).add(&bqHigh1); //Making the BiQuadChain
Joost38H 1:6aac013b0ba3 180 bqChain2.add(&bqNotch2).add(&bqHigh2);
Joost38H 5:81d3b53087c0 181
Joost38H 5:81d3b53087c0 182 bqChainTR.add(&bqNotchTR).add(&bqHighTR);
Joost38H 5:81d3b53087c0 183 bqChainTL.add(&bqNotchTR).add(&bqHighTL);
Joost38H 1:6aac013b0ba3 184 }
Joost38H 5:81d3b53087c0 185
Joost38H 5:81d3b53087c0 186 // Initial input value for couting the X-values
Joost38H 3:59b504840b95 187 int Xin=0;
Joost38H 5:81d3b53087c0 188 int Xin_new;
Joost38H 10:952377fbbbfe 189 float huidigetijdX;
Joost38H 5:81d3b53087c0 190
Joost38H 5:81d3b53087c0 191 // Feedback system for counting values of X
Joost38H 5:81d3b53087c0 192 void ledtX(){
Joost38H 5:81d3b53087c0 193 t.reset();
Joost38H 5:81d3b53087c0 194 Xin++;
Joost38H 5:81d3b53087c0 195 pc.printf("Xin is %i\n",Xin);
Joost38H 5:81d3b53087c0 196 led_G=0;
Joost38H 5:81d3b53087c0 197 led_R=1;
Joost38H 5:81d3b53087c0 198 wait(0.2);
Joost38H 5:81d3b53087c0 199 led_G=1;
Joost38H 5:81d3b53087c0 200 led_R=0;
Joost38H 5:81d3b53087c0 201 wait(0.5);
Joost38H 5:81d3b53087c0 202 }
Joost38H 5:81d3b53087c0 203
Joost38H 5:81d3b53087c0 204 // Couting system for values of X
Joost38H 5:81d3b53087c0 205 int tellerX(){
Joost38H 5:81d3b53087c0 206 if (Move_done == true) {
Joost38H 5:81d3b53087c0 207 t.reset();
Joost38H 5:81d3b53087c0 208 led_G=1;
Joost38H 5:81d3b53087c0 209 led_B=1;
Joost38H 5:81d3b53087c0 210 led_R=0;
Joost38H 5:81d3b53087c0 211 while(true){
Joost38H 6:2674fe30b610 212 button.fall(ledtX);
Joost38H 6:2674fe30b610 213 /*if (emgBRcomplete > thresholdBR) {
Joost38H 5:81d3b53087c0 214 ledtX();
Joost38H 6:2674fe30b610 215 } */
Joost38H 5:81d3b53087c0 216 t.start();
Joost38H 5:81d3b53087c0 217 huidigetijdX=t.read();
Joost38H 5:81d3b53087c0 218 if (huidigetijdX>2){
Joost38H 5:81d3b53087c0 219 led_R=1; //Go to the next program (counting values for Y)
Joost38H 5:81d3b53087c0 220 Xin_new = Xin;
Joost38H 5:81d3b53087c0 221 Xin = 0;
Joost38H 5:81d3b53087c0 222
Joost38H 5:81d3b53087c0 223 return Xin_new;
Joost38H 5:81d3b53087c0 224 }
Joost38H 5:81d3b53087c0 225
Joost38H 5:81d3b53087c0 226 }
Joost38H 5:81d3b53087c0 227
Joost38H 5:81d3b53087c0 228 }
Joost38H 5:81d3b53087c0 229 return 0;
Joost38H 5:81d3b53087c0 230 }
Joost38H 5:81d3b53087c0 231
Joost38H 5:81d3b53087c0 232 // Initial values needed for Y (see comments at X function)
Joost38H 5:81d3b53087c0 233 int Yin=0;
Joost38H 5:81d3b53087c0 234 int Yin_new;
Joost38H 10:952377fbbbfe 235 float huidigetijdY;
Joost38H 5:81d3b53087c0 236
Joost38H 5:81d3b53087c0 237 //Feedback system for couting values of Y
Joost38H 5:81d3b53087c0 238 void ledtY(){
Joost38H 5:81d3b53087c0 239 t.reset();
Joost38H 5:81d3b53087c0 240 Yin++;
Joost38H 5:81d3b53087c0 241 pc.printf("Yin is %i\n",Yin);
Joost38H 5:81d3b53087c0 242 led_G=0;
Joost38H 5:81d3b53087c0 243 led_B=1;
Joost38H 5:81d3b53087c0 244 wait(0.2);
Joost38H 5:81d3b53087c0 245 led_G=1;
Joost38H 5:81d3b53087c0 246 led_B=0;
Joost38H 5:81d3b53087c0 247 wait(0.5);
Joost38H 5:81d3b53087c0 248 }
Joost38H 5:81d3b53087c0 249
Joost38H 5:81d3b53087c0 250 // Couting system for values of Y
Joost38H 5:81d3b53087c0 251 int tellerY(){
Joost38H 5:81d3b53087c0 252 if (Move_done == true) {
Joost38H 5:81d3b53087c0 253 t.reset();
Joost38H 5:81d3b53087c0 254 led_G=1;
Joost38H 5:81d3b53087c0 255 led_B=0;
Joost38H 5:81d3b53087c0 256 led_R=1;
Joost38H 5:81d3b53087c0 257 while(true){
Joost38H 6:2674fe30b610 258 button.fall(ledtY);
Joost38H 6:2674fe30b610 259 /*if (emgBRcomplete > thresholdBR) {
Joost38H 5:81d3b53087c0 260 ledtY();
Joost38H 6:2674fe30b610 261 }*/
Joost38H 5:81d3b53087c0 262 t.start();
Joost38H 5:81d3b53087c0 263 huidigetijdY=t.read();
Joost38H 5:81d3b53087c0 264 if (huidigetijdY>2){
Joost38H 5:81d3b53087c0 265 led_B=1;
Joost38H 5:81d3b53087c0 266 Yin_new = Yin;
Joost38H 5:81d3b53087c0 267 Yin = 0;
Joost38H 14:291f9a29bd74 268 Input_done = true;
Joost38H 5:81d3b53087c0 269 Move_done = false;
Joost38H 5:81d3b53087c0 270 return Yin_new;
Joost38H 5:81d3b53087c0 271
Joost38H 5:81d3b53087c0 272 }
Joost38H 5:81d3b53087c0 273 }
Joost38H 5:81d3b53087c0 274 }
Joost38H 5:81d3b53087c0 275 return 0; // ga door naar het volgende programma
Joost38H 5:81d3b53087c0 276 }
Joost38H 5:81d3b53087c0 277
Joost38H 0:eb16ed402ffa 278 // Declaring all variables needed for calculating rope lengths,
Joost38H 10:952377fbbbfe 279 float Pox = 0;
Joost38H 10:952377fbbbfe 280 float Poy = 0;
Joost38H 10:952377fbbbfe 281 float Pbx = 0;
Joost38H 10:952377fbbbfe 282 float Pby = 887;
Joost38H 10:952377fbbbfe 283 float Prx = 768;
Joost38H 10:952377fbbbfe 284 float Pry = 443;
Joost38H 10:952377fbbbfe 285 float Pex=91;
Joost38H 10:952377fbbbfe 286 float Pey=278;
Joost38H 10:952377fbbbfe 287 float diamtrklosje=20;
Joost38H 10:952377fbbbfe 288 float pi=3.14159265359;
Joost38H 10:952377fbbbfe 289 float omtrekklosje=diamtrklosje*pi;
Joost38H 10:952377fbbbfe 290 float Lou;
Joost38H 10:952377fbbbfe 291 float Lbu;
Joost38H 10:952377fbbbfe 292 float Lru;
Joost38H 10:952377fbbbfe 293 float dLod;
Joost38H 10:952377fbbbfe 294 float dLbd;
Joost38H 10:952377fbbbfe 295 float dLrd;
Joost38H 5:81d3b53087c0 296
Joost38H 0:eb16ed402ffa 297 // Declaring variables needed for calculating motor counts
Joost38H 10:952377fbbbfe 298 float roto;
Joost38H 10:952377fbbbfe 299 float rotb;
Joost38H 10:952377fbbbfe 300 float rotr;
Joost38H 10:952377fbbbfe 301 float rotzo;
Joost38H 10:952377fbbbfe 302 float rotzb;
Joost38H 10:952377fbbbfe 303 float rotzr;
Joost38H 10:952377fbbbfe 304 float counto;
Joost38H 10:952377fbbbfe 305 float countb;
Joost38H 10:952377fbbbfe 306 float countr;
Joost38H 10:952377fbbbfe 307 float countzo;
Joost38H 10:952377fbbbfe 308 float countzb;
Joost38H 10:952377fbbbfe 309 float countzr;
Joost38H 12:ea9afe159eb1 310
Joost38H 10:952377fbbbfe 311 float hcounto;
Joost38H 10:952377fbbbfe 312 float dcounto;
Joost38H 10:952377fbbbfe 313 float hcountb;
Joost38H 10:952377fbbbfe 314 float dcountb;
Joost38H 10:952377fbbbfe 315 float hcountr;
Joost38H 10:952377fbbbfe 316 float dcountr;
Joost38H 5:81d3b53087c0 317
Joost38H 0:eb16ed402ffa 318 // Declaring variables neeeded for calculating motor movements to get to a certain point <- klopt dit?
Joost38H 10:952377fbbbfe 319 float Psx;
Joost38H 10:952377fbbbfe 320 float Psy;
Joost38H 10:952377fbbbfe 321 float Vex;
Joost38H 10:952377fbbbfe 322 float Vey;
Joost38H 10:952377fbbbfe 323 float Kz=0.7; // nadersnelheid instellen
Joost38H 10:952377fbbbfe 324 float modVe;
Joost38H 10:952377fbbbfe 325 float Vmax=20;
Joost38H 10:952377fbbbfe 326 float Pstx;
Joost38H 10:952377fbbbfe 327 float Psty;
Joost38H 10:952377fbbbfe 328 float T=0.02;//seconds
Joost38H 8:f5065cd04213 329
Joost38H 10:952377fbbbfe 330 float kpo = 0.1;
Joost38H 10:952377fbbbfe 331 float kpb = 0.1;
Joost38H 10:952377fbbbfe 332 float kpr = 0.1;
Joost38H 5:81d3b53087c0 333
Joost38H 10:952377fbbbfe 334 float speedfactor1;
Joost38H 10:952377fbbbfe 335 float speedfactor2;
Joost38H 10:952377fbbbfe 336 float speedfactor3;
Joost38H 5:81d3b53087c0 337
Joost38H 5:81d3b53087c0 338
Joost38H 10:952377fbbbfe 339 //Deel om motor(en) aan te sturen--------------------------------------------
Joost38H 10:952377fbbbfe 340 float referenceVelocity1;
Joost38H 10:952377fbbbfe 341 float motorValue1;
Joost38H 5:81d3b53087c0 342
Joost38H 10:952377fbbbfe 343 float referenceVelocity2;
Joost38H 10:952377fbbbfe 344 float motorValue2;
Joost38H 0:eb16ed402ffa 345
Joost38H 10:952377fbbbfe 346 float referenceVelocity3;
Joost38H 10:952377fbbbfe 347 float motorValue3;
Joost38H 5:81d3b53087c0 348
Joost38H 5:81d3b53087c0 349 Ticker controlmotor1; // één ticker van maken?
Joost38H 5:81d3b53087c0 350 Ticker controlmotor2; // één ticker van maken?
Joost38H 5:81d3b53087c0 351 Ticker controlmotor3; // één ticker van maken?
Joost38H 8:f5065cd04213 352
Joost38H 8:f5065cd04213 353
Joost38H 13:3ad20c09b5b4 354 float P1(int erroro, float kpo) {
Joost38H 8:f5065cd04213 355 return erroro*kpo;
Joost38H 8:f5065cd04213 356 }
Joost38H 8:f5065cd04213 357
Joost38H 8:f5065cd04213 358 void MotorController1()
Joost38H 8:f5065cd04213 359 {
Joost38H 12:ea9afe159eb1 360 int reference_o = (int) (counto-hcounto);
Joost38H 10:952377fbbbfe 361 int position_o = Encoder1.getPulses();
Joost38H 10:952377fbbbfe 362
Joost38H 12:ea9afe159eb1 363 int error_o = reference_o - position_o;
Joost38H 10:952377fbbbfe 364
Joost38H 18:00379f0ecc7f 365 pc.printf("Position_o = %i reference_o=%i Error_o=%i\n\r" ,position_o,reference_o,error_o);
Joost38H 9:3874b23bb233 366
Joost38H 18:00379f0ecc7f 367 if (-10<error_o && error_o<10){
Joost38H 10:952377fbbbfe 368 motorValue1 = 0;
Joost38H 10:952377fbbbfe 369 }
Joost38H 10:952377fbbbfe 370 else {
Joost38H 16:d75bf6d7d60e 371 motorValue1 = 0.05*P1(error_o, kpo);
Joost38H 10:952377fbbbfe 372 }
Joost38H 10:952377fbbbfe 373
Joost38H 9:3874b23bb233 374 if (motorValue1 >=0) motor1DirectionPin=0;
Joost38H 9:3874b23bb233 375 else motor1DirectionPin=1;
Joost38H 8:f5065cd04213 376 if (fabs(motorValue1)>1) motor1MagnitudePin = 1;
Joost38H 8:f5065cd04213 377 else motor1MagnitudePin = fabs(motorValue1);
Joost38H 8:f5065cd04213 378 }
Joost38H 8:f5065cd04213 379
Joost38H 10:952377fbbbfe 380
Joost38H 8:f5065cd04213 381
Joost38H 13:3ad20c09b5b4 382 float P2(int error_b, float kpb) {
Joost38H 13:3ad20c09b5b4 383 return error_b*kpb;
Joost38H 8:f5065cd04213 384 }
Joost38H 0:eb16ed402ffa 385
Joost38H 8:f5065cd04213 386 void MotorController2()
Joost38H 8:f5065cd04213 387 {
Joost38H 9:3874b23bb233 388
Joost38H 11:f23fe69ba3ef 389 int reference_b = (int) (countb-hcountb);
Joost38H 10:952377fbbbfe 390 int position_b = Encoder2.getPulses();
Joost38H 9:3874b23bb233 391
Joost38H 13:3ad20c09b5b4 392 int error_b = reference_b - position_b;
Joost38H 10:952377fbbbfe 393
Joost38H 14:291f9a29bd74 394 //pc.printf("Position_b = %i reference_b=%i Error_b=%i " ,position_b,reference_b,error_b);
Joost38H 14:291f9a29bd74 395
Joost38H 18:00379f0ecc7f 396 if (-10<error_b && error_b<10){
Joost38H 10:952377fbbbfe 397 motorValue2 = 0;
Joost38H 10:952377fbbbfe 398 }
Joost38H 10:952377fbbbfe 399 else {
Joost38H 17:0acc8d4b142c 400 motorValue2 = 0.05*P2(error_b, kpb);
Joost38H 10:952377fbbbfe 401 }
Joost38H 10:952377fbbbfe 402
Joost38H 9:3874b23bb233 403
Joost38H 8:f5065cd04213 404 if (motorValue2 >=0) motor2DirectionPin=1;
Joost38H 8:f5065cd04213 405 else motor2DirectionPin=0;
Joost38H 8:f5065cd04213 406 if (fabs(motorValue2)>1) motor2MagnitudePin = 1;
Joost38H 8:f5065cd04213 407 else motor2MagnitudePin = fabs(motorValue2);
Joost38H 8:f5065cd04213 408 }
Joost38H 8:f5065cd04213 409
Joost38H 10:952377fbbbfe 410
Joost38H 8:f5065cd04213 411
Joost38H 13:3ad20c09b5b4 412 float P3(int error_r, float kpr) {
Joost38H 13:3ad20c09b5b4 413 return error_r*kpr;
Joost38H 8:f5065cd04213 414 }
Joost38H 8:f5065cd04213 415
Joost38H 9:3874b23bb233 416 void MotorController3()
Joost38H 9:3874b23bb233 417 {
Joost38H 11:f23fe69ba3ef 418 int reference_r = (int) (countr-hcountr);
Joost38H 10:952377fbbbfe 419 int position_r = Encoder3.getPulses();
Joost38H 10:952377fbbbfe 420
Joost38H 13:3ad20c09b5b4 421 int error_r = reference_r - position_r;
Joost38H 10:952377fbbbfe 422
Joost38H 14:291f9a29bd74 423 //pc.printf("Position_r = %i reference_r=%i Error_r=%i\n\r" ,position_r,reference_r,error_r);
Joost38H 10:952377fbbbfe 424
Joost38H 9:3874b23bb233 425
Joost38H 18:00379f0ecc7f 426 if (-10<error_r && error_r<10){
Joost38H 10:952377fbbbfe 427 motorValue3 = 0;
Joost38H 10:952377fbbbfe 428
Joost38H 10:952377fbbbfe 429 }
Joost38H 10:952377fbbbfe 430 else {
Joost38H 17:0acc8d4b142c 431 motorValue3 = 0.05*P3(error_r, kpr);
Joost38H 10:952377fbbbfe 432 }
Joost38H 10:952377fbbbfe 433
Joost38H 8:f5065cd04213 434 if (motorValue3 >=0) motor3DirectionPin=1;
Joost38H 8:f5065cd04213 435 else motor3DirectionPin=0;
Joost38H 8:f5065cd04213 436 if (fabs(motorValue3)>1) motor3MagnitudePin = 1;
Joost38H 8:f5065cd04213 437 else motor3MagnitudePin = fabs(motorValue3);
Joost38H 8:f5065cd04213 438 }
Joost38H 8:f5065cd04213 439
Joost38H 5:81d3b53087c0 440
Joost38H 0:eb16ed402ffa 441
Joost38H 0:eb16ed402ffa 442 // einde deel motor------------------------------------------------------------------------------------
Joost38H 5:81d3b53087c0 443
Joost38H 0:eb16ed402ffa 444 Ticker loop;
Joost38H 5:81d3b53087c0 445
Joost38H 0:eb16ed402ffa 446 /*Calculates ropelengths that are needed to get to new positions, based on the
Joost38H 0:eb16ed402ffa 447 set coordinates and the position of the poles */
Joost38H 10:952377fbbbfe 448 float touwlengtes(){
Joost38H 0:eb16ed402ffa 449 Lou=sqrt(pow((Pstx-Pox),2)+pow((Psty-Poy),2));
Joost38H 0:eb16ed402ffa 450 Lbu=sqrt(pow((Pstx-Pbx),2)+pow((Psty-Pby),2));
Joost38H 0:eb16ed402ffa 451 Lru=sqrt(pow((Pstx-Prx),2)+pow((Psty-Pry),2));
Joost38H 0:eb16ed402ffa 452 return 0;
Joost38H 0:eb16ed402ffa 453 }
Joost38H 5:81d3b53087c0 454
Joost38H 0:eb16ed402ffa 455 /* Calculates rotations (and associated counts) of the motor to get to the
Joost38H 0:eb16ed402ffa 456 desired new position*/
Joost38H 10:952377fbbbfe 457 float turns(){
Joost38H 0:eb16ed402ffa 458
Joost38H 0:eb16ed402ffa 459 roto=Lou/omtrekklosje;
Joost38H 0:eb16ed402ffa 460 rotb=Lbu/omtrekklosje;
Joost38H 0:eb16ed402ffa 461 rotr=Lru/omtrekklosje;
Joost38H 0:eb16ed402ffa 462 counto=roto*4200;
Joost38H 5:81d3b53087c0 463 dcounto=counto-hcounto;
Joost38H 11:f23fe69ba3ef 464 //pc.printf("counto = %f \n\r", counto);
Joost38H 8:f5065cd04213 465 //pc.printf("hcounto = %f \n\r", hcounto);
Joost38H 8:f5065cd04213 466 //pc.printf("dcounto = %f \n\r",dcounto);
Joost38H 0:eb16ed402ffa 467 countb=rotb*4200;
Joost38H 5:81d3b53087c0 468 dcountb=countb-hcountb;
Joost38H 8:f5065cd04213 469 //pc.printf("dcountb = %f \n\r",dcountb);
Joost38H 0:eb16ed402ffa 470 countr=rotr*4200;
Joost38H 5:81d3b53087c0 471 dcountr=countr-hcountr;
Joost38H 5:81d3b53087c0 472
Joost38H 0:eb16ed402ffa 473 return 0;
Joost38H 0:eb16ed402ffa 474 }
Joost38H 5:81d3b53087c0 475
Joost38H 0:eb16ed402ffa 476 // Waar komen Pstx en Psty vandaan en waar staan ze voor? En is dit maar voor een paal?
Joost38H 10:952377fbbbfe 477 float Pst(){
Joost38H 0:eb16ed402ffa 478 Pstx=Pex+Vex*T;
Joost38H 0:eb16ed402ffa 479 Psty=Pey+Vey*T;
Joost38H 0:eb16ed402ffa 480 touwlengtes();
Joost38H 0:eb16ed402ffa 481 Pex=Pstx;
Joost38H 0:eb16ed402ffa 482 Pey=Psty;
Joost38H 11:f23fe69ba3ef 483 //pc.printf("een stappie verder\n\r x=%.2f\n\r y=%.2f\n\r",Pstx,Psty);
Joost38H 0:eb16ed402ffa 484 //pc.printf("met lengtes:\n\r Lo=%.2f Lb=%.2f Lr=%.2f\n\r",Lou,Lbu,Lru);
Joost38H 0:eb16ed402ffa 485 turns();
Joost38H 0:eb16ed402ffa 486 //pc.printf("rotatie per motor:\n\r o=%.2f b=%.2f r=%.2f\n\r",roto,rotb,rotr);
Joost38H 11:f23fe69ba3ef 487 //pc.printf("counts per motor:\n\r o=%.2f b=%.2f r=%.2f\n\r",counto,countb,countr);
Joost38H 0:eb16ed402ffa 488 /*float R;
Joost38H 0:eb16ed402ffa 489 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 490 pc.printf("\n\r R=%f",R);*/
Joost38H 0:eb16ed402ffa 491 return 0;
Joost38H 0:eb16ed402ffa 492 }
Joost38H 5:81d3b53087c0 493
Joost38H 0:eb16ed402ffa 494 //Calculating desired end position based on the EMG input <- Waarom maar voor een paal?
Joost38H 10:952377fbbbfe 495 float Ps(){
Joost38H 7:ca880e05bb04 496 Psx=(Xin_new)*30+91;
Joost38H 7:ca880e05bb04 497 Psy=(Yin_new)*30+278;
Joost38H 17:0acc8d4b142c 498 // pc.printf("x=%.2f \n\r y=%.2f \n\r",Psx,Psy);
Joost38H 0:eb16ed402ffa 499 return 0;
Joost38H 0:eb16ed402ffa 500 }
Joost38H 5:81d3b53087c0 501
Joost38H 0:eb16ed402ffa 502 // Rekent dit de snelheid uit waarmee de motoren bewegen?
Joost38H 0:eb16ed402ffa 503 void Ve(){
Joost38H 10:952377fbbbfe 504 Vex=0.2*(Psx-Pex);
Joost38H 10:952377fbbbfe 505 Vey=0.2*(Psy-Pey);
Joost38H 10:952377fbbbfe 506 /* modVe=sqrt(pow(Vex,2)+pow(Vey,2));
Joost38H 0:eb16ed402ffa 507 if(modVe>Vmax){
Joost38H 0:eb16ed402ffa 508 Vex=(Vex/modVe)*Vmax;
Joost38H 0:eb16ed402ffa 509 Vey=(Vey/modVe)*Vmax;
Joost38H 10:952377fbbbfe 510 }*/
Joost38H 0:eb16ed402ffa 511 Pst();
Joost38H 17:0acc8d4b142c 512 // pc.printf("Vex=%.2f \r\n Vey=%.2f \r\n",Vex,Vey);
Joost38H 6:2674fe30b610 513 if((fabs(Vex)<0.01f)&&(fabs(Vey)<0.01f)){
Joost38H 3:59b504840b95 514 Move_done=true;
Joost38H 0:eb16ed402ffa 515 loop.detach();
Joost38H 0:eb16ed402ffa 516 }
Joost38H 0:eb16ed402ffa 517 }
Joost38H 5:81d3b53087c0 518
Joost38H 0:eb16ed402ffa 519 // Calculating the desired position, so that the motors can go here
Joost38H 0:eb16ed402ffa 520 int calculator(){
Joost38H 0:eb16ed402ffa 521 Ps();
Joost38H 10:952377fbbbfe 522 if (Move_done == false) {
Joost38H 0:eb16ed402ffa 523 loop.attach(&Ve,0.02);
Joost38H 10:952377fbbbfe 524 }
Joost38H 0:eb16ed402ffa 525 return 0;
Joost38H 0:eb16ed402ffa 526 }
Joost38H 5:81d3b53087c0 527
Joost38H 0:eb16ed402ffa 528 // Function which makes it possible to lower the end-effector to pick up a piece
Joost38H 0:eb16ed402ffa 529 void zakker(){
Joost38H 0:eb16ed402ffa 530 while(1){
Joost38H 0:eb16ed402ffa 531 wait(1);
Joost38H 3:59b504840b95 532 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 533 dLod=sqrt(pow(Lou,2)+pow(397.85,2))-Lou; //dit is wat je motoren moeten doen om te zakken
Joost38H 0:eb16ed402ffa 534 dLbd=sqrt(pow(Lbu,2)+pow(397.85,2))-Lbu;
Joost38H 0:eb16ed402ffa 535 dLrd=sqrt(pow(Lru,2)+pow(397.85,2))-Lru;
Joost38H 0:eb16ed402ffa 536 rotzo=dLod/omtrekklosje;
Joost38H 0:eb16ed402ffa 537 rotzb=dLbd/omtrekklosje;
Joost38H 0:eb16ed402ffa 538 rotzr=dLrd/omtrekklosje;
Joost38H 0:eb16ed402ffa 539 countzo=rotzo*4200;
Joost38H 0:eb16ed402ffa 540 countzb=rotzb*4200;
Joost38H 0:eb16ed402ffa 541 countzr=rotzr*4200;
Joost38H 0:eb16ed402ffa 542
Joost38H 5:81d3b53087c0 543 //pc.printf("o=%.2fb=%.2fr=%.2f",countzo,countzb,countzr); // hier moet komen te staan hoe het zakken gaat
Joost38H 0:eb16ed402ffa 544 }
Joost38H 0:eb16ed402ffa 545 }
Joost38H 0:eb16ed402ffa 546 }
Joost38H 7:ca880e05bb04 547
Joost38H 7:ca880e05bb04 548 void tiller(){
Joost38H 7:ca880e05bb04 549 dcountb = -8148;
Joost38H 7:ca880e05bb04 550 dcounto = -12487;
Joost38H 7:ca880e05bb04 551 dcountr = -7386;
Joost38H 7:ca880e05bb04 552 pc.printf("Tiller");
Joost38H 7:ca880e05bb04 553 Vex = 20;
Joost38H 7:ca880e05bb04 554 Vey = 20;
Joost38H 8:f5065cd04213 555 controlmotor1.attach(&MotorController1, 0.01);
Joost38H 8:f5065cd04213 556 controlmotor2.attach(&MotorController2, 0.01);
Joost38H 8:f5065cd04213 557 controlmotor3.attach(&MotorController3, 0.01);
Joost38H 7:ca880e05bb04 558 }
Joost38H 7:ca880e05bb04 559
Joost38H 7:ca880e05bb04 560
Joost38H 6:2674fe30b610 561 void setcurrentposition(){
Joost38H 14:291f9a29bd74 562 if(Input_done==true){
Joost38H 14:291f9a29bd74 563 hcounto=4200*((sqrt(pow((Pex-Pox),2)+pow((Pey-Poy),2)))/omtrekklosje);
Joost38H 14:291f9a29bd74 564 hcountb=4200*((sqrt(pow((Pex-Pbx),2)+pow((Pey-Pby),2)))/omtrekklosje);
Joost38H 14:291f9a29bd74 565 hcountr=4200*((sqrt(pow((Pex-Prx),2)+pow((Pey-Pry),2)))/omtrekklosje);
Joost38H 14:291f9a29bd74 566 pc.printf("ik reset hcounts");
Joost38H 15:46acc9b5decf 567 Input_done=false;
Joost38H 14:291f9a29bd74 568 }
Joost38H 6:2674fe30b610 569 }
Joost38H 7:ca880e05bb04 570
Joost38H 0:eb16ed402ffa 571 int main()
Joost38H 0:eb16ed402ffa 572 {
Joost38H 0:eb16ed402ffa 573 pc.baud(115200);
Joost38H 8:f5065cd04213 574 wait(1.0f);
Joost38H 8:f5065cd04213 575 //tiller();
Joost38H 1:6aac013b0ba3 576 getbqChain();
Joost38H 4:fddab1c875a9 577 threshold_timerR.attach(&Threshold_samplingBR, 0.002);
Joost38H 4:fddab1c875a9 578 threshold_timerL.attach(&Threshold_samplingBL, 0.002);
Joost38H 18:00379f0ecc7f 579 setcurrentposition();
Joost38H 2:2c4ee76dc830 580 while(true){
Joost38H 2:2c4ee76dc830 581 sample_timer.attach(&EMG_sample, 0.002);
Joost38H 2:2c4ee76dc830 582 wait(2.5f);
Joost38H 2:2c4ee76dc830 583 tellerX();
Joost38H 2:2c4ee76dc830 584 tellerY();
Joost38H 2:2c4ee76dc830 585 calculator();
Joost38H 8:f5065cd04213 586 controlmotor1.attach(&MotorController1, 0.01);
Joost38H 8:f5065cd04213 587 controlmotor2.attach(&MotorController2, 0.01);
Joost38H 8:f5065cd04213 588 controlmotor3.attach(&MotorController3, 0.01);
Joost38H 2:2c4ee76dc830 589 //zakker();
Joost38H 5:81d3b53087c0 590 wait(5.0f);
Joost38H 2:2c4ee76dc830 591 }
Joost38H 5:81d3b53087c0 592
Joost38H 5:81d3b53087c0 593 }
Joost38H 5:81d3b53087c0 594