Yea it is working. done programinging for a while!

Dependencies:   HIDScope QEI mbed

Committer:
yohoo15
Date:
Mon Nov 02 15:03:25 2015 +0000
Revision:
2:e84adf0a960f
Parent:
1:267ff9a39ee4
with more comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yohoo15 0:0f08f98ae7f8 1 #include "mbed.h"
yohoo15 0:0f08f98ae7f8 2 //#include "read_filter_emg.h"
yohoo15 0:0f08f98ae7f8 3 //included for fabs() function
yohoo15 0:0f08f98ae7f8 4 #include <math.h>
yohoo15 0:0f08f98ae7f8 5 #include "HIDScope.h"
yohoo15 0:0f08f98ae7f8 6 #include <iostream>
yohoo15 0:0f08f98ae7f8 7 #include "QEI.h"
yohoo15 0:0f08f98ae7f8 8
yohoo15 0:0f08f98ae7f8 9 //DigitalOut led_green(LED_GREEN);
yohoo15 0:0f08f98ae7f8 10 //DigitalOut led_red(LED_RED);
yohoo15 0:0f08f98ae7f8 11
yohoo15 0:0f08f98ae7f8 12 Serial pc(USBTX, USBRX);
yohoo15 0:0f08f98ae7f8 13 AnalogIn analog_emg_left(A0);
yohoo15 0:0f08f98ae7f8 14 AnalogIn analog_emg_right(A1);
yohoo15 0:0f08f98ae7f8 15
yohoo15 0:0f08f98ae7f8 16 Ticker HIDScope_timer;
yohoo15 0:0f08f98ae7f8 17 Ticker Filteren_timer;
yohoo15 0:0f08f98ae7f8 18 Ticker aansturen;
yohoo15 0:0f08f98ae7f8 19
yohoo15 0:0f08f98ae7f8 20 HIDScope scope(4);
yohoo15 0:0f08f98ae7f8 21
yohoo15 0:0f08f98ae7f8 22 // defining flags
yohoo15 0:0f08f98ae7f8 23 volatile bool Flag_filteren = false;
yohoo15 0:0f08f98ae7f8 24 volatile bool Flag_HIDScope = false;
yohoo15 0:0f08f98ae7f8 25 volatile bool left_movement = false;
yohoo15 0:0f08f98ae7f8 26 volatile bool right_movement = false;
yohoo15 0:0f08f98ae7f8 27
yohoo15 0:0f08f98ae7f8 28 // making function flags.
yohoo15 0:0f08f98ae7f8 29 void Go_flag_filteren()
yohoo15 0:0f08f98ae7f8 30 {
yohoo15 0:0f08f98ae7f8 31 Flag_filteren = true;
yohoo15 0:0f08f98ae7f8 32 }
yohoo15 0:0f08f98ae7f8 33
yohoo15 0:0f08f98ae7f8 34 void Go_flag_HIDScope()
yohoo15 0:0f08f98ae7f8 35 {
yohoo15 0:0f08f98ae7f8 36 Flag_HIDScope = true;
yohoo15 0:0f08f98ae7f8 37 }
yohoo15 0:0f08f98ae7f8 38
yohoo15 0:0f08f98ae7f8 39
yohoo15 2:e84adf0a960f 40 /*************************************************************Begin Filtering **********************************************************************************************************/
yohoo15 0:0f08f98ae7f8 41 double input_left = 0 ;
yohoo15 0:0f08f98ae7f8 42 double input_right = 0 ;
yohoo15 0:0f08f98ae7f8 43 //double input = 0;
yohoo15 0:0f08f98ae7f8 44 double filter_signal_hid_left = 0;
yohoo15 0:0f08f98ae7f8 45 double filter_signal_hid_right = 0;
yohoo15 0:0f08f98ae7f8 46 //double input_right = 0;
yohoo15 0:0f08f98ae7f8 47 // defining threshold
yohoo15 2:e84adf0a960f 48 double high_threshold = 900;
yohoo15 0:0f08f98ae7f8 49 double low_threshold = 125;
yohoo15 0:0f08f98ae7f8 50
yohoo15 2:e84adf0a960f 51 //*** making the v for everything and conquer the world ***
yohoo15 0:0f08f98ae7f8 52
yohoo15 0:0f08f98ae7f8 53 //for left
yohoo15 0:0f08f98ae7f8 54 //for Notchfilter
yohoo15 0:0f08f98ae7f8 55 double notch_v11=0;
yohoo15 0:0f08f98ae7f8 56 double notch_v21=0;
yohoo15 0:0f08f98ae7f8 57 double notch_v12=0;
yohoo15 0:0f08f98ae7f8 58 double notch_v22=0;
yohoo15 0:0f08f98ae7f8 59 double notch_v13=0;
yohoo15 0:0f08f98ae7f8 60 double notch_v23=0;
yohoo15 0:0f08f98ae7f8 61
yohoo15 0:0f08f98ae7f8 62 //for highpass filter
yohoo15 0:0f08f98ae7f8 63 double high_v11=0;
yohoo15 0:0f08f98ae7f8 64 double high_v21=0;
yohoo15 0:0f08f98ae7f8 65 double high_v12=0;
yohoo15 0:0f08f98ae7f8 66 double high_v22=0;
yohoo15 0:0f08f98ae7f8 67 double high_v13=0;
yohoo15 0:0f08f98ae7f8 68 double high_v23=0;
yohoo15 0:0f08f98ae7f8 69
yohoo15 0:0f08f98ae7f8 70 // for lowpasfilter
yohoo15 0:0f08f98ae7f8 71 double low_v11=0;
yohoo15 0:0f08f98ae7f8 72 double low_v21=0;
yohoo15 0:0f08f98ae7f8 73 double low_v12=0;
yohoo15 0:0f08f98ae7f8 74 double low_v22=0;
yohoo15 0:0f08f98ae7f8 75 double low_v13=0;
yohoo15 0:0f08f98ae7f8 76 double low_v23=0;
yohoo15 0:0f08f98ae7f8 77
yohoo15 0:0f08f98ae7f8 78 // for moving average
yohoo15 0:0f08f98ae7f8 79 double n1 = 0;
yohoo15 0:0f08f98ae7f8 80 double n2 = 0;
yohoo15 0:0f08f98ae7f8 81 double n3 = 0;
yohoo15 0:0f08f98ae7f8 82 double n4 = 0;
yohoo15 0:0f08f98ae7f8 83 double n5 = 0;
yohoo15 0:0f08f98ae7f8 84
yohoo15 0:0f08f98ae7f8 85 // for right
yohoo15 0:0f08f98ae7f8 86 //for Notchfilter
yohoo15 0:0f08f98ae7f8 87 double notch_v11_b=0;
yohoo15 0:0f08f98ae7f8 88 double notch_v21_b=0;
yohoo15 0:0f08f98ae7f8 89 double notch_v12_b=0;
yohoo15 0:0f08f98ae7f8 90 double notch_v22_b=0;
yohoo15 0:0f08f98ae7f8 91 double notch_v13_b=0;
yohoo15 0:0f08f98ae7f8 92 double notch_v23_b=0;
yohoo15 0:0f08f98ae7f8 93
yohoo15 0:0f08f98ae7f8 94 //for highpass filter
yohoo15 0:0f08f98ae7f8 95 double high_v11_b=0;
yohoo15 0:0f08f98ae7f8 96 double high_v21_b=0;
yohoo15 0:0f08f98ae7f8 97 double high_v12_b=0;
yohoo15 0:0f08f98ae7f8 98 double high_v22_b=0;
yohoo15 0:0f08f98ae7f8 99 double high_v13_b=0;
yohoo15 0:0f08f98ae7f8 100 double high_v23_b=0;
yohoo15 0:0f08f98ae7f8 101
yohoo15 0:0f08f98ae7f8 102 // for lowpasfilter
yohoo15 0:0f08f98ae7f8 103 double low_v11_b=0;
yohoo15 0:0f08f98ae7f8 104 double low_v21_b=0;
yohoo15 0:0f08f98ae7f8 105 double low_v12_b=0;
yohoo15 0:0f08f98ae7f8 106 double low_v22_b=0;
yohoo15 0:0f08f98ae7f8 107 double low_v13_b=0;
yohoo15 0:0f08f98ae7f8 108 double low_v23_b=0;
yohoo15 0:0f08f98ae7f8 109
yohoo15 0:0f08f98ae7f8 110 // for moving average
yohoo15 0:0f08f98ae7f8 111 double n1_b = 0;
yohoo15 0:0f08f98ae7f8 112 double n2_b = 0;
yohoo15 0:0f08f98ae7f8 113 double n3_b = 0;
yohoo15 0:0f08f98ae7f8 114 double n4_b = 0;
yohoo15 0:0f08f98ae7f8 115 double n5_b = 0;
yohoo15 0:0f08f98ae7f8 116
yohoo15 0:0f08f98ae7f8 117
yohoo15 0:0f08f98ae7f8 118 //general biquad filter that can be called in all the filter functions
yohoo15 0:0f08f98ae7f8 119 double biquad(double u, double &v1, double &v2, const double a1,
yohoo15 0:0f08f98ae7f8 120 const double a2, const double b0, const double b1, const double b2)
yohoo15 0:0f08f98ae7f8 121 {
yohoo15 0:0f08f98ae7f8 122 double v = u - a1*v1 - a2*v2;
yohoo15 0:0f08f98ae7f8 123 double y = b0*v + b1*v1 + b2*v2;
yohoo15 0:0f08f98ae7f8 124 //values of v2 and v1 are updated, as they are passed by reference
yohoo15 0:0f08f98ae7f8 125 //they update globally
yohoo15 0:0f08f98ae7f8 126 v2 = v1;
yohoo15 0:0f08f98ae7f8 127 v1 = v;
yohoo15 0:0f08f98ae7f8 128 return y;
yohoo15 0:0f08f98ae7f8 129 }
yohoo15 0:0f08f98ae7f8 130
yohoo15 0:0f08f98ae7f8 131 double moving_average(double y, double &n1, double &n2, double &n3, double &n4, double &n5)
yohoo15 0:0f08f98ae7f8 132 {
yohoo15 0:0f08f98ae7f8 133 double average = (y + n1 + n2 +n3 + n4 + n5)/5;
yohoo15 0:0f08f98ae7f8 134 n5 = n4;
yohoo15 0:0f08f98ae7f8 135 n4 = n3;
yohoo15 0:0f08f98ae7f8 136 n3 = n2;
yohoo15 0:0f08f98ae7f8 137 n2 = n1;
yohoo15 0:0f08f98ae7f8 138 n1 = y;
yohoo15 0:0f08f98ae7f8 139
yohoo15 0:0f08f98ae7f8 140 return average;
yohoo15 0:0f08f98ae7f8 141 }
yohoo15 2:e84adf0a960f 142
yohoo15 0:0f08f98ae7f8 143 */
yohoo15 0:0f08f98ae7f8 144 //Specifying filter coefficients highpass
yohoo15 0:0f08f98ae7f8 145
yohoo15 0:0f08f98ae7f8 146 /* notch filter with 3 cascaded biquads*/
yohoo15 0:0f08f98ae7f8 147 //first notch biquad
yohoo15 0:0f08f98ae7f8 148 const double notch1_a1 = -1.55951422433;
yohoo15 0:0f08f98ae7f8 149 const double notch1_a2 = 0.92705680308;
yohoo15 0:0f08f98ae7f8 150 const double notch1_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 151 const double notch1_b1 = -1.61854515325;
yohoo15 0:0f08f98ae7f8 152 const double notch1_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 153
yohoo15 0:0f08f98ae7f8 154 //second notch biquad
yohoo15 0:0f08f98ae7f8 155 const double notch2_a1 = -1.54767435801;
yohoo15 0:0f08f98ae7f8 156 const double notch2_a2 = 0.96124842048;
yohoo15 0:0f08f98ae7f8 157 const double notch2_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 158 const double notch2_b1 = -1.61854515325;
yohoo15 0:0f08f98ae7f8 159 const double notch2_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 160
yohoo15 0:0f08f98ae7f8 161 //third notch biquad
yohoo15 0:0f08f98ae7f8 162 const double notch3_a1 = -1.62600366964;
yohoo15 0:0f08f98ae7f8 163 const double notch3_a2 = 0.96453460373;
yohoo15 0:0f08f98ae7f8 164 const double notch3_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 165 const double notch3_b1 = -1.61854515325;
yohoo15 0:0f08f98ae7f8 166 const double notch3_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 167
yohoo15 0:0f08f98ae7f8 168 /* high pass filter consists of three cascaded biquads
yohoo15 0:0f08f98ae7f8 169 blow coefficients for those three biquads */
yohoo15 0:0f08f98ae7f8 170 //first high pass biquad
yohoo15 0:0f08f98ae7f8 171 const double highp1_a1 = -0.67538034389;
yohoo15 0:0f08f98ae7f8 172 const double highp1_a2 = 0.12769255668;
yohoo15 0:0f08f98ae7f8 173 const double highp1_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 174 const double highp1_b1 = -2.00000000000;
yohoo15 0:0f08f98ae7f8 175 const double highp1_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 176
yohoo15 0:0f08f98ae7f8 177 //second high pass biquad
yohoo15 0:0f08f98ae7f8 178 const double highp2_a1 = -0.76475499450;
yohoo15 0:0f08f98ae7f8 179 const double highp2_a2 = 0.27692273367;
yohoo15 0:0f08f98ae7f8 180 const double highp2_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 181 const double highp2_b1 = -2.00000000000;
yohoo15 0:0f08f98ae7f8 182 const double highp2_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 183
yohoo15 0:0f08f98ae7f8 184 //third high pass biquad
yohoo15 0:0f08f98ae7f8 185 const double highp3_a1 = -0.99216561242;
yohoo15 0:0f08f98ae7f8 186 const double highp3_a2 = 0.65663360837;
yohoo15 0:0f08f98ae7f8 187 const double highp3_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 188 const double highp3_b1 = -2.00000000000;
yohoo15 0:0f08f98ae7f8 189 const double highp3_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 190
yohoo15 0:0f08f98ae7f8 191 /* lowpass filter consists of three cascaded biquads
yohoo15 0:0f08f98ae7f8 192 below the coefficients for those three biquads */
yohoo15 0:0f08f98ae7f8 193 //first low pass biquad
yohoo15 0:0f08f98ae7f8 194 const double lowp1_a1 = -1.05207469728;
yohoo15 0:0f08f98ae7f8 195 const double lowp1_a2 = 0.28586907478;
yohoo15 0:0f08f98ae7f8 196 const double lowp1_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 197 const double lowp1_b1 = 2.00000000000;
yohoo15 0:0f08f98ae7f8 198 const double lowp1_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 199
yohoo15 0:0f08f98ae7f8 200 //second low pass biquad
yohoo15 0:0f08f98ae7f8 201 const double lowp2_a1 = -1.16338171052;
yohoo15 0:0f08f98ae7f8 202 const double lowp2_a2 = 0.42191097989;
yohoo15 0:0f08f98ae7f8 203 const double lowp2_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 204 const double lowp2_b1 = 2.00000000000;
yohoo15 0:0f08f98ae7f8 205 const double lowp2_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 206
yohoo15 0:0f08f98ae7f8 207 //third low pass biquad
yohoo15 0:0f08f98ae7f8 208 const double lowp3_a1 = -1.42439823874;
yohoo15 0:0f08f98ae7f8 209 const double lowp3_a2 = 0.74093118112;
yohoo15 0:0f08f98ae7f8 210 const double lowp3_b0 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 211 const double lowp3_b1 = 2.00000000000;
yohoo15 0:0f08f98ae7f8 212 const double lowp3_b2 = 1.00000000000;
yohoo15 0:0f08f98ae7f8 213
yohoo15 0:0f08f98ae7f8 214
yohoo15 0:0f08f98ae7f8 215
yohoo15 0:0f08f98ae7f8 216 double Filteren_left(double input)
yohoo15 0:0f08f98ae7f8 217 {
yohoo15 0:0f08f98ae7f8 218
yohoo15 0:0f08f98ae7f8 219 input = input-0.45; //FIRST SUBTRACT MEAN THEN FILTER
yohoo15 0:0f08f98ae7f8 220
yohoo15 0:0f08f98ae7f8 221 // notch filter
yohoo15 0:0f08f98ae7f8 222 double y1 = biquad(input, notch_v11, notch_v21, notch1_a1, notch1_a2, notch1_b0, notch1_b1, notch1_b2);
yohoo15 0:0f08f98ae7f8 223 double y2 = biquad(y1, notch_v12, notch_v22, notch2_a1, notch2_a2, notch2_b0, notch2_b1, notch2_b2);
yohoo15 0:0f08f98ae7f8 224 double y3 = biquad(y2, notch_v13, notch_v23, notch3_a1, notch3_a2, notch3_b0, notch3_b1, notch3_b2);
yohoo15 0:0f08f98ae7f8 225
yohoo15 0:0f08f98ae7f8 226 //higpass filter
yohoo15 0:0f08f98ae7f8 227 double y4 = biquad(y3, high_v11, high_v21, highp1_a1, highp1_a2, highp1_b0, highp1_b1, highp1_b2);
yohoo15 0:0f08f98ae7f8 228 double y5 = biquad(y4, high_v12, high_v22, highp2_a1, highp2_a2, highp2_b0, highp2_b1, highp2_b2);
yohoo15 0:0f08f98ae7f8 229 double y6 = biquad(y5, high_v13, high_v23, highp3_a1, highp3_a2, highp3_b0, highp3_b1, highp3_b2);
yohoo15 0:0f08f98ae7f8 230
yohoo15 0:0f08f98ae7f8 231 //rectivier
yohoo15 0:0f08f98ae7f8 232 double y7 = fabs(y6);
yohoo15 0:0f08f98ae7f8 233
yohoo15 0:0f08f98ae7f8 234 //lowpas filter cascade
yohoo15 0:0f08f98ae7f8 235 double y8 = biquad(y7, low_v11, low_v21, lowp1_a1, lowp1_a2, lowp1_b0, lowp1_b1, lowp1_b2);
yohoo15 0:0f08f98ae7f8 236 double y9 = biquad(y8, low_v12, low_v22, lowp2_a1, lowp2_a2, lowp2_b0, lowp2_b1, lowp2_b2);
yohoo15 0:0f08f98ae7f8 237 double y10= biquad(y9, low_v13, low_v23, lowp3_a1, lowp3_a2, lowp3_b0, lowp3_b1, lowp3_b2);
yohoo15 0:0f08f98ae7f8 238
yohoo15 0:0f08f98ae7f8 239 // moving average
yohoo15 0:0f08f98ae7f8 240 double filter_signal = moving_average(y10,n1,n2,n3,n4,n5);
yohoo15 0:0f08f98ae7f8 241
yohoo15 0:0f08f98ae7f8 242 return(filter_signal);
yohoo15 0:0f08f98ae7f8 243 }
yohoo15 0:0f08f98ae7f8 244
yohoo15 0:0f08f98ae7f8 245 double Filteren_right(double input_b)
yohoo15 0:0f08f98ae7f8 246 {
yohoo15 0:0f08f98ae7f8 247
yohoo15 0:0f08f98ae7f8 248 input_b = input_b-0.45; //FIRST SUBTRACT MEAN THEN FILTER
yohoo15 0:0f08f98ae7f8 249
yohoo15 0:0f08f98ae7f8 250 // notch filter
yohoo15 0:0f08f98ae7f8 251 double y1_b = biquad(input_b, notch_v11_b, notch_v21_b, notch1_a1, notch1_a2, notch1_b0, notch1_b1, notch1_b2);
yohoo15 0:0f08f98ae7f8 252 double y2_b = biquad(y1_b, notch_v12_b, notch_v22_b, notch2_a1, notch2_a2, notch2_b0, notch2_b1, notch2_b2);
yohoo15 0:0f08f98ae7f8 253 double y3_b = biquad(y2_b, notch_v13_b, notch_v23_b, notch3_a1, notch3_a2, notch3_b0, notch3_b1, notch3_b2);
yohoo15 0:0f08f98ae7f8 254
yohoo15 0:0f08f98ae7f8 255 //higpass filter
yohoo15 0:0f08f98ae7f8 256 double y4_b = biquad(y3_b, high_v11_b, high_v21_b, highp1_a1, highp1_a2, highp1_b0, highp1_b1, highp1_b2);
yohoo15 0:0f08f98ae7f8 257 double y5_b = biquad(y4_b, high_v12_b, high_v22_b, highp2_a1, highp2_a2, highp2_b0, highp2_b1, highp2_b2);
yohoo15 0:0f08f98ae7f8 258 double y6_b = biquad(y5_b, high_v13_b, high_v23_b, highp3_a1, highp3_a2, highp3_b0, highp3_b1, highp3_b2);
yohoo15 0:0f08f98ae7f8 259
yohoo15 0:0f08f98ae7f8 260 //rectivier
yohoo15 0:0f08f98ae7f8 261 double y7_b = fabs(y6_b);
yohoo15 0:0f08f98ae7f8 262
yohoo15 0:0f08f98ae7f8 263 //lowpas filter cascade
yohoo15 0:0f08f98ae7f8 264 double y8_b = biquad(y7_b, low_v11_b, low_v21_b, lowp1_a1, lowp1_a2, lowp1_b0, lowp1_b1, lowp1_b2);
yohoo15 0:0f08f98ae7f8 265 double y9_b = biquad(y8_b, low_v12_b, low_v22_b, lowp2_a1, lowp2_a2, lowp2_b0, lowp2_b1, lowp2_b2);
yohoo15 0:0f08f98ae7f8 266 double y10_b= biquad(y9_b, low_v13_b, low_v23_b, lowp3_a1, lowp3_a2, lowp3_b0, lowp3_b1, lowp3_b2);
yohoo15 0:0f08f98ae7f8 267
yohoo15 0:0f08f98ae7f8 268 // moving average
yohoo15 0:0f08f98ae7f8 269 double filter_signal_b = moving_average(y10_b,n1_b,n2_b,n3_b,n4_b,n5_b);
yohoo15 0:0f08f98ae7f8 270
yohoo15 0:0f08f98ae7f8 271 return(filter_signal_b);
yohoo15 0:0f08f98ae7f8 272 }
yohoo15 0:0f08f98ae7f8 273
yohoo15 2:e84adf0a960f 274
yohoo15 2:e84adf0a960f 275 /*************************************************************END Filtering **********************************************************************************************************/
yohoo15 2:e84adf0a960f 276 /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
yohoo15 2:e84adf0a960f 277 /*************************************************************BEGIN motor control*****************************************************************************************************/
yohoo15 0:0f08f98ae7f8 278 // Define pin for motor control
yohoo15 0:0f08f98ae7f8 279 DigitalOut directionPin(D4);
yohoo15 0:0f08f98ae7f8 280 PwmOut PWM(D5);
yohoo15 0:0f08f98ae7f8 281 DigitalOut directionPin_key(D7);
yohoo15 0:0f08f98ae7f8 282 PwmOut PWM_key(D6);
yohoo15 2:e84adf0a960f 283
yohoo15 2:e84adf0a960f 284 Ticker both_timer; // to make a timer to prevent loping of the key press function
yohoo15 0:0f08f98ae7f8 285 // define rotation direction and begin possition
yohoo15 0:0f08f98ae7f8 286 const int cw = 1;
yohoo15 0:0f08f98ae7f8 287 const int ccw = 0;
yohoo15 0:0f08f98ae7f8 288
yohoo15 0:0f08f98ae7f8 289 volatile bool flag_both = false;
yohoo15 0:0f08f98ae7f8 290
yohoo15 0:0f08f98ae7f8 291 // making function flags.
yohoo15 0:0f08f98ae7f8 292 void Go_flag_both()
yohoo15 0:0f08f98ae7f8 293 {
yohoo15 0:0f08f98ae7f8 294 flag_both = true;
yohoo15 0:0f08f98ae7f8 295 }
yohoo15 0:0f08f98ae7f8 296
yohoo15 0:0f08f98ae7f8 297
yohoo15 2:e84adf0a960f 298 /********* the old PI controller ***********/
yohoo15 0:0f08f98ae7f8 299
yohoo15 0:0f08f98ae7f8 300 /*
yohoo15 0:0f08f98ae7f8 301
yohoo15 2:e84adf0a960f 302 // Reusable PI controller
yohoo15 0:0f08f98ae7f8 303 double PI(double error, const double Kp, const double Ki, double Ts, double &e_int)
yohoo15 0:0f08f98ae7f8 304 {
yohoo15 0:0f08f98ae7f8 305
yohoo15 0:0f08f98ae7f8 306 e_int = e_int + Ts * error;
yohoo15 0:0f08f98ae7f8 307
yohoo15 0:0f08f98ae7f8 308 double PI_output = (Kp * error) + (Ki * e_int);
yohoo15 0:0f08f98ae7f8 309 return PI_output;
yohoo15 0:0f08f98ae7f8 310 }
yohoo15 0:0f08f98ae7f8 311 // Next task, measure the error and apply the output to the plant
yohoo15 0:0f08f98ae7f8 312
yohoo15 0:0f08f98ae7f8 313 // control for movement left right
yohoo15 0:0f08f98ae7f8 314 void motor1_Controller()
yohoo15 0:0f08f98ae7f8 315 {
yohoo15 0:0f08f98ae7f8 316 double reference = setpoint; // setpoint is in pulses
yohoo15 0:0f08f98ae7f8 317 double position = wheel.getPulses();
yohoo15 0:0f08f98ae7f8 318 double error_pulses = (reference - position); // calculate the error in pulses
yohoo15 0:0f08f98ae7f8 319 double error_rotation = error_pulses / pulses_per_revolution; //calculate how much the rotaions the motor has to turn
yohoo15 0:0f08f98ae7f8 320
yohoo15 0:0f08f98ae7f8 321 double output = abs(PI( error_rotation, motor1_Kp, motor1_Ki, M1_timestep, motor1_error_integraal ));
yohoo15 0:0f08f98ae7f8 322
yohoo15 0:0f08f98ae7f8 323 if(error_pulses > 0) {
yohoo15 0:0f08f98ae7f8 324 directionPin.write(cw);
yohoo15 0:0f08f98ae7f8 325
yohoo15 0:0f08f98ae7f8 326 } else if(error_pulses < 0) {
yohoo15 0:0f08f98ae7f8 327 directionPin.write(ccw);
yohoo15 0:0f08f98ae7f8 328 } else {
yohoo15 0:0f08f98ae7f8 329 output = 0;
yohoo15 0:0f08f98ae7f8 330 }
yohoo15 0:0f08f98ae7f8 331 PWM.write(output); // out of the if loop due to abs output
yohoo15 0:0f08f98ae7f8 332
yohoo15 0:0f08f98ae7f8 333
yohoo15 0:0f08f98ae7f8 334 }*/
yohoo15 0:0f08f98ae7f8 335
yohoo15 0:0f08f98ae7f8 336 /*************************************************************END motor control******************************************************************************************************/
yohoo15 1:267ff9a39ee4 337 /*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
yohoo15 1:267ff9a39ee4 338 /*************************************************************BEGIN switch******************************************************************************************************/
yohoo15 1:267ff9a39ee4 339 //button to press the key
yohoo15 1:267ff9a39ee4 340 DigitalIn button_only_key(PTC6);
yohoo15 1:267ff9a39ee4 341 DigitalIn button_demo(PTA4);
yohoo15 1:267ff9a39ee4 342 DigitalIn button_song(PTB9);
yohoo15 1:267ff9a39ee4 343 DigitalIn button_do_nothing(PTD1);
yohoo15 0:0f08f98ae7f8 344
yohoo15 2:e84adf0a960f 345 //int to check if the button is pressed, 0 is pressed.
yohoo15 1:267ff9a39ee4 346 const int Button_only_key_on = 0;
yohoo15 1:267ff9a39ee4 347 const int Button_demo_on = 0;
yohoo15 1:267ff9a39ee4 348 const int Button_song_on = 0;
yohoo15 1:267ff9a39ee4 349 const int Button_do_nothing_on = 0;
yohoo15 1:267ff9a39ee4 350
yohoo15 2:e84adf0a960f 351 //To say the buttons is not jet pressed.
yohoo15 1:267ff9a39ee4 352 volatile bool only_key = false;
yohoo15 1:267ff9a39ee4 353 volatile bool demo = false ;
yohoo15 1:267ff9a39ee4 354
yohoo15 1:267ff9a39ee4 355
yohoo15 1:267ff9a39ee4 356 /*************************************************************END switch******************************************************************************************************/
yohoo15 0:0f08f98ae7f8 357 void HIDScope_kijken()
yohoo15 0:0f08f98ae7f8 358 {
yohoo15 0:0f08f98ae7f8 359 scope.set(0, analog_emg_left.read());
yohoo15 0:0f08f98ae7f8 360 scope.set(1, filter_signal_hid_left);
yohoo15 0:0f08f98ae7f8 361 scope.set(2, analog_emg_right.read());
yohoo15 0:0f08f98ae7f8 362 scope.set(3, filter_signal_hid_right);
yohoo15 0:0f08f98ae7f8 363 scope.send();
yohoo15 0:0f08f98ae7f8 364 }
yohoo15 0:0f08f98ae7f8 365
yohoo15 0:0f08f98ae7f8 366
yohoo15 0:0f08f98ae7f8 367 int main()
yohoo15 0:0f08f98ae7f8 368 {
yohoo15 0:0f08f98ae7f8 369 pc.printf("at the begin, \n");
yohoo15 0:0f08f98ae7f8 370
yohoo15 0:0f08f98ae7f8 371 HIDScope_timer.attach(&Go_flag_HIDScope, 0.02);//0.02 had worked
yohoo15 0:0f08f98ae7f8 372 Filteren_timer.attach(&Go_flag_filteren,0.04);// 0.04 had worked
yohoo15 0:0f08f98ae7f8 373 both_timer.attach(&Go_flag_both, 2);
yohoo15 0:0f08f98ae7f8 374 while(1) {
yohoo15 0:0f08f98ae7f8 375
yohoo15 2:e84adf0a960f 376 /********************* Code for starting filtering ****************/
yohoo15 0:0f08f98ae7f8 377 if(Flag_filteren) {
yohoo15 0:0f08f98ae7f8 378 Flag_filteren = false;
yohoo15 0:0f08f98ae7f8 379 // filter left and set bool
yohoo15 0:0f08f98ae7f8 380 filter_signal_hid_left = Filteren_left(analog_emg_left.read());
yohoo15 0:0f08f98ae7f8 381
yohoo15 0:0f08f98ae7f8 382 if (filter_signal_hid_left > high_threshold) {
yohoo15 0:0f08f98ae7f8 383 left_movement = true;
yohoo15 0:0f08f98ae7f8 384 } else if (filter_signal_hid_left < low_threshold) {
yohoo15 0:0f08f98ae7f8 385 left_movement = false;
yohoo15 0:0f08f98ae7f8 386 }
yohoo15 0:0f08f98ae7f8 387 // make filter right and set bool
yohoo15 0:0f08f98ae7f8 388 filter_signal_hid_right = Filteren_right(analog_emg_right.read());
yohoo15 0:0f08f98ae7f8 389
yohoo15 0:0f08f98ae7f8 390 if (filter_signal_hid_right > high_threshold) {
yohoo15 0:0f08f98ae7f8 391 right_movement = true;
yohoo15 0:0f08f98ae7f8 392 } else if (filter_signal_hid_right < low_threshold) {
yohoo15 0:0f08f98ae7f8 393 right_movement = false;
yohoo15 0:0f08f98ae7f8 394 }
yohoo15 0:0f08f98ae7f8 395
yohoo15 0:0f08f98ae7f8 396
yohoo15 0:0f08f98ae7f8 397 }
yohoo15 0:0f08f98ae7f8 398
yohoo15 0:0f08f98ae7f8 399
yohoo15 2:e84adf0a960f 400 /********************* Code to look how the EMG/ filtered EMG signal looks like on hidsope ****************/
yohoo15 0:0f08f98ae7f8 401 if(Flag_HIDScope) {
yohoo15 0:0f08f98ae7f8 402 Flag_HIDScope = false;
yohoo15 0:0f08f98ae7f8 403 HIDScope_kijken();
yohoo15 0:0f08f98ae7f8 404
yohoo15 0:0f08f98ae7f8 405 }
yohoo15 0:0f08f98ae7f8 406
yohoo15 2:e84adf0a960f 407 /********************* Code to switch between modes (demo = left/right and keypress are active; only key = only the key press is active ****************/
yohoo15 0:0f08f98ae7f8 408
yohoo15 1:267ff9a39ee4 409 if (button_only_key.read() == Button_only_key_on){
yohoo15 1:267ff9a39ee4 410 demo = false;
yohoo15 1:267ff9a39ee4 411 only_key = true;
yohoo15 1:267ff9a39ee4 412 pc.printf("only key /n");
yohoo15 1:267ff9a39ee4 413 }
yohoo15 1:267ff9a39ee4 414
yohoo15 1:267ff9a39ee4 415
yohoo15 1:267ff9a39ee4 416 if (button_demo.read() == Button_demo_on){
yohoo15 1:267ff9a39ee4 417 only_key = false;
yohoo15 1:267ff9a39ee4 418 demo = true;
yohoo15 1:267ff9a39ee4 419 pc.printf("demo /n");
yohoo15 1:267ff9a39ee4 420 }
yohoo15 1:267ff9a39ee4 421
yohoo15 2:e84adf0a960f 422 /********************* Code Demo ****************/
yohoo15 1:267ff9a39ee4 423 if(demo){
yohoo15 0:0f08f98ae7f8 424 // Pussing buttons to get input signal
yohoo15 0:0f08f98ae7f8 425 if( left_movement and right_movement == false) {
yohoo15 0:0f08f98ae7f8 426 directionPin.write(cw);
yohoo15 0:0f08f98ae7f8 427 PWM.write(1);
yohoo15 0:0f08f98ae7f8 428
yohoo15 0:0f08f98ae7f8 429 } else if(right_movement and left_movement == false ) {
yohoo15 0:0f08f98ae7f8 430 directionPin.write(ccw);
yohoo15 0:0f08f98ae7f8 431 PWM.write(1);
yohoo15 0:0f08f98ae7f8 432 } else {
yohoo15 0:0f08f98ae7f8 433 PWM.write(0);
yohoo15 0:0f08f98ae7f8 434 }
yohoo15 0:0f08f98ae7f8 435
yohoo15 0:0f08f98ae7f8 436 if( left_movement and right_movement and flag_both){
yohoo15 0:0f08f98ae7f8 437 directionPin_key.write(cw);
yohoo15 0:0f08f98ae7f8 438 PWM_key.write(1);
yohoo15 2:e84adf0a960f 439 wait(0.05);
yohoo15 0:0f08f98ae7f8 440 PWM_key.write(0);
yohoo15 2:e84adf0a960f 441 wait(0.3);
yohoo15 0:0f08f98ae7f8 442 directionPin_key.write(ccw);
yohoo15 0:0f08f98ae7f8 443 PWM_key.write(1);
yohoo15 2:e84adf0a960f 444 wait(0.039f);
yohoo15 0:0f08f98ae7f8 445 PWM_key.write(0);
yohoo15 0:0f08f98ae7f8 446 flag_both = false;
yohoo15 0:0f08f98ae7f8 447 }
yohoo15 0:0f08f98ae7f8 448 else{
yohoo15 0:0f08f98ae7f8 449 PWM_key.write(0);
yohoo15 0:0f08f98ae7f8 450 }
yohoo15 1:267ff9a39ee4 451 }
yohoo15 1:267ff9a39ee4 452
yohoo15 2:e84adf0a960f 453 /********************* Code only key ****************/
yohoo15 2:e84adf0a960f 454
yohoo15 1:267ff9a39ee4 455 if(only_key){
yohoo15 1:267ff9a39ee4 456
yohoo15 1:267ff9a39ee4 457 if(left_movement and flag_both){
yohoo15 1:267ff9a39ee4 458 directionPin_key.write(cw);
yohoo15 1:267ff9a39ee4 459 PWM_key.write(1);
yohoo15 2:e84adf0a960f 460 wait(0.05);
yohoo15 1:267ff9a39ee4 461 PWM_key.write(0);
yohoo15 2:e84adf0a960f 462 wait(0.3);
yohoo15 1:267ff9a39ee4 463 directionPin_key.write(ccw);
yohoo15 1:267ff9a39ee4 464 PWM_key.write(1);
yohoo15 2:e84adf0a960f 465 wait(0.039f);
yohoo15 1:267ff9a39ee4 466 PWM_key.write(0);
yohoo15 1:267ff9a39ee4 467 flag_both = false;
yohoo15 1:267ff9a39ee4 468
yohoo15 1:267ff9a39ee4 469 }
yohoo15 1:267ff9a39ee4 470 }
yohoo15 2:e84adf0a960f 471 /********************* Code to kill the emg controll to set the system at 'stand by'****************/
yohoo15 1:267ff9a39ee4 472 if (button_do_nothing.read() == Button_do_nothing_on){
yohoo15 2:e84adf0a960f 473 demo = false;
yohoo15 2:e84adf0a960f 474 only_key = false;
yohoo15 1:267ff9a39ee4 475 }
yohoo15 2:e84adf0a960f 476 /********************* Code with pre programed feedforward commands to play the first bit of the dutch song 'Vader Jacob'. ****************/
yohoo15 1:267ff9a39ee4 477 if (button_song.read() == Button_song_on){
yohoo15 1:267ff9a39ee4 478
yohoo15 1:267ff9a39ee4 479 //pressing va
yohoo15 2:e84adf0a960f 480 directionPin_key.write(cw);
yohoo15 2:e84adf0a960f 481 PWM_key.write(1);
yohoo15 2:e84adf0a960f 482 wait(0.05);
yohoo15 2:e84adf0a960f 483 PWM_key.write(0);
yohoo15 2:e84adf0a960f 484 wait(0.3);
yohoo15 2:e84adf0a960f 485 directionPin_key.write(ccw);
yohoo15 2:e84adf0a960f 486 PWM_key.write(1);
yohoo15 2:e84adf0a960f 487 wait(0.039f);
yohoo15 2:e84adf0a960f 488 PWM_key.write(0);
yohoo15 1:267ff9a39ee4 489
yohoo15 1:267ff9a39ee4 490 // go 1 key to right
yohoo15 1:267ff9a39ee4 491 directionPin.write(cw);
yohoo15 1:267ff9a39ee4 492 PWM.write(1);
yohoo15 1:267ff9a39ee4 493 wait(2.2);
yohoo15 1:267ff9a39ee4 494 PWM.write(0);
yohoo15 1:267ff9a39ee4 495
yohoo15 1:267ff9a39ee4 496 //pressing der
yohoo15 1:267ff9a39ee4 497 directionPin_key.write(cw);
yohoo15 2:e84adf0a960f 498 PWM_key.write(1);
yohoo15 2:e84adf0a960f 499 wait(0.05);
yohoo15 2:e84adf0a960f 500 PWM_key.write(0);
yohoo15 2:e84adf0a960f 501 wait(0.3);
yohoo15 2:e84adf0a960f 502 directionPin_key.write(ccw);
yohoo15 2:e84adf0a960f 503 PWM_key.write(1);
yohoo15 2:e84adf0a960f 504 wait(0.039f);
yohoo15 2:e84adf0a960f 505 PWM_key.write(0);
yohoo15 1:267ff9a39ee4 506
yohoo15 1:267ff9a39ee4 507 // go 1 key to right
yohoo15 1:267ff9a39ee4 508 directionPin.write(cw);
yohoo15 1:267ff9a39ee4 509 PWM.write(1);
yohoo15 1:267ff9a39ee4 510 wait(2.2);
yohoo15 1:267ff9a39ee4 511 PWM.write(0);
yohoo15 1:267ff9a39ee4 512
yohoo15 1:267ff9a39ee4 513 //pressing ja
yohoo15 2:e84adf0a960f 514 directionPin_key.write(cw);
yohoo15 2:e84adf0a960f 515 PWM_key.write(1);
yohoo15 2:e84adf0a960f 516 wait(0.05);
yohoo15 2:e84adf0a960f 517 PWM_key.write(0);
yohoo15 2:e84adf0a960f 518 wait(0.3);
yohoo15 2:e84adf0a960f 519 directionPin_key.write(ccw);
yohoo15 2:e84adf0a960f 520 PWM_key.write(1);
yohoo15 2:e84adf0a960f 521 wait(0.039f);
yohoo15 2:e84adf0a960f 522 PWM_key.write(0);
yohoo15 2:e84adf0a960f 523
yohoo15 1:267ff9a39ee4 524 // go 2 key to left
yohoo15 1:267ff9a39ee4 525 directionPin.write(ccw);
yohoo15 1:267ff9a39ee4 526 PWM.write(1);
yohoo15 1:267ff9a39ee4 527 wait(4.5);
yohoo15 1:267ff9a39ee4 528 PWM.write(0);
yohoo15 1:267ff9a39ee4 529
yohoo15 1:267ff9a39ee4 530 //pressing cob
yohoo15 1:267ff9a39ee4 531 directionPin_key.write(cw);
yohoo15 2:e84adf0a960f 532 PWM_key.write(1);
yohoo15 2:e84adf0a960f 533 wait(0.05);
yohoo15 2:e84adf0a960f 534 PWM_key.write(0);
yohoo15 2:e84adf0a960f 535 wait(0.3);
yohoo15 2:e84adf0a960f 536 directionPin_key.write(ccw);
yohoo15 2:e84adf0a960f 537 PWM_key.write(1);
yohoo15 2:e84adf0a960f 538 wait(0.039f);
yohoo15 2:e84adf0a960f 539 PWM_key.write(0);
yohoo15 1:267ff9a39ee4 540
yohoo15 2:e84adf0a960f 541 wait(0.5);
yohoo15 1:267ff9a39ee4 542
yohoo15 1:267ff9a39ee4 543 //pressing va
yohoo15 2:e84adf0a960f 544 directionPin_key.write(cw);
yohoo15 2:e84adf0a960f 545 PWM_key.write(1);
yohoo15 2:e84adf0a960f 546 wait(0.05);
yohoo15 2:e84adf0a960f 547 PWM_key.write(0);
yohoo15 2:e84adf0a960f 548 wait(0.3);
yohoo15 2:e84adf0a960f 549 directionPin_key.write(ccw);
yohoo15 2:e84adf0a960f 550 PWM_key.write(1);
yohoo15 2:e84adf0a960f 551 wait(0.039f);
yohoo15 2:e84adf0a960f 552 PWM_key.write(0);
yohoo15 2:e84adf0a960f 553
yohoo15 1:267ff9a39ee4 554 // go 1 key to right
yohoo15 1:267ff9a39ee4 555 directionPin.write(cw);
yohoo15 1:267ff9a39ee4 556 PWM.write(1);
yohoo15 1:267ff9a39ee4 557 wait(2.2);
yohoo15 1:267ff9a39ee4 558 PWM.write(0);
yohoo15 1:267ff9a39ee4 559
yohoo15 1:267ff9a39ee4 560 //pressing der
yohoo15 2:e84adf0a960f 561 directionPin_key.write(cw);
yohoo15 2:e84adf0a960f 562 PWM_key.write(1);
yohoo15 2:e84adf0a960f 563 wait(0.05);
yohoo15 2:e84adf0a960f 564 PWM_key.write(0);
yohoo15 2:e84adf0a960f 565 wait(0.3);
yohoo15 2:e84adf0a960f 566 directionPin_key.write(ccw);
yohoo15 2:e84adf0a960f 567 PWM_key.write(1);
yohoo15 2:e84adf0a960f 568 wait(0.039f);
yohoo15 2:e84adf0a960f 569 PWM_key.write(0);
yohoo15 2:e84adf0a960f 570
yohoo15 1:267ff9a39ee4 571 // go 1 key to right
yohoo15 1:267ff9a39ee4 572 directionPin.write(cw);
yohoo15 1:267ff9a39ee4 573 PWM.write(1);
yohoo15 1:267ff9a39ee4 574 wait(2.2);
yohoo15 1:267ff9a39ee4 575 PWM.write(0);
yohoo15 1:267ff9a39ee4 576
yohoo15 1:267ff9a39ee4 577 //pressing ja
yohoo15 2:e84adf0a960f 578 directionPin_key.write(cw);
yohoo15 2:e84adf0a960f 579 PWM_key.write(1);
yohoo15 2:e84adf0a960f 580 wait(0.05);
yohoo15 2:e84adf0a960f 581 PWM_key.write(0);
yohoo15 2:e84adf0a960f 582 wait(0.3);
yohoo15 2:e84adf0a960f 583 directionPin_key.write(ccw);
yohoo15 2:e84adf0a960f 584 PWM_key.write(1);
yohoo15 2:e84adf0a960f 585 wait(0.039f);
yohoo15 2:e84adf0a960f 586 PWM_key.write(0);
yohoo15 1:267ff9a39ee4 587
yohoo15 1:267ff9a39ee4 588 // go 2 key to left
yohoo15 1:267ff9a39ee4 589 directionPin.write(ccw);
yohoo15 1:267ff9a39ee4 590 PWM.write(1);
yohoo15 1:267ff9a39ee4 591 wait(4.5);
yohoo15 1:267ff9a39ee4 592 PWM.write(0);
yohoo15 1:267ff9a39ee4 593
yohoo15 1:267ff9a39ee4 594 //pressing cob
yohoo15 2:e84adf0a960f 595 directionPin_key.write(cw);
yohoo15 2:e84adf0a960f 596 PWM_key.write(1);
yohoo15 2:e84adf0a960f 597 wait(0.05);
yohoo15 2:e84adf0a960f 598 PWM_key.write(0);
yohoo15 2:e84adf0a960f 599 wait(0.3);
yohoo15 2:e84adf0a960f 600 directionPin_key.write(ccw);
yohoo15 2:e84adf0a960f 601 PWM_key.write(1);
yohoo15 2:e84adf0a960f 602 wait(0.039f);
yohoo15 2:e84adf0a960f 603 PWM_key.write(0);
yohoo15 1:267ff9a39ee4 604 }
yohoo15 0:0f08f98ae7f8 605 }
yohoo15 0:0f08f98ae7f8 606 }
yohoo15 0:0f08f98ae7f8 607