Prototyp V2

Dependencies:   PM2_Libary

Branch:
michi
Revision:
46:eba2263eb626
Parent:
45:8050724fe19b
Child:
47:8963ca9829b9
Child:
50:058dc65d0fa4
--- a/main.cpp	Mon Apr 18 16:02:10 2022 +0200
+++ b/main.cpp	Tue Apr 19 18:36:19 2022 +0200
@@ -6,15 +6,15 @@
 #include "math.h"
 //*******************************************************************************************************************************************************************
 // Defined Variables in mm coming from Hardware-team. Need to be updated
-float wheel_diameter = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn (4)
-float arm_length = 118.5; // lenght of arm from pivotpoint to pivotpoint (3)
-float dist_arm_attach_distsensor = 20; // distance between pivot point arm  on body to start distancesensor on top in horizontal (6)
-float dist_distsensors = 200; // distance between the two distancesensors on top of Wall-E (9)
-float dist_arm_ground = 51; // distance between pivotpoint arm and ground (5)
-float gripper_area_height = 16 ; // Height of Grappler cutout to grapple Stair (8)
-float dist_grappleratt_grappler_uk = 33; // distance between pivotpoint Grappler and bottom edge (?)
+float wheel_diameter                = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn (4)
+float arm_length                    = 118.5; // lenght of arm from pivotpoint to pivotpoint (3)
+float dist_arm_attach_distsensor    = 20; // distance between pivot point arm  on body to start distancesensor on top in horizontal (6)
+float dist_distsensors              = 200; // distance between the two distancesensors on top of Wall-E (9)
+float dist_arm_ground               = 51; // distance between pivotpoint arm and ground (5)
+float gripper_area_height           = 16 ; // Height of Grappler cutout to grapple Stair (8)
+float dist_grappleratt_grappler_uk  = 33; // distance between pivotpoint Grappler and bottom edge (?)
 
-float height_stairs = 100; // height to top of stairs in mm
+float height_stairs = 100; // height to top of next stairstep in mm
 //***********************************************************************************************************************************************************
 // declaration of Input - Output pins
 
@@ -32,24 +32,25 @@
 DigitalOut enable_motors(PB_15);    // create DigitalOut object to enable dc motors
 float   pwm_period_s = 0.00005f;    // define pwm period time in seconds and create FastPWM objects to command dc motors
 //motor pin declaration
-FastPWM pwm_M_right(PB_13);              
-FastPWM pwm_M_left(PA_9);
-FastPWM pwm_M_arm(PA_10);
+FastPWM pwm_M_right (PB_13);    //motor pin decalaration for wheels right side         
+FastPWM pwm_M_left  (PA_9);     //motor pin decalaration for wheels left side 
+FastPWM pwm_M_arm   (PA_10);    //motor pin decalaration for arm 
 
 //Encoder pin declaration
-EncoderCounter  encoder_M_right(PA_6, PC_7); //encoder pin decalaration for wheels right side
-EncoderCounter  encoder_M_left(PB_6, PB_7); //encoder pin decalaration for wheels left side
-EncoderCounter encoder_M_arm(PA_0, PA_1); //encoder pin decalaration for arm
+EncoderCounter  encoder_M_right (PA_6, PC_7); //encoder pin decalaration for wheels right side
+EncoderCounter  encoder_M_left  (PB_6, PB_7); //encoder pin decalaration for wheels left side
+EncoderCounter  encoder_M_arm   (PA_0, PA_1); //encoder pin decalaration for arm
 //***********************************************************************************************************************************************************
 // Hardware controll Setup and functions (motors and sensors)
 
 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
-float max_voltage = 12.0f;                  // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
-float counts_per_turn_wheels = 20.0f * 78.125f;    // define counts per turn at gearbox end (counts/turn * gearratio) for wheels
-float counts_per_turn_arm = 20.0f * 78.125f * 10.0f;      // define counts per turn at gearbox end (counts/turn * gearratio) for arm
-float kn = 180.0f / 12.0f;                  // define motor constant in rpm per V
-float k_gear = 100.0f / 78.125f;            // define additional ratio in case you are using a dc motor with a different gear box, e.g. 100:1 (DC with 100:1 has 2'000 turns for 360°)
-float kp = 0.1f;                            // define custom kp, this is the default speed controller gain for gear box 78.125:1
+float max_voltage               = 12.0f;     // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
+float counts_per_turn_wheels    = 20.0f * 78.125f;    // define counts per turn at gearbox end (counts/turn * gearratio) for wheels
+float counts_per_turn_arm       = 20.0f * 78.125f * 10.0f;    // define counts per turn at gearbox end (counts/turn * gearratio) for arm
+float kn                        = 180.0f / 12.0f;      // define motor constant in rpm per V
+float k_gear                    = 100.0f / 78.125f;   // define additional ratio in case you are using a dc motor with a different gear box, e.g. 100:1 (DC with 100:1 has 2'000 turns for 360°)
+float kp                        = 0.1f;     // define custom kp, this is the default speed controller gain for gear box 78.125:1
+
 
 //motors for tracks
 PositionController positionController_M_right(counts_per_turn_wheels * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M_right, encoder_M_right); // parameters adjusted to 100:1 gear, we need a different speed controller gain here
