Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope QEI mbed
main.cpp@0:6f71e969a0b5, 2015-10-28 (annotated)
- Committer:
- yohoo15
- Date:
- Wed Oct 28 08:11:57 2015 +0000
- Revision:
- 0:6f71e969a0b5
- Child:
- 1:cfcd4825fe81
emg half werkend;
Who changed what in which revision?
| User | Revision | Line number | New 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 | 0:6f71e969a0b5 | 9 | |
| yohoo15 | 0:6f71e969a0b5 | 10 | |
| yohoo15 | 0:6f71e969a0b5 | 11 | |
| yohoo15 | 0:6f71e969a0b5 | 12 | Serial pc(USBTX, USBRX); |
| yohoo15 | 0:6f71e969a0b5 | 13 | |
| yohoo15 | 0:6f71e969a0b5 | 14 | Ticker HIDScope_timer; |
| yohoo15 | 0:6f71e969a0b5 | 15 | Ticker Filteren_timer; |
| yohoo15 | 0:6f71e969a0b5 | 16 | Ticker aansturen; |
| yohoo15 | 0:6f71e969a0b5 | 17 | |
| yohoo15 | 0:6f71e969a0b5 | 18 | HIDScope scope(4); |
| yohoo15 | 0:6f71e969a0b5 | 19 | |
| yohoo15 | 0:6f71e969a0b5 | 20 | // defining flags |
| yohoo15 | 0:6f71e969a0b5 | 21 | volatile bool Flag_filteren = false; |
| yohoo15 | 0:6f71e969a0b5 | 22 | volatile bool Flag_HIDScope = false; |
| yohoo15 | 0:6f71e969a0b5 | 23 | volatile bool left_movement = false; |
| yohoo15 | 0:6f71e969a0b5 | 24 | volatile bool right_movement = false; |
| yohoo15 | 0:6f71e969a0b5 | 25 | |
| yohoo15 | 0:6f71e969a0b5 | 26 | // making function flags. |
| yohoo15 | 0:6f71e969a0b5 | 27 | void Go_flag_filteren() |
| yohoo15 | 0:6f71e969a0b5 | 28 | { |
| yohoo15 | 0:6f71e969a0b5 | 29 | Flag_filteren = true; |
| yohoo15 | 0:6f71e969a0b5 | 30 | } |
| yohoo15 | 0:6f71e969a0b5 | 31 | |
| yohoo15 | 0:6f71e969a0b5 | 32 | void Go_flag_HIDScope() |
| yohoo15 | 0:6f71e969a0b5 | 33 | { |
| yohoo15 | 0:6f71e969a0b5 | 34 | Flag_HIDScope = true; |
| yohoo15 | 0:6f71e969a0b5 | 35 | } |
| yohoo15 | 0:6f71e969a0b5 | 36 | |
| yohoo15 | 0:6f71e969a0b5 | 37 | AnalogIn analog_emg_left(A0); |
| yohoo15 | 0:6f71e969a0b5 | 38 | AnalogIn analog_emg_right(A1); |
| yohoo15 | 0:6f71e969a0b5 | 39 | |
| yohoo15 | 0:6f71e969a0b5 | 40 | double input_left = 0 ; |
| yohoo15 | 0:6f71e969a0b5 | 41 | double input_right = 0 ; |
| yohoo15 | 0:6f71e969a0b5 | 42 | //double input = 0; |
| yohoo15 | 0:6f71e969a0b5 | 43 | double filter_signal_hid_left = 0; |
| yohoo15 | 0:6f71e969a0b5 | 44 | double filter_signal_hid_right = 0; |
| yohoo15 | 0:6f71e969a0b5 | 45 | //double input_right = 0; |
| yohoo15 | 0:6f71e969a0b5 | 46 | // defining threshold |
| yohoo15 | 0:6f71e969a0b5 | 47 | double high_threshold = 1200; |
| yohoo15 | 0:6f71e969a0b5 | 48 | double low_threshold = 500; |
| yohoo15 | 0:6f71e969a0b5 | 49 | |
| yohoo15 | 0:6f71e969a0b5 | 50 | //*** making the v for everything and conquer the world*** |
| yohoo15 | 0:6f71e969a0b5 | 51 | |
| yohoo15 | 0:6f71e969a0b5 | 52 | //for Notchfilter |
| yohoo15 | 0:6f71e969a0b5 | 53 | double notch_v11=0; |
| yohoo15 | 0:6f71e969a0b5 | 54 | double notch_v21=0; |
| yohoo15 | 0:6f71e969a0b5 | 55 | double notch_v12=0; |
| yohoo15 | 0:6f71e969a0b5 | 56 | double notch_v22=0; |
| yohoo15 | 0:6f71e969a0b5 | 57 | double notch_v13=0; |
| yohoo15 | 0:6f71e969a0b5 | 58 | double notch_v23=0; |
| yohoo15 | 0:6f71e969a0b5 | 59 | |
| yohoo15 | 0:6f71e969a0b5 | 60 | //for highpass filter |
| yohoo15 | 0:6f71e969a0b5 | 61 | double high_v11=0; |
| yohoo15 | 0:6f71e969a0b5 | 62 | double high_v21=0; |
| yohoo15 | 0:6f71e969a0b5 | 63 | double high_v12=0; |
| yohoo15 | 0:6f71e969a0b5 | 64 | double high_v22=0; |
| yohoo15 | 0:6f71e969a0b5 | 65 | double high_v13=0; |
| yohoo15 | 0:6f71e969a0b5 | 66 | double high_v23=0; |
| yohoo15 | 0:6f71e969a0b5 | 67 | |
| yohoo15 | 0:6f71e969a0b5 | 68 | // for lowpasfilter |
| yohoo15 | 0:6f71e969a0b5 | 69 | double low_v11=0; |
| yohoo15 | 0:6f71e969a0b5 | 70 | double low_v21=0; |
| yohoo15 | 0:6f71e969a0b5 | 71 | double low_v12=0; |
| yohoo15 | 0:6f71e969a0b5 | 72 | double low_v22=0; |
| yohoo15 | 0:6f71e969a0b5 | 73 | double low_v13=0; |
| yohoo15 | 0:6f71e969a0b5 | 74 | double low_v23=0; |
| yohoo15 | 0:6f71e969a0b5 | 75 | |
| yohoo15 | 0:6f71e969a0b5 | 76 | // for moving average |
| yohoo15 | 0:6f71e969a0b5 | 77 | double n1 = 0; |
| yohoo15 | 0:6f71e969a0b5 | 78 | double n2 = 0; |
| yohoo15 | 0:6f71e969a0b5 | 79 | double n3 = 0; |
| yohoo15 | 0:6f71e969a0b5 | 80 | double n4 = 0; |
| yohoo15 | 0:6f71e969a0b5 | 81 | double n5 = 0; |
| yohoo15 | 0:6f71e969a0b5 | 82 | |
| yohoo15 | 0:6f71e969a0b5 | 83 | double filter_left; |
| yohoo15 | 0:6f71e969a0b5 | 84 | double filter_right; |
| yohoo15 | 0:6f71e969a0b5 | 85 | |
| yohoo15 | 0:6f71e969a0b5 | 86 | //general biquad filter that can be called in all the filter functions |
| yohoo15 | 0:6f71e969a0b5 | 87 | double biquad(double u, double &v1, double &v2, const double a1, |
| yohoo15 | 0:6f71e969a0b5 | 88 | const double a2, const double b0, const double b1, const double b2) |
| yohoo15 | 0:6f71e969a0b5 | 89 | { |
| yohoo15 | 0:6f71e969a0b5 | 90 | double v = u - a1*v1 - a2*v2; |
| yohoo15 | 0:6f71e969a0b5 | 91 | double y = b0*v + b1*v1 + b2*v2; |
| yohoo15 | 0:6f71e969a0b5 | 92 | //values of v2 and v1 are updated, as they are passed by reference |
| yohoo15 | 0:6f71e969a0b5 | 93 | //they update globally |
| yohoo15 | 0:6f71e969a0b5 | 94 | v2 = v1; |
| yohoo15 | 0:6f71e969a0b5 | 95 | v1 = v; |
| yohoo15 | 0:6f71e969a0b5 | 96 | return y; |
| yohoo15 | 0:6f71e969a0b5 | 97 | } |
| yohoo15 | 0:6f71e969a0b5 | 98 | |
| yohoo15 | 0:6f71e969a0b5 | 99 | double moving_average(double y, double &n1, double &n2, double &n3, double &n4, double &n5) |
| yohoo15 | 0:6f71e969a0b5 | 100 | { |
| yohoo15 | 0:6f71e969a0b5 | 101 | double average = (y + n1 + n2 +n3 + n4 + n5)/5; |
| yohoo15 | 0:6f71e969a0b5 | 102 | n5 = n4; |
| yohoo15 | 0:6f71e969a0b5 | 103 | n4 = n3; |
| yohoo15 | 0:6f71e969a0b5 | 104 | n3 = n2; |
| yohoo15 | 0:6f71e969a0b5 | 105 | n2 = n1; |
| yohoo15 | 0:6f71e969a0b5 | 106 | n1 = y; |
| yohoo15 | 0:6f71e969a0b5 | 107 | |
| yohoo15 | 0:6f71e969a0b5 | 108 | return average; |
| yohoo15 | 0:6f71e969a0b5 | 109 | } |
| yohoo15 | 0:6f71e969a0b5 | 110 | /* |
| yohoo15 | 0:6f71e969a0b5 | 111 | double threshold(double signal, const double lowtreshold, const double hightreshold) |
| yohoo15 | 0:6f71e969a0b5 | 112 | { |
| yohoo15 | 0:6f71e969a0b5 | 113 | if (signal > hightreshold) |
| yohoo15 | 0:6f71e969a0b5 | 114 | left = true; |
| yohoo15 | 0:6f71e969a0b5 | 115 | else if (signal <lowtreshold) |
| yohoo15 | 0:6f71e969a0b5 | 116 | left = false; |
| yohoo15 | 0:6f71e969a0b5 | 117 | } |
| yohoo15 | 0:6f71e969a0b5 | 118 | */ |
| yohoo15 | 0:6f71e969a0b5 | 119 | //Specifying filter coefficients highpass |
| yohoo15 | 0:6f71e969a0b5 | 120 | |
| yohoo15 | 0:6f71e969a0b5 | 121 | /* notch filter with 3 cascaded biquads*/ |
| yohoo15 | 0:6f71e969a0b5 | 122 | //first notch biquad |
| yohoo15 | 0:6f71e969a0b5 | 123 | const double notch1_a1 = -1.55951422433; |
| yohoo15 | 0:6f71e969a0b5 | 124 | const double notch1_a2 = 0.92705680308; |
| yohoo15 | 0:6f71e969a0b5 | 125 | const double notch1_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 126 | const double notch1_b1 = -1.61854515325; |
| yohoo15 | 0:6f71e969a0b5 | 127 | const double notch1_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 128 | |
| yohoo15 | 0:6f71e969a0b5 | 129 | //second notch biquad |
| yohoo15 | 0:6f71e969a0b5 | 130 | const double notch2_a1 = -1.54767435801; |
| yohoo15 | 0:6f71e969a0b5 | 131 | const double notch2_a2 = 0.96124842048; |
| yohoo15 | 0:6f71e969a0b5 | 132 | const double notch2_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 133 | const double notch2_b1 = -1.61854515325; |
| yohoo15 | 0:6f71e969a0b5 | 134 | const double notch2_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 135 | |
| yohoo15 | 0:6f71e969a0b5 | 136 | //third notch biquad |
| yohoo15 | 0:6f71e969a0b5 | 137 | const double notch3_a1 = -1.62600366964; |
| yohoo15 | 0:6f71e969a0b5 | 138 | const double notch3_a2 = 0.96453460373; |
| yohoo15 | 0:6f71e969a0b5 | 139 | const double notch3_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 140 | const double notch3_b1 = -1.61854515325; |
| yohoo15 | 0:6f71e969a0b5 | 141 | const double notch3_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 142 | |
| yohoo15 | 0:6f71e969a0b5 | 143 | /* high pass filter consists of three cascaded biquads |
| yohoo15 | 0:6f71e969a0b5 | 144 | blow coefficients for those three biquads */ |
| yohoo15 | 0:6f71e969a0b5 | 145 | //first high pass biquad |
| yohoo15 | 0:6f71e969a0b5 | 146 | const double highp1_a1 = -0.67538034389; |
| yohoo15 | 0:6f71e969a0b5 | 147 | const double highp1_a2 = 0.12769255668; |
| yohoo15 | 0:6f71e969a0b5 | 148 | const double highp1_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 149 | const double highp1_b1 = -2.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 150 | const double highp1_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 151 | |
| yohoo15 | 0:6f71e969a0b5 | 152 | //second high pass biquad |
| yohoo15 | 0:6f71e969a0b5 | 153 | const double highp2_a1 = -0.76475499450; |
| yohoo15 | 0:6f71e969a0b5 | 154 | const double highp2_a2 = 0.27692273367; |
| yohoo15 | 0:6f71e969a0b5 | 155 | const double highp2_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 156 | const double highp2_b1 = -2.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 157 | const double highp2_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 158 | |
| yohoo15 | 0:6f71e969a0b5 | 159 | //third high pass biquad |
| yohoo15 | 0:6f71e969a0b5 | 160 | const double highp3_a1 = -0.99216561242; |
| yohoo15 | 0:6f71e969a0b5 | 161 | const double highp3_a2 = 0.65663360837; |
| yohoo15 | 0:6f71e969a0b5 | 162 | const double highp3_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 163 | const double highp3_b1 = -2.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 164 | const double highp3_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 165 | |
| yohoo15 | 0:6f71e969a0b5 | 166 | /* lowpass filter consists of three cascaded biquads |
| yohoo15 | 0:6f71e969a0b5 | 167 | below the coefficients for those three biquads */ |
| yohoo15 | 0:6f71e969a0b5 | 168 | //first low pass biquad |
| yohoo15 | 0:6f71e969a0b5 | 169 | const double lowp1_a1 = -1.05207469728; |
| yohoo15 | 0:6f71e969a0b5 | 170 | const double lowp1_a2 = 0.28586907478; |
| yohoo15 | 0:6f71e969a0b5 | 171 | const double lowp1_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 172 | const double lowp1_b1 = 2.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 173 | const double lowp1_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 174 | |
| yohoo15 | 0:6f71e969a0b5 | 175 | //second low pass biquad |
| yohoo15 | 0:6f71e969a0b5 | 176 | const double lowp2_a1 = -1.16338171052; |
| yohoo15 | 0:6f71e969a0b5 | 177 | const double lowp2_a2 = 0.42191097989; |
| yohoo15 | 0:6f71e969a0b5 | 178 | const double lowp2_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 179 | const double lowp2_b1 = 2.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 180 | const double lowp2_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 181 | |
| yohoo15 | 0:6f71e969a0b5 | 182 | //third low pass biquad |
| yohoo15 | 0:6f71e969a0b5 | 183 | const double lowp3_a1 = -1.42439823874; |
| yohoo15 | 0:6f71e969a0b5 | 184 | const double lowp3_a2 = 0.74093118112; |
| yohoo15 | 0:6f71e969a0b5 | 185 | const double lowp3_b0 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 186 | const double lowp3_b1 = 2.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 187 | const double lowp3_b2 = 1.00000000000; |
| yohoo15 | 0:6f71e969a0b5 | 188 | |
| yohoo15 | 0:6f71e969a0b5 | 189 | |
| yohoo15 | 0:6f71e969a0b5 | 190 | |
| yohoo15 | 0:6f71e969a0b5 | 191 | double Filteren(double input) |
| yohoo15 | 0:6f71e969a0b5 | 192 | { |
| yohoo15 | 0:6f71e969a0b5 | 193 | |
| yohoo15 | 0:6f71e969a0b5 | 194 | input = input-0.45; //FIRST SUBTRACT MEAN THEN FILTER |
| yohoo15 | 0:6f71e969a0b5 | 195 | //input_right = analog_emg_right.read(); |
| yohoo15 | 0:6f71e969a0b5 | 196 | |
| yohoo15 | 0:6f71e969a0b5 | 197 | // notch filter |
| yohoo15 | 0:6f71e969a0b5 | 198 | double y1 = biquad(input, notch_v11, notch_v21, notch1_a1, notch1_a2, notch1_b0, notch1_b1, notch1_b2); |
| yohoo15 | 0:6f71e969a0b5 | 199 | double y2 = biquad(y1, notch_v12, notch_v22, notch2_a1, notch2_a2, notch2_b0, notch2_b1, notch2_b2); |
| yohoo15 | 0:6f71e969a0b5 | 200 | double y3 = biquad(y2, notch_v13, notch_v23, notch3_a1, notch3_a2, notch3_b0, notch3_b1, notch3_b2); |
| yohoo15 | 0:6f71e969a0b5 | 201 | |
| yohoo15 | 0:6f71e969a0b5 | 202 | //higpass filter |
| yohoo15 | 0:6f71e969a0b5 | 203 | double y4 = biquad(y3, high_v11, high_v21, highp1_a1, highp1_a2, highp1_b0, highp1_b1, highp1_b2); |
| yohoo15 | 0:6f71e969a0b5 | 204 | double y5 = biquad(y4, high_v12, high_v22, highp2_a1, highp2_a2, highp2_b0, highp2_b1, highp2_b2); |
| yohoo15 | 0:6f71e969a0b5 | 205 | double y6 = biquad(y5, high_v13, high_v23, highp3_a1, highp3_a2, highp3_b0, highp3_b1, highp3_b2); |
| yohoo15 | 0:6f71e969a0b5 | 206 | |
| yohoo15 | 0:6f71e969a0b5 | 207 | //rectivier |
| yohoo15 | 0:6f71e969a0b5 | 208 | double y7 = fabs(y6); |
| yohoo15 | 0:6f71e969a0b5 | 209 | |
| yohoo15 | 0:6f71e969a0b5 | 210 | //lowpas filter cascade |
| yohoo15 | 0:6f71e969a0b5 | 211 | double y8 = biquad(y7, low_v11, low_v21, lowp1_a1, lowp1_a2, lowp1_b0, lowp1_b1, lowp1_b2); |
| yohoo15 | 0:6f71e969a0b5 | 212 | double y9 = biquad(y8, low_v12, low_v22, lowp2_a1, lowp2_a2, lowp2_b0, lowp2_b1, lowp2_b2); |
| yohoo15 | 0:6f71e969a0b5 | 213 | double y10= biquad(y9, low_v13, low_v23, lowp3_a1, lowp3_a2, lowp3_b0, lowp3_b1, lowp3_b2); |
| yohoo15 | 0:6f71e969a0b5 | 214 | |
| yohoo15 | 0:6f71e969a0b5 | 215 | // moving average |
| yohoo15 | 0:6f71e969a0b5 | 216 | double filter_signal = moving_average(y10,n1,n2,n3,n4,n5); |
| yohoo15 | 0:6f71e969a0b5 | 217 | |
| yohoo15 | 0:6f71e969a0b5 | 218 | return(filter_signal); |
| yohoo15 | 0:6f71e969a0b5 | 219 | } |
| yohoo15 | 0:6f71e969a0b5 | 220 | |
| yohoo15 | 0:6f71e969a0b5 | 221 | /*************************************************************BEGIN motor control******************************************************************************************************/ |
| yohoo15 | 0:6f71e969a0b5 | 222 | // Define pin for motor control |
| yohoo15 | 0:6f71e969a0b5 | 223 | DigitalOut directionPin(D4); |
| yohoo15 | 0:6f71e969a0b5 | 224 | PwmOut PWM(D5); |
| yohoo15 | 0:6f71e969a0b5 | 225 | DigitalIn buttonccw(PTA4); |
| yohoo15 | 0:6f71e969a0b5 | 226 | DigitalIn buttoncw(PTC6); |
| yohoo15 | 0:6f71e969a0b5 | 227 | |
| yohoo15 | 0:6f71e969a0b5 | 228 | QEI wheel (PTC10, PTC11, NC, 624); // Pin for counting (analog in) |
| yohoo15 | 0:6f71e969a0b5 | 229 | // define ticker |
| yohoo15 | 0:6f71e969a0b5 | 230 | |
| yohoo15 | 0:6f71e969a0b5 | 231 | // define rotation direction and begin possition |
| yohoo15 | 0:6f71e969a0b5 | 232 | const int cw = 1; |
| yohoo15 | 0:6f71e969a0b5 | 233 | const int ccw = 0; |
| yohoo15 | 0:6f71e969a0b5 | 234 | double setpoint = 0; //setpoint is in pulses |
| yohoo15 | 0:6f71e969a0b5 | 235 | |
| yohoo15 | 0:6f71e969a0b5 | 236 | |
| yohoo15 | 0:6f71e969a0b5 | 237 | // Controller gain proportional and intergrator |
| yohoo15 | 0:6f71e969a0b5 | 238 | const double motor1_Kp = 5; // more or les random number. |
| yohoo15 | 0:6f71e969a0b5 | 239 | const double motor1_Ki = 0; |
| yohoo15 | 0:6f71e969a0b5 | 240 | const double M1_timestep = 0.01; // reason ticker works with 100 Hz. |
| yohoo15 | 0:6f71e969a0b5 | 241 | double motor1_error_integraal = 0; // initial value of integral error |
| yohoo15 | 0:6f71e969a0b5 | 242 | // Defining pulses per revolution (calculating pulses to rotations in degree.) |
| yohoo15 | 0:6f71e969a0b5 | 243 | const double pulses_per_revolution = 4200 ;//8400 counts is aangegeven op de motor for x4. 10 - 30 counts oveshoot. for moter 1(tape)! Motor 2 almost the same(nice) |
| yohoo15 | 0:6f71e969a0b5 | 244 | /* |
| yohoo15 | 0:6f71e969a0b5 | 245 | double Rotation = -2; // rotation |
| yohoo15 | 0:6f71e969a0b5 | 246 | double movement = Rotation * pulses_per_revolution; // times 360 to make Rotations degree. |
| yohoo15 | 0:6f71e969a0b5 | 247 | */ |
| yohoo15 | 0:6f71e969a0b5 | 248 | |
| yohoo15 | 0:6f71e969a0b5 | 249 | // defining flags |
| yohoo15 | 0:6f71e969a0b5 | 250 | volatile bool flag_motor = false; |
| yohoo15 | 0:6f71e969a0b5 | 251 | |
| yohoo15 | 0:6f71e969a0b5 | 252 | // making function flags. |
| yohoo15 | 0:6f71e969a0b5 | 253 | void Go_flag_motor() |
| yohoo15 | 0:6f71e969a0b5 | 254 | { |
| yohoo15 | 0:6f71e969a0b5 | 255 | flag_motor = true; |
| yohoo15 | 0:6f71e969a0b5 | 256 | } |
| yohoo15 | 0:6f71e969a0b5 | 257 | |
| yohoo15 | 0:6f71e969a0b5 | 258 | |
| yohoo15 | 0:6f71e969a0b5 | 259 | // To make a new setpoint |
| yohoo15 | 0:6f71e969a0b5 | 260 | double making_setpoint(double direction) |
| yohoo15 | 0:6f71e969a0b5 | 261 | { |
| yohoo15 | 0:6f71e969a0b5 | 262 | if ( cw == direction) { |
| yohoo15 | 0:6f71e969a0b5 | 263 | setpoint = setpoint + (pulses_per_revolution/40000); |
| yohoo15 | 0:6f71e969a0b5 | 264 | } |
| yohoo15 | 0:6f71e969a0b5 | 265 | if ( ccw == direction) { |
| yohoo15 | 0:6f71e969a0b5 | 266 | setpoint = setpoint - (pulses_per_revolution/40000); |
| yohoo15 | 0:6f71e969a0b5 | 267 | } |
| yohoo15 | 0:6f71e969a0b5 | 268 | return(setpoint); |
| yohoo15 | 0:6f71e969a0b5 | 269 | |
| yohoo15 | 0:6f71e969a0b5 | 270 | } |
| yohoo15 | 0:6f71e969a0b5 | 271 | |
| yohoo15 | 0:6f71e969a0b5 | 272 | // Reusable P controller |
| yohoo15 | 0:6f71e969a0b5 | 273 | double PI(double error, const double Kp, const double Ki, double Ts, double &e_int) |
| yohoo15 | 0:6f71e969a0b5 | 274 | { |
| yohoo15 | 0:6f71e969a0b5 | 275 | |
| yohoo15 | 0:6f71e969a0b5 | 276 | e_int = e_int + Ts * error; |
| yohoo15 | 0:6f71e969a0b5 | 277 | |
| yohoo15 | 0:6f71e969a0b5 | 278 | double PI_output = (Kp * error) + (Ki * e_int); |
| yohoo15 | 0:6f71e969a0b5 | 279 | return PI_output; |
| yohoo15 | 0:6f71e969a0b5 | 280 | } |
| yohoo15 | 0:6f71e969a0b5 | 281 | // Next task, measure the error and apply the output to the plant |
| yohoo15 | 0:6f71e969a0b5 | 282 | void motor1_Controller() |
| yohoo15 | 0:6f71e969a0b5 | 283 | { |
| yohoo15 | 0:6f71e969a0b5 | 284 | double reference = setpoint; // setpoint is in pulses |
| yohoo15 | 0:6f71e969a0b5 | 285 | double position = wheel.getPulses(); |
| yohoo15 | 0:6f71e969a0b5 | 286 | double error_pulses = (reference - position); // calculate the error in pulses |
| yohoo15 | 0:6f71e969a0b5 | 287 | double error_rotation = error_pulses / pulses_per_revolution; //calculate how much the rotaions the motor has to turn |
| yohoo15 | 0:6f71e969a0b5 | 288 | |
| yohoo15 | 0:6f71e969a0b5 | 289 | double output = abs(PI( error_rotation, motor1_Kp, motor1_Ki, M1_timestep, motor1_error_integraal )); |
| yohoo15 | 0:6f71e969a0b5 | 290 | |
| yohoo15 | 0:6f71e969a0b5 | 291 | if(error_pulses > 0) { |
| yohoo15 | 0:6f71e969a0b5 | 292 | directionPin.write(cw); |
| yohoo15 | 0:6f71e969a0b5 | 293 | |
| yohoo15 | 0:6f71e969a0b5 | 294 | } else if(error_pulses < 0) { |
| yohoo15 | 0:6f71e969a0b5 | 295 | directionPin.write(ccw); |
| yohoo15 | 0:6f71e969a0b5 | 296 | } else { |
| yohoo15 | 0:6f71e969a0b5 | 297 | output = 0; |
| yohoo15 | 0:6f71e969a0b5 | 298 | } |
| yohoo15 | 0:6f71e969a0b5 | 299 | PWM.write(output); // out of the if loop due to abs output |
| yohoo15 | 0:6f71e969a0b5 | 300 | |
| yohoo15 | 0:6f71e969a0b5 | 301 | |
| yohoo15 | 0:6f71e969a0b5 | 302 | } |
| yohoo15 | 0:6f71e969a0b5 | 303 | |
| yohoo15 | 0:6f71e969a0b5 | 304 | /*************************************************************END motor control******************************************************************************************************/ |
| yohoo15 | 0:6f71e969a0b5 | 305 | |
| yohoo15 | 0:6f71e969a0b5 | 306 | void HIDScope_kijken() |
| yohoo15 | 0:6f71e969a0b5 | 307 | { |
| yohoo15 | 0:6f71e969a0b5 | 308 | scope.set(0, analog_emg_left.read()); |
| yohoo15 | 0:6f71e969a0b5 | 309 | scope.set(1, filter_signal_hid_left); |
| yohoo15 | 0:6f71e969a0b5 | 310 | scope.set(2, analog_emg_right.read()); |
| yohoo15 | 0:6f71e969a0b5 | 311 | scope.set(3, filter_signal_hid_right); |
| yohoo15 | 0:6f71e969a0b5 | 312 | scope.send(); |
| yohoo15 | 0:6f71e969a0b5 | 313 | } |
| yohoo15 | 0:6f71e969a0b5 | 314 | |
| yohoo15 | 0:6f71e969a0b5 | 315 | |
| yohoo15 | 0:6f71e969a0b5 | 316 | int main() |
| yohoo15 | 0:6f71e969a0b5 | 317 | { |
| yohoo15 | 0:6f71e969a0b5 | 318 | aansturen.attach( &Go_flag_motor, 0.01f ); // 100 Hz // timer 0.00001f motor keeps spinning |
| yohoo15 | 0:6f71e969a0b5 | 319 | HIDScope_timer.attach(&Go_flag_HIDScope, 0.02); |
| yohoo15 | 0:6f71e969a0b5 | 320 | Filteren_timer.attach(&Go_flag_filteren,0.04); |
| yohoo15 | 0:6f71e969a0b5 | 321 | while(1) { |
| yohoo15 | 0:6f71e969a0b5 | 322 | if(Flag_filteren) { |
| yohoo15 | 0:6f71e969a0b5 | 323 | Flag_filteren = false; |
| yohoo15 | 0:6f71e969a0b5 | 324 | // filter left and set bool |
| yohoo15 | 0:6f71e969a0b5 | 325 | filter_signal_hid_left = Filteren(analog_emg_left.read()); |
| yohoo15 | 0:6f71e969a0b5 | 326 | |
| yohoo15 | 0:6f71e969a0b5 | 327 | if (filter_signal_hid_left > high_threshold) { |
| yohoo15 | 0:6f71e969a0b5 | 328 | left_movement = true; |
| yohoo15 | 0:6f71e969a0b5 | 329 | } else if (filter_signal_hid_left < low_threshold) { |
| yohoo15 | 0:6f71e969a0b5 | 330 | left_movement = false; |
| yohoo15 | 0:6f71e969a0b5 | 331 | } |
| yohoo15 | 0:6f71e969a0b5 | 332 | // make filter right and set bool |
| yohoo15 | 0:6f71e969a0b5 | 333 | filter_signal_hid_right = Filteren(analog_emg_right.read()); |
| yohoo15 | 0:6f71e969a0b5 | 334 | |
| yohoo15 | 0:6f71e969a0b5 | 335 | if (filter_signal_hid_right > high_threshold) { |
| yohoo15 | 0:6f71e969a0b5 | 336 | right_movement = true; |
| yohoo15 | 0:6f71e969a0b5 | 337 | } else if (filter_signal_hid_right < low_threshold) { |
| yohoo15 | 0:6f71e969a0b5 | 338 | right_movement = false; |
| yohoo15 | 0:6f71e969a0b5 | 339 | } |
| yohoo15 | 0:6f71e969a0b5 | 340 | |
| yohoo15 | 0:6f71e969a0b5 | 341 | |
| yohoo15 | 0:6f71e969a0b5 | 342 | } |
| yohoo15 | 0:6f71e969a0b5 | 343 | |
| yohoo15 | 0:6f71e969a0b5 | 344 | if(Flag_HIDScope) { |
| yohoo15 | 0:6f71e969a0b5 | 345 | Flag_HIDScope = false; |
| yohoo15 | 0:6f71e969a0b5 | 346 | HIDScope_kijken(); |
| yohoo15 | 0:6f71e969a0b5 | 347 | |
| yohoo15 | 0:6f71e969a0b5 | 348 | } |
| yohoo15 | 0:6f71e969a0b5 | 349 | |
| yohoo15 | 0:6f71e969a0b5 | 350 | if(flag_motor) { |
| yohoo15 | 0:6f71e969a0b5 | 351 | flag_motor = false; |
| yohoo15 | 0:6f71e969a0b5 | 352 | motor1_Controller(); |
| yohoo15 | 0:6f71e969a0b5 | 353 | |
| yohoo15 | 0:6f71e969a0b5 | 354 | } |
| yohoo15 | 0:6f71e969a0b5 | 355 | |
| yohoo15 | 0:6f71e969a0b5 | 356 | |
| yohoo15 | 0:6f71e969a0b5 | 357 | |
| yohoo15 | 0:6f71e969a0b5 | 358 | // Pussing buttons to get input signal |
| yohoo15 | 0:6f71e969a0b5 | 359 | |
| yohoo15 | 0:6f71e969a0b5 | 360 | if(left_movement) { |
| yohoo15 | 0:6f71e969a0b5 | 361 | setpoint = making_setpoint(cw); |
| yohoo15 | 0:6f71e969a0b5 | 362 | |
| yohoo15 | 0:6f71e969a0b5 | 363 | } |
| yohoo15 | 0:6f71e969a0b5 | 364 | if(right_movement) { |
| yohoo15 | 0:6f71e969a0b5 | 365 | setpoint = making_setpoint(ccw); |
| yohoo15 | 0:6f71e969a0b5 | 366 | } |
| yohoo15 | 0:6f71e969a0b5 | 367 | } |
| yohoo15 | 0:6f71e969a0b5 | 368 | } |
| yohoo15 | 0:6f71e969a0b5 | 369 | |
| yohoo15 | 0:6f71e969a0b5 | 370 | |
| yohoo15 | 0:6f71e969a0b5 | 371 | |
| yohoo15 | 0:6f71e969a0b5 | 372 |