Prototyp V2

Dependencies:   PM2_Libary

Committer:
raomen
Date:
Tue Apr 19 20:35:29 2022 +0200
Branch:
michi
Revision:
50:058dc65d0fa4
Parent:
46:eba2263eb626
Child:
53:42b3280e4510
quick fix mbed.h (unsure if working)

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 41:4a4978d1a578 3 #include <cmath>
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 46:eba2263eb626 9 float wheel_diameter = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn (4)
raomen 46:eba2263eb626 10 float arm_length = 118.5; // lenght of arm from pivotpoint to pivotpoint (3)
raomen 46:eba2263eb626 11 float dist_arm_attach_distsensor = 20; // distance between pivot point arm on body to start distancesensor on top in horizontal (6)
raomen 46:eba2263eb626 12 float dist_distsensors = 200; // distance between the two distancesensors on top of Wall-E (9)
raomen 46:eba2263eb626 13 float dist_arm_ground = 51; // distance between pivotpoint arm and ground (5)
raomen 46:eba2263eb626 14 float gripper_area_height = 16 ; // Height of Grappler cutout to grapple Stair (8)
raomen 46:eba2263eb626 15 float dist_grappleratt_grappler_uk = 33; // distance between pivotpoint Grappler and bottom edge (?)
raomen 41:4a4978d1a578 16
raomen 50:058dc65d0fa4 17 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
pmic 24:86f1a63e35a0 29 AnalogIn ir_analog_in(PC_2); // 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
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 46:eba2263eb626 47 float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
raomen 46:eba2263eb626 48 float counts_per_turn_wheels = 20.0f * 78.125f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels
raomen 46:eba2263eb626 49 float counts_per_turn_arm = 20.0f * 78.125f * 10.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm
raomen 46:eba2263eb626 50 float kn = 180.0f / 12.0f; // define motor constant in rpm per V
raomen 46:eba2263eb626 51 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 46:eba2263eb626 52 float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
raomen 46:eba2263eb626 53
pmic 6:e1fa1a2d7483 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 46:eba2263eb626 67 int drive_stright_mm = 100; // placeholder for testing drives amount forward
raomen 46:eba2263eb626 68 int drive_back_mm = -100; // placeholder for testing drives amount backwards
raomen 46:eba2263eb626 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 46:eba2263eb626 72 float pi = 2 * acos(0.0); // definiton of pi
raomen 46:eba2263eb626 73 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 46:eba2263eb626 74 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 46:eba2263eb626 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 46:eba2263eb626 76 float current_deg_arm = start_deg_arm; // saves the current degree the arm has.
raomen 46:eba2263eb626 77
raomen 46:eba2263eb626 78 // 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 79 double calc_arm_deg_for_height(int height_mm)
raomen 40:e32c57763d92 80 {
raomen 46:eba2263eb626 81 if ((height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height)) > arm_length) //check if height is reachable
raomen 41:4a4978d1a578 82 {
raomen 43:7964411b4a6b 83 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 84 }
raomen 46:eba2263eb626 85 else
raomen 46:eba2263eb626 86 {
raomen 46:eba2263eb626 87 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 46:eba2263eb626 88 float deg_arm = asin(height_arm / arm_length) * 180.0/pi; // calculates the absolute degrees which the arm has to reach
raomen 46:eba2263eb626 89 return deg_arm;
raomen 46:eba2263eb626 90 }
raomen 46:eba2263eb626 91 return NULL; // <------ maybe error testing necessary (value deg_arm might not be returned)
raomen 40:e32c57763d92 92 }
raomen 38:c2663f7dcccb 93
raomen 46:eba2263eb626 94 //calculates the deg which the wheels have to turn in order to cover specified distance in mm
raomen 45:8050724fe19b 95 float wheel_dist_to_deg(int distance) // distance has to be in mm.
raomen 45:8050724fe19b 96 {
raomen 45:8050724fe19b 97 float deg_wheel = distance * 360 /(wheel_diameter * pi);
raomen 45:8050724fe19b 98 return deg_wheel;
raomen 45:8050724fe19b 99 }
raomen 45:8050724fe19b 100
raomen 46:eba2263eb626 101 // bring arm in starting position. Height of stairs.
raomen 42:6e7ab1136354 102 int start_position()
raomen 42:6e7ab1136354 103 {
raomen 46:eba2263eb626 104 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 105 float deg = deg_up_from_horizon + start_deg_arm;
raomen 43:7964411b4a6b 106 if ((0.0 > deg) || (deg > 360.0))
raomen 42:6e7ab1136354 107 {
raomen 46:eba2263eb626 108 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 109 }
raomen 45:8050724fe19b 110 positionController_M_Arm.setDesiredRotation(deg / 360.0, max_speed_rps_arm); // command to turn motor to desired deg.
raomen 50:058dc65d0fa4 111 current_deg_arm = positionController_M_Arm.getRotation() * 360.0; //write new position to variable
raomen 42:6e7ab1136354 112 return NULL;
raomen 42:6e7ab1136354 113 }
raomen 42:6e7ab1136354 114
lupomic 33:70ea029a69e8 115 //Drives forward into the next step
raomen 39:025d1bee1397 116 // calculatioin of acctual distance with wheels is needed
raomen 43:7964411b4a6b 117 int drive_straight(float distance)
raomen 40:e32c57763d92 118 {
raomen 46:eba2263eb626 119 float deg_to_turn = wheel_dist_to_deg(distance);
raomen 45:8050724fe19b 120 positionController_M_right.setDesiredRotation(deg_to_turn / 360.0, max_speed_rps_wheel);
raomen 45:8050724fe19b 121 positionController_M_left.setDesiredRotation(deg_to_turn / 360.0, max_speed_rps_wheel);
raomen 45:8050724fe19b 122 return NULL;
lupomic 33:70ea029a69e8 123 }
lupomic 33:70ea029a69e8 124
lupomic 33:70ea029a69e8 125 //only turns the arm until the robot is on the next step
raomen 46:eba2263eb626 126 int lift_up()
raomen 40:e32c57763d92 127 {
raomen 46:eba2263eb626 128 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 129
raomen 46:eba2263eb626 130 positionController_M_Arm.setDesiredRotation(0, max_speed_rps_arm);
raomen 45:8050724fe19b 131 return NULL;
lupomic 33:70ea029a69e8 132 }
raomen 43:7964411b4a6b 133 //***********************************************************************************************************************************************************
raomen 38:c2663f7dcccb 134
raomen 43:7964411b4a6b 135 //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 136 int check_start()
raomen 43:7964411b4a6b 137 {
raomen 43:7964411b4a6b 138
raomen 46:eba2263eb626 139 return NULL;
raomen 43:7964411b4a6b 140 }
raomen 43:7964411b4a6b 141
raomen 43:7964411b4a6b 142 //pow function is here so we dont have to use the math.h library ************* unnecessary math.h is used any way ***************
lupomic 33:70ea029a69e8 143 //it takes 2 arguments the base can be any negative or positive floating point number the power has to be a hos to be an "integer" defined as a double
raomen 40:e32c57763d92 144 double powerx(double base, double pow2)
raomen 40:e32c57763d92 145 {
lupomic 33:70ea029a69e8 146 double result = -1;
lupomic 33:70ea029a69e8 147 double power = pow2;
lupomic 33:70ea029a69e8 148 double basis = base;
lupomic 33:70ea029a69e8 149 result = 1;
lupomic 33:70ea029a69e8 150 //handling negative exponents
raomen 40:e32c57763d92 151 if(power<0)
raomen 40:e32c57763d92 152 {
raomen 40:e32c57763d92 153 for(double i=1; i<=(power*(-1.0)); i++)
raomen 40:e32c57763d92 154 {
lupomic 33:70ea029a69e8 155 result *= basis;
lupomic 33:70ea029a69e8 156 }
lupomic 33:70ea029a69e8 157 result = 1.0/result;
lupomic 33:70ea029a69e8 158 }
lupomic 33:70ea029a69e8 159 //handling positive exponents
raomen 40:e32c57763d92 160 else
raomen 40:e32c57763d92 161 {
raomen 40:e32c57763d92 162 for(double i=1; i<=power; i++)
raomen 40:e32c57763d92 163 {
raomen 40:e32c57763d92 164 result *= basis;
raomen 40:e32c57763d92 165 }
raomen 40:e32c57763d92 166 }
lupomic 33:70ea029a69e8 167 return result;
raomen 40:e32c57763d92 168 }
lupomic 33:70ea029a69e8 169
raomen 40:e32c57763d92 170 double mapping(float adc_value_mV)
raomen 40:e32c57763d92 171 {
lupomic 33:70ea029a69e8 172 double distance = 0.0f; //distance in mm
lupomic 33:70ea029a69e8 173 double infY =360 , supY = 2360; //Window for sensor values
lupomic 33:70ea029a69e8 174 double voltage_mV = adc_value_mV;
raomen 46:eba2263eb626 175 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
raomen 45:8050724fe19b 176 if((voltage_mV > infY) && (voltage_mV < supY))
raomen 40:e32c57763d92 177 {
lupomic 33:70ea029a69e8 178 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;
lupomic 33:70ea029a69e8 179 }
lupomic 33:70ea029a69e8 180 return (distance);
lupomic 33:70ea029a69e8 181 }
lupomic 33:70ea029a69e8 182
raomen 41:4a4978d1a578 183 // while loop gets executed every main_task_period_ms milliseconds
raomen 41:4a4978d1a578 184 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 185 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 186 //***********************************************************************************************************************************************************
raomen 39:025d1bee1397 187
lupomic 33:70ea029a69e8 188 int main(void)
pmic 23:26b3a25fc637 189 {
raomen 46:eba2263eb626 190 // attach button fall and rise functions to user button object
raomen 46:eba2263eb626 191 user_button.fall(&user_button_pressed_fcn);
raomen 46:eba2263eb626 192 user_button.rise(&user_button_released_fcn);
lupomic 33:70ea029a69e8 193
raomen 40:e32c57763d92 194 while (true)
raomen 40:e32c57763d92 195 {
raomen 45:8050724fe19b 196 enable_motors = 1;
raomen 45:8050724fe19b 197 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
lupomic 33:70ea029a69e8 198
raomen 40:e32c57763d92 199 switch (ToNextFunction)
raomen 40:e32c57763d92 200 {
raomen 46:eba2263eb626 201 case 0:
lupomic 33:70ea029a69e8 202 break;
raomen 46:eba2263eb626 203
raomen 45:8050724fe19b 204 case 1:
raomen 45:8050724fe19b 205 start_position();
raomen 46:eba2263eb626 206 printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 207 break;
raomen 46:eba2263eb626 208
raomen 45:8050724fe19b 209 case 2:
raomen 45:8050724fe19b 210 drive_straight(drive_stright_mm);
raomen 45:8050724fe19b 211 printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
raomen 46:eba2263eb626 212 positionController_M_right.getRotation(),positionController_M_left.getRotation());
raomen 46:eba2263eb626 213 break;
raomen 46:eba2263eb626 214
raomen 45:8050724fe19b 215 case 3:
raomen 46:eba2263eb626 216 lift_up();
raomen 46:eba2263eb626 217 printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 218 break;
raomen 46:eba2263eb626 219
raomen 45:8050724fe19b 220 case 4:
raomen 46:eba2263eb626 221 drive_straight(drive_back_mm);
lupomic 34:9f779e91168e 222 printf("Case 4: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
raomen 46:eba2263eb626 223 positionController_M_right.getRotation(),positionController_M_left.getRotation());
raomen 46:eba2263eb626 224 break;
raomen 46:eba2263eb626 225
raomen 45:8050724fe19b 226 case 5:
raomen 46:eba2263eb626 227 lift_up();
raomen 46:eba2263eb626 228 printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 46:eba2263eb626 229 ToNextFunction = 0;
raomen 46:eba2263eb626 230 break;
raomen 46:eba2263eb626 231
raomen 46:eba2263eb626 232 default: ;
lupomic 33:70ea029a69e8 233 }
lupomic 33:70ea029a69e8 234 }
lupomic 33:70ea029a69e8 235 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 236 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 237 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
lupomic 33:70ea029a69e8 238 return 0;
pmic 1:93d997d6b232 239 }
pmic 6:e1fa1a2d7483 240
lupomic 33:70ea029a69e8 241
pmic 24:86f1a63e35a0 242 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 243 {
pmic 26:28693b369945 244 user_button_timer.start();
pmic 6:e1fa1a2d7483 245 user_button_timer.reset();
pmic 6:e1fa1a2d7483 246 }
pmic 6:e1fa1a2d7483 247
raomen 43:7964411b4a6b 248 void user_button_released_fcn()
raomen 43:7964411b4a6b 249 {
pmic 24:86f1a63e35a0 250 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 251 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 252 user_button_timer.stop();
raomen 43:7964411b4a6b 253 if (user_button_elapsed_time_ms > 200)
raomen 43:7964411b4a6b 254 {
raomen 43:7964411b4a6b 255 ToNextFunction += 1;
raomen 43:7964411b4a6b 256 }
raomen 43:7964411b4a6b 257 }