@@ -63,49 +64,51 @@
 // logic functions for basic movement
 
 //Platzhalter Variabeln für die Positionierung
-int drive_stright_mm    = 100;
-int PositionBackOff  = -100;
-float degArmStart      = 0.0;
-float degArmLift       = -0.5;
-int ToNextFunction = 0;  // current state of the system (which function is beeing executed)
-float max_speed_rps_wheel = 0.6f;  // define maximum speed that the position controller is changig the speed for the wheels, has to be smaller or equal to kn * max_voltage
-float max_speed_rps_arm = 0.3f; // define maximum speed that the position controller is changig the speed for the arm, has to be smaller or equal to kn * max_voltage
-double start_deg_arm = asin((dist_arm_ground - dist_grappleratt_grappler_uk) / arm_length); //calculates the starting degree of the arm (gripper has to touch ground in frotn of Wall-E)
-double current_deg_arm = start_deg_arm; // saves the current degree the arm has.
+int     drive_stright_mm = 100; // placeholder for testing drives amount forward
+int     drive_back_mm = -100; // placeholder for testing drives amount backwards
+int     ToNextFunction = 0;  // current state of the system (which function is beeing executed)
 
-// calculates the deg which the arm has to take to reach a certain height (the input height will be the height of OK Gripper area)
+// definition important variables
+float   pi = 2 * acos(0.0); // definiton of pi
+float   max_speed_rps_wheel = 0.6f;  // define maximum speed that the position controller is changig the speed for the wheels, has to be smaller or equal to kn * max_voltage
+float   max_speed_rps_arm = 0.3f; // define maximum speed that the position controller is changig the speed for the arm, has to be smaller or equal to kn * max_voltage
+float   start_deg_arm = -asin((dist_arm_ground - dist_grappleratt_grappler_uk) / arm_length) * 180.0/pi ; //calculates the starting degree of the arm (gripper has to touch ground in frotn of Wall-E)
+float   current_deg_arm = start_deg_arm; // saves the current degree the arm has.
+
+// calculates the deg which the arm has to take to reach a certain height (the input height has to be the height of OK Gripper area)
 double calc_arm_deg_for_height(int height_mm)
 {
-    if ((height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height)) > arm_length)
+    if ((height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height)) > arm_length) //check if height is reachable
     {
         printf("Error in calc_arm_deg_for_height: desired height is bigger than Wall-E arm lenght."); // error message when desired height is not reachable. 
     }
-    float height_arm = height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height);
-    float deg_arm_rad = asin(height_arm / arm_length); // deg in radians
-    float pi = 2 * acos(0.0); // definiton of pi
-    float deg_arm = deg_arm_rad * 180.0/pi; // deg in degrees
-    return deg_arm;
+    else 
+    {
+        float height_arm = height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height); // calculates the height which only the arm has to cover (- attachement height (arm to robot) etc.)
+        float deg_arm = asin(height_arm / arm_length) * 180.0/pi; // calculates the absolute degrees which the arm has to reach
+        return deg_arm;
+    }
+    return NULL; // <------ maybe error testing necessary (value deg_arm might not be returned)
 }
 
-//calculates the deg which the wheels have to turn in order to cover specified distnace in mm
+//calculates the deg which the wheels have to turn in order to cover specified distance in mm
 float wheel_dist_to_deg(int distance) // distance has to be in mm.
 {
-    float pi = 2 * acos(0.0); // definiton of pi
     float deg_wheel = distance * 360 /(wheel_diameter * pi);
     return deg_wheel;
 }
 
-// bring arme in starting position height of stairs.
+// bring arm in starting position. Height of stairs.
 int start_position()
 {
-    float deg_up_from_horizon = calc_arm_deg_for_height(height_stairs); //deg which arm motor has to turn to in order to grab stair. starting from horizontal position
+    double deg_up_from_horizon = calc_arm_deg_for_height(height_stairs); //deg which arm motor has to turn to in order to grab stair. starting from horizontal position
     float deg = deg_up_from_horizon + start_deg_arm;
     if ((0.0 > deg) || (deg > 360.0))
     {
-        printf("**************Error in start_position: degree is out of bound for Start Position.***************"); // error when desired reaching point is out of reach.
+        printf("Error in start_position: degree is out of bound for Start Position."); // error when desired reaching point is out of reach.
     }
     positionController_M_Arm.setDesiredRotation(deg / 360.0, max_speed_rps_arm); // command to turn motor to desired deg.
-    current_deg_arm = positionController_M_Arm.getRotation() / 360.0;
+    current_deg_arm = positionController_M_Arm.getRotation() * 360.0;
     return NULL;
 }
 
