![](/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
- Committer:
- jessekaiser
- Date:
- 2015-06-22
- Revision:
- 76:627b0537110e
- Parent:
- 75:9fa809932adf
- Child:
- 77:f3290f86ae4f
File content as of revision 76:627b0537110e:
/*Code by Jesse Kaiser, s1355783 for control of the 2DOF Planar Table Some variables are also numbered at the end. The numbers stands for the muscle that controls it. Biceps = 1 Triceps = 2 Pectoralis Major = 3 Deltoid = 4 The "x" and "y" at the end of variables stand for the X-Spindle or Y-Spindle respectivly. */ #include "mbed.h" #include "C12832_lcd.h" #include "arm_math.h" //#include "HIDScope.h" #define P_Gain 0.99 #define K_Gain 150 //Gain of the filtered EMG signal #define Damp 5 //Deceleration of the motor #define Mass 1 // Mass value #define dt 0.01 //Sample frequency #define EMG_tresh1 0.01 #define EMG_tresh2 0.01 #define EMG_tresh3 0.01 #define EMG_tresh4 0.01 #define H_Gain 3.5 #define Pt_x 0.83 #define error_tresh 0.01 //Motor control DigitalOut Dirx(p25); PwmOut Stepx(p26); //Signal to and from computer Serial pc(USBTX, USBRX); //Position sensors AnalogIn Posx(p20); DigitalOut Enablex(p30); //Microstepping DigitalOut MS1(p27); DigitalOut MS2(p28); DigitalOut MS3(p29); //EMG inputs AnalogIn emg1(p19); AnalogIn emg2(p18); //HIDScope scope(4); //Ticker scopeTimer; //lcd screen C12832_LCD lcd; //Variables for motor control float setpoint = 2000; //Frequentie setpoint float step_freq2 = 1; //EMG filter arm_biquad_casd_df1_inst_f32 lowpass_biceps; arm_biquad_casd_df1_inst_f32 lowpass_triceps; //lowpass filter settings: Fc = 2 Hz, Fs = 500 Hz float lowpass_const[] = {0.00015514839749793376, 0.00031029679499586753, 0.00015514839749793376, 1.9644602512795832, -0.9650808448695751}; arm_biquad_casd_df1_inst_f32 highnotch_biceps; arm_biquad_casd_df1_inst_f32 highnotch_triceps; //highpass filter settings: Fc = 20 Hz, Fs = 500 Hz, notch Fc = 50, Fs = 500 Hz float highnotch_const[] = {0.8370879899975344, -1.6741759799950688, 0.8370879899975344, 1.6474576182593796, -0.7008943417307579, 0.7063988100714527, -1.1429772843080923, 0.7063988100714527, 1.1429772843080923, -0.41279762014290533}; //state values float lowpass_biceps_states[4]; float highnotch_biceps_states[8]; float lowpass_triceps_states[4]; float highnotch_triceps_states[8]; //global variabels float filtered_biceps, filtered_triceps; float speed_old1, speed_old2; float acc1, acc2; float force1, force2; float speed1, speed2; float damping1, damping2; float emg_x, emg_y; float cx = 0; float cy = 0; float errorx = 0.2; float errory = 0.2; float Ps_x = 0; float Ps_y = 0; float hstep_freqx = 1; float hstep_freqy = 1; float emg_y_abs = 0; float emg_x_abs = 0; void looper_emg() { float emg_value1_f32, emg_value2_f32; emg_value1_f32 = emg1.read(); emg_value2_f32 = emg2.read(); //process emg biceps arm_biquad_cascade_df1_f32(&highnotch_biceps, &emg_value1_f32, &filtered_biceps, 1 ); //High pass and notch filter filtered_biceps = fabs(filtered_biceps); //Rectifier, The Gain is already implemented. arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 ); //low pass filter //process emg triceps arm_biquad_cascade_df1_f32(&highnotch_triceps, &emg_value2_f32, &filtered_triceps, 1 ); filtered_triceps = fabs(filtered_triceps); arm_biquad_cascade_df1_f32(&lowpass_triceps, &filtered_triceps, &filtered_triceps, 1 ); /*send value to PC. scope.set(0,filtered_biceps); //Filtered EMG signal scope.set(1,filtered_triceps); scope.set(2,filtered_pect); scope.set(3,filtered_deltoid);*/ } void looper_motorx() { emg_x = (filtered_biceps - filtered_triceps); emg_x_abs = fabs(emg_x); force2 = emg_x_abs*K_Gain; force2 = force2 - damping2; acc2 = force2/Mass; speed2 = speed_old2 + (acc2 * dt); damping2 = speed2 * Damp; step_freq2 = setpoint * speed2; Stepx.period(1.0/step_freq2); speed_old2 = speed2; if (emg_x > 0) { Dirx = 0; } if (emg_x < 0) { Dirx = 1; } //Speed limit if (speed2 > 1) { speed2 = 1; step_freq2 = setpoint; } //EMG treshold if (filtered_biceps < EMG_tresh1 && filtered_triceps < EMG_tresh2) { Enablex = 1; //Enable = 1 turns the motor off. } else { Enablex = 0; } } int main() { // Attach the HIDScope::send method from the scope object to the timer at 500Hz. Hier wordt de sample freq aangegeven. // scopeTimer.attach_us(&scope, &HIDScope::send, 2e3); MS1 = 1; MS2 = 0; MS3 = 0; Stepx.write(0.5); // Duty cycle of 50% Enablex = 1; wait(1); pc.printf("Start homing"); wait(2); wait(1); Enablex = 0; //Homing of the motor, so you start from the same position every time. while(errorx > error_tresh) { Ps_x = Posx.read(); errorx = fabs(Pt_x - Ps_x); pc.printf("%.2f \n", Stepx.read()); if (Ps_x < Pt_x && errorx > error_tresh) { Dirx = 0; cx = errorx * H_Gain; float hnew_step_freqx; hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx); hstep_freqx = hnew_step_freqx; Stepx.period(1.0/hstep_freqx); wait(0.01); } if (Ps_x > Pt_x && errorx > error_tresh) { Dirx = 1; cx = errorx * H_Gain; float hnew_step_freqx; hnew_step_freqx = ((1-P_Gain)*setpoint*cx) + (P_Gain*hstep_freqx); hstep_freqx = hnew_step_freqx; Stepx.period(1.0/hstep_freqx); wait(0.01); } } pc.printf("Done"); wait(5); wait(1); Enablex = 1; wait(3); pc.printf("Start EMG Control"); wait(2); wait(1); Enablex = 0; MS1 = 1; MS2 = 0; MS3 = 0; Stepx.write(0.5); // Duty cycle of 50% Ticker emgtimer; //biceps arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states); arm_biquad_cascade_df1_init_f32(&highnotch_biceps, 2 , highnotch_const, highnotch_biceps_states); //triceps arm_biquad_cascade_df1_init_f32(&lowpass_triceps, 1 , lowpass_const, lowpass_triceps_states); arm_biquad_cascade_df1_init_f32(&highnotch_triceps, 2 , highnotch_const, highnotch_triceps_states); emgtimer.attach(looper_emg, 0.01); Ticker looptimer1; looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor, why this freq? //Ticker looptimer2; //looptimer2.attach(looper_motory, 0.01); //Y-Spindle motor //Microstepping control, now configured as half stepping (MS1=1,MS2=0,MS3=0) while (1) { //pc.printf("x %.2f, y %.2f \n", Posx.read(), Posy.read()); // pc.printf("%.2f %.2f \n", Stepx.read(), step_freq2); wait(0.01); } }