![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
4 directional EMG control of the XY table. Made during my bachelor end assignment.
Dependencies: C12832_lcd HIDScope mbed-dsp mbed
main.cpp@66:c094d1868b30, 2015-06-18 (annotated)
- Committer:
- jessekaiser
- Date:
- Thu Jun 18 11:50:07 2015 +0000
- Revision:
- 66:c094d1868b30
- Parent:
- 65:821683c7eb98
- Child:
- 67:fba5b64bb295
Homing 5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jessekaiser | 56:6ea03cce1175 | 1 | /*Code by Jesse Kaiser, s1355783 for control of the 2DOF Planar Table |
jessekaiser | 56:6ea03cce1175 | 2 | Some variables are also numbered at the end. The numbers stands for the muscle that controls it. |
jessekaiser | 56:6ea03cce1175 | 3 | Biceps = 1 |
jessekaiser | 56:6ea03cce1175 | 4 | Triceps = 2 |
jessekaiser | 56:6ea03cce1175 | 5 | Pectoralis Major = 3 |
jessekaiser | 56:6ea03cce1175 | 6 | Deltoid = 4 |
jessekaiser | 56:6ea03cce1175 | 7 | The "x" and "y" at the end of variables stand for the X-Spindle or Y-Spindle respectivly. |
jessekaiser | 56:6ea03cce1175 | 8 | */ |
jessekaiser | 56:6ea03cce1175 | 9 | |
jessekaiser | 0:3acdd563582f | 10 | #include "mbed.h" |
jessekaiser | 10:7f94cd65c910 | 11 | #include "C12832_lcd.h" |
jessekaiser | 56:6ea03cce1175 | 12 | #include "arm_math.h" |
jessekaiser | 57:0a278c60d28b | 13 | //#include "HIDScope.h" |
jessekaiser | 0:3acdd563582f | 14 | |
jessekaiser | 62:509622cce1c6 | 15 | #define P_Gain 0.995 |
jessekaiser | 66:c094d1868b30 | 16 | #define K_Gain 25 //Gain of the filtered EMG signal |
jessekaiser | 66:c094d1868b30 | 17 | #define Damp 5 //Deceleration of the motor |
jessekaiser | 66:c094d1868b30 | 18 | #define Mass 1 // Mass value |
jessekaiser | 66:c094d1868b30 | 19 | #define dt 0.01 //Sample frequency |
jessekaiser | 60:664f9b907c02 | 20 | #define MAX_bi 0.40 //Can be used for normalisation of the EMG signal of the biceps |
jessekaiser | 60:664f9b907c02 | 21 | #define MAX_tri 0.60 |
jessekaiser | 60:664f9b907c02 | 22 | #define MAX_pect 0.48 |
jessekaiser | 60:664f9b907c02 | 23 | #define MAX_delt 1.07 |
jessekaiser | 60:664f9b907c02 | 24 | #define EMG_tresh1 0.02 |
jessekaiser | 60:664f9b907c02 | 25 | #define EMG_tresh2 0.02 |
jessekaiser | 60:664f9b907c02 | 26 | #define EMG_tresh3 0.01 |
jessekaiser | 60:664f9b907c02 | 27 | #define EMG_tresh4 0.01 |
jessekaiser | 66:c094d1868b30 | 28 | #define H_Gain 3 |
jessekaiser | 66:c094d1868b30 | 29 | #define Pt_x 0.88 |
jessekaiser | 66:c094d1868b30 | 30 | #define Pt_y 0.25 |
jessekaiser | 31:372ff8d49430 | 31 | |
jessekaiser | 56:6ea03cce1175 | 32 | //Motor control |
jessekaiser | 56:6ea03cce1175 | 33 | DigitalOut Dirx(p21); |
jessekaiser | 56:6ea03cce1175 | 34 | PwmOut Stepx(p22); |
jessekaiser | 56:6ea03cce1175 | 35 | DigitalOut Diry(p23); |
jessekaiser | 56:6ea03cce1175 | 36 | PwmOut Stepy(p24); |
jessekaiser | 56:6ea03cce1175 | 37 | |
jessekaiser | 56:6ea03cce1175 | 38 | //Signal to and from computer |
jessekaiser | 56:6ea03cce1175 | 39 | Serial pc(USBTX, USBRX); |
jessekaiser | 56:6ea03cce1175 | 40 | |
jessekaiser | 56:6ea03cce1175 | 41 | //Position sensors |
jessekaiser | 56:6ea03cce1175 | 42 | AnalogIn Posx(p19); |
jessekaiser | 58:3ea066215c31 | 43 | AnalogIn Posy(p20); |
jessekaiser | 58:3ea066215c31 | 44 | DigitalOut Enablex(p25); |
jessekaiser | 58:3ea066215c31 | 45 | DigitalOut Enabley(p26); |
jessekaiser | 56:6ea03cce1175 | 46 | |
jessekaiser | 56:6ea03cce1175 | 47 | //Microstepping |
jessekaiser | 0:3acdd563582f | 48 | DigitalOut MS1(p27); |
jessekaiser | 0:3acdd563582f | 49 | DigitalOut MS2(p28); |
jessekaiser | 0:3acdd563582f | 50 | DigitalOut MS3(p29); |
jessekaiser | 56:6ea03cce1175 | 51 | |
jessekaiser | 57:0a278c60d28b | 52 | //EMG inputs |
jessekaiser | 56:6ea03cce1175 | 53 | AnalogIn emg1(p15); //EMG bordje bovenop, biceps |
jessekaiser | 56:6ea03cce1175 | 54 | AnalogIn emg2(p16); //triceps |
jessekaiser | 56:6ea03cce1175 | 55 | AnalogIn emg3(p17); |
jessekaiser | 56:6ea03cce1175 | 56 | AnalogIn emg4(p18); |
jessekaiser | 56:6ea03cce1175 | 57 | |
jessekaiser | 57:0a278c60d28b | 58 | //HIDScope scope(4); |
jessekaiser | 57:0a278c60d28b | 59 | //Ticker scopeTimer; |
jessekaiser | 56:6ea03cce1175 | 60 | |
jessekaiser | 56:6ea03cce1175 | 61 | //lcd |
jessekaiser | 30:0a8f849e0292 | 62 | C12832_LCD lcd; |
jessekaiser | 0:3acdd563582f | 63 | |
jessekaiser | 56:6ea03cce1175 | 64 | //Variables for motor control |
jessekaiser | 60:664f9b907c02 | 65 | float setpoint = 2000; //Frequentie setpoint |
jessekaiser | 56:6ea03cce1175 | 66 | float step_freq1 = 1; |
jessekaiser | 56:6ea03cce1175 | 67 | float step_freq2 = 1; |
jessekaiser | 56:6ea03cce1175 | 68 | float step_freq3 = 1; |
jessekaiser | 56:6ea03cce1175 | 69 | float step_freq4 = 1; |
jessekaiser | 56:6ea03cce1175 | 70 | |
jessekaiser | 56:6ea03cce1175 | 71 | //EMG filter |
jessekaiser | 56:6ea03cce1175 | 72 | arm_biquad_casd_df1_inst_f32 lowpass_biceps; |
jessekaiser | 56:6ea03cce1175 | 73 | arm_biquad_casd_df1_inst_f32 lowpass_triceps; |
jessekaiser | 56:6ea03cce1175 | 74 | arm_biquad_casd_df1_inst_f32 lowpass_pect; |
jessekaiser | 56:6ea03cce1175 | 75 | arm_biquad_casd_df1_inst_f32 lowpass_deltoid; |
jessekaiser | 56:6ea03cce1175 | 76 | //lowpass filter settings: Fc = 2 Hz, Fs = 500 Hz, Gain = -3 dB |
jessekaiser | 56:6ea03cce1175 | 77 | float lowpass_const[] = {0.00015514839749793376, 0.00031029679499586753, 0.00015514839749793376, 1.9644602512795832, -0.9650808448695751}; |
jessekaiser | 56:6ea03cce1175 | 78 | arm_biquad_casd_df1_inst_f32 highnotch_biceps; |
jessekaiser | 56:6ea03cce1175 | 79 | arm_biquad_casd_df1_inst_f32 highnotch_triceps; |
jessekaiser | 56:6ea03cce1175 | 80 | arm_biquad_casd_df1_inst_f32 highnotch_pect; |
jessekaiser | 56:6ea03cce1175 | 81 | arm_biquad_casd_df1_inst_f32 highnotch_deltoid; |
jessekaiser | 56:6ea03cce1175 | 82 | //highpass filter settings: Fc = 20 Hz, Fs = 500 Hz, notch Fc = 50, Fs = 500 Hz |
jessekaiser | 56:6ea03cce1175 | 83 | float highnotch_const[] = {0.8370879899975344, -1.6741759799950688, 0.8370879899975344, 1.6474576182593796, -0.7008943417307579, 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, 1.1429772843080923, -0.41279762014290533}; |
jessekaiser | 56:6ea03cce1175 | 84 | |
jessekaiser | 56:6ea03cce1175 | 85 | //state values |
jessekaiser | 56:6ea03cce1175 | 86 | float lowpass_biceps_states[4]; |
jessekaiser | 56:6ea03cce1175 | 87 | float highnotch_biceps_states[8]; |
jessekaiser | 56:6ea03cce1175 | 88 | float lowpass_triceps_states[4]; |
jessekaiser | 56:6ea03cce1175 | 89 | float highnotch_triceps_states[8]; |
jessekaiser | 56:6ea03cce1175 | 90 | float lowpass_pect_states[4]; |
jessekaiser | 56:6ea03cce1175 | 91 | float highnotch_pect_states[8]; |
jessekaiser | 56:6ea03cce1175 | 92 | float lowpass_deltoid_states[4]; |
jessekaiser | 56:6ea03cce1175 | 93 | float highnotch_deltoid_states[8]; |
jessekaiser | 56:6ea03cce1175 | 94 | |
jessekaiser | 56:6ea03cce1175 | 95 | //global variabels |
jessekaiser | 56:6ea03cce1175 | 96 | float filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid; |
jessekaiser | 56:6ea03cce1175 | 97 | float speed_old1, speed_old2, speed_old3, speed_old4; |
jessekaiser | 56:6ea03cce1175 | 98 | float acc1, acc2, acc3, acc4; |
jessekaiser | 56:6ea03cce1175 | 99 | float force1, force2, force3, force4; |
jessekaiser | 56:6ea03cce1175 | 100 | float speed1, speed2, speed3, speed4; |
jessekaiser | 56:6ea03cce1175 | 101 | float damping1, damping2, damping3, damping4; |
jessekaiser | 60:664f9b907c02 | 102 | float emg_x, emg_y; |
jessekaiser | 61:99f3ae6e053b | 103 | float cx = 0; |
jessekaiser | 61:99f3ae6e053b | 104 | float cy = 0; |
jessekaiser | 61:99f3ae6e053b | 105 | float errorx = 0.2; |
jessekaiser | 61:99f3ae6e053b | 106 | float errory = 0.2; |
jessekaiser | 61:99f3ae6e053b | 107 | float Ps_x = 0; |
jessekaiser | 61:99f3ae6e053b | 108 | float Ps_y = 0; |
jessekaiser | 62:509622cce1c6 | 109 | float hstep_freqx = 1; |
jessekaiser | 62:509622cce1c6 | 110 | float hstep_freqy = 1; |
jessekaiser | 56:6ea03cce1175 | 111 | |
jessekaiser | 56:6ea03cce1175 | 112 | void looper_emg() |
jessekaiser | 56:6ea03cce1175 | 113 | { |
jessekaiser | 56:6ea03cce1175 | 114 | float emg_value1_f32, emg_value2_f32, emg_value3_f32, emg_value4_f32; |
jessekaiser | 56:6ea03cce1175 | 115 | emg_value1_f32 = emg1.read(); |
jessekaiser | 56:6ea03cce1175 | 116 | emg_value2_f32 = emg2.read(); |
jessekaiser | 56:6ea03cce1175 | 117 | emg_value3_f32 = emg3.read(); |
jessekaiser | 56:6ea03cce1175 | 118 | emg_value4_f32 = emg4.read(); |
jessekaiser | 56:6ea03cce1175 | 119 | |
jessekaiser | 56:6ea03cce1175 | 120 | //process emg biceps |
jessekaiser | 56:6ea03cce1175 | 121 | arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 ); //High pass and notch filter |
jessekaiser | 60:664f9b907c02 | 122 | filtered_biceps = fabs(filtered_biceps); //Rectifier, The Gain is already implemented. |
jessekaiser | 56:6ea03cce1175 | 123 | arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 ); //low pass filter |
jessekaiser | 56:6ea03cce1175 | 124 | |
jessekaiser | 56:6ea03cce1175 | 125 | //process emg triceps |
jessekaiser | 56:6ea03cce1175 | 126 | arm_biquad_cascade_df1_f32(&highnotch_triceps, &emg_value2_f32, &filtered_triceps, 1 ); |
jessekaiser | 60:664f9b907c02 | 127 | filtered_triceps = fabs(filtered_triceps); |
jessekaiser | 56:6ea03cce1175 | 128 | arm_biquad_cascade_df1_f32(&lowpass_triceps, &filtered_triceps, &filtered_triceps, 1 ); |
jessekaiser | 56:6ea03cce1175 | 129 | |
jessekaiser | 56:6ea03cce1175 | 130 | //process emg pectoralis major |
jessekaiser | 56:6ea03cce1175 | 131 | arm_biquad_cascade_df1_f32(&highnotch_pect, &emg_value3_f32, &filtered_pect, 1 ); |
jessekaiser | 60:664f9b907c02 | 132 | filtered_pect = fabs(filtered_pect); |
jessekaiser | 56:6ea03cce1175 | 133 | arm_biquad_cascade_df1_f32(&lowpass_pect, &filtered_pect, &filtered_pect, 1 ); |
jessekaiser | 56:6ea03cce1175 | 134 | |
jessekaiser | 56:6ea03cce1175 | 135 | //process emg deltoid |
jessekaiser | 56:6ea03cce1175 | 136 | arm_biquad_cascade_df1_f32(&highnotch_deltoid, &emg_value4_f32, &filtered_deltoid, 1 ); |
jessekaiser | 60:664f9b907c02 | 137 | filtered_deltoid = fabs(filtered_deltoid); |
jessekaiser | 56:6ea03cce1175 | 138 | arm_biquad_cascade_df1_f32(&lowpass_deltoid, &filtered_deltoid, &filtered_deltoid, 1 ); |
jessekaiser | 56:6ea03cce1175 | 139 | |
jessekaiser | 58:3ea066215c31 | 140 | /*send value to PC. |
jessekaiser | 56:6ea03cce1175 | 141 | scope.set(0,filtered_biceps); //Filtered EMG signal |
jessekaiser | 56:6ea03cce1175 | 142 | scope.set(1,filtered_triceps); |
jessekaiser | 56:6ea03cce1175 | 143 | scope.set(2,filtered_pect); |
jessekaiser | 57:0a278c60d28b | 144 | scope.set(3,filtered_deltoid);*/ |
jessekaiser | 56:6ea03cce1175 | 145 | } |
jessekaiser | 56:6ea03cce1175 | 146 | |
jessekaiser | 60:664f9b907c02 | 147 | void looper_motory() |
jessekaiser | 60:664f9b907c02 | 148 | { |
jessekaiser | 60:664f9b907c02 | 149 | emg_y = fabs(filtered_biceps - filtered_triceps); |
jessekaiser | 60:664f9b907c02 | 150 | force1 = emg_y*K_Gain; |
jessekaiser | 60:664f9b907c02 | 151 | force1 = force1 - damping1; |
jessekaiser | 60:664f9b907c02 | 152 | acc1 = force1/Mass; |
jessekaiser | 60:664f9b907c02 | 153 | speed1 = speed_old1 + (acc1 * dt); |
jessekaiser | 60:664f9b907c02 | 154 | damping1 = speed1 * Damp; |
jessekaiser | 60:664f9b907c02 | 155 | step_freq1 = setpoint * speed1; |
jessekaiser | 60:664f9b907c02 | 156 | Stepy.period(1.0/step_freq1); |
jessekaiser | 60:664f9b907c02 | 157 | speed_old1 = speed1; |
jessekaiser | 60:664f9b907c02 | 158 | |
jessekaiser | 60:664f9b907c02 | 159 | |
jessekaiser | 66:c094d1868b30 | 160 | if (emg_y > 0) { |
jessekaiser | 60:664f9b907c02 | 161 | Diry = 1; |
jessekaiser | 60:664f9b907c02 | 162 | } |
jessekaiser | 60:664f9b907c02 | 163 | |
jessekaiser | 66:c094d1868b30 | 164 | if (emg_y < 0) { |
jessekaiser | 60:664f9b907c02 | 165 | Diry = 0; |
jessekaiser | 60:664f9b907c02 | 166 | } |
jessekaiser | 60:664f9b907c02 | 167 | //Speed limit |
jessekaiser | 60:664f9b907c02 | 168 | if (speed1 > 1) { |
jessekaiser | 60:664f9b907c02 | 169 | speed1 = 1; |
jessekaiser | 60:664f9b907c02 | 170 | step_freq1 = setpoint; |
jessekaiser | 60:664f9b907c02 | 171 | } |
jessekaiser | 60:664f9b907c02 | 172 | //EMG treshold |
jessekaiser | 60:664f9b907c02 | 173 | if (filtered_biceps < EMG_tresh1 && filtered_triceps < EMG_tresh2) { |
jessekaiser | 60:664f9b907c02 | 174 | Enabley = 1; //Enable = 1 turns the motor off. |
jessekaiser | 60:664f9b907c02 | 175 | } else { |
jessekaiser | 60:664f9b907c02 | 176 | Enabley = 0; |
jessekaiser | 60:664f9b907c02 | 177 | } |
jessekaiser | 60:664f9b907c02 | 178 | |
jessekaiser | 60:664f9b907c02 | 179 | } |
jessekaiser | 60:664f9b907c02 | 180 | |
jessekaiser | 60:664f9b907c02 | 181 | void looper_motorx() |
jessekaiser | 60:664f9b907c02 | 182 | { |
jessekaiser | 60:664f9b907c02 | 183 | emg_x = fabs(filtered_pect - filtered_deltoid); |
jessekaiser | 60:664f9b907c02 | 184 | force2 = emg_x*K_Gain; |
jessekaiser | 60:664f9b907c02 | 185 | force2 = force2 - damping2; |
jessekaiser | 60:664f9b907c02 | 186 | acc2 = force2/Mass; |
jessekaiser | 60:664f9b907c02 | 187 | speed2 = speed_old2 + (acc2 * dt); |
jessekaiser | 60:664f9b907c02 | 188 | damping2 = speed2 * Damp; |
jessekaiser | 60:664f9b907c02 | 189 | step_freq2 = setpoint * speed2; |
jessekaiser | 60:664f9b907c02 | 190 | Stepx.period(1.0/step_freq2); |
jessekaiser | 60:664f9b907c02 | 191 | speed_old2 = speed2; |
jessekaiser | 60:664f9b907c02 | 192 | |
jessekaiser | 66:c094d1868b30 | 193 | if (emg_x > 0) { |
jessekaiser | 60:664f9b907c02 | 194 | Dirx = 0; |
jessekaiser | 60:664f9b907c02 | 195 | } |
jessekaiser | 66:c094d1868b30 | 196 | if (emg_x < 0) { |
jessekaiser | 60:664f9b907c02 | 197 | Dirx = 1; |
jessekaiser | 60:664f9b907c02 | 198 | } |
jessekaiser | 60:664f9b907c02 | 199 | //Speed limit |
jessekaiser | 60:664f9b907c02 | 200 | if (speed2 > 1) { |
jessekaiser | 60:664f9b907c02 | 201 | speed2 = 1; |
jessekaiser | 60:664f9b907c02 | 202 | step_freq2 = setpoint; |
jessekaiser | 60:664f9b907c02 | 203 | } |
jessekaiser | 60:664f9b907c02 | 204 | //EMG treshold |
jessekaiser | 60:664f9b907c02 | 205 | if (filtered_pect < EMG_tresh3 && filtered_deltoid < EMG_tresh4) { |
jessekaiser | 60:664f9b907c02 | 206 | Enablex = 1; //Enable = 1 turns the motor off. |
jessekaiser | 60:664f9b907c02 | 207 | } else { |
jessekaiser | 60:664f9b907c02 | 208 | Enablex = 0; |
jessekaiser | 60:664f9b907c02 | 209 | } |
jessekaiser | 60:664f9b907c02 | 210 | |
jessekaiser | 60:664f9b907c02 | 211 | } |
jessekaiser | 59:ba22a8c26dee | 212 | |
jessekaiser | 59:ba22a8c26dee | 213 | int main() |
jessekaiser | 56:6ea03cce1175 | 214 | { |
jessekaiser | 59:ba22a8c26dee | 215 | // Attach the HIDScope::send method from the scope object to the timer at 500Hz. Hier wordt de sample freq aangegeven. |
jessekaiser | 59:ba22a8c26dee | 216 | // scopeTimer.attach_us(&scope, &HIDScope::send, 2e3); |
jessekaiser | 66:c094d1868b30 | 217 | |
jessekaiser | 59:ba22a8c26dee | 218 | MS1 = 1; |
jessekaiser | 59:ba22a8c26dee | 219 | MS2 = 0; |
jessekaiser | 59:ba22a8c26dee | 220 | MS3 = 0; |
jessekaiser | 66:c094d1868b30 | 221 | |
jessekaiser | 59:ba22a8c26dee | 222 | Stepx.write(0.5); // Duty cycle of 50% |
jessekaiser | 59:ba22a8c26dee | 223 | Stepy.write(0.5); |
jessekaiser | 66:c094d1868b30 | 224 | |
jessekaiser | 63:e336c16c8957 | 225 | Enablex = 1; |
jessekaiser | 63:e336c16c8957 | 226 | Enabley = 1; |
jessekaiser | 66:c094d1868b30 | 227 | wait(1); |
jessekaiser | 62:509622cce1c6 | 228 | lcd.printf("Start homing"); |
jessekaiser | 62:509622cce1c6 | 229 | wait(2); |
jessekaiser | 62:509622cce1c6 | 230 | lcd.cls(); |
jessekaiser | 62:509622cce1c6 | 231 | wait(1); |
jessekaiser | 62:509622cce1c6 | 232 | Enablex = 0; |
jessekaiser | 62:509622cce1c6 | 233 | Enabley = 0; |
jessekaiser | 66:c094d1868b30 | 234 | while(errorx > 0.03 || errory > 0.03) { |
jessekaiser | 66:c094d1868b30 | 235 | lcd.printf("%.2f %.2f \n", Ps_x, Ps_y); |
jessekaiser | 59:ba22a8c26dee | 236 | |
jessekaiser | 66:c094d1868b30 | 237 | Ps_x = Posx.read(); |
jessekaiser | 66:c094d1868b30 | 238 | Ps_y = Posy.read(); |
jessekaiser | 62:509622cce1c6 | 239 | |
jessekaiser | 66:c094d1868b30 | 240 | if (Ps_x < 0.88) { |
jessekaiser | 61:99f3ae6e053b | 241 | Dirx = 0; |
jessekaiser | 61:99f3ae6e053b | 242 | errorx = Pt_x - Ps_x; |
jessekaiser | 61:99f3ae6e053b | 243 | cx = errorx * H_Gain; |
jessekaiser | 66:c094d1868b30 | 244 | |
jessekaiser | 62:509622cce1c6 | 245 | float hnew_step_freqx; |
jessekaiser | 62:509622cce1c6 | 246 | hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx); |
jessekaiser | 62:509622cce1c6 | 247 | hstep_freqx = hnew_step_freqx; |
jessekaiser | 62:509622cce1c6 | 248 | Stepx.period(1.0/hstep_freqx); |
jessekaiser | 62:509622cce1c6 | 249 | wait(0.01); |
jessekaiser | 66:c094d1868b30 | 250 | } |
jessekaiser | 66:c094d1868b30 | 251 | if (Ps_y > 0.25) { |
jessekaiser | 61:99f3ae6e053b | 252 | Diry = 0; |
jessekaiser | 61:99f3ae6e053b | 253 | errory = Ps_y - Pt_y; |
jessekaiser | 61:99f3ae6e053b | 254 | cy = errory * H_Gain; |
jessekaiser | 66:c094d1868b30 | 255 | |
jessekaiser | 62:509622cce1c6 | 256 | float hnew_step_freqy; |
jessekaiser | 62:509622cce1c6 | 257 | hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); |
jessekaiser | 62:509622cce1c6 | 258 | hstep_freqy = hnew_step_freqy; |
jessekaiser | 62:509622cce1c6 | 259 | Stepy.period(1.0/hstep_freqy); |
jessekaiser | 62:509622cce1c6 | 260 | wait(0.01); |
jessekaiser | 66:c094d1868b30 | 261 | } |
jessekaiser | 66:c094d1868b30 | 262 | |
jessekaiser | 61:99f3ae6e053b | 263 | } |
jessekaiser | 62:509622cce1c6 | 264 | lcd.cls(); |
jessekaiser | 62:509622cce1c6 | 265 | wait(1); |
jessekaiser | 62:509622cce1c6 | 266 | lcd.printf("Done"); |
jessekaiser | 66:c094d1868b30 | 267 | wait(1); |
jessekaiser | 66:c094d1868b30 | 268 | lcd.cls(); |
jessekaiser | 66:c094d1868b30 | 269 | wait(1); |
jessekaiser | 62:509622cce1c6 | 270 | Enablex = 1; |
jessekaiser | 61:99f3ae6e053b | 271 | Enabley = 1; |
jessekaiser | 64:5a66361c8318 | 272 | wait(3); |
jessekaiser | 66:c094d1868b30 | 273 | lcd.printf("Start EMG Control"); |
jessekaiser | 66:c094d1868b30 | 274 | wait(2); |
jessekaiser | 66:c094d1868b30 | 275 | lcd.cls(); |
jessekaiser | 66:c094d1868b30 | 276 | wait(1); |
jessekaiser | 66:c094d1868b30 | 277 | Enablex = 0; |
jessekaiser | 66:c094d1868b30 | 278 | Enabley = 0; |
jessekaiser | 66:c094d1868b30 | 279 | Ticker emgtimer; //biceps |
jessekaiser | 66:c094d1868b30 | 280 | arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states); |
jessekaiser | 66:c094d1868b30 | 281 | arm_biquad_cascade_df1_init_f32(&highnotch_biceps, 2 , highnotch_const, highnotch_biceps_states); |
jessekaiser | 66:c094d1868b30 | 282 | //triceps |
jessekaiser | 66:c094d1868b30 | 283 | arm_biquad_cascade_df1_init_f32(&lowpass_triceps, 1 , lowpass_const, lowpass_triceps_states); |
jessekaiser | 66:c094d1868b30 | 284 | arm_biquad_cascade_df1_init_f32(&highnotch_triceps, 2 , highnotch_const, highnotch_triceps_states); |
jessekaiser | 66:c094d1868b30 | 285 | //pectoralis major |
jessekaiser | 66:c094d1868b30 | 286 | arm_biquad_cascade_df1_init_f32(&lowpass_pect, 1 , lowpass_const, lowpass_pect_states); |
jessekaiser | 66:c094d1868b30 | 287 | arm_biquad_cascade_df1_init_f32(&highnotch_pect, 2 , highnotch_const, highnotch_pect_states); |
jessekaiser | 66:c094d1868b30 | 288 | //deltoid |
jessekaiser | 66:c094d1868b30 | 289 | arm_biquad_cascade_df1_init_f32(&lowpass_deltoid, 1 , lowpass_const, lowpass_deltoid_states); |
jessekaiser | 66:c094d1868b30 | 290 | arm_biquad_cascade_df1_init_f32(&highnotch_deltoid, 2 , highnotch_const, highnotch_deltoid_states); |
jessekaiser | 66:c094d1868b30 | 291 | emgtimer.attach(looper_emg, 0.01); |
jessekaiser | 59:ba22a8c26dee | 292 | |
jessekaiser | 66:c094d1868b30 | 293 | Ticker looptimer1; |
jessekaiser | 66:c094d1868b30 | 294 | looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor, why this freq? |
jessekaiser | 61:99f3ae6e053b | 295 | |
jessekaiser | 66:c094d1868b30 | 296 | Ticker looptimer2; |
jessekaiser | 66:c094d1868b30 | 297 | looptimer2.attach(looper_motory, 0.01); //Y-Spindle motor |
jessekaiser | 61:99f3ae6e053b | 298 | |
jessekaiser | 66:c094d1868b30 | 299 | //Microstepping control, now configured as half stepping (MS1=1,MS2=0,MS3=0) |
jessekaiser | 66:c094d1868b30 | 300 | MS1 = 1; |
jessekaiser | 66:c094d1868b30 | 301 | MS2 = 0; |
jessekaiser | 66:c094d1868b30 | 302 | MS3 = 0; |
jessekaiser | 66:c094d1868b30 | 303 | Stepx.write(0.5); // Duty cycle of 50% |
jessekaiser | 66:c094d1868b30 | 304 | Stepy.write(0.5); |
jessekaiser | 59:ba22a8c26dee | 305 | |
jessekaiser | 58:3ea066215c31 | 306 | |
jessekaiser | 66:c094d1868b30 | 307 | while (1) { |
jessekaiser | 61:99f3ae6e053b | 308 | |
jessekaiser | 60:664f9b907c02 | 309 | |
jessekaiser | 66:c094d1868b30 | 310 | //lcd.printf("x %.2f, y %.2f \n", Posx.read(), Posy.read()); |
jessekaiser | 66:c094d1868b30 | 311 | //lcd.printf("%.2f, %.2f %.2f %.2f \n", filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid); //Filtered EMG values |
jessekaiser | 66:c094d1868b30 | 312 | //lcd.printf("1 %.0f, 2 %.0f \n", step_freq1, step_freq2); //step_freq value of every EMG sensor |
jessekaiser | 66:c094d1868b30 | 313 | lcd.printf("%.2f %.2f \n", force1, force2); |
jessekaiser | 66:c094d1868b30 | 314 | //lcd.printf("%.2f, %.2f %.2f %.2f \n", gain_biceps, gain_triceps, gain_pect, gain_deltoid); |
jessekaiser | 66:c094d1868b30 | 315 | //lcd.printf("%.2f, %.2f %.2f %.2f \n", norm_biceps, norm_triceps, norm_pect, norm_deltoid); |
jessekaiser | 66:c094d1868b30 | 316 | wait(0.01); |
jessekaiser | 61:99f3ae6e053b | 317 | |
jessekaiser | 66:c094d1868b30 | 318 | } |
jessekaiser | 56:6ea03cce1175 | 319 | } |