@@ -113,18 +116,18 @@
 // calculatioin of acctual distance with wheels is needed
 int drive_straight(float distance)
 {
-    double deg_to_turn = wheel_dist_to_deg(distance);
+    float deg_to_turn = wheel_dist_to_deg(distance);
     positionController_M_right.setDesiredRotation(deg_to_turn / 360.0, max_speed_rps_wheel);
     positionController_M_left.setDesiredRotation(deg_to_turn / 360.0, max_speed_rps_wheel);
     return NULL;   
 }
 
 //only turns the arm until the robot is on the next step
-//not yet clear if the motor controler function drives to a absolute poition or if it drives the given distance relative to the current position
-int lift_up(float deg)
+int lift_up()
 {
-    int8_t i = 0;         //prov condition variable
-    positionController_M_Arm.setDesiredRotation(deg / 360.0, max_speed_rps_arm);
+    float position_lift_end_deg = asin((-dist_arm_ground - (dist_grappleratt_grappler_uk-gripper_area_height)) / arm_length) - 90; // calculates the degree which has to be reached in order to get on top of next step
+    
+    positionController_M_Arm.setDesiredRotation(0, max_speed_rps_arm);
     return NULL;
 }
 //***********************************************************************************************************************************************************
@@ -133,7 +136,7 @@
 int check_start()
 {
 
-    return 0;
+    return NULL;
 }
 
 //pow function is here so we dont have to use the math.h library ************* unnecessary math.h is used any way ***************
@@ -169,7 +172,7 @@
     double distance = 0.0f; //distance in mm
     double infY =360 , supY = 2360; //Window for sensor values
     double voltage_mV = adc_value_mV;
-    double p1 = -1.127*powerx(10,-14), p2 = 8.881*powerx(10,-11), p3 = -2.76*powerx(10,-7), p4 = 0.0004262, p5 = -0.3363, p6 = 120.1 ; //faktoren für polynomkurve -> von matlab exportiert
+    double p1 = -1.127*powerx(10,-14), p2 = 8.881*powerx(10,-11), p3 = -2.76*powerx(10,-7), p4 = 0.0004262, p5 = -0.3363, p6 = 120.1 ; //faktoren fuer polynomkurve -> von matlab exportiert
     if((voltage_mV > infY) && (voltage_mV < supY))
     {
         distance = p1*powerx(voltage_mV,5) + p2*powerx(voltage_mV,4) + p3*powerx(voltage_mV,3) + p4*powerx(voltage_mV,2) + p5*voltage_mV + p6;
@@ -177,9 +180,6 @@
     return (distance);
 }
 
-// logical variable main task
-bool do_execute_main_task = false;  // this variable will be toggled via the user button (blue button) to or not to execute the main task
-
 // while loop gets executed every main_task_period_ms milliseconds
 int main_task_period_ms = 30;   // define main task period time in ms e.g. 30 ms -> main task runns ~33,33 times per second
 Timer main_task_timer;          // create Timer object which we use to run the main task every main task period time in ms
@@ -187,9 +187,9 @@
 
 int main(void)
 {
-// attach button fall and rise functions to user button object
-user_button.fall(&user_button_pressed_fcn);
-user_button.rise(&user_button_released_fcn);
+    // attach button fall and rise functions to user button object
+    user_button.fall(&user_button_pressed_fcn);
+    user_button.rise(&user_button_released_fcn);
   
     while (true)
     {
@@ -198,36 +198,38 @@
         
         switch (ToNextFunction) 
         {
-            case 0: 
+            case 0:
             break;
+
             case 1: 
             start_position();
-                printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
-                //   ToNextFunction+=1;
-                break;
+            printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
+            break;
+
             case 2: 
             drive_straight(drive_stright_mm);
             printf("Case 2: Position Right(rot): %3.3f;    Position Left (rot): %3.3f\n",
-               positionController_M_right.getRotation(),positionController_M_left.getRotation());
-                // ToNextFunction+=1;
-                break;
+            positionController_M_right.getRotation(),positionController_M_left.getRotation());
+            break;
+
             case 3: 
-            lift_up(degArmLift);
-                //  ToNextFunction+=1;
-                printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
-                break;
+            lift_up();
+            printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
+            break;
+
             case 4: 
-            drive_straight(PositionBackOff);
+            drive_straight(drive_back_mm);
             printf("Case 4: Position Right(rot): %3.3f;    Position Left (rot): %3.3f\n",
-               positionController_M_right.getRotation(),positionController_M_left.getRotation());
-                //   ToNextFunction+=1;
-                break;
+            positionController_M_right.getRotation(),positionController_M_left.getRotation());
+            break;
+
             case 5: 
-            lift_up(degArmStart);
-                printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
-                //  ToNextFunction = 0;
-                break;  
-                default:  ;
+            lift_up();
+            printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
+            ToNextFunction = 0;
+            break;  
+
+            default:  ;
         } 
     }
        // read timer and make the main thread sleep for the remaining time span (non blocking)