BE@R lab / Mbed 2 deprecated MPU9250AHRS-PGear_Stabilizer

Dependencies:   BEAR_Protocol mbed Stabilizer iSerial

Fork of MPU9250AHRS by BE@R lab

Revision:
17:51ac4252fa1a
Parent:
16:b5b9827dd5dc
Child:
18:02559b51ec08
diff -r b5b9827dd5dc -r 51ac4252fa1a main.cpp
--- a/main.cpp	Wed Jan 20 01:54:44 2016 +0000
+++ b/main.cpp	Sat Jan 23 04:10:16 2016 +0000
@@ -8,7 +8,9 @@
 
 MPU9250 mpu9250;
 Stabilizer Stabilize(5.0f,0.0f);
-Kinematic L('Z',10,10,30,30),R('Z',10,10,30,30);
+//Kinematic L('Z',10,10,30,30),R('Z',10,10,30,30);
+Kinematic L(0,0),R(0,0);
+Bear_Communicate bcom(PA_15,PB_7,1000000);
 
 Timer t;
 
@@ -34,394 +36,635 @@
 
 void SwMode()
 {
-    int option; // user's entered option will be saved in this variable
-    float kp1,kp2,kp3,kp4;
-    float ki1,ki2,ki3,ki4;
-    float kd1,kd2,kd3,kd4;
+    int option;
+    float temp;
     float LH,LK,RH,RK;
+    float LLink[2],RLink[2];
+    float LLink0,LLink1,RLink0,RLink1;
+    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
+    do {
         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) Save Dog Stand Position \t\t*\n");
-        pc.printf("*\t22) Save Dog Sit Position \t\t*\n");
-        pc.printf("*\t23) Exit Program \t\t\t*\n");
+
+        pc.printf("*\t8) Kp : Left Knee \t\t\t*\n");
+        pc.printf("*\t9) Ki : Left Knee \t\t\t*\n");
+        pc.printf("*\t10) Kd : Left Knee \t\t\t*\n");
+
+        pc.printf("*\t11) Kp : Right Hip \t\t\t*\n");
+        pc.printf("*\t12) Ki : Right Hip \t\t\t*\n");
+        pc.printf("*\t13) Kd : Right Hip \t\t\t*\n");
+
+        pc.printf("*\t14) Kp : Right Knee \t\t\t*\n");
+        pc.printf("*\t15) Ki : Right Knee \t\t\t*\n");
+        pc.printf("*\t16) Kd : Right Knee \t\t\t*\n");
+
+        pc.printf("*\t17) Set Left Hip Margin\n");
+        pc.printf("*\t18) Set Left Knee Margin\n");
+        pc.printf("*\t19) Set Right Hip Margin\n");
+        pc.printf("*\t20) Set Right Knee Margin\n");
+
+        pc.printf("*\t21) Set Lenght of Left Link Hip\n");
+        pc.printf("*\t22) Set Lenght of Left Link Knee\n");
+        pc.printf("*\t23) Set Lenght of Right Link Hip\n");
+        pc.printf("*\t24) Set Lenght of Right Link Knee\n");
+
+        pc.printf("*\t25) Set Offset\n");
+
+        pc.printf("*\t26) Set Body Width\n");
+
+        pc.printf("*\t27) Set Maximum Hip Angle Range\n");
+        pc.printf("*\t28) Set Minimum Hip Angle Range\n");
+        pc.printf("*\t29) Set Maximum Knee Angle Range\n");
+        pc.printf("*\t30) 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
+        if(option == 1)// Left Hip
             do {
-                pc.printf("\n1) Input Degree\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
-
-                if(option == 1) {
-                    //Send Position to Motor
-                    pc.printf("Input Degree : \n");
-                    pc.scanf("%f",&LH);
+                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);
 
-                    L.setMotorPos(0x01,LH,temp_LK);
+                    bcom.setMotorPos(LEFT_SIDE,LH,temp_LK);
                     temp_LH = LH;
 
-                } else if(option == 2) {
                     /********************Save Data*********************/
-                }
-            } while(option != 2);
+                } else bcom.saveDataToEEPROM(LEFT_SIDE,MOTOR_UPPER_ANG);
 
-        } else if(option == 2) { //Left Knee
+            } while(temp != 9999);
+
+        else if(option == 2) //Left Knee
             do {
-                pc.printf("\n1) Input Degree\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
+                pc.printf("\nType 9999 to Exit to Main Menu\n");
 
-                if(option == 1) {
-                    //Send Position to Motor
-                    pc.printf("Input Degree : \n");
-                    pc.scanf("%f",&LK);
+                //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);
 
-                    L.setMotorPos(0x01,temp_LH,LK);
+                    bcom.setMotorPos(LEFT_SIDE,temp_LH,LK);
                     temp_LK = LK;
 
-                } else if(option == 2) {
+
                     /********************Save Data*********************/
-                }
-            } while(option != 2);
+                } else bcom.saveDataToEEPROM(LEFT_SIDE,MOTOR_LOWER_ANG);
 
