SwitchMode
Dependencies: BEAR_Protocol InverseLeg iSerial mbed
main.cpp
- Committer:
- icyzkungz
- Date:
- 2016-01-22
- Revision:
- 1:183e6088a1fa
- Parent:
- 0:f73c80b36c20
- Child:
- 2:d6be1c49d9d5
File content as of revision 1:183e6088a1fa:
#include "string" #include "BEAR_Protocol.h" #include "Kinematic.h" Serial pc(SERIAL_TX,SERIAL_RX); Bear_Communicate bcom(PA_15,PB_7,115200); DigitalIn button(USER_BUTTON); #define LEFT_SIDE 0x01 #define RIGHT_SIDE 0x02 void SwMode() { int option; // user's entered option will be saved in this variable float temp; float LH,LK,RH,RK; float LLink[2],RLink[2]; struct max_ang { float Hip; float Knee; }; struct min_ang { float Hip; float Knee; }; max_ang Lmax,Lmin; min_ang Rmax,Rmin; Lmax.Knee = Lmax.Hip = Lmin.Knee = Lmin.Hip = Rmax.Knee = Rmax.Hip = Rmin.Knee = Rmin.Hip = 0; bool a,b,c,d; a = b = c = d = false; float temp_LH=0,temp_LK=0,temp_RH=0,temp_RK=0; do { // do-while loop starts here.that display menu again and again until user select to exit program // Displaying Options for the menu pc.printf("************Don't Forget to Save Data************\n"); pc.printf("*\t1) Motor Left Hip \t\t\t*\n"); pc.printf("*\t2) Motor Left Knee \t\t\t*\n"); pc.printf("*\t3) Motor Right Hip \t\t\t*\n"); pc.printf("*\t4) Motor Right Knee \t\t\t*\n"); pc.printf("*\t5) Kp : Left Hip \t\t\t*\n"); pc.printf("*\t6) Ki : Left Hip \t\t\t*\n"); pc.printf("*\t7) Kd : Left Hip \t\t\t*\n"); pc.printf("*\t8) Save Left Hip(p,i,d) data\t\t*\n"); pc.printf("*\t9) Kp : Left Knee \t\t\t*\n"); pc.printf("*\t10) Ki : Left Knee \t\t\t*\n"); pc.printf("*\t11) Kd : Left Knee \t\t\t*\n"); pc.printf("*\t12) Save Left Knee(p,i,d) data\t\t*\n"); pc.printf("*\t13) Kp : Right Hip \t\t\t*\n"); pc.printf("*\t14) Ki : Right Hip \t\t\t*\n"); pc.printf("*\t15) Kd : Right Hip \t\t\t*\n"); pc.printf("*\t16) Save Right Hip(p,i,d) data\t\t*\n"); pc.printf("*\t17) Kp : Right Knee \t\t\t*\n"); pc.printf("*\t18) Ki : Right Knee \t\t\t*\n"); pc.printf("*\t19) Kd : Right Knee \t\t\t*\n"); pc.printf("*\t20) Save Right Knee data\t\t\t*\n"); pc.printf("*\t21) Set Left Hip Margin\n"); pc.printf("*\t22) Set Left Knee Margin\n"); pc.printf("*\t23) Set Right Hip Margin\n"); pc.printf("*\t24) Set Right Knee Margin\n"); pc.printf("*\t25) Set Offset\n"); pc.printf("*\t26) Set Body Lenght of Left Side\n"); pc.printf("*\t27) Set Body Lenght of Right Side\n"); pc.printf("*\t28) Set Maximum Hip Angle Range\n"); pc.printf("*\t29) Set Minimum Hip Angle Range\n"); pc.printf("*\t30) Set Maximum Knee Angle Range\n"); pc.printf("*\t31) Set Minimum Knee Angle Range\n"); pc.printf("*\t40) Exit Program \t\t\t*\n"); pc.printf("************Don't Forget to Save Data************\n"); // Prompting user to enter an option according to menu pc.printf("Please select an option : "); pc.scanf("%d",&option); pc.printf("\n"); if(option == 1)// Left Hip do { pc.printf("\nType 9999 to Exit to Main Menu\n"); //Send Position to Motor pc.printf("Input Degree : \n"); pc.scanf("%f",&temp); if(temp!=9999) { LH = temp; pc.printf("Move Left Hip Motor to %f Degree\n",LH); bcom.setMotorPos(LEFT_SIDE,LH,temp_LK); temp_LH = LH; /********************Save Data*********************/ } else bcom.saveDataToEEPROM(LEFT_SIDE,MOTOR_UPPER_ANG); } while(temp != 9999); else if(option == 2) //Left Knee do { pc.printf("\nType 9999 to Exit to Main Menu\n"); //Send Position to Motor pc.printf("Input Degree : \n"); pc.scanf("%f",&temp); if(temp!=9999) { LK = temp; pc.printf("Move Left Knee Motor to %f Degree\n",LK); bcom.setMotorPos(LEFT_SIDE,temp_LH,LK); temp_LK = LK; /********************Save Data*********************/ } else bcom.saveDataToEEPROM(LEFT_SIDE,MOTOR_LOWER_ANG); } while(temp != 9999); else if(option == 3) { //Right Hip do { pc.printf("\nType 9999 to Exit to Main Menu\n"); //Send Position to Motor pc.printf("Input Degree : \n"); pc.scanf("%f",&RH); if(temp!=9999) { RH = temp; pc.printf("Move Right Hip Motor to %f Degree\n",RH); bcom.setMotorPos(RIGHT_SIDE,RH,temp_RK); temp_RH = RH; /********************Save Data*********************/ } else bcom.saveDataToEEPROM(RIGHT_SIDE,MOTOR_UPPER_ANG); } while(temp != 9999); } else if(option == 4) { //Right Knee do { pc.printf("\nType 9999 to Exit to Main Menu\n"); //Send Position to Motor pc.printf("Input Degree : \n"); pc.scanf("%f",&temp); if(temp != 9999) { RK = temp; pc.printf("Move Right Knee Motor to %f Degree\n",RK); bcom.setMotorPos(RIGHT_SIDE,temp_RH,RK); temp_RK = RK; /********************Save Data*********************/ } else bcom.saveDataToEEPROM(RIGHT_SIDE,MOTOR_LOWER_ANG); } while(temp != 9999); } else if(option == 5) { //Left Hip do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Kp of Left Hip\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Kp of Left Hip to %f",temp); bcom.setUpMotorKp(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,KP_UPPER_MOTOR); } while(temp != 9999); } else if(option == 6) { //Left Hip do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Ki of Left Hip\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Ki of Left Hip to %f",temp); bcom.setUpMotorKi(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,KI_UPPER_MOTOR); } while(temp != 9999); } else if(option == 7) { //Left Hip do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Kd of Left Hip\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Kd of Left Hip to %f",temp); bcom.setUpMotorKd(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,KD_UPPER_MOTOR); } while(temp != 9999); } else if(option == 8) { pc.printf("\nType 9999 to Exit to Main Menu\n"); /*******************************Save Left Hip data*************************************/ } else if(option == 9) { //Left Knee do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Kp of Left Knee\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Kp of Left Knee to %f",temp); bcom.setLowMotorKp(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,KP_LOWER_MOTOR); } while(temp != 9999); } else if(option == 10) { //Left Knee do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Ki of Left Knee\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Ki of Left Knee to %f",temp); bcom.setLowMotorKi(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,KI_LOWER_MOTOR); } while(temp != 9999); } else if(option == 11) { //Left Knee do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Kd of Left Knee\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Kd of Left Knee to %f",temp); bcom.setLowMotorKd(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,KD_LOWER_MOTOR); } while(temp != 9999); } else if(option == 12) { pc.printf("\nType 9999 to Exit to Main Menu\n"); /*******************************Save Left Knee data*************************************/ } else if(option == 13) { //Right Hip do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Kp of Right Hip\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Kp of Right Hip to %f",temp); bcom.setUpMotorKp(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,KP_UPPER_MOTOR); } while(temp != 9999); } else if(option == 14) { //Right Hip do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Ki of Right Hip\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Ki of Right Hip to %f",temp); bcom.setUpMotorKi(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,KI_UPPER_MOTOR); } while(temp != 9999); } else if(option == 15) { //Right Hip do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Kd of Right Hip\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Kd of Right Hip to %f",temp); bcom.setUpMotorKd(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,KD_UPPER_MOTOR); } while(temp != 9999); } else if(option == 16) { pc.printf("\nType 9999 to Exit to Main Menu\n"); /*******************************Save Right Hip data*************************************/ } else if(option == 17) { //Right Knee do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Kp of Right Knee\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Kp of Right Knee to %f",temp); bcom.setLowMotorKp(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,KP_LOWER_MOTOR); } while(temp != 9999); } else if(option == 18) { //Right Knee do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Ki of Right Knee\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Ki of Right Knee to %f Degree\n",temp); bcom.setLowMotorKi(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,KI_LOWER_MOTOR); } while(temp != 9999); } else if(option == 19) { //Right Knee do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("\nInput Kd of Right Knee\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Kd of Right Knee to %f",temp); bcom.setLowMotorKd(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,KD_LOWER_MOTOR); } while(temp != 9999); } else if(option == 20) { pc.printf("\nType 9999 to Exit to Main Menu\n"); /*******************************Save Right Knee data*************************************/ } else if(option == 21) { //Left Hip Margin pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("Input Margin\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Left Hip Margin to %f\n",temp); bcom.setUpMargin(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,UP_MARGIN); } else if(option == 22) { //Left Knee Margin pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("Input Margin\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Left Knee Margin to %f\n",temp); bcom.setLowMargin(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,LOW_MARGIN); } else if(option == 23) { //Right Hip Margin pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("Input Margin\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Right Hip Margin to %f\n",temp); bcom.setUpMargin(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,UP_MARGIN); } else if(option == 24) { //Right Knee Margin pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("Input Margin\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nChange Right Knee Margin to %f\n",temp); bcom.setLowMargin(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,LOW_MARGIN); } else if(option == 25) { //Offset if(a == true && b == true && c == true && d == true) { //float HipAngle[2],KneeAngle[2]; float LHipAngle,LKneeAngle; float RHipAngle,RKneeAngle; bcom.getMotorPos(LEFT_SIDE,&LHipAngle,&LKneeAngle); bcom.getMotorPos(RIGHT_SIDE,&RHipAngle,&RKneeAngle); Kinematic L( 'Z', LLink[0], LLink[1], LHipAngle, LKneeAngle); Kinematic R( 'Z', RLink[0], RLink[1], RHipAngle, RKneeAngle); L.ForwardKinematicCalculation(); R.ForwardKinematicCalculation(); float offset_Y,offset_Z; offset_Y = L.get_Position_Y()-R.get_Position_Y(); offset_Z = L.get_Position_Z()-R.get_Position_Z(); bcom.setOffset(LEFT_SIDE,offset_Y,offset_Z); bcom.saveDataToEEPROM(LEFT_SIDE,OFFSET); } else pc.printf("\nYou have to do choice 25-28 first\n\n"); } else if(option == 26) { //Body Lenght of Left Side do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("Input Body Lenght of Left Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nBody Lenght = %f\n",temp); bcom.setBodyLength(LEFT_SIDE,temp); } else bcom.saveDataToEEPROM(LEFT_SIDE,BODY_LENGTH); } while(temp!=9999); } else if(option == 27) { //Body Lenght of Right Side do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("Input Body Lenght of Right Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nBody Lenght = %f\n",temp); bcom.setBodyLength(RIGHT_SIDE,temp); } else bcom.saveDataToEEPROM(RIGHT_SIDE,BODY_LENGTH); } while(temp!=9999); } else if(option == 28) { //Set Maximum Hip Angle Range do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("1) Left Side\n"); pc.printf("2) Right Side\n"); pc.scanf("%f\n",&temp); if(temp==1) { //Left pc.printf("Input Maximum Hip Angle Range of Left Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nMaximum Hip Angle Range of Left Side = %f\n",temp); bcom.setUpAngleRange(LEFT_SIDE,temp,Lmin.Hip); Lmax.Hip = temp; } else bcom.saveDataToEEPROM(LEFT_SIDE,ANGLE_RANGE_UP); } else if(temp==2) { //Right pc.printf("Input Maximum Hip Angle Range of Right Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nMaximum Hip Angle Range of Right Side = %f\n",temp); bcom.setUpAngleRange(RIGHT_SIDE,temp,Rmin.Hip); Rmax.Hip = temp; } else bcom.saveDataToEEPROM(RIGHT_SIDE,ANGLE_RANGE_UP); } } while(temp!=9999); } else if(option == 29) { //Set Minumum Hip Angle Range do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("1) Left Side\n"); pc.printf("2) Right Side\n"); pc.scanf("%f\n",&temp); if(temp==1) { //Left pc.printf("Input Minumum Hip Angle Range of Left Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nMinumum Hip Angle Range of Left Side = %f\n",temp); bcom.setLowAngleRange(LEFT_SIDE,Lmax.Hip,temp); Lmin.Hip = temp; } else bcom.saveDataToEEPROM(LEFT_SIDE,ANGLE_RANGE_UP); } else if(temp==2) { //Right pc.printf("Input Minumum Hip Angle Range of Right Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nMinumum Hip Angle Range of Right Side = %f\n",temp); bcom.setLowAngleRange(RIGHT_SIDE,Rmax.Hip,temp); Rmin.Hip = temp; } else bcom.saveDataToEEPROM(RIGHT_SIDE,ANGLE_RANGE_UP); } } while(temp!=9999); } else if(option == 30) { //Set Maximum Knee Angle Range do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("1) Left Side\n"); pc.printf("2) Right Side\n"); pc.scanf("%f\n",&temp); if(temp==1) { //Left pc.printf("Input Maximum Knee Angle Range of Left Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nMaximum Knee Angle Range of Left Side = %f\n",temp); bcom.setLowAngleRange(LEFT_SIDE,temp,Lmin.Knee); Lmax.Knee = temp; } else bcom.saveDataToEEPROM(LEFT_SIDE,ANGLE_RANGE_LOW); } else if(temp==2) { //Right pc.printf("Input Maximum Knee Angle Range of Right Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nMaximum Knee Angle Range of Right Side = %f\n",temp); bcom.setLowAngleRange(RIGHT_SIDE,temp,Rmin.Knee); Rmax.Knee = temp; } else bcom.saveDataToEEPROM(RIGHT_SIDE,ANGLE_RANGE_LOW); } } while(temp!=9999); } else if(option == 28) { //Set Minumum Knee Angle Range do { pc.printf("\nType 9999 to Exit to Main Menu\n"); pc.printf("1) Left Side\n"); pc.printf("2) Right Side\n"); pc.scanf("%f\n",&temp); if(temp==1) { //Left pc.printf("Input Minumum Knee Angle Range of Left Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nMinumum Knee Angle Range of Left Side = %f\n",temp); bcom.setLowAngleRange(LEFT_SIDE,Lmax.Knee,temp); Lmin.Knee = temp; } else bcom.saveDataToEEPROM(LEFT_SIDE,ANGLE_RANGE_LOW); } else if(temp==2) { //Right pc.printf("Input Minumum Knee Angle Range of Right Side\n"); pc.scanf("%f",&temp); if(temp != 9999) { pc.printf("\nMinumum Knee Angle Range of Right Side = %f\n",temp); bcom.setLowAngleRange(RIGHT_SIDE,Rmax.Knee,temp); Rmin.Knee = temp; } else bcom.saveDataToEEPROM(RIGHT_SIDE,ANGLE_RANGE_LOW); } } while(temp!=9999); } else if(option == 40) { pc.printf("Are You Sure ?\n"); pc.printf("\n1) Yes\n"); pc.printf("2) No\n"); pc.scanf("%d",&option); if(option==1) { option = 40; pc.printf("Please Push Button Restart\n"); } else pc.printf("Return to Main Menu\n"); } else { // if user has entered invalid choice (other than 1,2,3 or 4) // Displaying error message pc.printf("\nInvalid Option entered\n"); } } while(option != 40); // condition of do-while loop } int main() { pc.baud(115200); pc.printf("Start\n"); if(!button) { while(!button); SwMode(); } } /*int main() { pc.baud(115200); int num; do { //float num; //Float number --> Working pc.printf("Start\n"); pc.scanf("%d",&num); pc.printf("temp : %d\n",num); if( num == 15 ) pc.printf("Yes\n"); pc.printf("End\n"); } while(num!=20); }*/