
fancy lampje
Dependencies: mbed QEI HIDScope biquadFilter MODSERIAL FXOS8700Q FastPWM
main.cpp
- Committer:
- MatthewMaat
- Date:
- 2019-10-21
- Revision:
- 21:a316452da8cd
- Parent:
- 20:0f6a88f29a71
- Parent:
- 19:fb3d570a115e
- Child:
- 22:335a30b0825d
File content as of revision 21:a316452da8cd:
#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 ); QEI motor1 (D8, D9, NC, 32); QEI motor2 (D12, D13, NC, 32); Ticker ticktick; Timer state_time; Timeout EMG_peak; Timeout turn; 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_peaks=false; volatile bool ignore_turn=true; enum states{Waiting,Position_calibration,EMG_calibration,Homing,Operating,Demo,Failure}; states currentState=Waiting; volatile bool motor1_calibrated=false; 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 = 8; static float Ki = 1.02; 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=asin(3.8/35.0); 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.8f); pulses=motor1_pos.getPulses(); if(pos_time_counter%500==0&&fabs(pulses-last_ticks)<1) { ledblue=!ledblue; motor1_pos.reset(); last_ticks=10000; state_time.reset(); dir1=!dir1; } else if(pos_time_counter%500==0) { last_ticks=motor1_pos.getPulses(); } } else if(t>1.0f) { motor1_pwm.write(0.0f); dir2=1; //??? motor2_pwm.write(0.8f); 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_peaks(void) { ignore_peaks=false; } void start_ignore_turn(void) { ignore_turn=true; } void set_PWM(void) { static bool motor_on=false; float Q0; Q0=2.0f*(P0-(EMG_min0+EMG_max0)/2.0f)/(EMG_max0-EMG_min0); if (Q0>-0.2f && !ignore_peaks) { if (motor_on) { motor1_pwm.write(0.0f); EMG_peak.attach(unignore_peaks,0.8); turn.attach(start_ignore_turn,1); ignore_turn=false; ignore_peaks=true; motor_on=false; } else if(ignore_turn) { motor1_pwm.write(1.0f); EMG_peak.attach(unignore_peaks,0.8); turn.attach(start_ignore_turn,1); ignore_turn=false; ignore_peaks=true; motor_on=true; } else { motor1_pwm.write(1.0f); dir1=!dir1; EMG_peak.attach(unignore_peaks,1.5); ignore_peaks=true; motor_on=true; } } motor2_pwm.write(ain1.read()); } void error(void){ float dvd=L * (tan(theta2) * pow(tan(theta1),2) - tan(theta1) * pow(tan(theta2),2)); 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; } 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 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(fabs(u_1)); if(u_2 < 0.0f){ dir2 = 0; }else{ dir2 = 1; } motor2_pwm.write(fabs(u_1)); } 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_PWM(); ledred=1; ledgreen=0; ledblue=1; break; case Demo: 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; wait(1); } else { currentState=EMG_calibration; } 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); } }