Prototyp V2

Dependencies:   PM2_Libary

Committer:
raomen
Date:
Wed Apr 27 16:57:11 2022 +0200
Branch:
michi
Revision:
80:4eae727a13b5
Parent:
79:368cbf09cf6a
Child:
81:909670edc2a2
correct turn_absolut_deg()

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmic 1:93d997d6b232 1 #include "mbed.h"
pmic 17:c19b471f05cb 2 #include "PM2_Libary.h"
lupomic 33:70ea029a69e8 3 #include <cstdint>
raomen 41:4a4978d1a578 4 #include <cstdio>
raomen 39:025d1bee1397 5 #include "math.h"
raomen 39:025d1bee1397 6 //*******************************************************************************************************************************************************************
raomen 39:025d1bee1397 7 // Defined Variables in mm coming from Hardware-team. Need to be updated
raomen 55:8cb262e56efb 8 const float wheel_diameter = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn (4)
raomen 55:8cb262e56efb 9 const float arm_length = 118.5; // lenght of arm from pivotpoint to pivotpoint (3)
raomen 55:8cb262e56efb 10 const float dist_arm_attach_distsensor = 20; // distance between pivot point arm on body to start distancesensor on top in horizontal (6)
raomen 55:8cb262e56efb 11 const float dist_distsensors = 200; // distance between the two distancesensors on top of Wall-E (9)
raomen 55:8cb262e56efb 12 const float dist_arm_ground = 51; // distance between pivotpoint arm and ground (5)
raomen 55:8cb262e56efb 13 const float gripper_area_height = 16 ; // Height of Grappler cutout to grapple Stair (8)
raomen 55:8cb262e56efb 14 const float dist_grappleratt_grappler_uk = 33; // distance between pivotpoint Grappler and bottom edge (?)
raomen 41:4a4978d1a578 15
raomen 55:8cb262e56efb 16 const float height_stairs = 100; // height to top of next stairstep in mm
raomen 39:025d1bee1397 17 //***********************************************************************************************************************************************************
raomen 41:4a4978d1a578 18 // declaration of Input - Output pins
pmic 17:c19b471f05cb 19
pmic 24:86f1a63e35a0 20 // user button on nucleo board
pmic 24:86f1a63e35a0 21 Timer user_button_timer; // create Timer object which we use to check if user button was pressed for a certain time (robust against signal bouncing)
pmic 24:86f1a63e35a0 22 InterruptIn user_button(PC_13); // create InterruptIn interface object to evaluate user button falling and rising edge (no blocking code in ISR)
pmic 24:86f1a63e35a0 23 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 24 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 25
pmic 24:86f1a63e35a0 26 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
raomen 38:c2663f7dcccb 27 float ir_distance_mV = 0.0f; // define variable to store measurement from infrared distancesensor in mVolt
raomen 55:8cb262e56efb 28 AnalogIn ir_analog_in(PC_3); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1
pmic 6:e1fa1a2d7483 29
pmic 24:86f1a63e35a0 30 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 24:86f1a63e35a0 31 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 24:86f1a63e35a0 32 float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors
raomen 48:0ab6b1fd455f 33
lupomic 33:70ea029a69e8 34 //motor pin declaration
raomen 46:eba2263eb626 35 FastPWM pwm_M_right (PB_13); //motor pin decalaration for wheels right side
raomen 46:eba2263eb626 36 FastPWM pwm_M_left (PA_9); //motor pin decalaration for wheels left side
raomen 46:eba2263eb626 37 FastPWM pwm_M_arm (PA_10); //motor pin decalaration for arm
pmic 17:c19b471f05cb 38
lupomic 33:70ea029a69e8 39 //Encoder pin declaration
raomen 46:eba2263eb626 40 EncoderCounter encoder_M_right (PA_6, PC_7); //encoder pin decalaration for wheels right side
raomen 46:eba2263eb626 41 EncoderCounter encoder_M_left (PB_6, PB_7); //encoder pin decalaration for wheels left side
raomen 46:eba2263eb626 42 EncoderCounter encoder_M_arm (PA_0, PA_1); //encoder pin decalaration for arm
raomen 41:4a4978d1a578 43 //***********************************************************************************************************************************************************
raomen 43:7964411b4a6b 44 // Hardware controll Setup and functions (motors and sensors)
pmic 17:c19b471f05cb 45
pmic 30:1e8295770bc1 46 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
raomen 55:8cb262e56efb 47 const float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
raomen 55:8cb262e56efb 48 const float counts_per_turn_wheels = 20.0f * 78.125f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels
raomen 59:f6c3e42f16c7 49 const float counts_per_turn_arm = 20.0f * 78.125f * 20.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm
raomen 55:8cb262e56efb 50 const float kn = 180.0f / 12.0f; // define motor constant in rpm per V
raomen 55:8cb262e56efb 51 const 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°)
raomen 55:8cb262e56efb 52 const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
raomen 46:eba2263eb626 53
lupomic 33:70ea029a69e8 54 //motors for tracks
lupomic 33:70ea029a69e8 55 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
lupomic 33:70ea029a69e8 56 PositionController positionController_M_left(counts_per_turn_wheels * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M_left, encoder_M_left); // parameters adjusted to 100:1 gear, we need a different speed controller gain here
lupomic 33:70ea029a69e8 57 //Arm Motor
lupomic 33:70ea029a69e8 58 PositionController positionController_M_Arm(counts_per_turn_arm * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M_arm, encoder_M_arm); // parameters adjusted to 100:1 gear, we need a different speed controller gain here
pmic 17:c19b471f05cb 59
lupomic 33:70ea029a69e8 60 // PositionController positionController_M3(counts_per_turn, kn, max_voltage, pwm_M3, encoder_M3); // default 78.125:1 gear with default contoller parameters
lupomic 33:70ea029a69e8 61 //PositionController positionController_M3(counts_per_turn * k_gear, kn / k_gear, kp * k_gear, max_voltage, pwm_M3, encoder_M3); // parameters adjusted to 100:1 gear, we need a different speed controller gain here
raomen 41:4a4978d1a578 62 //***********************************************************************************************************************************************************
raomen 80:4eae727a13b5 63 // calculations for basic movment and controll
raomen 41:4a4978d1a578 64
raomen 50:058dc65d0fa4 65 //placeholder variables for prototype testing
raomen 80:4eae727a13b5 66 const int drive_straight_mm = 100; // placeholder for testing drives amount forward
raomen 65:1ee1f319a199 67 const int drive_back_mm = -100; // placeholder for testing drives amount backwards
raomen 65:1ee1f319a199 68 int ToNextFunction = 0; // current state of the system (which function is beeing executed)
lupomic 33:70ea029a69e8 69
raomen 46:eba2263eb626 70 // definition important variables
raomen 55:8cb262e56efb 71 const float pi = 2 * acos(0.0); // definiton of pi
raomen 66:b4e55e1eebfc 72 const float max_speed_rps_wheel = 0.5f; // define maximum speed that the position controller is changig the speed for the wheels, has to be smaller or equal to kn * max_voltage
raomen 55:8cb262e56efb 73 const 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
raomen 55:8cb262e56efb 74 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)
raomen 65:1ee1f319a199 75
raomen 46:eba2263eb626 76 // 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)
raomen 74:d7569d530f6c 77 // PARAM: height_mm = height which OK Gripperarea has to reach.
raomen 74:d7569d530f6c 78 // RETURN: deg_arm = absolut Position in deg that the arm has to take.
raomen 65:1ee1f319a199 79 float calc_arm_deg_for_height(int height_mm)
raomen 40:e32c57763d92 80 {
raomen 51:7d165baaa646 81 float deg_arm;
raomen 46:eba2263eb626 82 if ((height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height)) > arm_length) //check if height is reachable
raomen 41:4a4978d1a578 83 {
raomen 43:7964411b4a6b 84 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.
raomen 41:4a4978d1a578 85 }
raomen 46:eba2263eb626 86 else
raomen 46:eba2263eb626 87 {
raomen 80:4eae727a13b5 88 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.)
raomen 51:7d165baaa646 89 deg_arm = asin(height_arm / arm_length) * 180.0/pi; // calculates the absolute degrees which the arm has to reach
raomen 46:eba2263eb626 90 }
raomen 51:7d165baaa646 91 return deg_arm;
raomen 40:e32c57763d92 92 }
raomen 38:c2663f7dcccb 93
raomen 67:3debc9a3cca5 94 //calculates position of arm when lift up has ended.
raomen 67:3debc9a3cca5 95 //RETURN: end_deg = degree which the motor has to turn in order to reach end lift position.
raomen 66:b4e55e1eebfc 96 float calc_pos_end_lift()
raomen 66:b4e55e1eebfc 97 {
raomen 66:b4e55e1eebfc 98 float end_deg;
raomen 80:4eae727a13b5 99 end_deg = asin((dist_arm_ground-(dist_grappleratt_grappler_uk-gripper_area_height))/arm_length);
raomen 80:4eae727a13b5 100 end_deg = 180 + (end_deg * 180 / pi);
raomen 66:b4e55e1eebfc 101 return end_deg;
raomen 66:b4e55e1eebfc 102 }
raomen 66:b4e55e1eebfc 103
raomen 46:eba2263eb626 104 //calculates the deg which the wheels have to turn in order to cover specified distance in mm
raomen 60:b2e9958f2298 105 //RETURN: deg_wheel = degree which the motor has to turn in order to cover distance(mm)
raomen 45:8050724fe19b 106 float wheel_dist_to_deg(int distance) // distance has to be in mm.
raomen 45:8050724fe19b 107 {
raomen 45:8050724fe19b 108 float deg_wheel = distance * 360 /(wheel_diameter * pi);
raomen 45:8050724fe19b 109 return deg_wheel;
raomen 45:8050724fe19b 110 }
raomen 45:8050724fe19b 111
raomen 58:3cd93949a7d7 112 // increments the Motor for defined degree from the current one
raomen 58:3cd93949a7d7 113 // PARAM: deg_to_turn = degree to turn the Motor
raomen 80:4eae727a13b5 114 // PARAM: current_rotation = the current rotation of the Motor (Motor.getRotation())
raomen 80:4eae727a13b5 115 // RETURN: new_turn_rotation = new Rotation value in rotations
raomen 80:4eae727a13b5 116 float turn_relative_deg(float deg_to_turn, float current_rotation)
raomen 57:79732e5818d7 117 {
raomen 80:4eae727a13b5 118 float new_turn_rotation = current_rotation + deg_to_turn/360.0;
raomen 57:79732e5818d7 119 return new_turn_rotation;
raomen 57:79732e5818d7 120 }
raomen 57:79732e5818d7 121
raomen 80:4eae727a13b5 122 // sets the Motor to a specified degree in one rotation
raomen 80:4eae727a13b5 123 // PARAM: end_deg = new position of the arm in degree 0 <= value >=360
raomen 80:4eae727a13b5 124 // PARAM: current_rotation = the current rotation of the Motor (Motor.getRotation())
raomen 80:4eae727a13b5 125 // RETURN: new_partial_rotation = new deg value in rotations
raomen 80:4eae727a13b5 126 float turn_absolut_deg(float end_deg, float current_rotations)
raomen 80:4eae727a13b5 127 {
raomen 80:4eae727a13b5 128 int full_rotations;
raomen 80:4eae727a13b5 129 if(current_rotations > 0)
raomen 80:4eae727a13b5 130 {
raomen 80:4eae727a13b5 131 full_rotations = round(current_rotations - 0.5);
raomen 80:4eae727a13b5 132 }
raomen 80:4eae727a13b5 133 else if(current_rotations < 0)
raomen 80:4eae727a13b5 134 {
raomen 80:4eae727a13b5 135 full_rotations = round(current_rotations + 0.5);
raomen 80:4eae727a13b5 136 }
raomen 80:4eae727a13b5 137 else
raomen 80:4eae727a13b5 138 {
raomen 80:4eae727a13b5 139 full_rotations = 0;
raomen 80:4eae727a13b5 140 }
raomen 80:4eae727a13b5 141 float new_partial_rotation = full_rotations - start_deg_arm/360 + end_deg/360;
raomen 80:4eae727a13b5 142 return new_partial_rotation;
raomen 80:4eae727a13b5 143 }
raomen 80:4eae727a13b5 144 //***********************************************************************************************************************************************************
raomen 80:4eae727a13b5 145
raomen 80:4eae727a13b5 146 // import functions from file mapping
raomen 80:4eae727a13b5 147 extern double powerx(double base, double pow2);
raomen 80:4eae727a13b5 148 extern double mapping (float adc_value_mV);
raomen 75:3831b90a4ae0 149
raomen 75:3831b90a4ae0 150 //simple check if there is an object in proximity
raomen 75:3831b90a4ae0 151 //returns 0 if there is NO object present
raomen 75:3831b90a4ae0 152 //returns 1 if there is an object present
raomen 75:3831b90a4ae0 153 //returns 2 if the distance isn't in the expected range
raomen 80:4eae727a13b5 154 uint8_t StepDetection(double distance)
raomen 80:4eae727a13b5 155 {
raomen 75:3831b90a4ae0 156 double d_valueMM = distance;
raomen 79:368cbf09cf6a 157 if(d_valueMM >= 4) return 0;
raomen 79:368cbf09cf6a 158 else if(d_valueMM < 4) return 1;
raomen 79:368cbf09cf6a 159 else if(d_valueMM <= 0 || d_valueMM > 100 ) return 2;
raomen 79:368cbf09cf6a 160 else return 2;
raomen 75:3831b90a4ae0 161
raomen 75:3831b90a4ae0 162 }
raomen 58:3cd93949a7d7 163
raomen 46:eba2263eb626 164 // bring arm in starting position. Height of stairs.
raomen 68:e3fc5ed0bc0e 165 void set_arm_stair_height()
raomen 42:6e7ab1136354 166 {
raomen 46:eba2263eb626 167 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
raomen 45:8050724fe19b 168 float deg = deg_up_from_horizon + start_deg_arm;
raomen 66:b4e55e1eebfc 169
raomen 64:72b9efe62ece 170 enable_motors = 1;
raomen 45:8050724fe19b 171 positionController_M_Arm.setDesiredRotation(deg / 360.0, max_speed_rps_arm); // command to turn motor to desired deg.
raomen 64:72b9efe62ece 172 enable_motors = 0;
raomen 42:6e7ab1136354 173 }
raomen 42:6e7ab1136354 174
raomen 55:8cb262e56efb 175 //Drives forward into the next step
raomen 55:8cb262e56efb 176 //Prameter:distance in milimeter
raomen 64:72b9efe62ece 177 void drive_straight(float distance)
raomen 40:e32c57763d92 178 {
raomen 46:eba2263eb626 179 float deg_to_turn = wheel_dist_to_deg(distance);
raomen 60:b2e9958f2298 180
raomen 60:b2e9958f2298 181 float relativ_turns_rightmotor = turn_relative_deg(deg_to_turn, positionController_M_right.getRotation());
raomen 60:b2e9958f2298 182 float relativ_turns_leftmotor = turn_relative_deg(deg_to_turn, positionController_M_left.getRotation());
raomen 66:b4e55e1eebfc 183
raomen 64:72b9efe62ece 184 enable_motors = 1;
raomen 60:b2e9958f2298 185 positionController_M_right.setDesiredRotation(relativ_turns_rightmotor, max_speed_rps_wheel);
raomen 64:72b9efe62ece 186 positionController_M_left.setDesiredRotation(relativ_turns_leftmotor, max_speed_rps_wheel);
raomen 64:72b9efe62ece 187 enable_motors = 0;
lupomic 33:70ea029a69e8 188 }
lupomic 33:70ea029a69e8 189
raomen 74:d7569d530f6c 190 //turns the arm until the robot is on the next step
raomen 64:72b9efe62ece 191 void lift_up()
raomen 40:e32c57763d92 192 {
raomen 78:44291588a875 193 float position_lift_end_deg = calc_pos_end_lift(); // calculates the degree which has to be reached in order to get on top of next step
raomen 77:508d8fda4aa0 194 float absolut_pos_arm = turn_absolut_deg(position_lift_end_deg, positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 195
raomen 64:72b9efe62ece 196 enable_motors = 1;
raomen 77:508d8fda4aa0 197 positionController_M_Arm.setDesiredRotation(absolut_pos_arm, max_speed_rps_arm);
raomen 64:72b9efe62ece 198 enable_motors = 0;
lupomic 33:70ea029a69e8 199 }
raomen 43:7964411b4a6b 200 //***********************************************************************************************************************************************************
raomen 38:c2663f7dcccb 201
raomen 80:4eae727a13b5 202 //Function which checks if sensors and motors have been wired correctly and the expectet results will happen. otherwise Wall-E will ???.
raomen 65:1ee1f319a199 203 void check_start()
raomen 43:7964411b4a6b 204 {
raomen 43:7964411b4a6b 205
raomen 43:7964411b4a6b 206 }
raomen 43:7964411b4a6b 207
raomen 41:4a4978d1a578 208 // while loop gets executed every main_task_period_ms milliseconds
raomen 41:4a4978d1a578 209 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
raomen 41:4a4978d1a578 210 Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms
raomen 43:7964411b4a6b 211 //***********************************************************************************************************************************************************
raomen 39:025d1bee1397 212
lupomic 33:70ea029a69e8 213 int main(void)
pmic 23:26b3a25fc637 214 {
raomen 46:eba2263eb626 215 // attach button fall and rise functions to user button object
raomen 46:eba2263eb626 216 user_button.fall(&user_button_pressed_fcn);
raomen 46:eba2263eb626 217 user_button.rise(&user_button_released_fcn);
lupomic 34:9f779e91168e 218
raomen 40:e32c57763d92 219 while (true)
raomen 40:e32c57763d92 220 {
raomen 45:8050724fe19b 221 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
raomen 75:3831b90a4ae0 222 printf("detection: %d\n",StepDetection(mapping(ir_distance_mV)));
raomen 75:3831b90a4ae0 223
raomen 40:e32c57763d92 224 switch (ToNextFunction)
raomen 40:e32c57763d92 225 {
raomen 46:eba2263eb626 226
raomen 45:8050724fe19b 227 case 1:
raomen 68:e3fc5ed0bc0e 228 set_arm_stair_height();
raomen 46:eba2263eb626 229 printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 230 break;
raomen 46:eba2263eb626 231
raomen 45:8050724fe19b 232 case 2:
raomen 80:4eae727a13b5 233 drive_straight(drive_straight_mm);
raomen 45:8050724fe19b 234 printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
raomen 46:eba2263eb626 235 positionController_M_right.getRotation(),positionController_M_left.getRotation());
raomen 46:eba2263eb626 236 break;
raomen 46:eba2263eb626 237
raomen 45:8050724fe19b 238 case 3:
raomen 46:eba2263eb626 239 lift_up();
raomen 46:eba2263eb626 240 printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 241 break;
raomen 46:eba2263eb626 242
raomen 45:8050724fe19b 243 case 4:
raomen 46:eba2263eb626 244 drive_straight(drive_back_mm);
lupomic 34:9f779e91168e 245 printf("Case 4: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
raomen 46:eba2263eb626 246 positionController_M_right.getRotation(),positionController_M_left.getRotation());
raomen 65:1ee1f319a199 247 ToNextFunction = 0;
raomen 46:eba2263eb626 248 break;
raomen 46:eba2263eb626 249
raomen 46:eba2263eb626 250 default: ;
lupomic 33:70ea029a69e8 251 }
lupomic 33:70ea029a69e8 252 }
raomen 80:4eae727a13b5 253 // read timer and make the main thread sleep for the remaining time span (non blocking)
raomen 80:4eae727a13b5 254 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
raomen 80:4eae727a13b5 255 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
raomen 80:4eae727a13b5 256 return 0;
pmic 1:93d997d6b232 257 }
pmic 6:e1fa1a2d7483 258
lupomic 33:70ea029a69e8 259
pmic 24:86f1a63e35a0 260 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 261 {
pmic 26:28693b369945 262 user_button_timer.start();
pmic 6:e1fa1a2d7483 263 user_button_timer.reset();
pmic 6:e1fa1a2d7483 264 }
pmic 6:e1fa1a2d7483 265
raomen 43:7964411b4a6b 266 void user_button_released_fcn()
raomen 43:7964411b4a6b 267 {
pmic 24:86f1a63e35a0 268 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 269 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 270 user_button_timer.stop();
raomen 43:7964411b4a6b 271 if (user_button_elapsed_time_ms > 200)
raomen 43:7964411b4a6b 272 {
raomen 43:7964411b4a6b 273 ToNextFunction += 1;
raomen 43:7964411b4a6b 274 }
raomen 43:7964411b4a6b 275 }