Werkende PID controller met EMG aansturing.

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of Motor_EMG_V2 by BMT M9 groep 7

Committer:
laura94
Date:
Mon Oct 19 10:06:36 2015 +0000
Revision:
0:5816557b2064
Child:
1:3f49c8818619
Motor Robert + EMG totaal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
laura94 0:5816557b2064 1 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 2 // Motorscript voor 2 motoren voor de "SJOEL ROBOT", Groep 7
laura94 0:5816557b2064 3 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 4 // Libraries
laura94 0:5816557b2064 5 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 6 #include "mbed.h"
laura94 0:5816557b2064 7 #include "MODSERIAL.h"
laura94 0:5816557b2064 8 #include "HIDScope.h"
laura94 0:5816557b2064 9 #include "QEI.h"
laura94 0:5816557b2064 10 #include "biquadFilter.h"
laura94 0:5816557b2064 11
laura94 0:5816557b2064 12 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 13 // Constanten/Inputs/Outputs
laura94 0:5816557b2064 14 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 15 MODSERIAL pc(USBTX, USBRX); // To/From PC
laura94 0:5816557b2064 16 QEI Encoder2(D3, D2, NC, 32); // Encoder Motor 2
laura94 0:5816557b2064 17 QEI Encoder1(D13,D12,NC, 32); // Encoder Motor 1
laura94 0:5816557b2064 18 HIDScope scope(5); // Scope, 4 channels
laura94 0:5816557b2064 19
laura94 0:5816557b2064 20 // LEDs
laura94 0:5816557b2064 21 DigitalOut LedR(LED_RED);
laura94 0:5816557b2064 22 DigitalOut LedG(LED_GREEN);
laura94 0:5816557b2064 23 DigitalOut LedB(LED_BLUE);
laura94 0:5816557b2064 24
laura94 0:5816557b2064 25 // Motor
laura94 0:5816557b2064 26 DigitalOut motor1direction(D7); // Motor 1, Direction & Speed
laura94 0:5816557b2064 27 PwmOut motor1speed(D6);
laura94 0:5816557b2064 28 DigitalOut motor2direction(D4); // Motor 2, Direction & Speed
laura94 0:5816557b2064 29 PwmOut motor2speed(D5);
laura94 0:5816557b2064 30
laura94 0:5816557b2064 31 //EMG
laura94 0:5816557b2064 32 AnalogIn EMG_left(A0); //Analog input
laura94 0:5816557b2064 33 AnalogIn EMG_right(A1);
laura94 0:5816557b2064 34
laura94 0:5816557b2064 35 // Tickers
laura94 0:5816557b2064 36 Ticker ScopeTime;
laura94 0:5816557b2064 37 Ticker myControllerTicker2;
laura94 0:5816557b2064 38 Ticker myControllerTicker1;
laura94 0:5816557b2064 39 Ticker SampleEMGLeft;
laura94 0:5816557b2064 40 Ticker SampleEMGRight;
laura94 0:5816557b2064 41 Ticker ScopeTimer;
laura94 0:5816557b2064 42 Ticker serial;
laura94 0:5816557b2064 43 Ticker MovingAverageLeft;
laura94 0:5816557b2064 44 Ticker MovingAverageRight;
laura94 0:5816557b2064 45
laura94 0:5816557b2064 46 // Constants
laura94 0:5816557b2064 47 double reference2, reference1;
laura94 0:5816557b2064 48 double position2 = 0, position1 = 0;
laura94 0:5816557b2064 49 double m2_ref = 0, m1_ref = 0;
laura94 0:5816557b2064 50 int count = 0;
laura94 0:5816557b2064 51 double Grens2 = 90, Grens1 = 90;
laura94 0:5816557b2064 52 double Stapgrootte = 5;
laura94 0:5816557b2064 53
laura94 0:5816557b2064 54 DigitalOut led(LED_RED);
laura94 0:5816557b2064 55 DigitalOut ledG(LED_GREEN);
laura94 0:5816557b2064 56 DigitalOut ledB(LED_BLUE);
laura94 0:5816557b2064 57
laura94 0:5816557b2064 58 // Declaring variables
laura94 0:5816557b2064 59 double EMG_L_f_v1 = 0, EMG_L_f_v2 = 0;
laura94 0:5816557b2064 60 double EMG_L_fh=0;
laura94 0:5816557b2064 61 double EMG_left_value;
laura94 0:5816557b2064 62 double EMG_left_f1;
laura94 0:5816557b2064 63 double EMG_left_f2;
laura94 0:5816557b2064 64 double EMG_left_f3;
laura94 0:5816557b2064 65 double EMG_left_abs;
laura94 0:5816557b2064 66
laura94 0:5816557b2064 67 double EMG_right_value;
laura94 0:5816557b2064 68 double EMG_right_f1;
laura94 0:5816557b2064 69 double EMG_right_f2;
laura94 0:5816557b2064 70 double EMG_right_f3;
laura94 0:5816557b2064 71 double EMG_right_abs;
laura94 0:5816557b2064 72 double Threshold1;
laura94 0:5816557b2064 73 double Threshold2;
laura94 0:5816557b2064 74 double Threshold3;
laura94 0:5816557b2064 75 double Threshold4;
laura94 0:5816557b2064 76
laura94 0:5816557b2064 77 int N = 50;
laura94 0:5816557b2064 78 double MAF_left[50];
laura94 0:5816557b2064 79 double EMG_left_MAF;
laura94 0:5816557b2064 80 double MAF_right[50];
laura94 0:5816557b2064 81 double EMG_right_MAF;
laura94 0:5816557b2064 82
laura94 0:5816557b2064 83
laura94 0:5816557b2064 84 //Sample time (motor-step)
laura94 0:5816557b2064 85 const double m2_Ts = 0.01, m1_Ts = 0.01;
laura94 0:5816557b2064 86
laura94 0:5816557b2064 87 //Controller gain Motor 2 & 1
laura94 0:5816557b2064 88 const double m2_Kp = 5,m2_Ki = 0.01, m2_Kd = 20;
laura94 0:5816557b2064 89 const double m1_Kp = 5,m1_Ki = 0.01, m1_Kd = 20;
laura94 0:5816557b2064 90 double m2_err_int = 0, m2_prev_err = 0;
laura94 0:5816557b2064 91 double m1_err_int = 0, m1_prev_err = 0;
laura94 0:5816557b2064 92
laura94 0:5816557b2064 93 //Derivative filter coeffs Motor 2 & 1
laura94 0:5816557b2064 94 const double BiGain2 = 0.012, BiGain1 = 0.016955;
laura94 0:5816557b2064 95 const double m2_f_a1 = -0.96608908283*BiGain2, m2_f_a2 = 0.0*BiGain2, m2_f_b0 = 1.0*BiGain2, m2_f_b1 = 1.0*BiGain2, m2_f_b2 = 0.0*BiGain2;
laura94 0:5816557b2064 96 const double m1_f_a1 = -0.96608908283*BiGain1, m1_f_a2 = 0.0*BiGain1, m1_f_b0 = 1.0*BiGain1, m1_f_b1 = 1.0*BiGain1, m1_f_b2 = 0.0*BiGain1;
laura94 0:5816557b2064 97
laura94 0:5816557b2064 98 // coëfficiënten
laura94 0:5816557b2064 99 const double BiGainEMG_H1 = 0.796821;
laura94 0:5816557b2064 100 const double EMGH1_a1 = -1.47500228332, EMGH1_a2 = 0.55273994299, EMGH1_b0 = 1.0*BiGainEMG_H1, EMGH1_b1 = -1.99922446977*BiGainEMG_H1, EMGH1_b2 = 1.0*BiGainEMG_H1; //coefficients for high-pass filter
laura94 0:5816557b2064 101
laura94 0:5816557b2064 102 const double BiGainEMG_L1= 0.001041;
laura94 0:5816557b2064 103 const double EMGL1_a1 = -1.87506717001, EMGL1_a2 = 0.87923101463, EMGL1_b0 = 1.0*BiGainEMG_L1, EMGL1_b1 = 2.00000000000*BiGainEMG_L1, EMGL1_b2 = 1.0*BiGainEMG_L1; // coefficients for low-pass filter
laura94 0:5816557b2064 104
laura94 0:5816557b2064 105 const double BiGainEMG_N1 = 1.0;
laura94 0:5816557b2064 106 const double EMGN1_a1 = -1.58174308681, EMGN1_a2 = 0.96540248979, EMGN1_b0 = 1.0*BiGainEMG_N1, EMGN1_b1 = -1.61816176147*BiGainEMG_N1, EMGN1_b2 = 1.0*BiGainEMG_N1; //coefficients for notch filter
laura94 0:5816557b2064 107
laura94 0:5816557b2064 108 // Filter variables
laura94 0:5816557b2064 109 double m2_f_v1 = 0, m2_f_v2 = 0;
laura94 0:5816557b2064 110 double m1_f_v1 = 0, m1_f_v2 = 0;
laura94 0:5816557b2064 111
laura94 0:5816557b2064 112 // Creating the filters
laura94 0:5816557b2064 113 biquadFilter EMG_highpass1 (EMGH1_a1, EMGH1_a2, EMGH1_b0, EMGH1_b1, EMGH1_b2); // creates the high pass filter
laura94 0:5816557b2064 114 biquadFilter EMG_lowpass1 (EMGL1_a1, EMGL1_a2, EMGL1_b0, EMGL1_b1, EMGL1_b2); // creates the low pass filter
laura94 0:5816557b2064 115 biquadFilter EMG_notch1 (EMGN1_a1, EMGN1_a2, EMGN1_b0, EMGN1_b1, EMGN1_b2); // creates the notch filter
laura94 0:5816557b2064 116
laura94 0:5816557b2064 117 biquadFilter EMG_highpass1R (EMGH1_a1, EMGH1_a2, EMGH1_b0, EMGH1_b1, EMGH1_b2); // creates the high pass filter
laura94 0:5816557b2064 118 biquadFilter EMG_lowpass1R (EMGL1_a1, EMGL1_a2, EMGL1_b0, EMGL1_b1, EMGL1_b2); // creates the low pass filter
laura94 0:5816557b2064 119 biquadFilter EMG_notch1R (EMGN1_a1, EMGN1_a2, EMGN1_b0, EMGN1_b1, EMGN1_b2); // creates the notch filter
laura94 0:5816557b2064 120
laura94 0:5816557b2064 121
laura94 0:5816557b2064 122 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 123 // General Functions
laura94 0:5816557b2064 124 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 125
laura94 0:5816557b2064 126 //HIDScope
laura94 0:5816557b2064 127 void ScopeSend()//Functie die de gegevens voor de scope uitleest en doorstuurt
laura94 0:5816557b2064 128 {
laura94 0:5816557b2064 129 scope.set(0, reference2 - position2);
laura94 0:5816557b2064 130 scope.set(1, position2);
laura94 0:5816557b2064 131 scope.set(2, reference1 - position1);
laura94 0:5816557b2064 132 scope.set(3, position1);
laura94 0:5816557b2064 133 scope.set(4, EMG_left_MAF);
laura94 0:5816557b2064 134 scope.set(5, EMG_right_MAF);
laura94 0:5816557b2064 135 scope.send();
laura94 0:5816557b2064 136
laura94 0:5816557b2064 137 }
laura94 0:5816557b2064 138
laura94 0:5816557b2064 139 // Reusable PID controller
laura94 0:5816557b2064 140 double PID( double e, const double Kp, const double Ki, const double Kd, double Ts, double &e_int, double &e_prev)
laura94 0:5816557b2064 141 {
laura94 0:5816557b2064 142 // Derivative
laura94 0:5816557b2064 143 double e_der = (e-e_prev)/Ts;
laura94 0:5816557b2064 144 e_prev = e;
laura94 0:5816557b2064 145 // Integral
laura94 0:5816557b2064 146 e_int = e_int + Ts*e;
laura94 0:5816557b2064 147 // PID
laura94 0:5816557b2064 148 return Kp * e + Ki*e_int + Kd*e_der;
laura94 0:5816557b2064 149 }
laura94 0:5816557b2064 150
laura94 0:5816557b2064 151 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 152 //EMG functions
laura94 0:5816557b2064 153 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 154
laura94 0:5816557b2064 155 // EMG filtering function
laura94 0:5816557b2064 156 void EMGfilterLeft()
laura94 0:5816557b2064 157 {
laura94 0:5816557b2064 158 EMG_left_value = EMG_left.read();
laura94 0:5816557b2064 159 EMG_left_f1 = EMG_highpass1.step(EMG_left_value);
laura94 0:5816557b2064 160 EMG_left_abs = fabs(EMG_left_f1);
laura94 0:5816557b2064 161 EMG_left_f2 = EMG_lowpass1.step(EMG_left_abs);
laura94 0:5816557b2064 162 EMG_left_f3 = EMG_notch1.step(EMG_left_f2);
laura94 0:5816557b2064 163
laura94 0:5816557b2064 164 }
laura94 0:5816557b2064 165
laura94 0:5816557b2064 166 void EMGfilterRight()
laura94 0:5816557b2064 167 {
laura94 0:5816557b2064 168 EMG_right_value = EMG_right.read();
laura94 0:5816557b2064 169 EMG_right_f1 = EMG_highpass1R.step(EMG_right_value);
laura94 0:5816557b2064 170 EMG_right_abs = fabs(EMG_right_f1);
laura94 0:5816557b2064 171 EMG_right_f2 = EMG_lowpass1R.step(EMG_right_abs);
laura94 0:5816557b2064 172 EMG_right_f3 = EMG_notch1R.step(EMG_right_f2);
laura94 0:5816557b2064 173 }
laura94 0:5816557b2064 174
laura94 0:5816557b2064 175 // Movingaverage Filter
laura94 0:5816557b2064 176 void MovingAverageFilterLeft()
laura94 0:5816557b2064 177 {
laura94 0:5816557b2064 178 EMG_left_MAF = (MAF_left[0]+MAF_left[1]+MAF_left[2]+MAF_left[3]+MAF_left[4]+MAF_left[5]+MAF_left[6]+MAF_left[7]+MAF_left[8]+MAF_left[9]+MAF_left[10]+MAF_left[11]+MAF_left[12]+MAF_left[13]+MAF_left[14]+MAF_left[15]+MAF_left[16]+MAF_left[17]+MAF_left[18]+MAF_left[19]+MAF_left[20]+MAF_left[21]+MAF_left[22]+MAF_left[23]+MAF_left[24]+MAF_left[25]+MAF_left[26]+MAF_left[27]+MAF_left[28]+MAF_left[29]+MAF_left[30]+MAF_left[31]+MAF_left[32]+MAF_left[33]+MAF_left[34]+MAF_left[35]+MAF_left[36]+MAF_left[37]+MAF_left[38]+MAF_left[39]+MAF_left[40]+MAF_left[41]+MAF_left[42]+MAF_left[43]+MAF_left[44]+MAF_left[45]+MAF_left[46]+MAF_left[47]+MAF_left[48]+MAF_left[49])/N;
laura94 0:5816557b2064 179 MAF_left[49] = MAF_left[48], MAF_left[48] = MAF_left[47], MAF_left[47] = MAF_left[46], MAF_left[46] = MAF_left[45], MAF_left[45] = MAF_left[44], MAF_left[44] = MAF_left[43], MAF_left[43] = MAF_left[42], MAF_left[42] = MAF_left[41], MAF_left[41] = MAF_left[40], MAF_left[40] = MAF_left[39], MAF_left[39] = MAF_left[38], MAF_left[38] = MAF_left[37], MAF_left[37] = MAF_left[36], MAF_left[36] = MAF_left[35], MAF_left[35] = MAF_left[34], MAF_left[34] = MAF_left[33], MAF_left[33] = MAF_left[32], MAF_left[32] = MAF_left[31], MAF_left[31] = MAF_left[30], MAF_left[30] = MAF_left[29], MAF_left[29] = MAF_left[28], MAF_left[28] = MAF_left[27], MAF_left[27] = MAF_left[26], MAF_left[26] = MAF_left[25];
laura94 0:5816557b2064 180 MAF_left[25] = MAF_left[24], MAF_left[24] = MAF_left[23], MAF_left[23] = MAF_left[22], MAF_left[22] = MAF_left[21], MAF_left[21] = MAF_left[20], MAF_left[20] = MAF_left[19], MAF_left[19] = MAF_left[18], MAF_left[18] = MAF_left[17], MAF_left[17] = MAF_left[16], MAF_left[16] = MAF_left[15], MAF_left[15] = MAF_left[14], MAF_left[14] = MAF_left[13], MAF_left[13] = MAF_left[12], MAF_left[12] = MAF_left[11], MAF_left[11] = MAF_left[10], MAF_left[10] = MAF_left[9], MAF_left[9] = MAF_left[8], MAF_left[8] = MAF_left[7], MAF_left[7] = MAF_left[6], MAF_left[6] = MAF_left[5], MAF_left[5] = MAF_left[4], MAF_left[4] = MAF_left[3], MAF_left[3] = MAF_left[2], MAF_left[2] = MAF_left[1], MAF_left[1] = MAF_left[0];
laura94 0:5816557b2064 181 MAF_left[0] = EMG_left_f3;
laura94 0:5816557b2064 182 }
laura94 0:5816557b2064 183
laura94 0:5816557b2064 184 void MovingAverageFilterRight()
laura94 0:5816557b2064 185 {
laura94 0:5816557b2064 186 EMG_right_MAF = (MAF_right[0]+MAF_right[1]+MAF_right[2]+MAF_right[3]+MAF_right[4]+MAF_right[5]+MAF_right[6]+MAF_right[7]+MAF_right[8]+MAF_right[9]+MAF_right[10]+MAF_right[11]+MAF_right[12]+MAF_right[13]+MAF_right[14]+MAF_right[15]+MAF_right[16]+MAF_right[17]+MAF_right[18]+MAF_right[19]+MAF_right[20]+MAF_right[21]+MAF_right[22]+MAF_right[23]+MAF_right[24]+MAF_right[25]+MAF_right[26]+MAF_right[27]+MAF_right[28]+MAF_right[29]+MAF_right[30]+MAF_right[31]+MAF_right[32]+MAF_right[33]+MAF_right[34]+MAF_right[35]+MAF_right[36]+MAF_right[37]+MAF_right[38]+MAF_right[39]+MAF_right[40]+MAF_right[41]+MAF_right[42]+MAF_right[43]+MAF_right[44]+MAF_right[45]+MAF_right[46]+MAF_right[47]+MAF_right[48]+MAF_right[49])/N;
laura94 0:5816557b2064 187 MAF_right[49] = MAF_right[48], MAF_right[48] = MAF_right[47], MAF_right[47] = MAF_right[46], MAF_right[46] = MAF_right[45], MAF_right[45] = MAF_right[44], MAF_right[44] = MAF_right[43], MAF_right[43] = MAF_right[42], MAF_right[42] = MAF_right[41], MAF_right[41] = MAF_right[40], MAF_right[40] = MAF_right[39], MAF_right[39] = MAF_right[38], MAF_right[38] = MAF_right[37], MAF_right[37] = MAF_right[36], MAF_right[36] = MAF_right[35], MAF_right[35] = MAF_right[34], MAF_right[34] = MAF_right[33], MAF_right[33] = MAF_right[32], MAF_right[32] = MAF_right[31], MAF_right[31] = MAF_right[30], MAF_right[30] = MAF_right[29], MAF_right[29] = MAF_right[28], MAF_right[28] = MAF_right[27], MAF_right[27] = MAF_right[26], MAF_right[26] = MAF_right[25];
laura94 0:5816557b2064 188 MAF_right[25] = MAF_right[24], MAF_right[24] = MAF_right[23], MAF_right[23] = MAF_right[22], MAF_right[22] = MAF_right[21], MAF_right[21] = MAF_right[20], MAF_right[20] = MAF_right[19], MAF_right[19] = MAF_right[18], MAF_right[18] = MAF_right[17], MAF_right[17] = MAF_right[16], MAF_right[16] = MAF_right[15], MAF_right[15] = MAF_right[14], MAF_right[14] = MAF_right[13], MAF_right[13] = MAF_right[12], MAF_right[12] = MAF_right[11], MAF_right[11] = MAF_right[10], MAF_right[10] = MAF_right[9], MAF_right[9] = MAF_right[8], MAF_right[8] = MAF_right[7], MAF_right[7] = MAF_right[6], MAF_right[6] = MAF_right[5], MAF_right[5] = MAF_right[4], MAF_right[4] = MAF_right[3], MAF_right[3] = MAF_right[2], MAF_right[2] = MAF_right[1], MAF_right[1] = MAF_right[0];
laura94 0:5816557b2064 189 MAF_right[0] = EMG_right_f3;
laura94 0:5816557b2064 190 }
laura94 0:5816557b2064 191
laura94 0:5816557b2064 192 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 193 // Motor control functions
laura94 0:5816557b2064 194 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 195
laura94 0:5816557b2064 196 // Motor2 control
laura94 0:5816557b2064 197 void motor2_Controller()
laura94 0:5816557b2064 198 {
laura94 0:5816557b2064 199 // Setpoint motor 2
laura94 0:5816557b2064 200 reference2 = m2_ref; // Reference in degrees
laura94 0:5816557b2064 201 position2 = Encoder2.getPulses()*360/(32*131); // Position in degrees
laura94 0:5816557b2064 202 // Speed control
laura94 0:5816557b2064 203 double m2_P1 = PID( reference2 - position2, m2_Kp, m2_Ki, m2_Kd, m2_Ts, m2_err_int, m2_prev_err);
laura94 0:5816557b2064 204 double m2_P2 = m2_P1;
laura94 0:5816557b2064 205 motor2speed = abs(m2_P2);
laura94 0:5816557b2064 206 // Direction control
laura94 0:5816557b2064 207 if(m2_P2 > 0)
laura94 0:5816557b2064 208 {
laura94 0:5816557b2064 209 motor2direction = 0;
laura94 0:5816557b2064 210 }
laura94 0:5816557b2064 211 else
laura94 0:5816557b2064 212 {
laura94 0:5816557b2064 213 motor2direction = 1;
laura94 0:5816557b2064 214 }
laura94 0:5816557b2064 215 }
laura94 0:5816557b2064 216
laura94 0:5816557b2064 217 // Motor1 control
laura94 0:5816557b2064 218 void motor1_Controller()
laura94 0:5816557b2064 219 {
laura94 0:5816557b2064 220 // Setpoint Motor 1
laura94 0:5816557b2064 221 reference1 = m1_ref; // Reference in degrees
laura94 0:5816557b2064 222 position1 = Encoder1.getPulses()*360/(32*131); // Position in degrees
laura94 0:5816557b2064 223 // Speed control
laura94 0:5816557b2064 224 double m1_P1 = PID( reference1 - position1, m1_Kp, m1_Ki, m1_Kd, m1_Ts, m1_err_int, m1_prev_err);
laura94 0:5816557b2064 225 double m1_P2 = m1_P1;
laura94 0:5816557b2064 226 motor1speed = abs(m1_P2);
laura94 0:5816557b2064 227 // Direction control
laura94 0:5816557b2064 228 if(m1_P2 > 0)
laura94 0:5816557b2064 229 {
laura94 0:5816557b2064 230 motor1direction = 1;
laura94 0:5816557b2064 231 }
laura94 0:5816557b2064 232 else
laura94 0:5816557b2064 233 {
laura94 0:5816557b2064 234 motor1direction = 0;
laura94 0:5816557b2064 235 }
laura94 0:5816557b2064 236 }
laura94 0:5816557b2064 237
laura94 0:5816557b2064 238 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 239 // Main function
laura94 0:5816557b2064 240 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 241 int main()
laura94 0:5816557b2064 242 {
laura94 0:5816557b2064 243 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 244 // Initalizing
laura94 0:5816557b2064 245 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 246 //LEDs OFF
laura94 0:5816557b2064 247 LedR = LedB = LedG = 1;
laura94 0:5816557b2064 248
laura94 0:5816557b2064 249 //PC connection & check
laura94 0:5816557b2064 250 pc.baud(115200);
laura94 0:5816557b2064 251 pc.printf("Tot aan loop werkt\n");
laura94 0:5816557b2064 252
laura94 0:5816557b2064 253 // Tickers
laura94 0:5816557b2064 254 ScopeTime.attach(&ScopeSend, 0.01f); // 100 Hz, Scope
laura94 0:5816557b2064 255 myControllerTicker2.attach(&motor2_Controller, 0.01f ); // 100 Hz, Motor 2
laura94 0:5816557b2064 256 myControllerTicker1.attach(&motor1_Controller, 0.01f ); // 100 Hz, Motor 1
laura94 0:5816557b2064 257 SampleEMGLeft.attach(&EMGfilterLeft, 0.01f);
laura94 0:5816557b2064 258 SampleEMGRight.attach(&EMGfilterRight, 0.01f);
laura94 0:5816557b2064 259 MovingAverageLeft.attach(&MovingAverageFilterLeft, 0.01f);
laura94 0:5816557b2064 260 MovingAverageRight.attach(&MovingAverageFilterRight, 0.01f);
laura94 0:5816557b2064 261
laura94 0:5816557b2064 262 // Defining threshold
laura94 0:5816557b2064 263 ledG.write(1), led.write(1), ledB.write(1);
laura94 0:5816557b2064 264 wait(20);
laura94 0:5816557b2064 265
laura94 0:5816557b2064 266 ledG.write(1);
laura94 0:5816557b2064 267 wait(0.2);
laura94 0:5816557b2064 268 ledG.write(0);
laura94 0:5816557b2064 269 wait(0.2);
laura94 0:5816557b2064 270 ledG.write(1);
laura94 0:5816557b2064 271 wait(0.2);
laura94 0:5816557b2064 272 ledG.write(0);
laura94 0:5816557b2064 273 wait(0.2);
laura94 0:5816557b2064 274 ledG.write(1);
laura94 0:5816557b2064 275 wait(0.2);
laura94 0:5816557b2064 276 ledG.write(0);
laura94 0:5816557b2064 277 wait(2);
laura94 0:5816557b2064 278 Threshold1 = 0.5*EMG_left_MAF;
laura94 0:5816557b2064 279 Threshold2 = 0.2*EMG_left_MAF;
laura94 0:5816557b2064 280 ledG.write(1);
laura94 0:5816557b2064 281
laura94 0:5816557b2064 282 wait(2);
laura94 0:5816557b2064 283 ledB.write(1);
laura94 0:5816557b2064 284 wait(0.2);
laura94 0:5816557b2064 285 ledB.write(0);
laura94 0:5816557b2064 286 wait(0.2);
laura94 0:5816557b2064 287 ledB.write(1);
laura94 0:5816557b2064 288 wait(0.2);
laura94 0:5816557b2064 289 ledB.write(0);
laura94 0:5816557b2064 290 wait(0.2);
laura94 0:5816557b2064 291 ledB.write(1);
laura94 0:5816557b2064 292 wait(0.2);
laura94 0:5816557b2064 293 ledB.write(0);
laura94 0:5816557b2064 294 wait(2);
laura94 0:5816557b2064 295 Threshold3 = 0.5*EMG_right_MAF;
laura94 0:5816557b2064 296 Threshold4 = 0.2*EMG_right_MAF;
laura94 0:5816557b2064 297 ledB.write(1);
laura94 0:5816557b2064 298
laura94 0:5816557b2064 299 pc.printf("T1 = %f, T2 = %f, T3 = %f, T4 = %f\n", Threshold1, Threshold2, Threshold3, Threshold4);
laura94 0:5816557b2064 300 ledG.write(1);
laura94 0:5816557b2064 301
laura94 0:5816557b2064 302 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 303 // Control Program
laura94 0:5816557b2064 304 //--------------------------------------------------------------------------------------------------------------------------//
laura94 0:5816557b2064 305 while(true)
laura94 0:5816557b2064 306 {
laura94 0:5816557b2064 307 //char c = pc.getc();
laura94 0:5816557b2064 308 // 1 Program UP
laura94 0:5816557b2064 309 if ((EMG_right_MAF >= Threshold1) && (EMG_left_MAF >= Threshold1)) //if(c == 'e') //
laura94 0:5816557b2064 310 {
laura94 0:5816557b2064 311 count = count + 1;
laura94 0:5816557b2064 312 if(count > 2)
laura94 0:5816557b2064 313 {
laura94 0:5816557b2064 314 count = 2;
laura94 0:5816557b2064 315 }
laura94 0:5816557b2064 316
laura94 0:5816557b2064 317 }
laura94 0:5816557b2064 318 // 1 Program DOWN
laura94 0:5816557b2064 319 // if(c == 'd') // Hoe gaat dit aangestuurd worden?
laura94 0:5816557b2064 320 // {
laura94 0:5816557b2064 321 // count = count - 1;
laura94 0:5816557b2064 322 // if(count < 0)
laura94 0:5816557b2064 323 // {
laura94 0:5816557b2064 324 // count = 0;
laura94 0:5816557b2064 325 // }
laura94 0:5816557b2064 326 // }
laura94 0:5816557b2064 327 // PROGRAM 0: Motor 2 control and indirect control of motor 1, Green LED
laura94 0:5816557b2064 328 if(count == 0)
laura94 0:5816557b2064 329 {
laura94 0:5816557b2064 330
laura94 0:5816557b2064 331 LedR = LedB = 1;
laura94 0:5816557b2064 332 LedG = 0;
laura94 0:5816557b2064 333 if ((EMG_right_MAF >= Threshold1) && (EMG_left_MAF <= Threshold1)) //if(c == 'r') //
laura94 0:5816557b2064 334 {
laura94 0:5816557b2064 335 m2_ref = m2_ref + Stapgrootte;
laura94 0:5816557b2064 336 m1_ref = m1_ref - Stapgrootte;
laura94 0:5816557b2064 337 if (m2_ref > Grens2)
laura94 0:5816557b2064 338 {
laura94 0:5816557b2064 339 m2_ref = Grens2;
laura94 0:5816557b2064 340 m1_ref = -1*Grens1;
laura94 0:5816557b2064 341 }
laura94 0:5816557b2064 342 }
laura94 0:5816557b2064 343 if((EMG_right_MAF < Threshold1) && (EMG_left_MAF > Threshold1)) //if (c == 'f') //
laura94 0:5816557b2064 344 {
laura94 0:5816557b2064 345 m2_ref = m2_ref - Stapgrootte;
laura94 0:5816557b2064 346 m1_ref = m1_ref + Stapgrootte;
laura94 0:5816557b2064 347 if (m2_ref < -1*Grens2)
laura94 0:5816557b2064 348 {
laura94 0:5816557b2064 349 m2_ref = -1*Grens2;
laura94 0:5816557b2064 350 m1_ref = Grens1;
laura94 0:5816557b2064 351 }
laura94 0:5816557b2064 352 }
laura94 0:5816557b2064 353
laura94 0:5816557b2064 354 // PROGRAM 1: Motor 1 control, Red LED
laura94 0:5816557b2064 355 if(count == 1)
laura94 0:5816557b2064 356 {
laura94 0:5816557b2064 357 LedG = LedB = 1;
laura94 0:5816557b2064 358 LedR = 0;
laura94 0:5816557b2064 359 if ((EMG_right_MAF >= Threshold1) && (EMG_left_MAF <= Threshold1)) // if(c == 't') //
laura94 0:5816557b2064 360 {
laura94 0:5816557b2064 361 m1_ref = m1_ref + Stapgrootte;
laura94 0:5816557b2064 362 if (m1_ref > Grens1)
laura94 0:5816557b2064 363 {
laura94 0:5816557b2064 364 m1_ref = Grens1;
laura94 0:5816557b2064 365 }
laura94 0:5816557b2064 366 }
laura94 0:5816557b2064 367 if ((EMG_left_MAF > Threshold1) && (EMG_right_MAF < Threshold1)) //if(c == 'g') //
laura94 0:5816557b2064 368 {
laura94 0:5816557b2064 369 m1_ref = m1_ref - Stapgrootte;
laura94 0:5816557b2064 370 if (m1_ref < -1*Grens1)
laura94 0:5816557b2064 371 {
laura94 0:5816557b2064 372 m1_ref = -1*Grens1;
laura94 0:5816557b2064 373 }
laura94 0:5816557b2064 374 }
laura94 0:5816557b2064 375 }
laura94 0:5816557b2064 376 // PROGRAM 2: Firing mechanism & Reset, Blue LED
laura94 0:5816557b2064 377 if(count == 2)
laura94 0:5816557b2064 378 {
laura94 0:5816557b2064 379
laura94 0:5816557b2064 380 LedR = LedG = 1;
laura94 0:5816557b2064 381 LedB = 0;
laura94 0:5816557b2064 382 //VUUUUR!! (To Do)
laura94 0:5816557b2064 383 wait(1);
laura94 0:5816557b2064 384 m2_ref = 0;
laura94 0:5816557b2064 385 m1_ref = 0;
laura94 0:5816557b2064 386 count = 0;
laura94 0:5816557b2064 387 }
laura94 0:5816557b2064 388 }
laura94 0:5816557b2064 389 }
laura94 0:5816557b2064 390 }