Prototyp V2

Dependencies:   PM2_Libary

Committer:
raomen
Date:
Fri Apr 22 14:15:25 2022 +0200
Branch:
michi
Revision:
63:43c14a63b337
Parent:
62:188f3a8ebf49
Child:
64:72b9efe62ece
after first test fix

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"
raomen 62:188f3a8ebf49 3 #include <cmath> //<-- is <cmath> needed? check later.
lupomic 33:70ea029a69e8 4 #include <cstdint>
raomen 41:4a4978d1a578 5 #include <cstdio>
raomen 39:025d1bee1397 6 #include "math.h"
raomen 39:025d1bee1397 7 //*******************************************************************************************************************************************************************
raomen 39:025d1bee1397 8 // Defined Variables in mm coming from Hardware-team. Need to be updated
raomen 55:8cb262e56efb 9 const float wheel_diameter = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn (4)
raomen 55:8cb262e56efb 10 const float arm_length = 118.5; // lenght of arm from pivotpoint to pivotpoint (3)
raomen 55:8cb262e56efb 11 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 12 const float dist_distsensors = 200; // distance between the two distancesensors on top of Wall-E (9)
raomen 55:8cb262e56efb 13 const float dist_arm_ground = 51; // distance between pivotpoint arm and ground (5)
raomen 55:8cb262e56efb 14 const float gripper_area_height = 16 ; // Height of Grappler cutout to grapple Stair (8)
raomen 55:8cb262e56efb 15 const float dist_grappleratt_grappler_uk = 33; // distance between pivotpoint Grappler and bottom edge (?)
raomen 41:4a4978d1a578 16
raomen 55:8cb262e56efb 17 const float height_stairs = 100; // height to top of next stairstep in mm
raomen 39:025d1bee1397 18 //***********************************************************************************************************************************************************
raomen 41:4a4978d1a578 19 // declaration of Input - Output pins
pmic 17:c19b471f05cb 20
pmic 24:86f1a63e35a0 21 // user button on nucleo board
pmic 24:86f1a63e35a0 22 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 23 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 24 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 25 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 26
pmic 24:86f1a63e35a0 27 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
raomen 38:c2663f7dcccb 28 float ir_distance_mV = 0.0f; // define variable to store measurement from infrared distancesensor in mVolt
raomen 55:8cb262e56efb 29 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 30
pmic 24:86f1a63e35a0 31 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 24:86f1a63e35a0 32 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 24:86f1a63e35a0 33 float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors
raomen 48:0ab6b1fd455f 34
lupomic 33:70ea029a69e8 35 //motor pin declaration
raomen 46:eba2263eb626 36 FastPWM pwm_M_right (PB_13); //motor pin decalaration for wheels right side
raomen 46:eba2263eb626 37 FastPWM pwm_M_left (PA_9); //motor pin decalaration for wheels left side
raomen 46:eba2263eb626 38 FastPWM pwm_M_arm (PA_10); //motor pin decalaration for arm
pmic 17:c19b471f05cb 39
lupomic 33:70ea029a69e8 40 //Encoder pin declaration
raomen 46:eba2263eb626 41 EncoderCounter encoder_M_right (PA_6, PC_7); //encoder pin decalaration for wheels right side
raomen 46:eba2263eb626 42 EncoderCounter encoder_M_left (PB_6, PB_7); //encoder pin decalaration for wheels left side
raomen 46:eba2263eb626 43 EncoderCounter encoder_M_arm (PA_0, PA_1); //encoder pin decalaration for arm
raomen 41:4a4978d1a578 44 //***********************************************************************************************************************************************************
raomen 43:7964411b4a6b 45 // Hardware controll Setup and functions (motors and sensors)
pmic 17:c19b471f05cb 46
pmic 30:1e8295770bc1 47 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
raomen 55:8cb262e56efb 48 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 49 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 50 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 51 const float kn = 180.0f / 12.0f; // define motor constant in rpm per V
raomen 55:8cb262e56efb 52 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 53 const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
raomen 46:eba2263eb626 54
lupomic 33:70ea029a69e8 55 //motors for tracks
lupomic 33:70ea029a69e8 56 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 57 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 58 //Arm Motor
lupomic 33:70ea029a69e8 59 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 60
lupomic 33:70ea029a69e8 61 // 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 62 //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 63 //***********************************************************************************************************************************************************
raomen 43:7964411b4a6b 64 // logic functions for basic movement
raomen 41:4a4978d1a578 65
raomen 50:058dc65d0fa4 66 //placeholder variables for prototype testing
raomen 55:8cb262e56efb 67 const int drive_stright_mm = 100; // placeholder for testing drives amount forward
raomen 55:8cb262e56efb 68 const int drive_back_mm = -100; // placeholder for testing drives amount backwards
raomen 55:8cb262e56efb 69 int ToNextFunction = 0; // current state of the system (which function is beeing executed)
lupomic 33:70ea029a69e8 70
raomen 46:eba2263eb626 71 // definition important variables
raomen 55:8cb262e56efb 72 const float pi = 2 * acos(0.0); // definiton of pi
raomen 55:8cb262e56efb 73 const 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
raomen 55:8cb262e56efb 74 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 75 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 55:8cb262e56efb 76 float current_deg_arm = start_deg_arm; // saves the current degree the arm has.
raomen 46:eba2263eb626 77
raomen 47:8963ca9829b9 78 // import functions from file mapping
raomen 52:adfcbf71be5b 79 extern double powerx(double base, double pow2);
raomen 52:adfcbf71be5b 80 extern double mapping (float adc_value_mV);
lupomic 36:6116ce98080d 81
raomen 46:eba2263eb626 82 // 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 42:6e7ab1136354 83 double calc_arm_deg_for_height(int height_mm)
raomen 40:e32c57763d92 84 {
raomen 51:7d165baaa646 85 float deg_arm;
raomen 46:eba2263eb626 86 if ((height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height)) > arm_length) //check if height is reachable
raomen 41:4a4978d1a578 87 {
raomen 43:7964411b4a6b 88 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 89 }
raomen 46:eba2263eb626 90 else
raomen 46:eba2263eb626 91 {
raomen 46:eba2263eb626 92 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 93 deg_arm = asin(height_arm / arm_length) * 180.0/pi; // calculates the absolute degrees which the arm has to reach
raomen 46:eba2263eb626 94 }
raomen 51:7d165baaa646 95 return deg_arm;
raomen 40:e32c57763d92 96 }
raomen 38:c2663f7dcccb 97
raomen 46:eba2263eb626 98 //calculates the deg which the wheels have to turn in order to cover specified distance in mm
raomen 60:b2e9958f2298 99 //RETURN: deg_wheel = degree which the motor has to turn in order to cover distance(mm)
raomen 45:8050724fe19b 100 float wheel_dist_to_deg(int distance) // distance has to be in mm.
raomen 45:8050724fe19b 101 {
raomen 45:8050724fe19b 102 float deg_wheel = distance * 360 /(wheel_diameter * pi);
raomen 45:8050724fe19b 103 return deg_wheel;
raomen 45:8050724fe19b 104 }
raomen 45:8050724fe19b 105
raomen 58:3cd93949a7d7 106 // increments the Motor for defined degree from the current one
raomen 58:3cd93949a7d7 107 // PARAM: deg_to_turn = degree to turn the Motor
raomen 58:3cd93949a7d7 108 // PARAM: current_full_rotation = the current rotation of the Motor (Motor.getRotation())
raomen 58:3cd93949a7d7 109 // RETURN: new Rotation value in rotations
raomen 57:79732e5818d7 110 float turn_relative_deg(float deg_to_turn, float current_full_rotation)
raomen 57:79732e5818d7 111 {
raomen 57:79732e5818d7 112 float current_deg = current_full_rotation * 360;
raomen 58:3cd93949a7d7 113 float new_turn_rotation = (current_deg - deg_to_turn) / 360.0;
raomen 57:79732e5818d7 114 return new_turn_rotation;
raomen 57:79732e5818d7 115 }
raomen 57:79732e5818d7 116
raomen 58:3cd93949a7d7 117 // sets the Motor to a specified degree in one rotation
raomen 58:3cd93949a7d7 118 // PARAM: end_deg = new position of the arm in degree 0 <= value >=360
raomen 60:b2e9958f2298 119 // PARAM: current_rotations = the current rotation of the Motor (Motor.getRotation())
raomen 60:b2e9958f2298 120 // RETURN: new_partial_rotation = new deg value in rotations
raomen 61:2ff627973f2c 121 float turn_absolut_deg(float end_deg, float current_rotations)
raomen 58:3cd93949a7d7 122 {
raomen 60:b2e9958f2298 123 int full_rotations = current_rotations;
raomen 60:b2e9958f2298 124 int new_partial_rotation = current_rotations + start_deg_arm/360;
raomen 60:b2e9958f2298 125 return new_partial_rotation;
raomen 58:3cd93949a7d7 126 }
raomen 58:3cd93949a7d7 127
raomen 46:eba2263eb626 128 // bring arm in starting position. Height of stairs.
raomen 42:6e7ab1136354 129 int start_position()
raomen 42:6e7ab1136354 130 {
raomen 46:eba2263eb626 131 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 132 float deg = deg_up_from_horizon + start_deg_arm;
raomen 43:7964411b4a6b 133 if ((0.0 > deg) || (deg > 360.0))
raomen 42:6e7ab1136354 134 {
raomen 46:eba2263eb626 135 printf("Error in start_position: degree is out of bound for Start Position."); // error when desired reaching point is out of reach.
raomen 42:6e7ab1136354 136 }
raomen 45:8050724fe19b 137 positionController_M_Arm.setDesiredRotation(deg / 360.0, max_speed_rps_arm); // command to turn motor to desired deg.
raomen 50:058dc65d0fa4 138 current_deg_arm = positionController_M_Arm.getRotation() * 360.0; //write new position to variable
raomen 42:6e7ab1136354 139 return NULL;
raomen 42:6e7ab1136354 140 }
raomen 42:6e7ab1136354 141
raomen 55:8cb262e56efb 142 //Drives forward into the next step
raomen 55:8cb262e56efb 143 //Prameter:distance in milimeter
raomen 43:7964411b4a6b 144 int drive_straight(float distance)
raomen 40:e32c57763d92 145 {
raomen 46:eba2263eb626 146 float deg_to_turn = wheel_dist_to_deg(distance);
raomen 60:b2e9958f2298 147
raomen 60:b2e9958f2298 148 float relativ_turns_rightmotor = turn_relative_deg(deg_to_turn, positionController_M_right.getRotation());
raomen 60:b2e9958f2298 149 float relativ_turns_leftmotor = turn_relative_deg(deg_to_turn, positionController_M_left.getRotation());
raomen 60:b2e9958f2298 150 positionController_M_right.setDesiredRotation(relativ_turns_rightmotor, max_speed_rps_wheel);
raomen 60:b2e9958f2298 151 positionController_M_left.setDesiredRotation(relativ_turns_leftmotor, max_speed_rps_wheel);
raomen 45:8050724fe19b 152 return NULL;
lupomic 33:70ea029a69e8 153 }
lupomic 33:70ea029a69e8 154
lupomic 33:70ea029a69e8 155 //only turns the arm until the robot is on the next step
raomen 46:eba2263eb626 156 int lift_up()
raomen 40:e32c57763d92 157 {
raomen 46:eba2263eb626 158 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
raomen 46:eba2263eb626 159
raomen 46:eba2263eb626 160 positionController_M_Arm.setDesiredRotation(0, max_speed_rps_arm);
raomen 45:8050724fe19b 161 return NULL;
lupomic 33:70ea029a69e8 162 }
raomen 43:7964411b4a6b 163 //***********************************************************************************************************************************************************
raomen 38:c2663f7dcccb 164
raomen 43:7964411b4a6b 165 //Function which checks if sensors and motors have been wired correctly and the expectet results will happen. otherwise Wall-E will show with armmovement.
raomen 43:7964411b4a6b 166 int check_start()
raomen 43:7964411b4a6b 167 {
raomen 43:7964411b4a6b 168
raomen 46:eba2263eb626 169 return NULL;
raomen 43:7964411b4a6b 170 }
raomen 43:7964411b4a6b 171
raomen 41:4a4978d1a578 172 // while loop gets executed every main_task_period_ms milliseconds
raomen 41:4a4978d1a578 173 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 174 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 175 //***********************************************************************************************************************************************************
raomen 39:025d1bee1397 176
lupomic 33:70ea029a69e8 177 int main(void)
pmic 23:26b3a25fc637 178 {
raomen 46:eba2263eb626 179 // attach button fall and rise functions to user button object
raomen 46:eba2263eb626 180 user_button.fall(&user_button_pressed_fcn);
raomen 46:eba2263eb626 181 user_button.rise(&user_button_released_fcn);
lupomic 34:9f779e91168e 182
raomen 40:e32c57763d92 183 while (true)
raomen 40:e32c57763d92 184 {
raomen 63:43c14a63b337 185 enable_motors = 1; <-- (fix activate just when needed.)
raomen 45:8050724fe19b 186 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
lupomic 33:70ea029a69e8 187
raomen 40:e32c57763d92 188 switch (ToNextFunction)
raomen 40:e32c57763d92 189 {
raomen 46:eba2263eb626 190
raomen 45:8050724fe19b 191 case 1:
raomen 45:8050724fe19b 192 start_position();
raomen 46:eba2263eb626 193 printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 194 break;
raomen 46:eba2263eb626 195
raomen 45:8050724fe19b 196 case 2:
raomen 45:8050724fe19b 197 drive_straight(drive_stright_mm);
raomen 45:8050724fe19b 198 printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
raomen 46:eba2263eb626 199 positionController_M_right.getRotation(),positionController_M_left.getRotation());
raomen 46:eba2263eb626 200 break;
raomen 46:eba2263eb626 201
raomen 45:8050724fe19b 202 case 3:
raomen 46:eba2263eb626 203 lift_up();
raomen 46:eba2263eb626 204 printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 205 break;
raomen 46:eba2263eb626 206
raomen 45:8050724fe19b 207 case 4:
raomen 46:eba2263eb626 208 drive_straight(drive_back_mm);
lupomic 34:9f779e91168e 209 printf("Case 4: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
raomen 46:eba2263eb626 210 positionController_M_right.getRotation(),positionController_M_left.getRotation());
raomen 46:eba2263eb626 211 break;
raomen 46:eba2263eb626 212
raomen 45:8050724fe19b 213 case 5:
raomen 46:eba2263eb626 214 lift_up();
raomen 46:eba2263eb626 215 printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 216 ToNextFunction = 0;
raomen 46:eba2263eb626 217 break;
raomen 46:eba2263eb626 218
raomen 46:eba2263eb626 219 default: ;
lupomic 33:70ea029a69e8 220 }
lupomic 33:70ea029a69e8 221 }
lupomic 33:70ea029a69e8 222 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 223 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 224 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
raomen 47:8963ca9829b9 225 return 0;
pmic 1:93d997d6b232 226 }
pmic 6:e1fa1a2d7483 227
lupomic 33:70ea029a69e8 228
pmic 24:86f1a63e35a0 229 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 230 {
pmic 26:28693b369945 231 user_button_timer.start();
pmic 6:e1fa1a2d7483 232 user_button_timer.reset();
pmic 6:e1fa1a2d7483 233 }
pmic 6:e1fa1a2d7483 234
raomen 43:7964411b4a6b 235 void user_button_released_fcn()
raomen 43:7964411b4a6b 236 {
pmic 24:86f1a63e35a0 237 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 238 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 239 user_button_timer.stop();
raomen 43:7964411b4a6b 240 if (user_button_elapsed_time_ms > 200)
raomen 43:7964411b4a6b 241 {
raomen 43:7964411b4a6b 242 ToNextFunction += 1;
raomen 43:7964411b4a6b 243 }
raomen 43:7964411b4a6b 244 }