
fancy lampje
Dependencies: mbed QEI HIDScope biquadFilter MODSERIAL FXOS8700Q FastPWM
main.cpp
- Committer:
- MatthewMaat
- Date:
- 2019-10-25
- Revision:
- 23:b0222fa7c131
- Parent:
- 22:335a30b0825d
- Child:
- 24:3c9ac44890f0
File content as of revision 23:b0222fa7c131:
#include "mbed.h" #include "HIDScope.h" #include "QEI.h" #include "MODSERIAL.h" #include "BiQuad.h" #include "FastPWM.h" #include <iostream> MODSERIAL pc(USBTX, USBRX); QEI motor2_pos (D8, D9, NC, 32); QEI motor1_pos (D12, D13, NC, 32); AnalogIn ain2(A2); AnalogIn ain1(A3); DigitalOut dir2(D4); DigitalOut dir1(D7); //D4,D7 direction of motors 2,1 on board, D5,D6- PWM of motors 2,1 on board PwmOut motor2_pwm(D5); PwmOut motor1_pwm(D6); AnalogIn emg0( A0 ); AnalogIn emg1( A1 ); Ticker ticktick; Timer state_time; Timeout EMG_peakx; Timeout turnx; Timeout EMG_peaky; Timeout turny; Ticker sample_timer; HIDScope scope( 4); DigitalOut ledred(LED_RED); DigitalOut ledblue(LED_BLUE); DigitalOut ledgreen(LED_GREEN); InterruptIn err(SW2); InterruptIn button(SW3); volatile float P0; volatile float P1; volatile float EMG_min0=1; volatile float EMG_max0=0; volatile float EMG_min1=1; volatile float EMG_max1=0; volatile bool ignore_peaksx=false; volatile bool ignore_turnx=true; volatile bool ignore_peaksy=false; volatile bool ignore_turny=true; enum states{Waiting,Position_calibration,EMG_calibration,Homing,Operating,Demo,Failure}; states currentState=Waiting; volatile bool motor1_calibrated=false; volatile float x_ref=0.175; volatile float y_ref=0.175; static float v_refx; //reference speeds static float v_refy; static float T = 0.002; //measuring period static float L = 0.35; //distance between the motor axles, has to be checked volatile float theta1_old = 0.0; //feedback variables volatile float theta2_old = 0.0; float error_1; float error_2; //PID controller variables bool q = true; float error_prev; static float Kp = 24.6; static float Ki = 28.3; static float Kd = 0.1; float u; float u_p; float u_i; float u_d; float error_integral1 = 0.0; float error_integral2 = 0.0; float u_1; float u_2; const float angle2_offset=asin(0.2); const float angle1_offset=atan(2.2/32.8); const double pi=3.1415926535897932384626; volatile float theta1; volatile float theta2; void read_emg() { //EMG signal 0 static int count0=0; static float RMS_value0=0; static float HighPass_value0=0; count0+=1; static float RMS0[150]; static float HighPass0[30]; static BiQuad Notch0(0.9695f,-1.5695f,0.9695f,-1.5695f,0.9391f); static BiQuad Notch1(0.9695f,-1.5695f,0.9695f,-1.5695f,0.9391f); float I0; float If0; //signal 1 static int count1=0; static float RMS_value1=0; static float HighPass_value1=0; count1+=1; static float RMS1[150]; static float HighPass1[30]; float I1; float If1; I0=emg0.read(); //read signal double notched0=Notch0.step(I0); HighPass_value0+=(notched0-HighPass0[count0%30])/30.0; HighPass0[count0%30]=notched0; If0=pow(I0-HighPass_value0,2.0f); // Highpass-filtered value squared RMS_value0+=(If0-RMS0[count0%150])/150.0; RMS0[count0%150]=If0; /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ P0=sqrt(RMS_value0); I1=emg1.read(); //read signal double notched1=Notch1.step(I1); HighPass_value1+=(notched1-HighPass1[count1%30])/30.0; HighPass1[count1%30]=notched1; If1=pow(I1-HighPass_value1,2.0f); // Highpass-filtered value squared RMS_value1+=(If1-RMS1[count1%150])/150.0; RMS1[count1%150]=If1; /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */ P1=sqrt(RMS_value1); /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) * Ensure that enough channels are available (HIDScope scope( 2 )) * Finally, send all channels to the PC at once */ /* To indicate that the function is working, the LED is toggled */ ledred=1; ledgreen=0; ledblue=1; } void get_angles(void) { float pulses1=motor1_pos.getPulses(); float pulses2=motor2_pos.getPulses(); theta1=angle1_offset+pulses1*17.0/16.0*2*pi/131.0/32.0; theta2=angle2_offset+pulses2*17.0/16.0*2*pi/131.0/32.0; } void pos_cal(void) { float t=state_time.read(); static int pos_time_counter=0; static int last_ticks=10000; float pulses; pos_time_counter+=1; if(!motor1_calibrated&&t>1.0f) { dir1=1; //??? motor1_pwm.write(0.75f); pulses=motor1_pos.getPulses(); if(pos_time_counter%500==0&&fabs(pulses-last_ticks)<1) { ledblue=!ledblue; motor1_pos.reset(); last_ticks=10000; } else if(pos_time_counter%500==0) { last_ticks=motor1_pos.getPulses(); } } else if(!motor1_calibrated) { motor2_pwm.write(0.75f); dir2=1; } else if(t>1.0f) { motor1_pwm.write(0.0f); dir2=1; //??? motor2_pwm.write(0.75f); pulses=motor2_pos.getPulses(); if(pos_time_counter%500==0&&fabs(pulses-last_ticks)<1) { ledblue=!ledblue; motor2_pos.reset(); motor2_pwm.write(0.0f); } else if(pos_time_counter%500==0) { last_ticks=motor2_pos.getPulses(); } } } void record_min_max(void) { float t=state_time.read(); if(t>0.4) { if(P0<EMG_min0) { EMG_min0=P0; } else if(P0>EMG_max0) { EMG_max0=P0; } if(P1<EMG_min1) { EMG_min1=P1; } else if(P1>EMG_max1) { EMG_max1=P1; } } } void unignore_peaksx(void) { ignore_peaksx=false; } void start_ignore_turnx(void) { ignore_turnx=true; } void unignore_peaksy(void) { ignore_peaksy=false; } void start_ignore_turny(void) { ignore_turny=true; } void set_v_refxy(void) { static bool motorx_on=false; static float signx=0.15; float Q0; Q0=2.0f*(P0-(EMG_min0+EMG_max0)/2.0f)/(EMG_max0-EMG_min0); static bool motory_on=false; static float signy=0.15; float Q1; Q1=2.0f*(P1-(EMG_min1+EMG_max1)/2.0f)/(EMG_max1-EMG_min1); if (Q0>-0.2f && !ignore_peaksx) { if (motorx_on) { v_refx=0; EMG_peakx.attach(unignore_peaksx,0.8); turnx.attach(start_ignore_turnx,1); ignore_turnx=false; ignore_peaksx=true; motorx_on=false; } else if(ignore_turnx) { v_refx=1.0*signx; EMG_peakx.attach(unignore_peaksx,0.8); turnx.attach(start_ignore_turnx,1); ignore_turnx=false; ignore_peaksx=true; motorx_on=true; } else { signx=-1.0*signx; v_refx=1.0*signx; EMG_peakx.attach(unignore_peaksx,1.5); ignore_peaksx=true; motorx_on=true; } } if (Q1>-0.2f && !ignore_peaksy) { if (motory_on) { v_refy=0; EMG_peaky.attach(unignore_peaksy,0.8); turny.attach(start_ignore_turny,1); ignore_turny=false; ignore_peaksy=true; motory_on=false; } else if(ignore_turny) { v_refy=1.0*signy; EMG_peaky.attach(unignore_peaksy,0.8); turny.attach(start_ignore_turny,1); ignore_turny=false; ignore_peaksy=true; motory_on=true; } else { signy=-1.0*signy; v_refy=1.0*signy; EMG_peaky.attach(unignore_peaksy,1.5); ignore_peaksy=true; motory_on=true; } } } void error(void){ float dvd=L * (tan(theta2) * pow(tan(theta1),2) + tan(theta1) * pow(tan(theta2),2));//+ is aangepast float a = ( -pow(cos(theta1),2) * pow((tan(theta1)+tan(theta2)),2) * pow(tan(theta1),2) ) / dvd; // inverse matrix components of differential x and differential y float b = ( pow(cos(theta1),2) * pow((tan(theta1)+tan(theta2)),2) * tan(theta1) ) / dvd; float c = ( pow(cos(theta2),2) * pow((tan(theta1)+tan(theta2)),2) * pow(tan(theta2),2) ) / dvd; float d = ( pow(cos(theta2),2) * pow((tan(theta1)+tan(theta2)),2) * pow(tan(theta2),2) ) / dvd; float theta1_dot = a*v_refx + b*v_refy; float theta2_dot = c*v_refx + d*v_refy; float theta1_ref; float theta2_ref; if(currentState==Operating) { theta1_ref = theta1_old+theta1_dot*T; theta2_ref = theta2_old+theta2_dot*T; } else if(currentState==Homing) { theta1_ref=pi/4.0f; theta2_ref=pi/4.0f; } else if(currentState==Demo) { theta1_ref=atan(y_ref/x_ref); theta2_ref=atan(y_ref/(0.35f-x_ref)); } error_1 = theta1 - theta1_ref; error_2 = theta2 - theta2_ref; theta1_old = theta1_ref; theta2_old = theta2_ref; } float PID1(float err){ //P action u_p = Kp * err; //I action error_integral1 = error_integral1 + err * T; u_i = Ki * error_integral1; /* //D action if(q){ error_prev = err; q=false; } float error_derivative = (err - error_prev)/T; float filtered_error_derivative = LowPassFilter.step(error_derivative); u_d = Kd * filtered_error_derivative; error_prev = err; */ u = u_p + u_i; return u; } float PID2(float err){ //P action u_p = Kp * err; //I action error_integral2 = error_integral2 + err * T; u_i = Ki * error_integral2; /* //D action if(q){ error_prev = err; q=false; } float error_derivative = (err - error_prev)/T; float filtered_error_derivative = LowPassFilter.step(error_derivative); u_d = Kd * filtered_error_derivative; error_prev = err; */ u = u_p + u_i; return u; } void set_refxy(void) { float t=state_time.read(); int tfloor=floor(t)/1; if(tfloor%8==0||tfloor%8==1) { x_ref=0.075+0.05*(t-tfloor+tfloor%8); y_ref=0.2-0.05*(t-tfloor+tfloor%8); } else if(tfloor%8==2||tfloor%8==3) { x_ref=0.175+0.05*(t-tfloor+tfloor%8-2); y_ref=0.1+0.05*(t-tfloor+tfloor%8-2); } else if(tfloor%8==4||tfloor%8==5) { x_ref=0.275-0.05*(t-tfloor+tfloor%8-4); y_ref=0.2+0.05*(t-tfloor+tfloor%8-4); } else { x_ref=0.175-0.05*(t-tfloor+tfloor%8-6); y_ref=0.3-0.05*(t-tfloor+tfloor%8-6); } } void setPWM_controller(void){ error(); u_1 = PID1(error_1); u_2 = PID2(error_2); if(u_1 < 0.0f){ dir1 = 0; }else{ dir1 = 1; } motor1_pwm.write(min(fabs(u_1),1.0f)); if(u_2 < 0.0f){ dir2 = 0; }else{ dir2 = 1; } motor2_pwm.write(min(fabs(u_2),1.0f)); } void sample() { get_angles(); scope.set(0,P0); scope.set(1,P1); scope.set(2,theta1); scope.set(3,theta2); scope.send(); switch(currentState) { case Waiting: ledred=0; ledgreen=0; ledblue=1; break; case Position_calibration: ledred=1; ledgreen=1; ledblue=0; pos_cal(); break; case EMG_calibration: ledred=1; ledgreen=0; ledblue=0; read_emg(); record_min_max(); break; case Homing: setPWM_controller(); ledred=0; ledgreen=1; ledblue=0; break; case Operating: read_emg(); set_v_refxy(); setPWM_controller(); ledred=1; ledgreen=0; ledblue=1; break; case Demo: set_refxy(); setPWM_controller(); ledred=0; ledgreen=0; ledblue=0; break; case Failure: ledred=0; ledgreen=1; ledblue=1; motor1_pwm.write(0.0); motor2_pwm.write(0.0); break; } //Preventing the machine from breaking if(theta1>=1.6f) { dir1=1; } if(theta2>=1.6f) { dir2=2; } } void error_occur() { currentState=Failure; } void button_press(void) //Press button to change state { state_time.reset(); switch(currentState) { case Waiting: currentState=Position_calibration; wait(1); break; case Position_calibration: if(!motor1_calibrated) { motor1_calibrated=true; state_time.reset(); dir1=!dir1; motor1_pwm.write(0.0f); } else { currentState=EMG_calibration; motor2_pwm.write(0.0f); motor1_pwm.write(0.0f); } wait(1); break; case EMG_calibration: currentState=Homing; wait(1); break; case Homing: currentState=Operating; wait(1); break; case Operating: currentState=Demo; wait(1); break; case Demo: currentState=Operating; wait(1); break; } } int main() { pc.baud(115200); pc.printf("Starting..."); ledred=0; sample_timer.attach(&sample, 0.002); err.fall(error_occur); button.fall(button_press); int frequency_pwm=10000; motor1_pwm.period(1.0/frequency_pwm); motor2_pwm.period(1.0/frequency_pwm); state_time.start(); while (true) { wait(10); } }