Biorobotics / Mbed 2 deprecated EMG_MOTOR_LEFT_RIGHT

Dependencies:   HIDScope QEI mbed

Committer:
yohoo15
Date:
Wed Oct 28 11:22:08 2015 +0000
Revision:
1:cfcd4825fe81
Parent:
0:6f71e969a0b5
Child:
2:1cd28768a9bf
something;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yohoo15 0:6f71e969a0b5 1 #include "mbed.h"
yohoo15 0:6f71e969a0b5 2 //#include "read_filter_emg.h"
yohoo15 0:6f71e969a0b5 3 //included for fabs() function
yohoo15 0:6f71e969a0b5 4 #include <math.h>
yohoo15 0:6f71e969a0b5 5 #include "HIDScope.h"
yohoo15 0:6f71e969a0b5 6 #include <iostream>
yohoo15 0:6f71e969a0b5 7 #include "QEI.h"
yohoo15 0:6f71e969a0b5 8
yohoo15 1:cfcd4825fe81 9 //DigitalOut led_green(LED_GREEN);
yohoo15 1:cfcd4825fe81 10 //DigitalOut led_red(LED_RED);
yohoo15 0:6f71e969a0b5 11
yohoo15 0:6f71e969a0b5 12 Serial pc(USBTX, USBRX);
yohoo15 1:cfcd4825fe81 13 AnalogIn analog_emg_left(A0);
yohoo15 1:cfcd4825fe81 14 AnalogIn analog_emg_right(A1);
yohoo15 0:6f71e969a0b5 15
yohoo15 0:6f71e969a0b5 16 Ticker HIDScope_timer;
yohoo15 0:6f71e969a0b5 17 Ticker Filteren_timer;
yohoo15 0:6f71e969a0b5 18 Ticker aansturen;
yohoo15 0:6f71e969a0b5 19
yohoo15 0:6f71e969a0b5 20 HIDScope scope(4);
yohoo15 0:6f71e969a0b5 21
yohoo15 0:6f71e969a0b5 22 // defining flags
yohoo15 0:6f71e969a0b5 23 volatile bool Flag_filteren = false;
yohoo15 0:6f71e969a0b5 24 volatile bool Flag_HIDScope = false;
yohoo15 0:6f71e969a0b5 25 volatile bool left_movement = false;
yohoo15 0:6f71e969a0b5 26 volatile bool right_movement = false;
yohoo15 0:6f71e969a0b5 27
yohoo15 0:6f71e969a0b5 28 // making function flags.
yohoo15 0:6f71e969a0b5 29 void Go_flag_filteren()
yohoo15 0:6f71e969a0b5 30 {
yohoo15 0:6f71e969a0b5 31 Flag_filteren = true;
yohoo15 0:6f71e969a0b5 32 }
yohoo15 0:6f71e969a0b5 33
yohoo15 0:6f71e969a0b5 34 void Go_flag_HIDScope()
yohoo15 0:6f71e969a0b5 35 {
yohoo15 0:6f71e969a0b5 36 Flag_HIDScope = true;
yohoo15 0:6f71e969a0b5 37 }
yohoo15 0:6f71e969a0b5 38
yohoo15 1:cfcd4825fe81 39
yohoo15 0:6f71e969a0b5 40
yohoo15 0:6f71e969a0b5 41 double input_left = 0 ;
yohoo15 0:6f71e969a0b5 42 double input_right = 0 ;
yohoo15 0:6f71e969a0b5 43 //double input = 0;
yohoo15 0:6f71e969a0b5 44 double filter_signal_hid_left = 0;
yohoo15 0:6f71e969a0b5 45 double filter_signal_hid_right = 0;
yohoo15 0:6f71e969a0b5 46 //double input_right = 0;
yohoo15 0:6f71e969a0b5 47 // defining threshold
yohoo15 1:cfcd4825fe81 48 double high_threshold = 1400;
yohoo15 1:cfcd4825fe81 49 double low_threshold = 700;
yohoo15 0:6f71e969a0b5 50
yohoo15 0:6f71e969a0b5 51 //*** making the v for everything and conquer the world***
yohoo15 0:6f71e969a0b5 52
yohoo15 1:cfcd4825fe81 53 //for left
yohoo15 0:6f71e969a0b5 54 //for Notchfilter
yohoo15 0:6f71e969a0b5 55 double notch_v11=0;
yohoo15 0:6f71e969a0b5 56 double notch_v21=0;
yohoo15 0:6f71e969a0b5 57 double notch_v12=0;
yohoo15 0:6f71e969a0b5 58 double notch_v22=0;
yohoo15 0:6f71e969a0b5 59 double notch_v13=0;
yohoo15 0:6f71e969a0b5 60 double notch_v23=0;
yohoo15 0:6f71e969a0b5 61
yohoo15 0:6f71e969a0b5 62 //for highpass filter
yohoo15 0:6f71e969a0b5 63 double high_v11=0;
yohoo15 0:6f71e969a0b5 64 double high_v21=0;
yohoo15 0:6f71e969a0b5 65 double high_v12=0;
yohoo15 0:6f71e969a0b5 66 double high_v22=0;
yohoo15 0:6f71e969a0b5 67 double high_v13=0;
yohoo15 0:6f71e969a0b5 68 double high_v23=0;
yohoo15 0:6f71e969a0b5 69
yohoo15 0:6f71e969a0b5 70 // for lowpasfilter
yohoo15 0:6f71e969a0b5 71 double low_v11=0;
yohoo15 0:6f71e969a0b5 72 double low_v21=0;
yohoo15 0:6f71e969a0b5 73 double low_v12=0;
yohoo15 0:6f71e969a0b5 74 double low_v22=0;
yohoo15 0:6f71e969a0b5 75 double low_v13=0;
yohoo15 0:6f71e969a0b5 76 double low_v23=0;
yohoo15 0:6f71e969a0b5 77
yohoo15 0:6f71e969a0b5 78 // for moving average
yohoo15 0:6f71e969a0b5 79 double n1 = 0;
yohoo15 0:6f71e969a0b5 80 double n2 = 0;
yohoo15 0:6f71e969a0b5 81 double n3 = 0;
yohoo15 0:6f71e969a0b5 82 double n4 = 0;
yohoo15 0:6f71e969a0b5 83 double n5 = 0;
yohoo15 0:6f71e969a0b5 84
yohoo15 1:cfcd4825fe81 85 // for right
yohoo15 1:cfcd4825fe81 86 //for Notchfilter
yohoo15 1:cfcd4825fe81 87 double notch_v11_b=0;
yohoo15 1:cfcd4825fe81 88 double notch_v21_b=0;
yohoo15 1:cfcd4825fe81 89 double notch_v12_b=0;
yohoo15 1:cfcd4825fe81 90 double notch_v22_b=0;
yohoo15 1:cfcd4825fe81 91 double notch_v13_b=0;
yohoo15 1:cfcd4825fe81 92 double notch_v23_b=0;
yohoo15 1:cfcd4825fe81 93
yohoo15 1:cfcd4825fe81 94 //for highpass filter
yohoo15 1:cfcd4825fe81 95 double high_v11_b=0;
yohoo15 1:cfcd4825fe81 96 double high_v21_b=0;
yohoo15 1:cfcd4825fe81 97 double high_v12_b=0;
yohoo15 1:cfcd4825fe81 98 double high_v22_b=0;
yohoo15 1:cfcd4825fe81 99 double high_v13_b=0;
yohoo15 1:cfcd4825fe81 100 double high_v23_b=0;
yohoo15 1:cfcd4825fe81 101
yohoo15 1:cfcd4825fe81 102 // for lowpasfilter
yohoo15 1:cfcd4825fe81 103 double low_v11_b=0;
yohoo15 1:cfcd4825fe81 104 double low_v21_b=0;
yohoo15 1:cfcd4825fe81 105 double low_v12_b=0;
yohoo15 1:cfcd4825fe81 106 double low_v22_b=0;
yohoo15 1:cfcd4825fe81 107 double low_v13_b=0;
yohoo15 1:cfcd4825fe81 108 double low_v23_b=0;
yohoo15 1:cfcd4825fe81 109
yohoo15 1:cfcd4825fe81 110 // for moving average
yohoo15 1:cfcd4825fe81 111 double n1_b = 0;
yohoo15 1:cfcd4825fe81 112 double n2_b = 0;
yohoo15 1:cfcd4825fe81 113 double n3_b = 0;
yohoo15 1:cfcd4825fe81 114 double n4_b = 0;
yohoo15 1:cfcd4825fe81 115 double n5_b = 0;
yohoo15 1:cfcd4825fe81 116
yohoo15 1:cfcd4825fe81 117
yohoo15 0:6f71e969a0b5 118 double filter_left;
yohoo15 0:6f71e969a0b5 119 double filter_right;
yohoo15 0:6f71e969a0b5 120
yohoo15 0:6f71e969a0b5 121 //general biquad filter that can be called in all the filter functions
yohoo15 0:6f71e969a0b5 122 double biquad(double u, double &v1, double &v2, const double a1,
yohoo15 0:6f71e969a0b5 123 const double a2, const double b0, const double b1, const double b2)
yohoo15 0:6f71e969a0b5 124 {
yohoo15 0:6f71e969a0b5 125 double v = u - a1*v1 - a2*v2;
yohoo15 0:6f71e969a0b5 126 double y = b0*v + b1*v1 + b2*v2;
yohoo15 0:6f71e969a0b5 127 //values of v2 and v1 are updated, as they are passed by reference
yohoo15 0:6f71e969a0b5 128 //they update globally
yohoo15 0:6f71e969a0b5 129 v2 = v1;
yohoo15 0:6f71e969a0b5 130 v1 = v;
yohoo15 0:6f71e969a0b5 131 return y;
yohoo15 0:6f71e969a0b5 132 }
yohoo15 0:6f71e969a0b5 133
yohoo15 0:6f71e969a0b5 134 double moving_average(double y, double &n1, double &n2, double &n3, double &n4, double &n5)
yohoo15 0:6f71e969a0b5 135 {
yohoo15 0:6f71e969a0b5 136 double average = (y + n1 + n2 +n3 + n4 + n5)/5;
yohoo15 0:6f71e969a0b5 137 n5 = n4;
yohoo15 0:6f71e969a0b5 138 n4 = n3;
yohoo15 0:6f71e969a0b5 139 n3 = n2;
yohoo15 0:6f71e969a0b5 140 n2 = n1;
yohoo15 0:6f71e969a0b5 141 n1 = y;
yohoo15 0:6f71e969a0b5 142
yohoo15 0:6f71e969a0b5 143 return average;
yohoo15 0:6f71e969a0b5 144 }
yohoo15 0:6f71e969a0b5 145 /*
yohoo15 0:6f71e969a0b5 146 double threshold(double signal, const double lowtreshold, const double hightreshold)
yohoo15 0:6f71e969a0b5 147 {
yohoo15 0:6f71e969a0b5 148 if (signal > hightreshold)
yohoo15 0:6f71e969a0b5 149 left = true;
yohoo15 0:6f71e969a0b5 150 else if (signal <lowtreshold)
yohoo15 0:6f71e969a0b5 151 left = false;
yohoo15 0:6f71e969a0b5 152 }
yohoo15 0:6f71e969a0b5 153 */
yohoo15 0:6f71e969a0b5 154 //Specifying filter coefficients highpass
yohoo15 0:6f71e969a0b5 155
yohoo15 0:6f71e969a0b5 156 /* notch filter with 3 cascaded biquads*/
yohoo15 0:6f71e969a0b5 157 //first notch biquad
yohoo15 0:6f71e969a0b5 158 const double notch1_a1 = -1.55951422433;
yohoo15 0:6f71e969a0b5 159 const double notch1_a2 = 0.92705680308;
yohoo15 0:6f71e969a0b5 160 const double notch1_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 161 const double notch1_b1 = -1.61854515325;
yohoo15 0:6f71e969a0b5 162 const double notch1_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 163
yohoo15 0:6f71e969a0b5 164 //second notch biquad
yohoo15 0:6f71e969a0b5 165 const double notch2_a1 = -1.54767435801;
yohoo15 0:6f71e969a0b5 166 const double notch2_a2 = 0.96124842048;
yohoo15 0:6f71e969a0b5 167 const double notch2_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 168 const double notch2_b1 = -1.61854515325;
yohoo15 0:6f71e969a0b5 169 const double notch2_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 170
yohoo15 0:6f71e969a0b5 171 //third notch biquad
yohoo15 0:6f71e969a0b5 172 const double notch3_a1 = -1.62600366964;
yohoo15 0:6f71e969a0b5 173 const double notch3_a2 = 0.96453460373;
yohoo15 0:6f71e969a0b5 174 const double notch3_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 175 const double notch3_b1 = -1.61854515325;
yohoo15 0:6f71e969a0b5 176 const double notch3_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 177
yohoo15 0:6f71e969a0b5 178 /* high pass filter consists of three cascaded biquads
yohoo15 0:6f71e969a0b5 179 blow coefficients for those three biquads */
yohoo15 0:6f71e969a0b5 180 //first high pass biquad
yohoo15 0:6f71e969a0b5 181 const double highp1_a1 = -0.67538034389;
yohoo15 0:6f71e969a0b5 182 const double highp1_a2 = 0.12769255668;
yohoo15 0:6f71e969a0b5 183 const double highp1_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 184 const double highp1_b1 = -2.00000000000;
yohoo15 0:6f71e969a0b5 185 const double highp1_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 186
yohoo15 0:6f71e969a0b5 187 //second high pass biquad
yohoo15 0:6f71e969a0b5 188 const double highp2_a1 = -0.76475499450;
yohoo15 0:6f71e969a0b5 189 const double highp2_a2 = 0.27692273367;
yohoo15 0:6f71e969a0b5 190 const double highp2_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 191 const double highp2_b1 = -2.00000000000;
yohoo15 0:6f71e969a0b5 192 const double highp2_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 193
yohoo15 0:6f71e969a0b5 194 //third high pass biquad
yohoo15 0:6f71e969a0b5 195 const double highp3_a1 = -0.99216561242;
yohoo15 0:6f71e969a0b5 196 const double highp3_a2 = 0.65663360837;
yohoo15 0:6f71e969a0b5 197 const double highp3_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 198 const double highp3_b1 = -2.00000000000;
yohoo15 0:6f71e969a0b5 199 const double highp3_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 200
yohoo15 0:6f71e969a0b5 201 /* lowpass filter consists of three cascaded biquads
yohoo15 0:6f71e969a0b5 202 below the coefficients for those three biquads */
yohoo15 0:6f71e969a0b5 203 //first low pass biquad
yohoo15 0:6f71e969a0b5 204 const double lowp1_a1 = -1.05207469728;
yohoo15 0:6f71e969a0b5 205 const double lowp1_a2 = 0.28586907478;
yohoo15 0:6f71e969a0b5 206 const double lowp1_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 207 const double lowp1_b1 = 2.00000000000;
yohoo15 0:6f71e969a0b5 208 const double lowp1_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 209
yohoo15 0:6f71e969a0b5 210 //second low pass biquad
yohoo15 0:6f71e969a0b5 211 const double lowp2_a1 = -1.16338171052;
yohoo15 0:6f71e969a0b5 212 const double lowp2_a2 = 0.42191097989;
yohoo15 0:6f71e969a0b5 213 const double lowp2_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 214 const double lowp2_b1 = 2.00000000000;
yohoo15 0:6f71e969a0b5 215 const double lowp2_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 216
yohoo15 0:6f71e969a0b5 217 //third low pass biquad
yohoo15 0:6f71e969a0b5 218 const double lowp3_a1 = -1.42439823874;
yohoo15 0:6f71e969a0b5 219 const double lowp3_a2 = 0.74093118112;
yohoo15 0:6f71e969a0b5 220 const double lowp3_b0 = 1.00000000000;
yohoo15 0:6f71e969a0b5 221 const double lowp3_b1 = 2.00000000000;
yohoo15 0:6f71e969a0b5 222 const double lowp3_b2 = 1.00000000000;
yohoo15 0:6f71e969a0b5 223
yohoo15 0:6f71e969a0b5 224
yohoo15 0:6f71e969a0b5 225
yohoo15 1:cfcd4825fe81 226 double Filteren_left(double input)
yohoo15 0:6f71e969a0b5 227 {
yohoo15 0:6f71e969a0b5 228
yohoo15 0:6f71e969a0b5 229 input = input-0.45; //FIRST SUBTRACT MEAN THEN FILTER
yohoo15 0:6f71e969a0b5 230 //input_right = analog_emg_right.read();
yohoo15 0:6f71e969a0b5 231
yohoo15 0:6f71e969a0b5 232 // notch filter
yohoo15 0:6f71e969a0b5 233 double y1 = biquad(input, notch_v11, notch_v21, notch1_a1, notch1_a2, notch1_b0, notch1_b1, notch1_b2);
yohoo15 0:6f71e969a0b5 234 double y2 = biquad(y1, notch_v12, notch_v22, notch2_a1, notch2_a2, notch2_b0, notch2_b1, notch2_b2);
yohoo15 0:6f71e969a0b5 235 double y3 = biquad(y2, notch_v13, notch_v23, notch3_a1, notch3_a2, notch3_b0, notch3_b1, notch3_b2);
yohoo15 0:6f71e969a0b5 236
yohoo15 0:6f71e969a0b5 237 //higpass filter
yohoo15 0:6f71e969a0b5 238 double y4 = biquad(y3, high_v11, high_v21, highp1_a1, highp1_a2, highp1_b0, highp1_b1, highp1_b2);
yohoo15 0:6f71e969a0b5 239 double y5 = biquad(y4, high_v12, high_v22, highp2_a1, highp2_a2, highp2_b0, highp2_b1, highp2_b2);
yohoo15 0:6f71e969a0b5 240 double y6 = biquad(y5, high_v13, high_v23, highp3_a1, highp3_a2, highp3_b0, highp3_b1, highp3_b2);
yohoo15 0:6f71e969a0b5 241
yohoo15 0:6f71e969a0b5 242 //rectivier
yohoo15 0:6f71e969a0b5 243 double y7 = fabs(y6);
yohoo15 0:6f71e969a0b5 244
yohoo15 0:6f71e969a0b5 245 //lowpas filter cascade
yohoo15 0:6f71e969a0b5 246 double y8 = biquad(y7, low_v11, low_v21, lowp1_a1, lowp1_a2, lowp1_b0, lowp1_b1, lowp1_b2);
yohoo15 0:6f71e969a0b5 247 double y9 = biquad(y8, low_v12, low_v22, lowp2_a1, lowp2_a2, lowp2_b0, lowp2_b1, lowp2_b2);
yohoo15 0:6f71e969a0b5 248 double y10= biquad(y9, low_v13, low_v23, lowp3_a1, lowp3_a2, lowp3_b0, lowp3_b1, lowp3_b2);
yohoo15 0:6f71e969a0b5 249
yohoo15 0:6f71e969a0b5 250 // moving average
yohoo15 0:6f71e969a0b5 251 double filter_signal = moving_average(y10,n1,n2,n3,n4,n5);
yohoo15 1:cfcd4825fe81 252
yohoo15 0:6f71e969a0b5 253 return(filter_signal);
yohoo15 0:6f71e969a0b5 254 }
yohoo15 0:6f71e969a0b5 255
yohoo15 1:cfcd4825fe81 256 double Filteren_right(double input_b)
yohoo15 1:cfcd4825fe81 257 {
yohoo15 1:cfcd4825fe81 258
yohoo15 1:cfcd4825fe81 259 input_b = input_b-0.45; //FIRST SUBTRACT MEAN THEN FILTER
yohoo15 1:cfcd4825fe81 260 //input_right = analog_emg_right.read();
yohoo15 1:cfcd4825fe81 261
yohoo15 1:cfcd4825fe81 262 // notch filter
yohoo15 1:cfcd4825fe81 263 double y1_b = biquad(input_b, notch_v11_b, notch_v21_b, notch1_a1, notch1_a2, notch1_b0, notch1_b1, notch1_b2);
yohoo15 1:cfcd4825fe81 264 double y2_b = biquad(y1_b, notch_v12_b, notch_v22_b, notch2_a1, notch2_a2, notch2_b0, notch2_b1, notch2_b2);
yohoo15 1:cfcd4825fe81 265 double y3_b = biquad(y2_b, notch_v13_b, notch_v23_b, notch3_a1, notch3_a2, notch3_b0, notch3_b1, notch3_b2);
yohoo15 1:cfcd4825fe81 266
yohoo15 1:cfcd4825fe81 267 //higpass filter
yohoo15 1:cfcd4825fe81 268 double y4_b = biquad(y3_b, high_v11_b, high_v21_b, highp1_a1, highp1_a2, highp1_b0, highp1_b1, highp1_b2);
yohoo15 1:cfcd4825fe81 269 double y5_b = biquad(y4_b, high_v12_b, high_v22_b, highp2_a1, highp2_a2, highp2_b0, highp2_b1, highp2_b2);
yohoo15 1:cfcd4825fe81 270 double y6_b = biquad(y5_b, high_v13_b, high_v23_b, highp3_a1, highp3_a2, highp3_b0, highp3_b1, highp3_b2);
yohoo15 1:cfcd4825fe81 271
yohoo15 1:cfcd4825fe81 272 //rectivier
yohoo15 1:cfcd4825fe81 273 double y7_b = fabs(y6_b);
yohoo15 1:cfcd4825fe81 274
yohoo15 1:cfcd4825fe81 275 //lowpas filter cascade
yohoo15 1:cfcd4825fe81 276 double y8_b = biquad(y7_b, low_v11_b, low_v21_b, lowp1_a1, lowp1_a2, lowp1_b0, lowp1_b1, lowp1_b2);
yohoo15 1:cfcd4825fe81 277 double y9_b = biquad(y8_b, low_v12_b, low_v22_b, lowp2_a1, lowp2_a2, lowp2_b0, lowp2_b1, lowp2_b2);
yohoo15 1:cfcd4825fe81 278 double y10_b= biquad(y9_b, low_v13_b, low_v23_b, lowp3_a1, lowp3_a2, lowp3_b0, lowp3_b1, lowp3_b2);
yohoo15 1:cfcd4825fe81 279
yohoo15 1:cfcd4825fe81 280 // moving average
yohoo15 1:cfcd4825fe81 281 double filter_signal_b = moving_average(y10_b,n1_b,n2_b,n3_b,n4_b,n5_b);
yohoo15 1:cfcd4825fe81 282
yohoo15 1:cfcd4825fe81 283 return(filter_signal_b);
yohoo15 1:cfcd4825fe81 284 }
yohoo15 1:cfcd4825fe81 285
yohoo15 0:6f71e969a0b5 286 /*************************************************************BEGIN motor control******************************************************************************************************/
yohoo15 0:6f71e969a0b5 287 // Define pin for motor control
yohoo15 0:6f71e969a0b5 288 DigitalOut directionPin(D4);
yohoo15 0:6f71e969a0b5 289 PwmOut PWM(D5);
yohoo15 1:cfcd4825fe81 290 DigitalOut directionPin_key(D6);
yohoo15 1:cfcd4825fe81 291 PwmOut PWM_key(D7);
yohoo15 0:6f71e969a0b5 292 DigitalIn buttonccw(PTA4);
yohoo15 0:6f71e969a0b5 293 DigitalIn buttoncw(PTC6);
yohoo15 0:6f71e969a0b5 294
yohoo15 0:6f71e969a0b5 295 QEI wheel (PTC10, PTC11, NC, 624); // Pin for counting (analog in)
yohoo15 1:cfcd4825fe81 296 QEI wheel_key (PTD0, PTD2, NC, 624); // Pin for counting (analog in)
yohoo15 1:cfcd4825fe81 297
yohoo15 1:cfcd4825fe81 298 // saying buttons are not pressed
yohoo15 1:cfcd4825fe81 299 const int Buttoncw_pressed = 0;
yohoo15 1:cfcd4825fe81 300 const int Buttonccw_pressed = 0;
yohoo15 1:cfcd4825fe81 301
yohoo15 0:6f71e969a0b5 302
yohoo15 0:6f71e969a0b5 303 // define rotation direction and begin possition
yohoo15 0:6f71e969a0b5 304 const int cw = 1;
yohoo15 0:6f71e969a0b5 305 const int ccw = 0;
yohoo15 0:6f71e969a0b5 306 double setpoint = 0; //setpoint is in pulses
yohoo15 1:cfcd4825fe81 307 double setpoint_key_press = 0;
yohoo15 0:6f71e969a0b5 308
yohoo15 0:6f71e969a0b5 309 // Controller gain proportional and intergrator
yohoo15 0:6f71e969a0b5 310 const double motor1_Kp = 5; // more or les random number.
yohoo15 0:6f71e969a0b5 311 const double motor1_Ki = 0;
yohoo15 0:6f71e969a0b5 312 const double M1_timestep = 0.01; // reason ticker works with 100 Hz.
yohoo15 0:6f71e969a0b5 313 double motor1_error_integraal = 0; // initial value of integral error
yohoo15 0:6f71e969a0b5 314 // Defining pulses per revolution (calculating pulses to rotations in degree.)
yohoo15 1:cfcd4825fe81 315 const double pulses_per_revolution = 3200 ;// motor gear is 1:1000
yohoo15 1:cfcd4825fe81 316
yohoo15 1:cfcd4825fe81 317 const double motor1_Kp_key = 5; // more or les random number.
yohoo15 1:cfcd4825fe81 318 const double motor1_Ki_key = 0;
yohoo15 1:cfcd4825fe81 319 const double M1_timestep_key = 0.01; // reason ticker works with 100 Hz.
yohoo15 1:cfcd4825fe81 320 double motor1_error_integraal_key = 0; // initial value of integral error
yohoo15 1:cfcd4825fe81 321 // Defining pulses per revolution (calculating pulses to rotations in degree.)
yohoo15 1:cfcd4825fe81 322 const double pulses_per_revolution_key = 4200 ; //8400 counts is aangegeven op de motor for x4 is 1:131.25
yohoo15 0:6f71e969a0b5 323 /*
yohoo15 0:6f71e969a0b5 324 double Rotation = -2; // rotation
yohoo15 0:6f71e969a0b5 325 double movement = Rotation * pulses_per_revolution; // times 360 to make Rotations degree.
yohoo15 0:6f71e969a0b5 326 */
yohoo15 0:6f71e969a0b5 327
yohoo15 0:6f71e969a0b5 328 // defining flags
yohoo15 0:6f71e969a0b5 329 volatile bool flag_motor = false;
yohoo15 0:6f71e969a0b5 330
yohoo15 0:6f71e969a0b5 331 // making function flags.
yohoo15 0:6f71e969a0b5 332 void Go_flag_motor()
yohoo15 0:6f71e969a0b5 333 {
yohoo15 0:6f71e969a0b5 334 flag_motor = true;
yohoo15 0:6f71e969a0b5 335 }
yohoo15 0:6f71e969a0b5 336
yohoo15 0:6f71e969a0b5 337
yohoo15 0:6f71e969a0b5 338 // To make a new setpoint
yohoo15 0:6f71e969a0b5 339 double making_setpoint(double direction)
yohoo15 0:6f71e969a0b5 340 {
yohoo15 0:6f71e969a0b5 341 if ( cw == direction) {
yohoo15 0:6f71e969a0b5 342 setpoint = setpoint + (pulses_per_revolution/40000);
yohoo15 0:6f71e969a0b5 343 }
yohoo15 0:6f71e969a0b5 344 if ( ccw == direction) {
yohoo15 0:6f71e969a0b5 345 setpoint = setpoint - (pulses_per_revolution/40000);
yohoo15 0:6f71e969a0b5 346 }
yohoo15 0:6f71e969a0b5 347 return(setpoint);
yohoo15 0:6f71e969a0b5 348
yohoo15 0:6f71e969a0b5 349 }
yohoo15 0:6f71e969a0b5 350
yohoo15 0:6f71e969a0b5 351 // Reusable P controller
yohoo15 0:6f71e969a0b5 352 double PI(double error, const double Kp, const double Ki, double Ts, double &e_int)
yohoo15 0:6f71e969a0b5 353 {
yohoo15 0:6f71e969a0b5 354
yohoo15 0:6f71e969a0b5 355 e_int = e_int + Ts * error;
yohoo15 0:6f71e969a0b5 356
yohoo15 0:6f71e969a0b5 357 double PI_output = (Kp * error) + (Ki * e_int);
yohoo15 0:6f71e969a0b5 358 return PI_output;
yohoo15 0:6f71e969a0b5 359 }
yohoo15 0:6f71e969a0b5 360 // Next task, measure the error and apply the output to the plant
yohoo15 1:cfcd4825fe81 361
yohoo15 1:cfcd4825fe81 362 // control for movement left right
yohoo15 0:6f71e969a0b5 363 void motor1_Controller()
yohoo15 0:6f71e969a0b5 364 {
yohoo15 0:6f71e969a0b5 365 double reference = setpoint; // setpoint is in pulses
yohoo15 0:6f71e969a0b5 366 double position = wheel.getPulses();
yohoo15 0:6f71e969a0b5 367 double error_pulses = (reference - position); // calculate the error in pulses
yohoo15 0:6f71e969a0b5 368 double error_rotation = error_pulses / pulses_per_revolution; //calculate how much the rotaions the motor has to turn
yohoo15 0:6f71e969a0b5 369
yohoo15 0:6f71e969a0b5 370 double output = abs(PI( error_rotation, motor1_Kp, motor1_Ki, M1_timestep, motor1_error_integraal ));
yohoo15 0:6f71e969a0b5 371
yohoo15 0:6f71e969a0b5 372 if(error_pulses > 0) {
yohoo15 0:6f71e969a0b5 373 directionPin.write(cw);
yohoo15 0:6f71e969a0b5 374
yohoo15 0:6f71e969a0b5 375 } else if(error_pulses < 0) {
yohoo15 0:6f71e969a0b5 376 directionPin.write(ccw);
yohoo15 0:6f71e969a0b5 377 } else {
yohoo15 0:6f71e969a0b5 378 output = 0;
yohoo15 0:6f71e969a0b5 379 }
yohoo15 0:6f71e969a0b5 380 PWM.write(output); // out of the if loop due to abs output
yohoo15 0:6f71e969a0b5 381
yohoo15 0:6f71e969a0b5 382
yohoo15 0:6f71e969a0b5 383 }
yohoo15 1:cfcd4825fe81 384 /*
yohoo15 1:cfcd4825fe81 385 //control for the keypress
yohoo15 1:cfcd4825fe81 386 void motor_key_Controller()
yohoo15 1:cfcd4825fe81 387 {
yohoo15 1:cfcd4825fe81 388 double reference_key = setpoint_key_press; // setpoint is in pulses
yohoo15 1:cfcd4825fe81 389 double position_key = wheel_key.getPulses();
yohoo15 1:cfcd4825fe81 390 double error_pulses_key = (reference_key - position_key); // calculate the error in pulses
yohoo15 1:cfcd4825fe81 391 double error_rotation_key = error_pulses_key / pulses_per_revolution_key; //calculate how much the rotaions the motor has to turn
yohoo15 0:6f71e969a0b5 392
yohoo15 1:cfcd4825fe81 393 while (fabs(error_pulses_key) > 40) {
yohoo15 1:cfcd4825fe81 394 //kijken = wheel_two.getPulses()/pulses_per_revolution;
yohoo15 1:cfcd4825fe81 395
yohoo15 1:cfcd4825fe81 396 double output_key = abs(PI( error_rotation_key, motor1_Kp_key, motor1_Ki_key, M1_timestep_key, motor1_error_integraal_key ));
yohoo15 1:cfcd4825fe81 397
yohoo15 1:cfcd4825fe81 398 if(error_pulses_key > 0) {
yohoo15 1:cfcd4825fe81 399 directionPin_key.write(cw);
yohoo15 1:cfcd4825fe81 400
yohoo15 1:cfcd4825fe81 401 } else if(error_pulses_key < 0) {
yohoo15 1:cfcd4825fe81 402 directionPin_key.write(ccw);
yohoo15 1:cfcd4825fe81 403 } else {
yohoo15 1:cfcd4825fe81 404 output_key = 0;
yohoo15 1:cfcd4825fe81 405 }
yohoo15 1:cfcd4825fe81 406
yohoo15 1:cfcd4825fe81 407 PWM.write(output_key); // out of the if loop due to abs output
yohoo15 1:cfcd4825fe81 408
yohoo15 1:cfcd4825fe81 409
yohoo15 1:cfcd4825fe81 410 }
yohoo15 1:cfcd4825fe81 411 }*/
yohoo15 0:6f71e969a0b5 412 /*************************************************************END motor control******************************************************************************************************/
yohoo15 0:6f71e969a0b5 413
yohoo15 0:6f71e969a0b5 414 void HIDScope_kijken()
yohoo15 0:6f71e969a0b5 415 {
yohoo15 0:6f71e969a0b5 416 scope.set(0, analog_emg_left.read());
yohoo15 0:6f71e969a0b5 417 scope.set(1, filter_signal_hid_left);
yohoo15 0:6f71e969a0b5 418 scope.set(2, analog_emg_right.read());
yohoo15 0:6f71e969a0b5 419 scope.set(3, filter_signal_hid_right);
yohoo15 0:6f71e969a0b5 420 scope.send();
yohoo15 0:6f71e969a0b5 421 }
yohoo15 0:6f71e969a0b5 422
yohoo15 0:6f71e969a0b5 423
yohoo15 0:6f71e969a0b5 424 int main()
yohoo15 0:6f71e969a0b5 425 {
yohoo15 1:cfcd4825fe81 426
yohoo15 1:cfcd4825fe81 427
yohoo15 0:6f71e969a0b5 428 aansturen.attach( &Go_flag_motor, 0.01f ); // 100 Hz // timer 0.00001f motor keeps spinning
yohoo15 1:cfcd4825fe81 429 HIDScope_timer.attach(&Go_flag_HIDScope, 0.2);//0.02 had worked
yohoo15 1:cfcd4825fe81 430 Filteren_timer.attach(&Go_flag_filteren,0.4);// 0.04 had worked
yohoo15 0:6f71e969a0b5 431 while(1) {
yohoo15 1:cfcd4825fe81 432 pc.printf("hi here, \n");
yohoo15 1:cfcd4825fe81 433 // led_green = 1;
yohoo15 1:cfcd4825fe81 434 // led_red = 1;
yohoo15 1:cfcd4825fe81 435
yohoo15 0:6f71e969a0b5 436 if(Flag_filteren) {
yohoo15 0:6f71e969a0b5 437 Flag_filteren = false;
yohoo15 1:cfcd4825fe81 438 // filter left and set bool
yohoo15 1:cfcd4825fe81 439 filter_signal_hid_left = Filteren_left(analog_emg_left.read());
yohoo15 0:6f71e969a0b5 440
yohoo15 0:6f71e969a0b5 441 if (filter_signal_hid_left > high_threshold) {
yohoo15 0:6f71e969a0b5 442 left_movement = true;
yohoo15 0:6f71e969a0b5 443 } else if (filter_signal_hid_left < low_threshold) {
yohoo15 0:6f71e969a0b5 444 left_movement = false;
yohoo15 0:6f71e969a0b5 445 }
yohoo15 0:6f71e969a0b5 446 // make filter right and set bool
yohoo15 1:cfcd4825fe81 447 filter_signal_hid_right = Filteren_right(analog_emg_right.read());
yohoo15 0:6f71e969a0b5 448
yohoo15 0:6f71e969a0b5 449 if (filter_signal_hid_right > high_threshold) {
yohoo15 0:6f71e969a0b5 450 right_movement = true;
yohoo15 0:6f71e969a0b5 451 } else if (filter_signal_hid_right < low_threshold) {
yohoo15 0:6f71e969a0b5 452 right_movement = false;
yohoo15 0:6f71e969a0b5 453 }
yohoo15 0:6f71e969a0b5 454
yohoo15 0:6f71e969a0b5 455
yohoo15 0:6f71e969a0b5 456 }
yohoo15 0:6f71e969a0b5 457
yohoo15 1:cfcd4825fe81 458
yohoo15 1:cfcd4825fe81 459
yohoo15 0:6f71e969a0b5 460 if(Flag_HIDScope) {
yohoo15 0:6f71e969a0b5 461 Flag_HIDScope = false;
yohoo15 0:6f71e969a0b5 462 HIDScope_kijken();
yohoo15 0:6f71e969a0b5 463
yohoo15 0:6f71e969a0b5 464 }
yohoo15 0:6f71e969a0b5 465
yohoo15 0:6f71e969a0b5 466 if(flag_motor) {
yohoo15 0:6f71e969a0b5 467 flag_motor = false;
yohoo15 0:6f71e969a0b5 468 motor1_Controller();
yohoo15 0:6f71e969a0b5 469
yohoo15 0:6f71e969a0b5 470 }
yohoo15 0:6f71e969a0b5 471
yohoo15 0:6f71e969a0b5 472
yohoo15 0:6f71e969a0b5 473
yohoo15 0:6f71e969a0b5 474 // Pussing buttons to get input signal
yohoo15 0:6f71e969a0b5 475
yohoo15 1:cfcd4825fe81 476 if( buttoncw.read() == Buttoncw_pressed) {
yohoo15 0:6f71e969a0b5 477 setpoint = making_setpoint(cw);
yohoo15 1:cfcd4825fe81 478 //led_green = 0;
yohoo15 0:6f71e969a0b5 479
yohoo15 0:6f71e969a0b5 480 }
yohoo15 1:cfcd4825fe81 481 if(buttonccw.read() == Buttonccw_pressed ) {
yohoo15 0:6f71e969a0b5 482 setpoint = making_setpoint(ccw);
yohoo15 0:6f71e969a0b5 483 }
yohoo15 1:cfcd4825fe81 484
yohoo15 1:cfcd4825fe81 485 // if(/*right_movement and left_movement or */buttoncw.read() == Buttoncw_pressed and buttonccw.read() == Buttonccw_pressed) {
yohoo15 1:cfcd4825fe81 486
yohoo15 1:cfcd4825fe81 487 /*
yohoo15 1:cfcd4825fe81 488
yohoo15 1:cfcd4825fe81 489 setpoint_key_press = 1050;
yohoo15 1:cfcd4825fe81 490 motor_key_Controller();
yohoo15 1:cfcd4825fe81 491 setpoint_key_press = 0;
yohoo15 1:cfcd4825fe81 492 motor_key_Controller();
yohoo15 1:cfcd4825fe81 493
yohoo15 1:cfcd4825fe81 494
yohoo15 1:cfcd4825fe81 495 }
yohoo15 1:cfcd4825fe81 496
yohoo15 1:cfcd4825fe81 497 */
yohoo15 0:6f71e969a0b5 498 }
yohoo15 0:6f71e969a0b5 499 }
yohoo15 0:6f71e969a0b5 500
yohoo15 0:6f71e969a0b5 501
yohoo15 0:6f71e969a0b5 502
yohoo15 0:6f71e969a0b5 503