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-27
- Revision:
- 29:cd47a5a772db
- Parent:
- 28:71a90073e482
- Child:
- 30:cd4d9754a357
File content as of revision 29:cd47a5a772db:
#include "mbed.h" #include "QEI.h" #include "HIDScope.h" #include "Biquad.h" #include "controlandadjust.h" #include "angleandposition.h" ///////////////////////////////////////////////info out HIDScope scope(6);// number of hidscope channels const double scope_frequency=500; //HIDscope frequency Timer checktimer; volatile double checktimervalue=0; Serial pc(USBTX,USBRX);// serial connection to pc DigitalOut ledred(LED_RED); DigitalOut ledgreen(LED_GREEN); DigitalOut ledblue(LED_BLUE); Ticker scope_ticker; /////////////////////////////////////////////ENCODERS const float cpr_sensor=32; const float cpr_shaft=cpr_sensor*131;//counts per rotation of the sensor QEI encoder1(D13,D12,NC,cpr_sensor);/// encoders on motors X2 encoding QEI encoder2(D10,D11,NC,cpr_sensor); const double PIE=3.14159265359; const float counttorad=((2*PIE)/cpr_shaft);// counts per rotation of the shaft /////////////////////////////////CALIBRATION (MODE) const double radpersec_calibrate=0.1*PIE;// speed of arms when in calibration mode int modecounter=1;//counter in which mode the robot is const double readbuttoncalibrate_frequency=10;//frequency at which the buttons are read when in calibration mode const double ledblink_frequency=4;//frequency at which the green led and leds on top blink when in resp button or calibration mode DigitalIn changemodebutton(PTA4);// button to change mode (sw3) Ticker readbuttoncalibrate_ticker; Ticker ledblink_ticker; DigitalIn buttonR(D2);//rigth button on biorobotics shield DigitalIn buttonL(D3);//left button on biorobotics shield /////////////////READSIGNAL const double readsignal_frequency=100;//frequency at wich the filtered emg signal is sampled to be 0 or 1 Ticker readsignal_ticker; DigitalOut led1(PTC12);// rigth led on biorobotics shield DigitalOut led2(D9);//left led on biorobotics shield //////////////////////////////////CONTROLLER const double control_frequency=250;// frequency at which the controller is called //controller constants float Kp=2; float Ki=0.5; float Kd=0.5; float factor_taup=1.5; float tau_p=1.0/(factor_taup*control_frequency); controlandadjust mycontroller(2,control_frequency); // make a controller, value in brackets is errorband in degrees and controller frequency Ticker control_ticker; const double Ts_control=1.0/control_frequency; float error1=0,//controller error storage variables error2=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 const double filter_frequency=500; #include "filtervalues.h"// call the values for the biquads Ticker filter_ticker; Biquad myfilter1;// make filter for signal 1 Biquad myfilter2;//make filter for signal 2 AnalogIn emg1_input(A0);//input for first emg signal AnalogIn emg2_input(A1);//input for second emg signal volatile double filteredsignal1=0;//the first filtered emg signal volatile double filteredsignal2=0;//the second filtered emg signal float filter_extragain=1; //////////////////////////////// POSITION AND ANGLE SHIZZLE const float safetymarginfield=0.075; //adjustable, tweak for maximum but safe range const float mm_per_sec_emg=0.1;// move the pod 100 mm per sec if muscle is flexed const float y_start=0.145;//starting y position of the pod const float y_punch=0.473;// position to where there is punched const float timetoshoot=0.25;// time it can take to shoot const float timetogoback=0.5;// time it can take to go back after shooting float desired_position=0; float desired_angle1=0; float desired_angle2=0; const float fieldwidth=0.473; const float maxdisplacement=((fieldwidth/2)-safetymarginfield); //so the pod doesn't hit the edges of the playfield angleandposition anglepos;// initiate the angle and position calculation library const float radtodeg=(180/PIE); //////////////////////GO FLAGS AND ACTIVATION FUNCTIONS volatile bool scopedata_go=false, control_go=false, filter_go=false, safetyandthreshold_go=false, readsignal_go=false, switchedmode=true, readbuttoncalibrate_go=false, ledblink_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; } void readbuttoncalibrate_activate() { readbuttoncalibrate_go=true; } void ledblink_activate() { ledblink_go=true; } ////////////////////////FUNCTIONS //gather data and send to scope void scopedata() { scope.set(0,desired_position); scope.set(1,emg1_input.read()); scope.set(2,emg2_input.read()); scope.set(3,filteredsignal1); scope.set(4,filteredsignal2); scope.set(5,threshold_value); 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 (100*threshold_pot.read()) )/100); // adjust the threshold value between 0 and 1 rounded to 2 decimals } /////filter void filtereverything(bool makeempty) { //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); if (makeempty==true) {//this is needed so the filtered value is nog high after shooting pass1_emg1 = pass1_emg2 =pass2_emg1 =pass2_emg2 =pass3_emg1 = pass3_emg2 =pass4_emg1 =pass4_emg2 =pass5_emg1 =pass5_emg2 =0; pass5_emg1_abs=pass5_emg2_abs=pass6_emg1 =pass6_emg2 =pass7_emg1=pass7_emg2 =filteredsignal1=filteredsignal2=0; v1_f1_emg1=v1_f1_emg2=v1_f2_emg1=v1_f2_emg2=v1_f3_emg1=v1_f3_emg2=v1_f4_emg1=v1_f4_emg2=v1_f5_emg1=0; v1_f5_emg2=v1_f6_emg1=v1_f6_emg2=v1_f7_emg1=v1_f7_emg2=0; } } //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("Factor of tau_p=1.0/(factor*controlfrequency) is now %f, enter new value\n",factor_taup); pc.scanf("%f", &factor_taup); pc.printf("Extra gain is now %f, enter new value\n",filter_extragain); pc.scanf("%f", &filter_extragain); } // shoot the pod forward void shoot() { ledgreen=1; float time=0; float stepsize=(y_punch-y_start)/(timetoshoot*control_frequency); float y_during_punch=y_start;// set initial y position to start position float x_punch=anglepos.angletoposition(counttorad*encoder1.getPulses(),counttorad*encoder2.getPulses()); Timer shoottimer; shoottimer.reset(); shoottimer.start(); //forward while (time<=timetoshoot) { ledblue=!ledblue; y_during_punch+=stepsize; // add stepsize to y position if (y_during_punch>=y_punch) {//to check if y position is not bigger than y_punch for safety y_during_punch=y_punch; } else { y_during_punch=y_during_punch; } desired_angle1=anglepos.positiontoangle1(x_punch,y_during_punch);// calculate desired angles desired_angle2=anglepos.positiontoangle2(x_punch,y_during_punch); error1=(desired_angle1-counttorad*encoder1.getPulses());//calculate errors error2=(desired_angle2-counttorad*encoder2.getPulses()); mycontroller.PID(error1,error2,Kp,Ki,Kd);;// send errors to controller scopedata();//send data to hidscope WARING lower freqyency than normal time+=(Ts_control);// add time it should take to calculated time //pc.printf("Time = %f\n",time); filtereverything(true);//set al filter variables to 0 wait(time-shoottimer.read());// IMPORTANT wait until the loop has taken the time it should need, if this is not done the loop wil go to fast and the motors can't keep up } //back time=0; shoottimer.reset(); stepsize=(y_punch-y_start)/(timetogoback*control_frequency); while (time<=timetogoback) { ledblue=!ledblue; y_during_punch-=stepsize; // add stepsize to y position if (y_during_punch<=y_start) {//to check if y position is not smaller than y_start for safety y_during_punch=y_start; } else { y_during_punch=y_during_punch; } desired_angle1=anglepos.positiontoangle1(x_punch,y_during_punch);// calculate desired angles desired_angle2=anglepos.positiontoangle2(x_punch,y_during_punch); error1=(desired_angle1-counttorad*encoder1.getPulses());//calculate errors error2=(desired_angle2-counttorad*encoder2.getPulses()); mycontroller.PID(error1,error2,Kp,Ki,Kd);;// send errors to controller scopedata();//send data to hidscope WARING lower freqyency than normal time+=(Ts_control);// add time it should take to calculated time //pc.printf("Time = %f\n",time); filtereverything(false); wait(time-shoottimer.read());// IMPORTANT wait until the loop has taken the time it should need, if this is not done the loop wil go to fast and the motors can't keep up } shoottimer.stop(); ledblue=1; ledgreen=0; } ////////////////////////////////////////////////////READ EMG AND MOVE DESIRED POSITION void readsignal() { //check if pod has to shoot if (filteredsignal1>=threshold_value && filteredsignal2>=threshold_value) { led1=led2=1; shoot(); // check if pod has to move to the right } else if (filteredsignal1>=threshold_value && filteredsignal2<=threshold_value) { led1=1; led2=0; desired_position += (mm_per_sec_emg/readsignal_frequency);// move desiredposition right if (desired_position>=maxdisplacement) {//check if the pod doesnt move too far and hit the edge desired_position=maxdisplacement; } else { desired_position=desired_position; } // check if pod has to move to the left } else if (filteredsignal1<=threshold_value && filteredsignal2>=threshold_value) { led1=0; led2=1; desired_position -= (mm_per_sec_emg/readsignal_frequency);//move desiredposition left if (desired_position<=(-1*maxdisplacement)) {//check if the pod doesnt move too far and hit the edge desired_position=(-1*maxdisplacement); } else { desired_position=desired_position; } } else { led1=led2=0; } } ///////////////////////////////////////////////READ BUTTON AND MOVE DESIRED POSITION void readsignalbutton() { //write value of button to variable int buttonr=buttonR.read(); int buttonl=buttonL.read(); //check if pod has to shoot if (buttonr==0 && buttonl==0) { led1=led2=1; shoot(); // check if pod has to move to the right } else if (buttonr==0 && buttonl==1) { led1=1; led2=0; desired_position += (mm_per_sec_emg/readsignal_frequency);// move desiredposition right if (desired_position>=maxdisplacement) {//check if the pod doesnt move too far and hit the edge desired_position=maxdisplacement; } else { desired_position=desired_position; } // check if pod has to move to the left } else if (buttonr==1 && buttonl==0) { led1=0; led2=1; desired_position -= (mm_per_sec_emg/readsignal_frequency);//move desiredposition left if (desired_position<=(-1*maxdisplacement)) {//check if the pod doesnt move too far and hit the edge desired_position=(-1*maxdisplacement); } else { desired_position=desired_position; } } else { led1=led2=0; } } void changemode() //this makes the counter higher to switch between modes { mycontroller.STOP(); switchedmode=true; modecounter++; if (modecounter==4) { modecounter=0; } else { modecounter=modecounter; } wait(1);// needed because else it is checked too fast if the button is pressed and modes change too fast // tried it with interruptin but dinn't work } ///////////////////////////////////////////////////MAIN 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); readbuttoncalibrate_ticker.attach(&readbuttoncalibrate_activate, 1.0/readbuttoncalibrate_frequency); ledblink_ticker.attach(&ledblink_activate, 1.0/ledblink_frequency); pc.baud(115200);//set baudrate to 115200 while(1) { valuechangebutton.fall(&valuechange);// used to change controller variables and the gain for the filter checktimer.reset(); checktimer.start(); if (changemodebutton==0) {// check if the change mode button is pressed changemode(); } if (scopedata_go==true) {//send scopedata //TIME THIS LOOP TAKES 0.000008 SEC (PEAKS AT 0.000015) scopedata(); scopedata_go=false; } if (safetyandthreshold_go==true) {// check the potmeters //TIME THIS LOOP TAKES: 0.000032 SEC safetyandthreshold(); safetyandthreshold_go=false; } ///////////////////////////////////////////NORMAL RUNNING MODE if(modecounter==0) { if (switchedmode==true) { encoder1.reset();// reset encoders so they are at 0 degrees encoder2.reset(); pc.printf("Program running\n");// ledgreen=0; led1=led2=ledred=ledblue=1; switchedmode=false; } if (filter_go==true) {// filter the emg signal // TIME THIS LOOP TAKES: 0.000173 SEC filtereverything(false); filter_go=false; } if (readsignal_go==true) {// check if signal is 0 or 1 and adjust wanted position // TIME THIS LOOP TAKES: 0.000005 SEC readsignal(); readsignal_go=false; } if (control_go==true) {// calculate angles from positions and send error to controller //TIME THIS LOOP TAKES: 0.000223 SEC desired_angle1=anglepos.positiontoangle1(desired_position,y_start); desired_angle2=anglepos.positiontoangle2(desired_position,y_start); float error1=(desired_angle1-counttorad*encoder1.getPulses()); float error2=(desired_angle2-counttorad*encoder2.getPulses()); mycontroller.PI(error1,error2,Kp,Ki); control_go=false; } valuechangebutton.fall(&valuechange);// used to change controller variables and the gain for the filter } ////////////////////////////////////////////////////CALIBRATE RIGHT ARM if (modecounter==1) { if(switchedmode==true) { pc.printf("Calibration mode! Use buttons to move rigth arm to 0 degrees\n"); led1=led2=ledred=0; ledgreen=ledblue=1; switchedmode=false; } if (ledblink_go==true) { led1=!led1;// blink rigth led on biorobotics shield (because rigth arm is being calibrated) ledblink_go=false; } if (readbuttoncalibrate_go==true) {//check wich button is pressed and adjust wanted angle of rigth arm if (buttonR.read()==0 && buttonL.read()==1) { desired_angle1 += (radpersec_calibrate/readbuttoncalibrate_frequency); readbuttoncalibrate_go=false; } if (buttonR.read()==1 && buttonL.read()==0) { desired_angle1 -= (radpersec_calibrate/readbuttoncalibrate_frequency); readbuttoncalibrate_go=false; } } if (control_go==true) {// calculate errors and send them to controllers error1=(desired_angle1-counttorad*encoder1.getPulses()); error2=0; mycontroller.PI(error1,error2,Kp,Ki); control_go=false; } } ////////////////////////////////////////////CALIBRATE LEFT ARM if (modecounter==2) { if(switchedmode==true) { pc.printf("Calibration mode! Use buttons to move left arm to 0 degrees\n"); led1=led2=ledred=0; ledgreen=ledblue=1; switchedmode=false; } if (ledblink_go==true) { led2=!led2;// blink left led on biorobotics shield (because left arm is being calibrated) ledblink_go=false; } if (readbuttoncalibrate_go==true) {// if (buttonR.read()==0 && buttonL.read()==1) {//check wich button is pressed and adjust wanted angle of left arm desired_angle2 += (radpersec_calibrate/readbuttoncalibrate_frequency); readbuttoncalibrate_go=false; } if (buttonR.read()==1 && buttonL.read()==0) { desired_angle2 -= (radpersec_calibrate/readbuttoncalibrate_frequency); readbuttoncalibrate_go=false; } } if (control_go==true) {// calculate errors and send to controller error1=0; error2=(desired_angle2-counttorad*encoder2.getPulses()); mycontroller.PI(error1,error2,Kp,Ki); control_go=false; } } ///////////////////////////////BUTTONCONTROLMODE if (modecounter==3) { if (switchedmode==true) { pc.printf("Buttonmode, you can use the buttons to control the robot\n"); led1=led2=0; ledred=ledblue=1; encoder1.reset();// reset encoders so they are at 0 degrees encoder2.reset(); switchedmode=false; } if (ledblink_go==true) { ledgreen=!ledgreen; ledblink_go=false; } if (readsignal_go==true) {// read buttons and adjus wanted position readsignalbutton(); readsignal_go=false; } if (control_go==true) {// calculate wanted angles from position, errors and send to controller desired_angle1=anglepos.positiontoangle1(desired_position,y_start); desired_angle2=anglepos.positiontoangle2(desired_position,y_start); error1=(desired_angle1-counttorad*encoder1.getPulses()); error2=(desired_angle2-counttorad*encoder2.getPulses()); mycontroller.PIDLowPass(error1,error2,Kp,Ki,Kd,tau_p); control_go=false; } } checktimervalue=checktimer.read(); checktimer.stop(); } }