first commit

Dependencies:   PM2_Libary

Committer:
lupomic
Date:
Mon May 02 14:35:00 2022 +0200
Branch:
lupo
Revision:
37:05252c4a2d4e
Parent:
36:a48b21a9635c
Child:
38:8121e7a79c0b
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>
lupomic 37:05252c4a2d4e 4 #include <cstdio>
lupomic 37:05252c4a2d4e 5 #include "math.h"
lupomic 37:05252c4a2d4e 6 //*******************************************************************************************************************************************************************
lupomic 37:05252c4a2d4e 7 // Defined Variables in mm coming from Hardware-team. Need to be updated
lupomic 37:05252c4a2d4e 8 const float wheel_diameter = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn (4)
lupomic 37:05252c4a2d4e 9 const float arm_length = 118.5; // lenght of arm from pivotpoint to pivotpoint (3)
lupomic 37:05252c4a2d4e 10 const float dist_arm_attach_distsensor = 20; // distance between pivot point arm on body to start distancesensor on top in horizontal (6)
lupomic 37:05252c4a2d4e 11 const float dist_distsensors = 200; // distance between the two distancesensors on top of Wall-E (9)
lupomic 37:05252c4a2d4e 12 const float dist_arm_ground = 51; // distance between pivotpoint arm and ground (5)
lupomic 37:05252c4a2d4e 13 const float gripper_area_height = 16 ; // Height of Grappler cutout to grapple Stair (8)
lupomic 37:05252c4a2d4e 14 const float dist_grappleratt_grappler_uk = 33; // distance between pivotpoint Grappler and bottom edge (?)
lupomic 35:96ed18b1af94 15
lupomic 37:05252c4a2d4e 16 const float height_stairs = 100; // height to top of next stairstep in mm
lupomic 37:05252c4a2d4e 17 //***********************************************************************************************************************************************************
lupomic 37:05252c4a2d4e 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
lupomic 37:05252c4a2d4e 26 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
lupomic 37:05252c4a2d4e 27 // define variable to store measurement from infrared distancesensor in mm
lupomic 37:05252c4a2d4e 28 float ir_distance_mm_L;
lupomic 37:05252c4a2d4e 29 float ir_distance_mm_R;
lupomic 37:05252c4a2d4e 30 float ir_distance_mm_Lookdown_B;
lupomic 37:05252c4a2d4e 31 float ir_distance_mm_Lookdown_F;
pmic 6:e1fa1a2d7483 32
lupomic 37:05252c4a2d4e 33 AnalogIn ir_analog_in_Distance_L(PC_2);
lupomic 37:05252c4a2d4e 34 AnalogIn ir_analog_in_Distance_R(PC_3);
lupomic 37:05252c4a2d4e 35 AnalogIn ir_analog_in_Lookdown_B(PC_5);
lupomic 37:05252c4a2d4e 36 AnalogIn ir_analog_in_Lookdown_F(PB_1);
lupomic 37:05252c4a2d4e 37 // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1
lupomic 33:70ea029a69e8 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
lupomic 37:05252c4a2d4e 41 float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors
pmic 17:c19b471f05cb 42
lupomic 33:70ea029a69e8 43 //motor pin declaration
lupomic 37:05252c4a2d4e 44 FastPWM pwm_M_right (PB_13); //motor pin decalaration for wheels right side
lupomic 37:05252c4a2d4e 45 FastPWM pwm_M_left (PA_9); //motor pin decalaration for wheels left side
lupomic 37:05252c4a2d4e 46 FastPWM pwm_M_arm (PA_10); //motor pin decalaration for arm
pmic 17:c19b471f05cb 47
lupomic 33:70ea029a69e8 48 //Encoder pin declaration
lupomic 37:05252c4a2d4e 49 EncoderCounter encoder_M_right (PA_6, PC_7); //encoder pin decalaration for wheels right side
lupomic 37:05252c4a2d4e 50 EncoderCounter encoder_M_left (PB_6, PB_7); //encoder pin decalaration for wheels left side
lupomic 37:05252c4a2d4e 51 EncoderCounter encoder_M_arm (PA_0, PA_1); //encoder pin decalaration for arm
lupomic 37:05252c4a2d4e 52 //***********************************************************************************************************************************************************
lupomic 37:05252c4a2d4e 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
lupomic 37:05252c4a2d4e 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
lupomic 37:05252c4a2d4e 57 const float counts_per_turn_wheels = 20.0f * 78.125f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels
lupomic 37:05252c4a2d4e 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
lupomic 37:05252c4a2d4e 59 const float kn = 180.0f / 12.0f; // define motor constant in rpm per V
lupomic 37:05252c4a2d4e 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°)
lupomic 37:05252c4a2d4e 61 const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1
pmic 6:e1fa1a2d7483 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
lupomic 37:05252c4a2d4e 71 //***********************************************************************************************************************************************************
lupomic 37:05252c4a2d4e 72 // logic functions for basic movement
pmic 17:c19b471f05cb 73
lupomic 37:05252c4a2d4e 74 //placeholder variables for prototype testing
pmic 20:7e7325edcf5c 75
lupomic 37:05252c4a2d4e 76 const int drive_stright_mm = 500; // placeholder for testing drives amount forward
lupomic 37:05252c4a2d4e 77 const int drive_back_mm = -100; // placeholder for testing drives amount backwards
lupomic 37:05252c4a2d4e 78 int ToNextFunction = 0; // current state of the system (which function is beeing executed)
lupomic 37:05252c4a2d4e 79 int state=0;
lupomic 33:70ea029a69e8 80
lupomic 36:a48b21a9635c 81
lupomic 37:05252c4a2d4e 82 // definition important variables
lupomic 37:05252c4a2d4e 83 const float pi = 2 * acos(0.0); // definiton of pi
lupomic 37:05252c4a2d4e 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
lupomic 37:05252c4a2d4e 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 37:05252c4a2d4e 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)
lupomic 37:05252c4a2d4e 87 // import functions from file mapping
lupomic 37:05252c4a2d4e 88 extern double powerx(double base, double pow2);
lupomic 37:05252c4a2d4e 89 extern double mapping (float adc_value_mV);
lupomic 33:70ea029a69e8 90
lupomic 37:05252c4a2d4e 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)
lupomic 37:05252c4a2d4e 92 float calc_arm_deg_for_height(int height_mm)
lupomic 37:05252c4a2d4e 93 {
lupomic 37:05252c4a2d4e 94 float deg_arm;
lupomic 37:05252c4a2d4e 95 if ((height_mm - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height)) > arm_length) //check if height is reachable
lupomic 37:05252c4a2d4e 96 {
lupomic 37:05252c4a2d4e 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.
lupomic 37:05252c4a2d4e 98 }
lupomic 37:05252c4a2d4e 99 else
lupomic 37:05252c4a2d4e 100 {
lupomic 37:05252c4a2d4e 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.)
lupomic 37:05252c4a2d4e 102 deg_arm = asin(height_arm / arm_length) * 180.0/pi; // calculates the absolute degrees which the arm has to reach
lupomic 37:05252c4a2d4e 103 }
lupomic 37:05252c4a2d4e 104 return deg_arm;
lupomic 37:05252c4a2d4e 105 }
lupomic 33:70ea029a69e8 106
lupomic 37:05252c4a2d4e 107 //calculates position of arm when lift up has ended.
lupomic 37:05252c4a2d4e 108 //RETURN: end_deg = degree which the motor has to turn in order to reach end lift position.
lupomic 37:05252c4a2d4e 109 float calc_pos_end_lift()
lupomic 37:05252c4a2d4e 110 {
lupomic 37:05252c4a2d4e 111 float end_deg;
lupomic 37:05252c4a2d4e 112 end_deg = asin((dist_arm_ground-(dist_grappleratt_grappler_uk-gripper_area_height))/arm_length) + start_deg_arm;
lupomic 37:05252c4a2d4e 113 end_deg = end_deg * 180 / pi;
lupomic 37:05252c4a2d4e 114 return end_deg;
lupomic 37:05252c4a2d4e 115 }
lupomic 37:05252c4a2d4e 116
lupomic 37:05252c4a2d4e 117 //calculates the deg which the wheels have to turn in order to cover specified distance in mm
lupomic 37:05252c4a2d4e 118 //RETURN: deg_wheel = degree which the motor has to turn in order to cover distance(mm)
lupomic 37:05252c4a2d4e 119 float wheel_dist_to_deg(int distance) // distance has to be in mm.
lupomic 37:05252c4a2d4e 120 {
lupomic 37:05252c4a2d4e 121 float deg_wheel = distance * 360 /(wheel_diameter * pi);
lupomic 37:05252c4a2d4e 122 return deg_wheel;
lupomic 33:70ea029a69e8 123 }
lupomic 37:05252c4a2d4e 124
lupomic 37:05252c4a2d4e 125 // increments the Motor for defined degree from the current one
lupomic 37:05252c4a2d4e 126 // PARAM: deg_to_turn = degree to turn the Motor
lupomic 37:05252c4a2d4e 127 // PARAM: current_full_rotation = the current rotation of the Motor (Motor.getRotation())
lupomic 37:05252c4a2d4e 128 // RETURN: new Rotation value in rotations
lupomic 37:05252c4a2d4e 129 float turn_relative_deg(float deg_to_turn, float current_full_rotation)
lupomic 37:05252c4a2d4e 130 {
lupomic 37:05252c4a2d4e 131 float current_rotations = current_full_rotation;
lupomic 37:05252c4a2d4e 132 float new_turn_rotation = current_rotations + deg_to_turn/360.0;
lupomic 37:05252c4a2d4e 133 return new_turn_rotation;
lupomic 37:05252c4a2d4e 134 }
lupomic 34:9f779e91168e 135
lupomic 37:05252c4a2d4e 136 // sets the Motor to a specified degree in one rotation
lupomic 37:05252c4a2d4e 137 // PARAM: end_deg = new position of the arm in degree 0 <= value >=360
lupomic 37:05252c4a2d4e 138 // PARAM: current_rotations = the current rotation of the Motor (Motor.getRotation())
lupomic 37:05252c4a2d4e 139 // RETURN: new_partial_rotation = new deg value in rotations
lupomic 37:05252c4a2d4e 140 float turn_absolut_deg(float end_deg, float current_rotations)
lupomic 37:05252c4a2d4e 141 {
lupomic 37:05252c4a2d4e 142 int full_rotations = current_rotations;
lupomic 37:05252c4a2d4e 143 float new_partial_rotation = current_rotations - start_deg_arm/360;
lupomic 37:05252c4a2d4e 144 return new_partial_rotation;
lupomic 37:05252c4a2d4e 145 }
lupomic 33:70ea029a69e8 146
lupomic 37:05252c4a2d4e 147 // bring arm in starting position. Height of stairs.
lupomic 37:05252c4a2d4e 148 int set_arm_stair_height()
lupomic 37:05252c4a2d4e 149 {
lupomic 37:05252c4a2d4e 150 float diff;
lupomic 37:05252c4a2d4e 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
lupomic 37:05252c4a2d4e 152 float deg = deg_up_from_horizon + start_deg_arm;
lupomic 37:05252c4a2d4e 153 if ((0.0 > deg) || (deg > 360.0))
lupomic 37:05252c4a2d4e 154 {
lupomic 37:05252c4a2d4e 155 printf("Error in start_position: degree is out of bound for Start Position."); // error when desired reaching point is out of reach.
lupomic 37:05252c4a2d4e 156 return 2;
lupomic 37:05252c4a2d4e 157 }
lupomic 37:05252c4a2d4e 158
lupomic 37:05252c4a2d4e 159 enable_motors = 1;
lupomic 37:05252c4a2d4e 160 positionController_M_Arm.setDesiredRotation(deg / 360.0, max_speed_rps_arm); // command to turn motor to desired deg.
lupomic 37:05252c4a2d4e 161
lupomic 37:05252c4a2d4e 162 diff = deg-(positionController_M_Arm.getRotation() * 360.0);
lupomic 37:05252c4a2d4e 163 if (diff<=0.3){
lupomic 37:05252c4a2d4e 164 return 1;
lupomic 37:05252c4a2d4e 165 }
lupomic 37:05252c4a2d4e 166 else {
lupomic 37:05252c4a2d4e 167 return NULL;}
lupomic 34:9f779e91168e 168
lupomic 33:70ea029a69e8 169
lupomic 37:05252c4a2d4e 170 enable_motors = 0;
lupomic 37:05252c4a2d4e 171
lupomic 37:05252c4a2d4e 172 }
lupomic 37:05252c4a2d4e 173
lupomic 37:05252c4a2d4e 174 //Drives forward into the next step
lupomic 37:05252c4a2d4e 175 //Prameter:distance in milimeter
lupomic 37:05252c4a2d4e 176 int drive_straight(float distance)
lupomic 37:05252c4a2d4e 177 {
lupomic 37:05252c4a2d4e 178 float diff_R;
lupomic 37:05252c4a2d4e 179 float diff_L;
lupomic 37:05252c4a2d4e 180 float deg_to_turn = wheel_dist_to_deg(distance);
lupomic 37:05252c4a2d4e 181 float relativ_turns_rightmotor = turn_relative_deg(deg_to_turn, positionController_M_right.getRotation());
lupomic 37:05252c4a2d4e 182 float relativ_turns_leftmotor = turn_relative_deg(deg_to_turn, positionController_M_left.getRotation());
lupomic 34:9f779e91168e 183
lupomic 37:05252c4a2d4e 184 ;
lupomic 37:05252c4a2d4e 185 positionController_M_right.setDesiredRotation(relativ_turns_rightmotor, max_speed_rps_wheel);
lupomic 37:05252c4a2d4e 186 positionController_M_left.setDesiredRotation(relativ_turns_leftmotor, max_speed_rps_wheel);
lupomic 37:05252c4a2d4e 187 enable_motors = 0;
lupomic 33:70ea029a69e8 188
lupomic 37:05252c4a2d4e 189 diff_R= fabs(relativ_turns_rightmotor-positionController_M_right.getRotation());
lupomic 37:05252c4a2d4e 190 diff_L= fabs(relativ_turns_leftmotor-positionController_M_left.getRotation());
lupomic 37:05252c4a2d4e 191
lupomic 37:05252c4a2d4e 192 printf("Case 2: Position Left(rot): %3.3f Position Right (rot): %3.3f Desired Rotation Left:%3.3f Desired Rotation Right;%3.3f Diff L:%3.3f Diff R:%3.3f \n",
lupomic 37:05252c4a2d4e 193 positionController_M_left.getRotation(),positionController_M_right.getRotation(),relativ_turns_leftmotor, relativ_turns_rightmotor, diff_L, diff_R );
lupomic 37:05252c4a2d4e 194 if ((diff_R<=0.01) && (diff_L<=0.01))
lupomic 37:05252c4a2d4e 195 {
lupomic 37:05252c4a2d4e 196 return 1;
lupomic 37:05252c4a2d4e 197 }
lupomic 37:05252c4a2d4e 198 else
lupomic 37:05252c4a2d4e 199 {
lupomic 37:05252c4a2d4e 200 return 0;
lupomic 37:05252c4a2d4e 201 }
lupomic 33:70ea029a69e8 202 }
lupomic 33:70ea029a69e8 203
lupomic 33:70ea029a69e8 204 //only turns the arm until the robot is on the next step
lupomic 37:05252c4a2d4e 205 int lift_up()
lupomic 37:05252c4a2d4e 206 {
lupomic 37:05252c4a2d4e 207 float diff;
lupomic 37:05252c4a2d4e 208 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 37:05252c4a2d4e 209 float relativ_turns_arm = turn_absolut_deg(position_lift_end_deg, positionController_M_Arm.getRotation());
lupomic 37:05252c4a2d4e 210
lupomic 37:05252c4a2d4e 211 enable_motors = 1;
lupomic 37:05252c4a2d4e 212 positionController_M_Arm.setDesiredRotation(relativ_turns_arm , max_speed_rps_arm);
lupomic 37:05252c4a2d4e 213 enable_motors = 0;
lupomic 33:70ea029a69e8 214
lupomic 37:05252c4a2d4e 215 diff=relativ_turns_arm-positionController_M_Arm.getRotation();
lupomic 37:05252c4a2d4e 216 if(diff<=0.01)
lupomic 37:05252c4a2d4e 217 { return 1;
lupomic 37:05252c4a2d4e 218 }
lupomic 37:05252c4a2d4e 219 else
lupomic 37:05252c4a2d4e 220 { return 0;
lupomic 37:05252c4a2d4e 221 }
lupomic 33:70ea029a69e8 222
lupomic 37:05252c4a2d4e 223
lupomic 37:05252c4a2d4e 224 }
lupomic 37:05252c4a2d4e 225 //***********************************************************************************************************************************************************
lupomic 37:05252c4a2d4e 226
lupomic 37:05252c4a2d4e 227 int NextStep (float){
lupomic 37:05252c4a2d4e 228 return 1;
lupomic 33:70ea029a69e8 229 }
lupomic 33:70ea029a69e8 230
lupomic 33:70ea029a69e8 231
lupomic 37:05252c4a2d4e 232 //simple check if there is an object in proximity
lupomic 37:05252c4a2d4e 233 //returns 0 if there is NO object present
lupomic 37:05252c4a2d4e 234 //returns 1 if there is an object present
lupomic 37:05252c4a2d4e 235 //returns 2 if the distance isn't in the expected range
lupomic 37:05252c4a2d4e 236
lupomic 37:05252c4a2d4e 237 uint8_t StepDetection(double distance){
lupomic 37:05252c4a2d4e 238 double d_valueMM = distance;
lupomic 37:05252c4a2d4e 239 if(d_valueMM >= 4) return 0;
lupomic 37:05252c4a2d4e 240 if(d_valueMM < 4) return 1;
lupomic 37:05252c4a2d4e 241 if(d_valueMM <= 0 || d_valueMM > 100 ) return 2;
lupomic 37:05252c4a2d4e 242 else return 2;
lupomic 37:05252c4a2d4e 243
lupomic 37:05252c4a2d4e 244 }
lupomic 37:05252c4a2d4e 245 //Function which checks if sensors and motors have been wired correctly and the expectet results will happen. otherwise Wall-E will show with armmovement.
lupomic 37:05252c4a2d4e 246 void check_start()
lupomic 37:05252c4a2d4e 247 {
lupomic 37:05252c4a2d4e 248
lupomic 37:05252c4a2d4e 249 }
lupomic 37:05252c4a2d4e 250
lupomic 37:05252c4a2d4e 251 // while loop gets executed every main_task_period_ms milliseconds
lupomic 37:05252c4a2d4e 252 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
lupomic 37:05252c4a2d4e 253 Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms
lupomic 37:05252c4a2d4e 254 //***********************************************************************************************************************************************************
lupomic 37:05252c4a2d4e 255
lupomic 33:70ea029a69e8 256 int main(void)
pmic 23:26b3a25fc637 257 {
pmic 24:86f1a63e35a0 258 // attach button fall and rise functions to user button object
lupomic 37:05252c4a2d4e 259 user_button.fall(&user_button_pressed_fcn);
lupomic 37:05252c4a2d4e 260 user_button.rise(&user_button_released_fcn);
lupomic 37:05252c4a2d4e 261
lupomic 37:05252c4a2d4e 262
lupomic 37:05252c4a2d4e 263 while (true)
lupomic 37:05252c4a2d4e 264 {
lupomic 37:05252c4a2d4e 265 enable_motors = 1;
lupomic 37:05252c4a2d4e 266 ir_distance_mm_L= mapping(ir_analog_in_Distance_L.read()*1.0e3f * 3.3f);
lupomic 37:05252c4a2d4e 267 ir_distance_mm_R= mapping(ir_analog_in_Distance_R.read()*1.0e3f * 3.3f);
lupomic 37:05252c4a2d4e 268 ir_distance_mm_Lookdown_B= mapping(ir_analog_in_Lookdown_B.read()*1.0e3f * 3.3f);
lupomic 37:05252c4a2d4e 269 ir_distance_mm_Lookdown_F= mapping(ir_analog_in_Lookdown_F.read()*1.0e3f * 3.3f);
pmic 24:86f1a63e35a0 270
pmic 6:e1fa1a2d7483 271
lupomic 37:05252c4a2d4e 272 switch (ToNextFunction)
lupomic 37:05252c4a2d4e 273 {
lupomic 34:9f779e91168e 274
lupomic 37:05252c4a2d4e 275 case 1:
lupomic 37:05252c4a2d4e 276 set_arm_stair_height();
lupomic 37:05252c4a2d4e 277 printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 37:05252c4a2d4e 278 if (state==1){
lupomic 37:05252c4a2d4e 279 ToNextFunction += 1;
lupomic 37:05252c4a2d4e 280 }
lupomic 37:05252c4a2d4e 281 break;
pmic 6:e1fa1a2d7483 282
lupomic 37:05252c4a2d4e 283 case 2:
lupomic 37:05252c4a2d4e 284 state=NextStep(ir_analog_in_Distance_L);
lupomic 37:05252c4a2d4e 285 if (state==1){
lupomic 37:05252c4a2d4e 286 ToNextFunction = 0;
lupomic 37:05252c4a2d4e 287 }
pmic 6:e1fa1a2d7483 288
lupomic 37:05252c4a2d4e 289 case 3:
lupomic 37:05252c4a2d4e 290 state=drive_straight(drive_stright_mm);
lupomic 37:05252c4a2d4e 291 printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
lupomic 37:05252c4a2d4e 292 positionController_M_right.getRotation(),positionController_M_left.getRotation());
lupomic 37:05252c4a2d4e 293 if (state==1){
lupomic 37:05252c4a2d4e 294 ToNextFunction = 0;
lupomic 37:05252c4a2d4e 295 }
lupomic 33:70ea029a69e8 296 break;
lupomic 37:05252c4a2d4e 297
lupomic 37:05252c4a2d4e 298 case 4:
lupomic 37:05252c4a2d4e 299 state=lift_up();
lupomic 34:9f779e91168e 300 printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 37:05252c4a2d4e 301 if ((state==1)&&(StepDetection(ir_distance_mm_Lookdown_B))&&StepDetection(ir_distance_mm_Lookdown_F)){
lupomic 37:05252c4a2d4e 302 ToNextFunction += 1;
lupomic 37:05252c4a2d4e 303 }
lupomic 37:05252c4a2d4e 304
lupomic 37:05252c4a2d4e 305
lupomic 37:05252c4a2d4e 306 case 5:
lupomic 37:05252c4a2d4e 307 state=drive_straight(drive_back_mm);
lupomic 34:9f779e91168e 308 printf("Case 4: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n",
lupomic 37:05252c4a2d4e 309 positionController_M_right.getRotation(),positionController_M_left.getRotation());
lupomic 37:05252c4a2d4e 310 if ((state==1)&&(StepDetection(ir_distance_mm_Lookdown_B)!=1)){
lupomic 37:05252c4a2d4e 311 ToNextFunction += 1;
lupomic 37:05252c4a2d4e 312 }
lupomic 33:70ea029a69e8 313 break;
lupomic 37:05252c4a2d4e 314
lupomic 37:05252c4a2d4e 315 case 6:
lupomic 37:05252c4a2d4e 316 state=lift_up();
lupomic 37:05252c4a2d4e 317 printf("Case 5: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation());
lupomic 37:05252c4a2d4e 318 if (state==1){
lupomic 37:05252c4a2d4e 319 ToNextFunction = 1;
lupomic 37:05252c4a2d4e 320 }
lupomic 33:70ea029a69e8 321 break;
lupomic 34:9f779e91168e 322 default: ;
lupomic 33:70ea029a69e8 323 }
lupomic 33:70ea029a69e8 324 }
lupomic 33:70ea029a69e8 325 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 326 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 327 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
lupomic 37:05252c4a2d4e 328 return 0;
pmic 1:93d997d6b232 329 }
pmic 6:e1fa1a2d7483 330
lupomic 33:70ea029a69e8 331
lupomic 37:05252c4a2d4e 332
pmic 24:86f1a63e35a0 333 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 334 {
pmic 26:28693b369945 335 user_button_timer.start();
pmic 6:e1fa1a2d7483 336 user_button_timer.reset();
pmic 6:e1fa1a2d7483 337 }
pmic 6:e1fa1a2d7483 338
lupomic 37:05252c4a2d4e 339 void user_button_released_fcn()
lupomic 37:05252c4a2d4e 340 {
pmic 24:86f1a63e35a0 341 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 342 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 343 user_button_timer.stop();
lupomic 37:05252c4a2d4e 344 if (user_button_elapsed_time_ms > 200)
lupomic 37:05252c4a2d4e 345 {
lupomic 37:05252c4a2d4e 346 ToNextFunction = 3;
lupomic 37:05252c4a2d4e 347 }
lupomic 37:05252c4a2d4e 348 }