The final program for the #include AIR robot
Dependencies: Biquad HIDScope QEI angleandposition controlandadjust mbed
Fork of includeair by
main.cpp
- Committer:
- Gerth
- Date:
- 2015-10-14
- Revision:
- 5:8ac5d0651e4d
- Parent:
- 4:bf7765b0f612
- Child:
- 6:37c94a5e205f
File content as of revision 5:8ac5d0651e4d:
#include "mbed.h" #include "QEI.h" #include "HIDScope.h" #include "Biquad.h" #include "controlandadjust.h" //info out HIDScope scope(4); Ticker scope_ticker; const double scope_frequency=500; Serial pc(USBTX,USBRX); ////////////////ENCODERS QEI encoder1(D12,D13,NC,32);/// maybe use Encoder in stead of QEI, because Encoder had setposition QEI encoder2(D10,D11,NC,32); //////////////////////////////////CONTROLLER controlandadjust mycontroller; // make a controller //controller constants float Kp=0.5; float Ki=0.01; float Kd=0.001; Ticker control_ticker; const double control_frequency=25; double error1_int=0;// storage variables for the errors double error2_int=0; double error1_prev=0; double error2_prev=0; InterruptIn valuechangebutton(PTC6);//button to change controller constants //safetyandthreshold AnalogIn safety_pot(A3);//pot 2, used for the safety cutoff value for the pwm AnalogIn threshold_pot(A2);//pot1, used to adjust threshold if signal differs per person Ticker safetyandthreshold_ticker; // ticker to read potmeters const double safetyandthreshold_frequency=1; // frequency for the ticker float threshold_value=1;//initial threshold value ////////////////////////////////FILTER #include "filtervalues.h" Ticker filter_ticker; const double filter_frequency=500; Biquad myfilter1; Biquad myfilter2; AnalogIn emg1_input(A0); AnalogIn emg2_input(A1); double filteredsignal1=0; double filteredsignal2=0; float filter_extragain=1; /////////////////READSIGNAL Ticker readsignal_ticker; const double readsignal_frequency=25; DigitalOut led1(D8); DigitalOut led2(D9); //////////////////////////////// POSITION AND ANGLE SHIZZLE float desired_position=0; float desired_angle[]= {0,0}; //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS volatile bool scopedata_go=false, control_go=false, filter_go=false, safetyandthreshold_go=false, readsignal_go=false; void scopedata_activate() { scopedata_go=true; } void control_activate() { control_go=true; } void filter_activate() { filter_go=true; } void safetyandthreshold_activate() { safetyandthreshold_go=true; } void readsignal_activate() { readsignal_go=true; } ////////////////////////FUNCTIONS //gather data and send to scope void scopedata() { scope.set(0,emg1_input.read()); scope.set(1,filteredsignal1); scope.send(); } //read potmeters and adjust the safetyfactor and threshold void safetyandthreshold() { mycontroller.cutoff((ceil (10*safety_pot.read()) )/10); // adjust the safetyfactor value between 0 and 1 rounded to 1 decimal threshold_value=((ceil (10*threshold_pot.read()) )/10); // adjust the threshold value between 0 and 1 rounded to 1 decimal } /////filter void filtereverything() { //filter_timer.reset(); // filter_timer.start(); //pass1 so f1 double pass1_emg1 = myfilter1.filter(emg1_input.read(), v1_f1_emg1 , v2_f1_emg1 , a1_f1 , a2_f1 , b0_f1 , b1_f1 , b2_f1); double pass1_emg2 = myfilter2.filter(emg2_input.read(), v1_f1_emg2 , v2_f1_emg2 , a1_f1 , a2_f1 , b0_f1 , b1_f1 , b2_f1); //pass2 so f2 double pass2_emg1 = myfilter1.filter(pass1_emg1, v1_f2_emg1 , v2_f2_emg1 , a1_f2 , a2_f2 , b0_f2 , b1_f2 , b2_f2); double pass2_emg2 = myfilter2.filter(pass1_emg2, v1_f2_emg2 , v2_f2_emg2 , a1_f2 , a2_f2 , b0_f2 , b1_f2 , b2_f2); //pass3 so f3 double pass3_emg1 = myfilter1.filter(pass2_emg1, v1_f3_emg1 , v2_f3_emg1 , a1_f3 , a2_f3 , b0_f3 , b1_f3 , b2_f3); double pass3_emg2 = myfilter2.filter(pass2_emg2, v1_f3_emg2 , v2_f3_emg2 , a1_f3 , a2_f3 , b0_f3 , b1_f3 , b2_f3); //pass4 so f4 double pass4_emg1 = myfilter1.filter(pass3_emg1, v1_f4_emg1 , v2_f4_emg1 , a1_f4 , a2_f4 , b0_f4 , b1_f4 , b2_f4); double pass4_emg2 = myfilter2.filter(pass3_emg2, v1_f4_emg2 , v2_f4_emg2 , a1_f4 , a2_f4 , b0_f4 , b1_f4 , b2_f4); //pass5 so f5 double pass5_emg1 = myfilter1.filter(pass4_emg1, v1_f5_emg1 , v2_f5_emg1 , a1_f5 , a2_f5 , b0_f5 , b1_f5 , b2_f5); double pass5_emg2 = myfilter2.filter(pass4_emg2, v1_f5_emg2 , v2_f5_emg2 , a1_f5 , a2_f5 , b0_f5 , b1_f5 , b2_f5); ///// take absolute value double pass5_emg1_abs=(fabs(pass5_emg1)); double pass5_emg2_abs=(fabs(pass5_emg2)); //pass6 so f6 double pass6_emg1 = myfilter1.filter(pass5_emg1_abs, v1_f6_emg1 , v2_f6_emg1 , a1_f6 , a2_f6 , b0_f6 , b1_f6 , b2_f6); double pass6_emg2 = myfilter2.filter(pass5_emg2_abs, v1_f6_emg2 , v2_f6_emg2 , a1_f6 , a2_f6 , b0_f6 , b1_f6 , b2_f6); //pass7 so f7 double pass7_emg1 = myfilter1.filter(pass6_emg1, v1_f7_emg1 , v2_f7_emg1 , a1_f7 , a2_f7 , b0_f7 , b1_f7 , b2_f7); double pass7_emg2 = myfilter2.filter(pass6_emg2, v1_f7_emg2 , v2_f7_emg2 , a1_f7 , a2_f7 , b0_f7 , b1_f7 , b2_f7); filteredsignal1=(pass7_emg1*9e11*filter_extragain); filteredsignal2=(pass7_emg2*9e11*filter_extragain); //filter_timer.stop(); } void control() { ///call desired controller here } //adjust controller values when sw2 is pressed void valuechange() { mycontroller.STOP(); pc.printf("KP is now %f, enter new value\n",Kp); pc.scanf("%f", &Kp); pc.printf("KI is now %f, enter new value\n",Ki); pc.scanf("%f", &Ki); pc.printf("KD is now %f, enter new value\n",Kd); pc.scanf("%f", &Kd); pc.printf("Extra gain is now %f, enter new value\n",filter_extragain); pc.scanf("%f", &filter_extragain); } void readsignal() { if (filteredsignal1>=threshold_value && filteredsignal2>=threshold_value) { led1=led2=1; } else if (filteredsignal1>=threshold_value && filteredsignal2<=threshold_value) { led1=1; led2=0; } else if (filteredsignal1<=threshold_value && filteredsignal2>=threshold_value) { led1=0; led2=1; } else { led1=led2=0; } } int main() { //tickers safetyandthreshold_ticker.attach(&safetyandthreshold_activate,1.0/safetyandthreshold_frequency); filter_ticker.attach(&filter_activate,1.0/filter_frequency); control_ticker.attach(&control_activate,1.0/control_frequency); scope_ticker.attach(&scopedata_activate,1.0/scope_frequency); readsignal_ticker.attach(&readsignal_activate, 1.0/readsignal_frequency); while(1) { if (scopedata_go==true) { scopedata(); scopedata_go=false; } if (filter_go==true) { filtereverything(); filter_go=false; } if (safetyandthreshold_go==true) { safetyandthreshold(); safetyandthreshold_go=false; } if (control_go==true) { control(); control_go=false; } if (readsignal_go==true) { readsignal(); readsignal_go=false; } valuechangebutton.fall(&valuechange); } }