4 directional EMG control of the XY table. Made during my bachelor end assignment.
Dependencies: C12832_lcd HIDScope mbed-dsp mbed
Revision 83:067e07db027c, committed 2015-06-22
- Comitter:
- jessekaiser
- Date:
- Mon Jun 22 20:30:23 2015 +0000
- Parent:
- 82:bcd96c98af2d
- Child:
- 84:8b3f18260431
- Commit message:
- Volledige code. Niet 100% zeker of alles werkt.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Jun 22 20:22:57 2015 +0000 +++ b/main.cpp Mon Jun 22 20:30:23 2015 +0000 @@ -1,5 +1,5 @@ /*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. +Some variables are also numbered at the end. The numbers stand for the muscle that controls it. Biceps = 1 Triceps = 2 Pectoralis Major = 3 @@ -94,11 +94,11 @@ //global variabels float filtered_biceps, filtered_triceps, filtered_pect, filtered_deltoid; -float speed_old1, speed_old2, speed_old3, speed_old4; -float acc1, acc2, acc3, acc4; -float force1, force2, force3, force4; -float speed1, speed2, speed3, speed4; -float damping1, damping2, damping3, damping4; +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; @@ -121,7 +121,7 @@ //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. + filtered_biceps = fabs(filtered_biceps); //Rectifier arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_biceps, &filtered_biceps, 1 ); //low pass filter //process emg triceps @@ -148,7 +148,7 @@ void looper_motory() { - + //Motor control emg_y = (filtered_biceps - filtered_triceps); emg_y_abs = fabs(emg_y); force1 = emg_y_abs*K_Gain; @@ -160,10 +160,11 @@ Stepy.period(1.0/step_freq1); speed_old1 = speed1; + //Direction control. 1 is up. if (emg_y > 0) { Diry = 1; } - + //Direction control. 0 is down. if (emg_y < 0) { Diry = 0; } @@ -181,9 +182,9 @@ } -/*void looper_motorx() +void looper_motorx() { - + //Motor control. emg_x = (filtered_pect - filtered_deltoid); emg_x_abs = fabs(emg_x); force2 = emg_x_abs*K_Gain; @@ -194,10 +195,11 @@ step_freq2 = setpoint * speed2; Stepx.period(1.0/step_freq2); speed_old2 = speed2; - + //Direction control. 0 is to the left. if (emg_x > 0) { Dirx = 0; } + //Direction control. 1 is to the right. if (emg_x < 0) { Dirx = 1; } @@ -213,13 +215,13 @@ 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); -/* + //Microstepping control, now configured as half stepping (MS1=1,MS2=0,MS3=0) MS1 = 1; MS2 = 0; MS3 = 0; @@ -236,6 +238,7 @@ wait(1); Enablex = 0; Enabley = 0; + //Homing loop. while(errorx > error_tresh || errory > error_tresh) { Ps_x = Posx.read(); @@ -243,24 +246,20 @@ errorx = fabs(Pt_x - Ps_x); errory = fabs(Ps_y - Pt_y); lcd.printf("%.2f %.2f \n", errorx, errory); - - - if (Ps_x < 0.50 && errorx > error_tresh) { + + + if (Ps_x < Pt_x && errorx > error_tresh) { Dirx = 0; - //errorx = Pt_x - Ps_x; 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_y > 0.50 && errory > error_tresh) { + if (Ps_y > Pt_y && errory > error_tresh) { Diry = 0; - //errory = Ps_y - Pt_y; cy = errory * H_Gain; - float hnew_step_freqy; hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); hstep_freqy = hnew_step_freqy; @@ -268,22 +267,18 @@ wait(0.01); } - if (Ps_x > 0.50 && errorx > error_tresh) { + if (Ps_x > Pt_x && errorx > error_tresh) { Dirx = 1; - //errorx = Pt_x - Ps_x; 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_y < 0.50 && errory > error_tresh) { + if (Ps_y < Pt_y && errory > error_tresh) { Diry = 1; - //errory = Ps_y - Pt_y; cy = errory * H_Gain; - float hnew_step_freqy; hnew_step_freqy = ((1-P_Gain)*setpoint*cy) + (P_Gain*hstep_freqy); hstep_freqy = hnew_step_freqy; @@ -305,12 +300,6 @@ wait(1); Enablex = 0; Enabley = 0; -*/ - MS1 = 1; - MS2 = 0; - MS3 = 0; - Stepx.write(0.5); // Duty cycle of 50% - Stepy.write(0.5); Ticker emgtimer; //biceps arm_biquad_cascade_df1_init_f32(&lowpass_biceps, 1 , lowpass_const, lowpass_biceps_states); @@ -326,21 +315,16 @@ arm_biquad_cascade_df1_init_f32(&highnotch_deltoid, 2 , highnotch_const, highnotch_deltoid_states); emgtimer.attach(looper_emg, 0.01); - //Ticker looptimer1; - //looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor, why this freq? + Ticker looptimer1; + looptimer1.attach(looper_motorx, 0.01); //X-Spindle motor 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("%.2f %.2f %.2f \n", Posy.read(), emg_y, step_freq1); //Send signal values to the computer. - wait(0.01); + wait(0.01); } }