Prototyp V2

Dependencies:   PM2_Libary

Committer:
raomen
Date:
Mon Apr 18 11:45:09 2022 +0200
Branch:
michi
Revision:
42:6e7ab1136354
Parent:
41:4a4978d1a578
Child:
43:7964411b4a6b
implement start_position

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 //*******************************************************************************************************************************************************************
raomen 39:025d1bee1397 9 // Defined Variables in mm coming from Hardware-team. Need to be updated
raomen 41:4a4978d1a578 10 float wheel_diameter = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn (4)
raomen 41:4a4978d1a578 11 float arm_length = 118.5; // lenght of arm from pivotpoint to pivotpoint (3)
raomen 41:4a4978d1a578 12 float dist_arm_attach_distsensor = 20; // distance between pivot point arm on body to start distancesensor on top in horizontal (6)
raomen 41:4a4978d1a578 13 float dist_distsensors = 200; // distance between the two distancesensors on top of Wall-E (9)
raomen 41:4a4978d1a578 14 float dist_arm_ground = 51; // distance between pivotpoint arm and ground (5)
raomen 41:4a4978d1a578 15 float gripper_area_height = 16 ; // Height of Grappler cutout to grapple Stair (8)
raomen 41:4a4978d1a578 16 float dist_grappleratt_grappler_uk = 33; // distance between pivotpoint Grappler and bottom edge (?)
raomen 41:4a4978d1a578 17
raomen 39:025d1bee1397 18 float height_stairs = 100; // height to top of stairs in mm
raomen 39:025d1bee1397 19
raomen 39:025d1bee1397 20 //***********************************************************************************************************************************************************
raomen 41:4a4978d1a578 21 // declaration of Input - Output pins
pmic 17:c19b471f05cb 22
pmic 24:86f1a63e35a0 23 // user button on nucleo board
pmic 24:86f1a63e35a0 24 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 25 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 26 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 27 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 28
pmic 24:86f1a63e35a0 29 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
raomen 38:c2663f7dcccb 30 float ir_distance_mV = 0.0f; // define variable to store measurement from infrared distancesensor in mVolt
pmic 24:86f1a63e35a0 31 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 32
pmic 24:86f1a63e35a0 33 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 24:86f1a63e35a0 34 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 24:86f1a63e35a0 35 float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors
lupomic 33:70ea029a69e8 36 //motor pin declaration
lupomic 34:9f779e91168e 37 FastPWM pwm_M_right(PB_13);
lupomic 34:9f779e91168e 38 FastPWM pwm_M_left(PA_9);
lupomic 33:70ea029a69e8 39 FastPWM pwm_M_arm(PA_10);
pmic 17:c19b471f05cb 40
lupomic 33:70ea029a69e8 41 //Encoder pin declaration
lupomic 33:70ea029a69e8 42 EncoderCounter encoder_M_right(PA_6, PC_7); //encoder pin decalaration for wheels right side
lupomic 33:70ea029a69e8 43 EncoderCounter encoder_M_left(PB_6, PB_7); //encoder pin decalaration for wheels left side
lupomic 33:70ea029a69e8 44 EncoderCounter encoder_M_arm(PA_0, PA_1); //encoder pin decalaration for arm
raomen 41:4a4978d1a578 45 //***********************************************************************************************************************************************************
raomen 41:4a4978d1a578 46 // Hardware controll functions and setup
pmic 17:c19b471f05cb 47
pmic 30:1e8295770bc1 48 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
pmic 24:86f1a63e35a0 49 float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack
raomen 39:025d1bee1397 50 float counts_per_turn_wheels = 20.0f * 78.125f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels
raomen 39:025d1bee1397 51 float counts_per_turn_arm = 20.0f * 78.125f * 10.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm
pmic 25:ea1d6e27c895 52 float kn = 180.0f / 12.0f; // define motor constant in rpm per V
raomen 39:025d1bee1397 53 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°)
pmic 30:1e8295770bc1 54 float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
pmic 6:e1fa1a2d7483 55
lupomic 33:70ea029a69e8 56 //motors for tracks
lupomic 33:70ea029a69e8 57 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 58 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 59 //Arm Motor
lupomic 33:70ea029a69e8 60 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 61
lupomic 33:70ea029a69e8 62 // 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 63 //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 40:e32c57763d92 64 // LSM9DS1 IMU, carefull: not all PES boards have an imu (chip shortage)
raomen 40:e32c57763d92 65 // LSM9DS1 imu(PC_9, PA_8); // create LSM9DS1 comunication object, if you want to be able to use the imu you need to #include "LSM9DS1_i2c.h"
pmic 17:c19b471f05cb 66
raomen 41:4a4978d1a578 67 //***********************************************************************************************************************************************************
raomen 41:4a4978d1a578 68
lupomic 33:70ea029a69e8 69 //Platzhalter Variabeln für die Positionierung
lupomic 34:9f779e91168e 70 float PositionStair = 0.2;
lupomic 34:9f779e91168e 71 float PositionBackOff = -0.5;
raomen 41:4a4978d1a578 72 float degArmStart = 0.0;
lupomic 34:9f779e91168e 73 float degArmLift = -0.5;
raomen 41:4a4978d1a578 74 int ToNextFunction = 0; // current state of the system (which function is beeing executed)
raomen 41:4a4978d1a578 75 float max_speed_rps = 0.5f; // define maximum speed that the position controller is changig the speed, has to be smaller or equal to kn * max_voltage
lupomic 33:70ea029a69e8 76
raomen 42:6e7ab1136354 77 // 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)
raomen 42:6e7ab1136354 78 double calc_arm_deg_for_height(int height_mm)
raomen 40:e32c57763d92 79 {
raomen 42:6e7ab1136354 80 if ((height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height)) > arm_length)
raomen 41:4a4978d1a578 81 {
raomen 41:4a4978d1a578 82 printf("Error in calc_arm_deg_for_height: desireed height is bigger than Wall-E arm lenght."); // error message when desired height is not reachable.
raomen 41:4a4978d1a578 83 }
raomen 42:6e7ab1136354 84 double height_arm = height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height);
raomen 42:6e7ab1136354 85 double deg_arm_rad = asin(height_mm / arm_length); // deg in radians
raomen 41:4a4978d1a578 86 double pi = 2 * acos(0.0); // definiton of pi
raomen 41:4a4978d1a578 87 double deg_arm = deg_arm_rad * 180/pi; // deg in degrees
raomen 41:4a4978d1a578 88 return deg_arm;
raomen 40:e32c57763d92 89 }
raomen 38:c2663f7dcccb 90
raomen 42:6e7ab1136354 91 int start_position()
raomen 42:6e7ab1136354 92 {
raomen 42:6e7ab1136354 93 float deg = calc_arm_deg_for_height(height_stairs);
raomen 42:6e7ab1136354 94 if ((0 > deg) && (deg > 360))
raomen 42:6e7ab1136354 95 {
raomen 42:6e7ab1136354 96 printf("degree is out of bound for Start Position.");
raomen 42:6e7ab1136354 97 }
raomen 42:6e7ab1136354 98 positionController_M_Arm.setDesiredRotation(deg);
raomen 42:6e7ab1136354 99 return NULL;
raomen 42:6e7ab1136354 100 }
raomen 42:6e7ab1136354 101
lupomic 33:70ea029a69e8 102 //Drives forward into the next step
raomen 39:025d1bee1397 103 // calculatioin of acctual distance with wheels is needed
raomen 42:6e7ab1136354 104 int drive(float distance)
raomen 40:e32c57763d92 105 {
raomen 38:c2663f7dcccb 106 positionController_M_right.setDesiredRotation(distance,max_speed_rps);
raomen 38:c2663f7dcccb 107 positionController_M_left.setDesiredRotation(distance,max_speed_rps);
raomen 38:c2663f7dcccb 108 return 0;
lupomic 33:70ea029a69e8 109 }
lupomic 33:70ea029a69e8 110
lupomic 33:70ea029a69e8 111 //only turns the arm until the robot is on the next step
lupomic 33:70ea029a69e8 112 //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
raomen 42:6e7ab1136354 113 int lift_up(float deg)
raomen 40:e32c57763d92 114 {
lupomic 33:70ea029a69e8 115 int8_t i = 0; //prov condition variable
raomen 38:c2663f7dcccb 116 positionController_M_Arm.setDesiredRotation(deg);
lupomic 33:70ea029a69e8 117 return 0;
lupomic 33:70ea029a69e8 118 }
raomen 38:c2663f7dcccb 119
lupomic 33:70ea029a69e8 120 //pow function is here so we dont have to use the math.h library
lupomic 33:70ea029a69e8 121 //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 122 double powerx(double base, double pow2)
raomen 40:e32c57763d92 123 {
lupomic 33:70ea029a69e8 124 double result = -1;
lupomic 33:70ea029a69e8 125 double power = pow2;
lupomic 33:70ea029a69e8 126 double basis = base;
lupomic 33:70ea029a69e8 127 result = 1;
lupomic 33:70ea029a69e8 128 //handling negative exponents
raomen 40:e32c57763d92 129 if(power<0)
raomen 40:e32c57763d92 130 {
raomen 40:e32c57763d92 131 for(double i=1; i<=(power*(-1.0)); i++)
raomen 40:e32c57763d92 132 {
lupomic 33:70ea029a69e8 133 result *= basis;
lupomic 33:70ea029a69e8 134 }
lupomic 33:70ea029a69e8 135 result = 1.0/result;
lupomic 33:70ea029a69e8 136 }
lupomic 33:70ea029a69e8 137 //handling positive exponents
raomen 40:e32c57763d92 138 else
raomen 40:e32c57763d92 139 {
raomen 40:e32c57763d92 140 for(double i=1; i<=power; i++)
raomen 40:e32c57763d92 141 {
raomen 40:e32c57763d92 142 result *= basis;
raomen 40:e32c57763d92 143 }
raomen 40:e32c57763d92 144 }
lupomic 33:70ea029a69e8 145 return result;
raomen 40:e32c57763d92 146 }
lupomic 33:70ea029a69e8 147
raomen 40:e32c57763d92 148 double mapping(float adc_value_mV)
raomen 40:e32c57763d92 149 {
lupomic 33:70ea029a69e8 150 double distance = 0.0f; //distance in mm
lupomic 33:70ea029a69e8 151 double infY =360 , supY = 2360; //Window for sensor values
lupomic 33:70ea029a69e8 152 double voltage_mV = adc_value_mV;
lupomic 33:70ea029a69e8 153 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
raomen 40:e32c57763d92 154 if(voltage_mV > infY && voltage_mV < supY)
raomen 40:e32c57763d92 155 {
lupomic 33:70ea029a69e8 156 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 157 }
lupomic 33:70ea029a69e8 158 return (distance);
lupomic 33:70ea029a69e8 159 }
lupomic 33:70ea029a69e8 160
raomen 41:4a4978d1a578 161 // logical variable main task
raomen 41:4a4978d1a578 162 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
raomen 41:4a4978d1a578 163
raomen 41:4a4978d1a578 164 // while loop gets executed every main_task_period_ms milliseconds
raomen 41:4a4978d1a578 165 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 166 Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms
lupomic 33:70ea029a69e8 167
raomen 39:025d1bee1397 168
raomen 39:025d1bee1397 169
lupomic 33:70ea029a69e8 170 int main(void)
pmic 23:26b3a25fc637 171 {
pmic 24:86f1a63e35a0 172 // attach button fall and rise functions to user button object
lupomic 33:70ea029a69e8 173 user_button.fall(&user_button_pressed_fcn);
lupomic 34:9f779e91168e 174 user_button.rise(&user_button_released_fcn);
lupomic 33:70ea029a69e8 175
pmic 24:86f1a63e35a0 176
pmic 6:e1fa1a2d7483 177
raomen 40:e32c57763d92 178 while (true)
raomen 40:e32c57763d92 179 {
lupomic 34:9f779e91168e 180 enable_motors = 1;
lupomic 33:70ea029a69e8 181 ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f;
lupomic 33:70ea029a69e8 182
raomen 40:e32c57763d92 183 switch (ToNextFunction)
raomen 40:e32c57763d92 184 {
raomen 42:6e7ab1136354 185 case 1: start_position();
lupomic 34:9f779e91168e 186 printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 187 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 188 break;
raomen 42:6e7ab1136354 189 case 2: drive(PositionStair);
lupomic 34:9f779e91168e 190 printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
lupomic 34:9f779e91168e 191 positionController_M_right.getRotation(),positionController_M_left.getRotation());
lupomic 33:70ea029a69e8 192 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 193 break;
raomen 42:6e7ab1136354 194 case 3: lift_up(degArmLift);
lupomic 33:70ea029a69e8 195 // ToNextFunction+=1;
lupomic 34:9f779e91168e 196 printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 197 break;
raomen 42:6e7ab1136354 198 case 4: drive(PositionBackOff);
lupomic 34:9f779e91168e 199 printf("Case 4: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
lupomic 34:9f779e91168e 200 positionController_M_right.getRotation(),positionController_M_left.getRotation());
lupomic 33:70ea029a69e8 201 // ToNextFunction+=1;
lupomic 33:70ea029a69e8 202 break;
raomen 42:6e7ab1136354 203 case 5: lift_up(degArmStart);
lupomic 34:9f779e91168e 204 printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 33:70ea029a69e8 205 // ToNextFunction = 0;
lupomic 33:70ea029a69e8 206 break;
lupomic 34:9f779e91168e 207 default: ;
lupomic 33:70ea029a69e8 208 }
lupomic 33:70ea029a69e8 209 }
lupomic 33:70ea029a69e8 210 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 211 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 212 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
lupomic 33:70ea029a69e8 213 return 0;
pmic 1:93d997d6b232 214 }
pmic 6:e1fa1a2d7483 215
lupomic 33:70ea029a69e8 216
pmic 24:86f1a63e35a0 217 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 218 {
pmic 26:28693b369945 219 user_button_timer.start();
pmic 6:e1fa1a2d7483 220 user_button_timer.reset();
pmic 6:e1fa1a2d7483 221 }
pmic 6:e1fa1a2d7483 222
lupomic 33:70ea029a69e8 223 void user_button_released_fcn() {
pmic 24:86f1a63e35a0 224 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 225 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 226 user_button_timer.stop();
pmic 24:86f1a63e35a0 227 if (user_button_elapsed_time_ms > 200) {
lupomic 33:70ea029a69e8 228 ToNextFunction += 1;}
lupomic 33:70ea029a69e8 229 }