Prototyp V2

Dependencies:   PM2_Libary

Committer:
raomen
Date:
Wed May 04 09:32:09 2022 +0200
Branch:
michi
Revision:
98:73a9b8833e57
Parent:
95:af4821333ddb
Child:
99:799e90067935
input iteration counter drive straight

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 85:fbcc3d8e945a 13 const float dist_arm_attach_OK_griparea = 10.5 ; // Height of Grappler cutout to grapple Stair (8) (maybe add 1mm so gripper is a bit over the plate)
raomen 85:fbcc3d8e945a 14 const float dist_grappleratt_grappler_uk = 36.5; // distance between pivotpoint Grappler and bottom edge (?)
raomen 91:e464d78fce0a 15 const float grip_area_depth = 32.5; // depth of the area that will gripp the stair (7)
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
lupomic 70:da5754e1514c 28 // define variable to store measurement from infrared distancesensor in mm
lupomic 86:56b35f01e4d4 29 float ir_distance_mm_L;
raomen 92:06c871d9a6ad 30 float ir_distance_mm_R;
lupomic 70:da5754e1514c 31
lupomic 70:da5754e1514c 32 AnalogIn ir_analog_in_Distance_L(PC_2);
lupomic 70:da5754e1514c 33 AnalogIn ir_analog_in_Distance_R(PC_3);
lupomic 70:da5754e1514c 34 AnalogIn ir_analog_in_Lookdown_B(PC_5);
lupomic 70:da5754e1514c 35 AnalogIn ir_analog_in_Lookdown_F(PB_1);
lupomic 70:da5754e1514c 36 // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1
pmic 6:e1fa1a2d7483 37
pmic 24:86f1a63e35a0 38 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB
pmic 24:86f1a63e35a0 39 DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors
pmic 24:86f1a63e35a0 40 float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors
raomen 48:0ab6b1fd455f 41
lupomic 33:70ea029a69e8 42 //motor pin declaration
raomen 46:eba2263eb626 43 FastPWM pwm_M_right (PB_13); //motor pin decalaration for wheels right side
raomen 46:eba2263eb626 44 FastPWM pwm_M_left (PA_9); //motor pin decalaration for wheels left side
raomen 46:eba2263eb626 45 FastPWM pwm_M_arm (PA_10); //motor pin decalaration for arm
pmic 17:c19b471f05cb 46
lupomic 33:70ea029a69e8 47 //Encoder pin declaration
raomen 46:eba2263eb626 48 EncoderCounter encoder_M_right (PA_6, PC_7); //encoder pin decalaration for wheels right side
raomen 46:eba2263eb626 49 EncoderCounter encoder_M_left (PB_6, PB_7); //encoder pin decalaration for wheels left side
raomen 46:eba2263eb626 50 EncoderCounter encoder_M_arm (PA_0, PA_1); //encoder pin decalaration for arm
raomen 41:4a4978d1a578 51 //***********************************************************************************************************************************************************
raomen 43:7964411b4a6b 52 // Hardware controll Setup and functions (motors and sensors)
pmic 17:c19b471f05cb 53
pmic 30:1e8295770bc1 54 // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box
raomen 55:8cb262e56efb 55 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 56 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 57 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 58 const float kn = 180.0f / 12.0f; // define motor constant in rpm per V
raomen 55:8cb262e56efb 59 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 60 const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
raomen 46:eba2263eb626 61
lupomic 33:70ea029a69e8 62 //motors for tracks
lupomic 33:70ea029a69e8 63 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 64 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 65 //Arm Motor
lupomic 33:70ea029a69e8 66 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 67
lupomic 33:70ea029a69e8 68 // 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 69 //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 70 //***********************************************************************************************************************************************************
raomen 80:4eae727a13b5 71 // calculations for basic movment and controll
raomen 41:4a4978d1a578 72
raomen 50:058dc65d0fa4 73 //placeholder variables for prototype testing
raomen 84:40d64e0bb1ea 74 const int drive_straight_mm = 200; // placeholder for testing drives amount forward
raomen 84:40d64e0bb1ea 75 const int drive_back_mm = -200; // placeholder for testing drives amount backwards
raomen 65:1ee1f319a199 76 int ToNextFunction = 0; // current state of the system (which function is beeing executed)
raomen 93:4661e91fdd50 77 int state = 0;
raomen 98:73a9b8833e57 78 int drive_straight_iteration = 0; // iteration of drive straight function to controll flow
lupomic 71:e740ef7c7813 79
raomen 81:909670edc2a2 80 // definition variables for calculations
raomen 55:8cb262e56efb 81 const float pi = 2 * acos(0.0); // definiton of pi
raomen 98:73a9b8833e57 82 const float end_pos_lift_deg = 180 + asin((dist_arm_ground-(dist_grappleratt_grappler_uk - dist_arm_attach_OK_griparea))/arm_length) * 180 / pi; // calculates the degree which the arm has to have when lift_up has been executed.
raomen 81:909670edc2a2 83 const 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)
lupomic 33:70ea029a69e8 84
raomen 81:909670edc2a2 85 // definition of rotation speeds for motors 0 = none 1.0 = max.
raomen 66:b4e55e1eebfc 86 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 87 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
lupomic 36:6116ce98080d 88
raomen 94:7cf17f2eaa28 89
raomen 46:eba2263eb626 90 // 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 91 // PARAM: height_mm = height which OK Gripperarea has to reach.
raomen 74:d7569d530f6c 92 // RETURN: deg_arm = absolut Position in deg that the arm has to take.
raomen 65:1ee1f319a199 93 float calc_arm_deg_for_height(int height_mm)
raomen 40:e32c57763d92 94 {
raomen 94:7cf17f2eaa28 95 float height_arm = height_mm - (dist_arm_ground - dist_arm_attach_OK_griparea + dist_grappleratt_grappler_uk); // calculates the height which only the arm has to cover (- attachement height (arm to robot) etc.)
raomen 81:909670edc2a2 96 float deg_arm = asin(height_arm / arm_length) * 180.0/pi; // calculates the absolute degrees which the arm has to reach
raomen 51:7d165baaa646 97 return deg_arm;
raomen 40:e32c57763d92 98 }
raomen 38:c2663f7dcccb 99
raomen 46:eba2263eb626 100 //calculates the deg which the wheels have to turn in order to cover specified distance in mm
raomen 85:fbcc3d8e945a 101 //PARAM: distance = distance to drive in milimeter
raomen 60:b2e9958f2298 102 //RETURN: deg_wheel = degree which the motor has to turn in order to cover distance(mm)
raomen 85:fbcc3d8e945a 103 float wheel_dist_to_deg(int distance)
raomen 45:8050724fe19b 104 {
raomen 85:fbcc3d8e945a 105 float deg_wheel = distance / (wheel_diameter * pi) * 360;
raomen 45:8050724fe19b 106 return deg_wheel;
raomen 45:8050724fe19b 107 }
raomen 45:8050724fe19b 108
raomen 58:3cd93949a7d7 109 // increments the Motor for defined degree from the current one
raomen 58:3cd93949a7d7 110 // PARAM: deg_to_turn = degree to turn the Motor
raomen 80:4eae727a13b5 111 // PARAM: current_rotation = the current rotation of the Motor (Motor.getRotation())
raomen 80:4eae727a13b5 112 // RETURN: new_turn_rotation = new Rotation value in rotations
raomen 80:4eae727a13b5 113 float turn_relative_deg(float deg_to_turn, float current_rotation)
raomen 57:79732e5818d7 114 {
raomen 80:4eae727a13b5 115 float new_turn_rotation = current_rotation + deg_to_turn/360.0;
raomen 57:79732e5818d7 116 return new_turn_rotation;
raomen 57:79732e5818d7 117 }
raomen 57:79732e5818d7 118
raomen 80:4eae727a13b5 119 // sets the Motor to a specified degree in one rotation
raomen 80:4eae727a13b5 120 // PARAM: end_deg = new position of the arm in degree 0 <= value >=360
raomen 80:4eae727a13b5 121 // PARAM: current_rotation = the current rotation of the Motor (Motor.getRotation())
raomen 80:4eae727a13b5 122 // RETURN: new_partial_rotation = new deg value in rotations
raomen 80:4eae727a13b5 123 float turn_absolut_deg(float end_deg, float current_rotations)
raomen 80:4eae727a13b5 124 {
raomen 80:4eae727a13b5 125 int full_rotations;
raomen 80:4eae727a13b5 126 if(current_rotations > 0)
raomen 80:4eae727a13b5 127 {
raomen 80:4eae727a13b5 128 full_rotations = round(current_rotations - 0.5);
raomen 80:4eae727a13b5 129 }
raomen 80:4eae727a13b5 130 else if(current_rotations < 0)
raomen 80:4eae727a13b5 131 {
raomen 80:4eae727a13b5 132 full_rotations = round(current_rotations + 0.5);
raomen 80:4eae727a13b5 133 }
raomen 80:4eae727a13b5 134 else
raomen 80:4eae727a13b5 135 {
raomen 80:4eae727a13b5 136 full_rotations = 0;
raomen 80:4eae727a13b5 137 }
raomen 80:4eae727a13b5 138 float new_partial_rotation = full_rotations - start_deg_arm/360 + end_deg/360;
raomen 80:4eae727a13b5 139 return new_partial_rotation;
raomen 80:4eae727a13b5 140 }
raomen 93:4661e91fdd50 141
raomen 67:3debc9a3cca5 142 //calculates position of arm when lift up has ended.
raomen 67:3debc9a3cca5 143 //RETURN: end_deg = degree which the motor has to turn in order to reach end lift position.
raomen 66:b4e55e1eebfc 144 float calc_pos_end_lift()
raomen 66:b4e55e1eebfc 145 {
raomen 66:b4e55e1eebfc 146 float end_deg;
raomen 93:4661e91fdd50 147 end_deg = asin((dist_arm_ground-(dist_grappleratt_grappler_uk - dist_arm_attach_OK_griparea))/arm_length) + start_deg_arm;
raomen 68:e3fc5ed0bc0e 148 end_deg = end_deg * 180 / pi;
raomen 66:b4e55e1eebfc 149 return end_deg;
raomen 66:b4e55e1eebfc 150 }
raomen 66:b4e55e1eebfc 151
raomen 80:4eae727a13b5 152 //***********************************************************************************************************************************************************
raomen 83:8cf96ccfac98 153 // important calculatet constant for Wall-E
raomen 81:909670edc2a2 154 const double deg_up_from_horizon_to_stair = calc_arm_deg_for_height(height_stairs);
raomen 45:8050724fe19b 155
raomen 80:4eae727a13b5 156 // import functions from file mapping
raomen 80:4eae727a13b5 157 extern double powerx(double base, double pow2);
raomen 80:4eae727a13b5 158 extern double mapping (float adc_value_mV);
raomen 75:3831b90a4ae0 159
raomen 75:3831b90a4ae0 160 //simple check if there is an object in proximity
raomen 75:3831b90a4ae0 161 //returns 0 if there is NO object present
raomen 75:3831b90a4ae0 162 //returns 1 if there is an object present
raomen 75:3831b90a4ae0 163 //returns 2 if the distance isn't in the expected range
raomen 98:73a9b8833e57 164 uint8_t StepDetection(double distance){
raomen 98:73a9b8833e57 165 double d_valueMM = distance;
raomen 98:73a9b8833e57 166 if(d_valueMM >= 4) return 0;
raomen 98:73a9b8833e57 167 if(d_valueMM < 4) return 1;
raomen 98:73a9b8833e57 168 if(d_valueMM <= 0 || d_valueMM > 100 ) return 2;
raomen 98:73a9b8833e57 169 else return 2;
raomen 98:73a9b8833e57 170
raomen 98:73a9b8833e57 171 }
raomen 98:73a9b8833e57 172
raomen 98:73a9b8833e57 173 //simple check if there is an object in proximity
raomen 98:73a9b8833e57 174 //returns 0 if there is NO object present
raomen 98:73a9b8833e57 175 //returns 1 if there is an object present
raomen 98:73a9b8833e57 176 //returns 2 if the distance isn't in the expected range
raomen 92:06c871d9a6ad 177 uint8_t StepDetection_down(float sensor)
raomen 57:79732e5818d7 178 {
raomen 92:06c871d9a6ad 179 double d_valueMM = mapping(sensor * 1.0e3f * 3.3f);
raomen 79:368cbf09cf6a 180 if(d_valueMM >= 4) return 0;
raomen 79:368cbf09cf6a 181 else if(d_valueMM < 4) return 1;
raomen 79:368cbf09cf6a 182 else if(d_valueMM <= 0 || d_valueMM > 100 ) return 2;
raomen 79:368cbf09cf6a 183 else return 2;
raomen 58:3cd93949a7d7 184 }
raomen 58:3cd93949a7d7 185
raomen 46:eba2263eb626 186 // bring arm in starting position. Height of stairs.
lupomic 71:e740ef7c7813 187 int set_arm_stair_height()
raomen 42:6e7ab1136354 188 {
raomen 81:909670edc2a2 189 float deg = deg_up_from_horizon_to_stair + start_deg_arm;
lupomic 70:da5754e1514c 190 float diff;
raomen 46:eba2263eb626 191 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 66:b4e55e1eebfc 192
raomen 45:8050724fe19b 193 positionController_M_Arm.setDesiredRotation(deg / 360.0, max_speed_rps_arm); // command to turn motor to desired deg.
lupomic 71:e740ef7c7813 194 diff = deg-(positionController_M_Arm.getRotation() * 360.0);
raomen 94:7cf17f2eaa28 195 if (diff <= 0.3){
raomen 98:73a9b8833e57 196 drive_straight_iteration = 0;
lupomic 70:da5754e1514c 197 return 1;
lupomic 70:da5754e1514c 198 }
lupomic 70:da5754e1514c 199 else {
raomen 94:7cf17f2eaa28 200 return 0;}
raomen 42:6e7ab1136354 201 }
raomen 42:6e7ab1136354 202
raomen 55:8cb262e56efb 203 //Drives forward into the next step
raomen 55:8cb262e56efb 204 //Prameter:distance in milimeter
raomen 43:7964411b4a6b 205 int drive_straight(float distance)
raomen 40:e32c57763d92 206 {
lupomic 70:da5754e1514c 207 float diff_R;
lupomic 70:da5754e1514c 208 float diff_L;
raomen 46:eba2263eb626 209 float deg_to_turn = wheel_dist_to_deg(distance);
raomen 98:73a9b8833e57 210 float relativ_turns_rightmotor;
raomen 98:73a9b8833e57 211 float relativ_turns_leftmotor;
raomen 60:b2e9958f2298 212
raomen 98:73a9b8833e57 213 if (drive_straight_iteration == 0)
raomen 98:73a9b8833e57 214 {
raomen 98:73a9b8833e57 215 relativ_turns_rightmotor = turn_relative_deg(deg_to_turn, positionController_M_right.getRotation());
raomen 98:73a9b8833e57 216 relativ_turns_leftmotor = turn_relative_deg(deg_to_turn, positionController_M_left.getRotation());
raomen 98:73a9b8833e57 217 }
raomen 66:b4e55e1eebfc 218
raomen 60:b2e9958f2298 219 positionController_M_right.setDesiredRotation(relativ_turns_rightmotor, max_speed_rps_wheel);
raomen 64:72b9efe62ece 220 positionController_M_left.setDesiredRotation(relativ_turns_leftmotor, max_speed_rps_wheel);
lupomic 71:e740ef7c7813 221
raomen 98:73a9b8833e57 222 drive_straight_iteration = 1;
raomen 98:73a9b8833e57 223
raomen 94:7cf17f2eaa28 224 diff_R = relativ_turns_rightmotor-positionController_M_right.getRotation();
raomen 94:7cf17f2eaa28 225 diff_L = relativ_turns_leftmotor-positionController_M_left.getRotation();
raomen 94:7cf17f2eaa28 226 if ((diff_R <= 0.3) && (diff_L <= 0.3))
lupomic 71:e740ef7c7813 227 {
lupomic 70:da5754e1514c 228 return 1;
lupomic 70:da5754e1514c 229 }
lupomic 71:e740ef7c7813 230 else
lupomic 71:e740ef7c7813 231 {
lupomic 70:da5754e1514c 232 return 0;
lupomic 70:da5754e1514c 233 }
raomen 93:4661e91fdd50 234
lupomic 33:70ea029a69e8 235 }
lupomic 33:70ea029a69e8 236
raomen 74:d7569d530f6c 237 //turns the arm until the robot is on the next step
raomen 94:7cf17f2eaa28 238 int lift_up()
raomen 40:e32c57763d92 239 {
lupomic 70:da5754e1514c 240 float diff;
raomen 81:909670edc2a2 241 float absolut_pos_arm = turn_absolut_deg(end_pos_lift_deg, positionController_M_Arm.getRotation()-1);
raomen 98:73a9b8833e57 242 float position_lift_end_deg = asin((-dist_arm_ground - (dist_grappleratt_grappler_uk - dist_arm_attach_OK_griparea)) / arm_length) - 90; // calculates the degree which has to be reached in order to get on top of next step
lupomic 71:e740ef7c7813 243 float relativ_turns_arm = turn_absolut_deg(position_lift_end_deg, positionController_M_Arm.getRotation());
raomen 94:7cf17f2eaa28 244
lupomic 71:e740ef7c7813 245 positionController_M_Arm.setDesiredRotation(relativ_turns_arm , max_speed_rps_arm);
raomen 93:4661e91fdd50 246
raomen 94:7cf17f2eaa28 247 diff = relativ_turns_arm - positionController_M_Arm.getRotation();
raomen 94:7cf17f2eaa28 248 if (diff <= 0.01)
raomen 94:7cf17f2eaa28 249 {
raomen 98:73a9b8833e57 250 drive_straight_iteration = 0;
raomen 94:7cf17f2eaa28 251 return 1;
raomen 94:7cf17f2eaa28 252 }
raomen 94:7cf17f2eaa28 253 else
raomen 94:7cf17f2eaa28 254 {
raomen 94:7cf17f2eaa28 255 return 0;
raomen 94:7cf17f2eaa28 256 }
lupomic 33:70ea029a69e8 257 }
raomen 90:2c0468f658ac 258
raomen 92:06c871d9a6ad 259 //function to climbe one step up and get in to position to drive in to the next step
raomen 92:06c871d9a6ad 260 //PARAM: None
raomen 92:06c871d9a6ad 261 //RETURN: None
raomen 94:7cf17f2eaa28 262 void lift_sequenze()
raomen 90:2c0468f658ac 263 {
raomen 92:06c871d9a6ad 264 lift_up();
raomen 92:06c871d9a6ad 265 if ((StepDetection_down(ir_analog_in_Lookdown_B) == 1) && (StepDetection_down(ir_analog_in_Lookdown_F) == 1))
raomen 92:06c871d9a6ad 266 {
raomen 92:06c871d9a6ad 267 thread_sleep_for(500);
raomen 92:06c871d9a6ad 268 drive_straight(-grip_area_depth - 2);
raomen 92:06c871d9a6ad 269 }
raomen 92:06c871d9a6ad 270 set_arm_stair_height();
raomen 93:4661e91fdd50 271
raomen 93:4661e91fdd50 272 positionController_M_Arm.setDesiredRotation(0, max_speed_rps_arm);
lupomic 33:70ea029a69e8 273 }
raomen 43:7964411b4a6b 274 //***********************************************************************************************************************************************************
raomen 94:7cf17f2eaa28 275 //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 94:7cf17f2eaa28 276 void check_start();
raomen 38:c2663f7dcccb 277
raomen 94:7cf17f2eaa28 278 int NextStep(float);
raomen 41:4a4978d1a578 279 // while loop gets executed every main_task_period_ms milliseconds
raomen 41:4a4978d1a578 280 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 281 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 282 //***********************************************************************************************************************************************************
raomen 39:025d1bee1397 283
raomen 98:73a9b8833e57 284
raomen 98:73a9b8833e57 285
lupomic 33:70ea029a69e8 286 int main(void)
pmic 23:26b3a25fc637 287 {
raomen 46:eba2263eb626 288 // attach button fall and rise functions to user button object
raomen 46:eba2263eb626 289 user_button.fall(&user_button_pressed_fcn);
lupomic 86:56b35f01e4d4 290 user_button.rise(&user_button_released_fcn);
lupomic 70:da5754e1514c 291
lupomic 34:9f779e91168e 292
raomen 40:e32c57763d92 293 while (true)
raomen 40:e32c57763d92 294 {
raomen 93:4661e91fdd50 295
lupomic 86:56b35f01e4d4 296 ir_distance_mm_L= mapping(ir_analog_in_Distance_L.read()*1.0e3f * 3.3f);
lupomic 86:56b35f01e4d4 297 ir_distance_mm_R= mapping(ir_analog_in_Distance_R.read()*1.0e3f * 3.3f);
raomen 93:4661e91fdd50 298
raomen 94:7cf17f2eaa28 299 if (ToNextFunction >= 1)
raomen 94:7cf17f2eaa28 300 {
raomen 94:7cf17f2eaa28 301 enable_motors =1;
raomen 94:7cf17f2eaa28 302 }
lupomic 71:e740ef7c7813 303
raomen 40:e32c57763d92 304 switch (ToNextFunction)
raomen 40:e32c57763d92 305 {
raomen 46:eba2263eb626 306
raomen 94:7cf17f2eaa28 307 case 1:
raomen 94:7cf17f2eaa28 308 state = set_arm_stair_height();
raomen 46:eba2263eb626 309 printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 70:da5754e1514c 310 if (state==1){
lupomic 70:da5754e1514c 311 ToNextFunction += 1;
lupomic 70:da5754e1514c 312 }
raomen 46:eba2263eb626 313 break;
raomen 46:eba2263eb626 314
raomen 94:7cf17f2eaa28 315 case 2:
raomen 94:7cf17f2eaa28 316 state = NextStep(ir_analog_in_Distance_L);
raomen 94:7cf17f2eaa28 317 if (state==1){
raomen 94:7cf17f2eaa28 318 ToNextFunction += 1;
raomen 94:7cf17f2eaa28 319 }
raomen 94:7cf17f2eaa28 320
raomen 94:7cf17f2eaa28 321 case 3:
raomen 94:7cf17f2eaa28 322 state = drive_straight(drive_straight_mm);
raomen 93:4661e91fdd50 323 printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
raomen 93:4661e91fdd50 324 positionController_M_right.getRotation(),positionController_M_left.getRotation());
raomen 94:7cf17f2eaa28 325 if (state==1){
raomen 94:7cf17f2eaa28 326 ToNextFunction += 1;
raomen 94:7cf17f2eaa28 327 }
raomen 46:eba2263eb626 328 break;
raomen 46:eba2263eb626 329
raomen 45:8050724fe19b 330 case 4:
raomen 94:7cf17f2eaa28 331 state = lift_up();
raomen 94:7cf17f2eaa28 332 printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 95:af4821333ddb 333 if (((state==1) && (StepDetection_down(ir_analog_in_Lookdown_B))) && (StepDetection_down(ir_analog_in_Lookdown_F)))
raomen 94:7cf17f2eaa28 334 {
raomen 94:7cf17f2eaa28 335 ToNextFunction += 1;
raomen 94:7cf17f2eaa28 336 }
raomen 46:eba2263eb626 337 break;
raomen 46:eba2263eb626 338
raomen 45:8050724fe19b 339 case 5:
raomen 94:7cf17f2eaa28 340 state = drive_straight(drive_back_mm);
raomen 95:af4821333ddb 341 if ((state == 1) && (StepDetection_down(ir_analog_in_Lookdown_B) != 1))
raomen 94:7cf17f2eaa28 342 {
raomen 94:7cf17f2eaa28 343 ToNextFunction += 1;
raomen 94:7cf17f2eaa28 344 }
raomen 94:7cf17f2eaa28 345 break;
raomen 94:7cf17f2eaa28 346
raomen 94:7cf17f2eaa28 347 case 6:
raomen 94:7cf17f2eaa28 348 state=lift_up();
raomen 94:7cf17f2eaa28 349 printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
raomen 94:7cf17f2eaa28 350 if (state==1)
raomen 94:7cf17f2eaa28 351 {
raomen 94:7cf17f2eaa28 352 ToNextFunction = 1;
lupomic 70:da5754e1514c 353 }
raomen 46:eba2263eb626 354 break;
raomen 46:eba2263eb626 355
raomen 93:4661e91fdd50 356
raomen 93:4661e91fdd50 357 default: ;
lupomic 33:70ea029a69e8 358 }
lupomic 33:70ea029a69e8 359 }
raomen 80:4eae727a13b5 360 // read timer and make the main thread sleep for the remaining time span (non blocking)
raomen 80:4eae727a13b5 361 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
raomen 80:4eae727a13b5 362 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
raomen 80:4eae727a13b5 363 return 0;
pmic 1:93d997d6b232 364 }
pmic 6:e1fa1a2d7483 365
lupomic 33:70ea029a69e8 366
lupomic 71:e740ef7c7813 367
pmic 24:86f1a63e35a0 368 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 369 {
pmic 26:28693b369945 370 user_button_timer.start();
pmic 6:e1fa1a2d7483 371 user_button_timer.reset();
pmic 6:e1fa1a2d7483 372 }
pmic 6:e1fa1a2d7483 373
raomen 43:7964411b4a6b 374 void user_button_released_fcn()
raomen 43:7964411b4a6b 375 {
pmic 24:86f1a63e35a0 376 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 377 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 378 user_button_timer.stop();
raomen 43:7964411b4a6b 379 if (user_button_elapsed_time_ms > 200)
raomen 43:7964411b4a6b 380 {
lupomic 86:56b35f01e4d4 381 ToNextFunction = 3;
raomen 43:7964411b4a6b 382 }
raomen 43:7964411b4a6b 383 }