Prototyp V2

Dependencies:   PM2_Libary

Committer:
lupomic
Date:
Wed Apr 27 11:08:49 2022 +0200
Branch:
Lupo_2
Revision:
71:e740ef7c7813
Parent:
70:da5754e1514c
Parent:
68:e3fc5ed0bc0e
Child:
86:56b35f01e4d4
first commit

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