-        } else if(option == 3) { //Right Hip
+            } while(temp != 9999);
+
+
+        else if(option == 3) { //Right Hip
             do {
-                pc.printf("\n1) Input Degree\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
+                pc.printf("\nType 9999 to Exit to Main Menu\n");
 
-                if(option == 1) {
-                    //Send Position to Motor
-                    pc.printf("Input Degree : \n");
-                    pc.scanf("%f",&RH);
+                //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);
 
-                    R.setMotorPos(0x02,RH,temp_RK);
+                    bcom.setMotorPos(RIGHT_SIDE,RH,temp_RK);
                     temp_RH = RH;
 
-                } else if(option == 2) {
+
                     /********************Save Data*********************/
-                }
-            } while(option != 2);
+                } else bcom.saveDataToEEPROM(RIGHT_SIDE,MOTOR_UPPER_ANG);
+
+            } while(temp != 9999);
 
         } else if(option == 4) { //Right Knee
             do {
-                pc.printf("\n1) Input Degree\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
+                pc.printf("\nType 9999 to Exit to Main Menu\n");
 
-                if(option == 1) {
-                    //Send Position to Motor
-                    pc.printf("Input Degree : \n");
-                    pc.scanf("%f",&RK);
+                //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);
 
-                    R.setMotorPos(0x02,temp_RH,RK);
+                    bcom.setMotorPos(RIGHT_SIDE,temp_RH,RK);
                     temp_RK = RK;
 
-                } else if(option == 2) {
+
                     /********************Save Data*********************/
-                }
-            } while(option != 2);
+                } else bcom.saveDataToEEPROM(RIGHT_SIDE,MOTOR_LOWER_ANG);
+
+            } while(temp != 9999);
 
         } else if(option == 5) { //Left Hip
             do {
-                pc.printf("\n1) Input Kp of Left Hip\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
+                pc.printf("\nType 9999 to Exit to Main Menu\n");
+                pc.printf("\nInput Kp of Left Hip\n");
+                pc.scanf("%f",&temp);
 
-                if(option == 1) {
-                    InputK(&kp1);
-                    pc.printf("\nChange Kp of Left Hip to %f",kp1);
+                if(temp != 9999) {
+                    pc.printf("\nChange Kp of Left Hip to %f",temp);
 
-                    L.setUpMotorKp(0x01,kp1);
-                }
-            } while(option != 2);
+                    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("\n1) Input Ki of Left Hip\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
+                pc.printf("\nType 9999 to Exit to Main Menu\n");
+                pc.printf("\nInput Ki of Left Hip\n");
+                pc.scanf("%f",&temp);
 
-                if(option == 1) {
-                    InputK(&ki1);
-                    pc.printf("\nChange Ki of Left Hip to %f",ki1);
+                if(temp != 9999) {
+                    pc.printf("\nChange Ki of Left Hip to %f",temp);
 
-                    L.setUpMotorKi(0x01,ki1);
-                }
-            } while(option != 2);
+                    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("\n1) Input Kd of Left Hip\n");
-                pc.printf("2) Main Menu\n");
-                pc.scanf("%d",&option);
+                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);
 
-                if(option == 1) {
-                    InputK(&kd1);
-                    pc.printf("\nChange Kd of Left Hip to %f",kd1);
+        } else if(option == 8) { //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);
 
-                    L.setUpMotorKd(0x01,kd1);
-                }
-            } while(option != 2);
+                if(temp != 9999) {
+                    pc.printf("\nChange Kp of Left Knee to %f",temp);
 
-        } else if(option == 8) {
-            /*******************************Save Left Hip data*************************************/
+                    bcom.setLowMotorKp(LEFT_SIDE,temp);
+                } else bcom.saveDataToEEPROM(LEFT_SIDE,KP_LOWER_MOTOR);
+
+            } while(temp != 9999);
 
         } else if(option == 9) { //Left Knee
             do {
-                pc.printf("\n1) Input Kp of Left Knee\n");
-                pc.printf("2) Main Menu\n");
-                pc.scanf("%d",&option);
+                pc.printf("\nType 9999 to Exit to Main Menu\n");
+                pc.printf("\nInput Ki of Left Knee\n");
+                pc.scanf("%f",&temp);
 
-                if(option == 1) {
-                    InputK(&kp2);
-                    pc.printf("\nChange Kp of Left Knee to %f",kp2);
+                if(temp != 9999) {
+                    pc.printf("\nChange Ki of Left Knee to %f",temp);
 
-                    L.setLowMotorKp(0x01,kp2);
-                }
-            } while(option != 2);
+                    bcom.setLowMotorKi(LEFT_SIDE,temp);
+                } else bcom.saveDataToEEPROM(LEFT_SIDE,KI_LOWER_MOTOR);
+
+            } while(temp != 9999);
 
         } else if(option == 10) { //Left Knee
             do {
-                pc.printf("\n1) Input Ki of Left Knee\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
-
-                if(option == 1) {
-                    InputK(&ki2);
-                    pc.printf("\nChange Ki of Left Knee to %f",ki2);
-
-                    L.setLowMotorKi(0x01,ki2);
-                }
-            } while(option != 2);
+                pc.printf("\nType 9999 to Exit to Main Menu\n");
+                pc.printf("\nInput Kd of Left Knee\n");
+                pc.scanf("%f",&temp);
 
-        } else if(option == 11) { //Left Knee
-            do {
-                pc.printf("\n1) Input Kd of Left Knee\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
+                if(temp != 9999) {
+                    pc.printf("\nChange Kd of Left Knee to %f",temp);
 
-                if(option == 1) {
-                    InputK(&kd2);
-                    pc.printf("\nChange Kd of Left Knee to %f",kd2);
+                    bcom.setLowMotorKd(LEFT_SIDE,temp);
+                } else bcom.saveDataToEEPROM(LEFT_SIDE,KD_LOWER_MOTOR);
 
-                    L.setLowMotorKd(0x01,kd2);
-                }
-            } while(option != 2);
+            } while(temp != 9999);
 
 
-        } else if(option == 12) {
-            /*******************************Save Left Knee data*************************************/
+        } else if(option == 11) { //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 == 12) { //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 == 13) { //Right Hip
             do {
-                pc.printf("\n1) Input Kp of Right Hip\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
-
-                if(option == 1) {
-                    InputK(&kp3);
-                    pc.printf("\nChange Kp of Right Hip to %f",kp3);
-
-                    R.setUpMotorKp(0x02,kp3);
-                }
-            } while(option != 2);
-
-        } else if(option == 14) { //Right Hip
-            do {
-                pc.printf("\n1) Input Ki of Right Hip\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
+                pc.printf("\nType 9999 to Exit to Main Menu\n");
+                pc.printf("\nInput Kd of Right Hip\n");
+                pc.scanf("%f",&temp);
 
-                if(option == 1) {
-                    InputK(&ki3);
-                    pc.printf("\nChange Ki of Right Hip to %f",ki3);
-
-                    R.setUpMotorKi(0x02,ki3);
-                }
-            } while(option != 2);
+                if(temp != 9999) {
+                    pc.printf("\nChange Kd of Right Hip to %f",temp);
 
-        } else if(option == 15) { //Right Hip
-            do {
-                pc.printf("\n1) Input Kd of Right Hip\n");
-                pc.printf("2) Main Menu\n");
-
-                pc.scanf("%d",&option);
+                    bcom.setUpMotorKd(RIGHT_SIDE,temp);
+                } else bcom.saveDataToEEPROM(RIGHT_SIDE,KD_UPPER_MOTOR);
 
-                if(option == 1) {
-                    InputK(&kd3);
-                    pc.printf("\nChange Kd of Right Hip to %f",kd3);
-
-                    R.setUpMotorKd(0x02,kd3);
-                }
-            } while(option != 2);
+            } while(temp != 9999);
 
 
-        } else if(option == 16) {
-            /*******************************Save Right Hip data*************************************/
+        } else if(option == 14) { //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 == 15) { //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 == 16) { //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 == 17) { //Right Knee
-            do {
-                pc.printf("\n1) Input Kp of Right Knee\n");
-                pc.printf("2) Main Menu\n");
+        } else if(option == 17) { //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 == 18) { //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);
+        }
 
-                pc.scanf("%d",&option);
+        else if(option == 19) { //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);
+        }
 
-                if(option == 1) {
-                    InputK(&kp4);
-                    pc.printf("\nChange Kp of Right Knee to %f",kp4);
+        else if(option == 20) { //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);
+        }
 
-                    R.setLowMotorKp(0x02,kp4);
-                }
+        else if(option == 21) { //Lenght of Left Link Hip
+            pc.printf("\nType 9999 to Exit to Main Menu\n");
+            pc.printf("Input Lenght of Left Link Hip\n");
+            pc.scanf("%f",&temp);
+
+            if(temp != 9999) {
+                pc.printf("\nLenght of Left Link Hip = %f\n",temp);
+                LLink0 = temp;
+                a = true;
+                bcom.setUpLinkLength(LEFT_SIDE,temp);
+            } else bcom.saveDataToEEPROM(LEFT_SIDE,UP_LINK_LENGTH);
+        }
+
+        else if(option == 22) { //Lenght of Left Link Knee
+            pc.printf("\nType 9999 to Exit to Main Menu\n");
+            pc.printf("Input Lenght of Left Link Knee\n");
+            pc.scanf("%f",&temp);
+
+            if(temp != 9999) {
+                pc.printf("\nLenght of Left Link Knee = %f\n",temp);
+                LLink1 = temp;
+                b = true;
+                bcom.setLowLinkLength(LEFT_SIDE,temp);
+            } else bcom.saveDataToEEPROM(LEFT_SIDE,LOW_LINK_LENGTH);
+        }
 
-            } while(option != 2);
-        } else if(option == 18) { //Right Knee
-            do {
-                pc.printf("\n1) Input Ki of Right Knee\n");
-                pc.printf("2) Main Menu\n");
+        else if(option == 23) { //Lenght of Right Link Hip
+            pc.printf("\nType 9999 to Exit to Main Menu\n");
+            pc.printf("Input Lenght of Right Link Hip\n");
+            pc.scanf("%f",&temp);
+
+            if(temp != 9999) {
+                pc.printf("\nLenght of Right Link Hip = %f\n",temp);
+                RLink0 = temp;
+                c = true;
+                bcom.setUpLinkLength(RIGHT_SIDE,temp);
+            } else bcom.saveDataToEEPROM(RIGHT_SIDE,UP_LINK_LENGTH);
+        }
 
-                pc.scanf("%d",&option);
+        else if(option == 24) { //Lenght of Right Link Knee
+            pc.printf("\nType 9999 to Exit to Main Menu\n");
+            pc.printf("Input Lenght of Right Link Knee\n");
+            pc.scanf("%f",&temp);
+
+            if(temp != 9999) {
+                pc.printf("\nLenght of Right Link Knee = %f\n",temp);
+                RLink1 = temp;
+                d = true;
+                bcom.setLowLinkLength(RIGHT_SIDE,temp);
+            } else bcom.saveDataToEEPROM(RIGHT_SIDE,LOW_LINK_LENGTH);
+        }
 
-                if(option == 1) {
-                    InputK(&ki4);
-                    pc.printf("\nChange Ki of Right Knee to %f Degree\n",ki4);
+        else if(option == 25) { //Offset
+            if(a == true && b == true && c == true && d == true) {
+                float LHipAngle,LKneeAngle;
+                float RHipAngle,RKneeAngle;
+                bcom.getMotorPos(LEFT_SIDE,&LHipAngle,&LKneeAngle);
+                wait_ms(90);
+                bcom.getMotorPos(RIGHT_SIDE,&RHipAngle,&RKneeAngle);
+                wait_ms(90);
+
+                L.set_Link_Hip(LLink0);
+                L.set_Link_Knee(LLink1);
+                L.set_Zeta_Hip(LHipAngle);
+                L.set_Zeta_Knee(LKneeAngle);
+
+                R.set_Link_Hip(RLink0);
+                R.set_Link_Knee(RLink1);
+                R.set_Zeta_Hip(RHipAngle);
+                R.set_Zeta_Knee(RKneeAngle);
+
+                L.ForwardKinematicCalculation();
+                T.ForwardKinematicCalculation();
 
-                    R.setLowMotorKi(0x02,ki4);
-                }
-            } while(option != 2);
+                float offset_Y,offset_Z;
+                float y1,y2,z1,z2;
+                y1 = L.get_Position_Y();
+                y2 = R.get_Position_Y();
+                z1 = L.get_Position_Z();
+                z2 = R.get_Position_Z();
+                offset_Y = y1-y2;
+                offset_Z = z1-z2;
+
+                bcom.setOffset(LEFT_SIDE,offset_Y,offset_Z);
+
+                bcom.saveDataToEEPROM(LEFT_SIDE,OFFSET);
 
-        } else if(option == 19) { //Right Knee
-            do {
-                pc.printf("\n1) Input Kd of Right Knee\n");
-                pc.printf("2) Main Menu\n");
+            } else {
+                pc.printf("\nYou have to do choice 21-23 first\n\n");
+                wait(2);
+            }
+        }
+
+        else if(option == 26) { //setBodyWidth
+            pc.printf("\nType 9999 to Exit to Main Menu\n");
+            pc.printf("Input Body Width\n");
+            pc.scanf("%f",&temp);
+            pc.printf("\nBody Lenght = %f\n",temp);
+            bcom.setBodyWidth(LEFT_SIDE,temp);
+            bcom.saveDataToEEPROM(LEFT_SIDE,BODY_WIDTH);
+        }
 
-                pc.scanf("%d",&option);
+        else if(option == 27) { //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",&temp);
+
+                if(temp==1) { //Left
+                    pc.printf("Input Maximum Hip Angle Range of Left Side\n");
+                    pc.scanf("%f",&temp);
 
-                if(option == 1) {
-                    InputK(&kd4);
-                    pc.printf("\nChange Kd of Right Knee to %f",kd4);
+                    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);
+        }
 
-                    R.setLowMotorKd(0x02,kd4);
+        else if(option == 28) { //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",&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(option != 2);
-
-        } else if(option == 20) {
-            /*******************************Save Right Knee data*************************************/
+            } while(temp!=9999);
+        }
 
 
-        } else if(option == 21) {
-            pc.printf("Save Dog Stand Position\n");
+        else if(option == 29) { //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",&temp);
+
+                if(temp==1) { //Left
+                    pc.printf("Input Maximum Knee Angle Range of Left Side\n");
+                    pc.scanf("%f",&temp);
 
-            /*************************************Save Data to EEPROM***************************/
+                    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 == 22) {
-            pc.printf("Save Dog Sit Position\n");
+        else if(option == 30) { //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",&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);
 
-            /*************************************Save Data to EEPROM***************************/
+                } else if(temp==2) { //Right
+                    pc.printf("Input Minumum Knee Angle Range of Right Side\n");
+                    pc.scanf("%f",&temp);
 
-        } else if(option == 23) {
+                    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 = 23;
+                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("Invalid Option entered\n");
+        else { // if user has entered invalid choice
+            pc.printf("\nInvalid Option entered\n");
         }
-    } while(option != 23); // condition of do-while loop
+    } while(option != 40);
 
 }
 
-void WheelChair()
+void getData()
 {
-
-    //Start Here
-    //Stabilize.set_Body_Lenght(5);
-    Stabilize.set_current_zeta(roll);
-    //Stabilize.set_zeta_set(0);
-    //Stabilize.ZetaErrorCalculation();
-    Stabilize.PID();
-
-    Stabilize.set_New_Height(L.get_Position_Z());
+    
+}
 
-    //pc.printf("Height : %f, delta : %f, New Height : %f\n",L.get_Position_Z(),Stabilize.get_delta_h(),Stabilize.get_New_Height());
-    L.print();
-    L.set_Position_Z(Stabilize.get_New_Height());
-    L.InverseKinematicCalculation();
-    L.print();
-
-    R.set_Position_Y(L.get_Position_Y()+Stabilize.get_Offset_Y());
-    R.set_Position_Z(L.get_Position_Z()+Stabilize.get_Offset_Z());
-    //R.set_offset_YZ(3,3);
-    //R.SumPositionWithOffset();
-    R.InverseKinematicCalculation();
-    R.print();
-    pc.printf("\n");
-
-    //Send Position of L&R Angle to Motion Board
-
-
-
-    //End Here
-}
 
 int main()
 {
     pc.baud(115200);
-
-    if(button==1) SwMode();
+    
+    if(!button) { //Set up mode
+        while(!button);
+        pc.printf("Switch Mode\n");
+        SwMode();
+    }
 
     /*while(1){
         Kinematic test('P',10,15,10,10);