first commit
Dependencies: PM2_Libary
main.cpp@41:a8557360c15e, 2022-05-19 (annotated)
- Committer:
- lupomic
- Date:
- Thu May 19 12:25:41 2022 +0200
- Branch:
- lupo
- Revision:
- 41:a8557360c15e
- Parent:
- 40:04b032b01dd5
- Child:
- 42:ec3a88a24666
testing
Who changed what in which revision?
User | Revision | Line number | New 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 | 41:a8557360c15e | 8 | const float wheel_diameter = 30.0f; // 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 | 38:8121e7a79c0b | 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) |
lupomic | 38:8121e7a79c0b | 14 | const float dist_grappleratt_grappler_uk = 36.5; // 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 | 38:8121e7a79c0b | 34 | |
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 | |
lupomic | 38:8121e7a79c0b | 39 | DigitalIn mechanical_button(PC_3); |
lupomic | 38:8121e7a79c0b | 40 | |
pmic | 24:86f1a63e35a0 | 41 | // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB |
pmic | 24:86f1a63e35a0 | 42 | DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors |
lupomic | 37:05252c4a2d4e | 43 | float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors |
pmic | 17:c19b471f05cb | 44 | |
lupomic | 33:70ea029a69e8 | 45 | //motor pin declaration |
lupomic | 37:05252c4a2d4e | 46 | FastPWM pwm_M_right (PB_13); //motor pin decalaration for wheels right side |
lupomic | 37:05252c4a2d4e | 47 | FastPWM pwm_M_left (PA_9); //motor pin decalaration for wheels left side |
lupomic | 37:05252c4a2d4e | 48 | FastPWM pwm_M_arm (PA_10); //motor pin decalaration for arm |
pmic | 17:c19b471f05cb | 49 | |
lupomic | 33:70ea029a69e8 | 50 | //Encoder pin declaration |
lupomic | 37:05252c4a2d4e | 51 | EncoderCounter encoder_M_right (PA_6, PC_7); //encoder pin decalaration for wheels right side |
lupomic | 37:05252c4a2d4e | 52 | EncoderCounter encoder_M_left (PB_6, PB_7); //encoder pin decalaration for wheels left side |
lupomic | 37:05252c4a2d4e | 53 | EncoderCounter encoder_M_arm (PA_0, PA_1); //encoder pin decalaration for arm |
lupomic | 37:05252c4a2d4e | 54 | //*********************************************************************************************************************************************************** |
lupomic | 37:05252c4a2d4e | 55 | // Hardware controll Setup and functions (motors and sensors) |
pmic | 17:c19b471f05cb | 56 | |
lupomic | 38:8121e7a79c0b | 57 | //these variables represent relative position NOT absolut |
lupomic | 41:a8557360c15e | 58 | float startPos = -0.555; //from last lift up position to start position |
lupomic | 38:8121e7a79c0b | 59 | float liftPos = -0.555; //from start position to lift up position |
lupomic | 38:8121e7a79c0b | 60 | |
pmic | 30:1e8295770bc1 | 61 | // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box |
lupomic | 37:05252c4a2d4e | 62 | 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 | 41:a8557360c15e | 63 | const float counts_per_turn_wheels = 20.0f * 78.125f;// define counts per turn at gearbox end (counts/turn * gearratio) for wheels |
lupomic | 38:8121e7a79c0b | 64 | const float counts_per_turn_arm = 20.0f * 78.125f * 19.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm |
lupomic | 37:05252c4a2d4e | 65 | const float kn = 180.0f / 12.0f; // define motor constant in rpm per V |
lupomic | 37:05252c4a2d4e | 66 | 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 | 67 | const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 |
pmic | 6:e1fa1a2d7483 | 68 | |
lupomic | 33:70ea029a69e8 | 69 | //motors for tracks |
lupomic | 33:70ea029a69e8 | 70 | 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 | 71 | 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 | 72 | //Arm Motor |
lupomic | 33:70ea029a69e8 | 73 | 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 | 74 | |
lupomic | 33:70ea029a69e8 | 75 | // 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 | 76 | //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 | 77 | //*********************************************************************************************************************************************************** |
lupomic | 38:8121e7a79c0b | 78 | // calculations for basic movment and controll |
pmic | 17:c19b471f05cb | 79 | |
lupomic | 37:05252c4a2d4e | 80 | //placeholder variables for prototype testing |
pmic | 20:7e7325edcf5c | 81 | |
lupomic | 41:a8557360c15e | 82 | const float drive_straight_mm = 250.0; // placeholder for testing drives amount forward |
lupomic | 41:a8557360c15e | 83 | const float drive_back_mm = -20.0f; // placeholder for testing drives amount backwards |
lupomic | 38:8121e7a79c0b | 84 | int ToNextFunction = 0; // current state of the system (which function is beeing executed) |
lupomic | 37:05252c4a2d4e | 85 | int state=0; |
lupomic | 38:8121e7a79c0b | 86 | float desired_pos; |
lupomic | 39:4c5e4ff386da | 87 | |
lupomic | 40:04b032b01dd5 | 88 | int nextStep=0; |
lupomic | 38:8121e7a79c0b | 89 | // definition variables for calculations |
lupomic | 38:8121e7a79c0b | 90 | const float pi = 2 * acos(0.0); // definiton of pi |
lupomic | 38:8121e7a79c0b | 91 | const float end_pos_lift_deg = 180 + asin((dist_arm_ground-(dist_grappleratt_grappler_uk))/arm_length) * 180 / pi; // calculates the degree which the arm has to have when lift_up has been executed. |
lupomic | 38:8121e7a79c0b | 92 | 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 | 38:8121e7a79c0b | 93 | |
lupomic | 38:8121e7a79c0b | 94 | // definition of rotation speeds for motors 0 = none 1.0 = max. |
lupomic | 38:8121e7a79c0b | 95 | const float max_speed_rps_wheel = 0.7f; // 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 | 38:8121e7a79c0b | 96 | const float max_speed_rps_arm = 0.9f; // 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 | 38:8121e7a79c0b | 97 | |
lupomic | 38:8121e7a79c0b | 98 | // 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 | 38:8121e7a79c0b | 99 | // PARAM: height_mm = height which OK Gripperarea has to reach. |
lupomic | 38:8121e7a79c0b | 100 | // RETURN: deg_arm = absolut Position in deg that the arm has to take. |
lupomic | 38:8121e7a79c0b | 101 | float calc_arm_deg_for_height(int height_mm) |
lupomic | 38:8121e7a79c0b | 102 | { |
lupomic | 38:8121e7a79c0b | 103 | float height_arm = height_mm - (dist_arm_ground - dist_arm_attach_OK_griparea); // calculates the height which only the arm has to cover (- attachement height (arm to robot) etc.) |
lupomic | 38:8121e7a79c0b | 104 | float deg_arm = asin(height_arm / arm_length) * 180.0/pi; // calculates the absolute degrees which the arm has to reach |
lupomic | 38:8121e7a79c0b | 105 | return deg_arm; |
lupomic | 38:8121e7a79c0b | 106 | } |
lupomic | 38:8121e7a79c0b | 107 | |
lupomic | 38:8121e7a79c0b | 108 | //calculates the deg which the wheels have to turn in order to cover specified distance in mm |
lupomic | 38:8121e7a79c0b | 109 | //PARAM: distance = distance to drive in milimeter |
lupomic | 38:8121e7a79c0b | 110 | //RETURN: deg_wheel = degree which the motor has to turn in order to cover distance(mm) |
lupomic | 41:a8557360c15e | 111 | float wheel_dist_to_deg(float distance) |
lupomic | 38:8121e7a79c0b | 112 | { |
lupomic | 41:a8557360c15e | 113 | float deg_wheel = (distance* 360.0f) / (wheel_diameter * pi) ; |
lupomic | 38:8121e7a79c0b | 114 | return deg_wheel; |
lupomic | 38:8121e7a79c0b | 115 | } |
lupomic | 33:70ea029a69e8 | 116 | |
lupomic | 36:a48b21a9635c | 117 | |
lupomic | 38:8121e7a79c0b | 118 | // increments the Motor for defined degree from the current one |
lupomic | 38:8121e7a79c0b | 119 | // PARAM: deg_to_turn = degree to turn the Motor |
lupomic | 38:8121e7a79c0b | 120 | // PARAM: current_rotation = the current rotation of the Motor (Motor.getRotation()) |
lupomic | 38:8121e7a79c0b | 121 | // RETURN: new_turn_rotation = new Rotation value in rotations |
lupomic | 38:8121e7a79c0b | 122 | float turn_relative_deg(float deg_to_turn, float current_rotation) |
lupomic | 38:8121e7a79c0b | 123 | { |
lupomic | 38:8121e7a79c0b | 124 | float new_turn_rotation = current_rotation + deg_to_turn; |
lupomic | 38:8121e7a79c0b | 125 | return new_turn_rotation; |
lupomic | 38:8121e7a79c0b | 126 | } |
lupomic | 33:70ea029a69e8 | 127 | |
lupomic | 38:8121e7a79c0b | 128 | // sets the Motor to a specified degree in one rotation |
lupomic | 38:8121e7a79c0b | 129 | // PARAM: end_deg = new position of the arm in degree 0 <= value >=360 |
lupomic | 38:8121e7a79c0b | 130 | // PARAM: current_rotation = the current rotation of the Motor (Motor.getRotation()) |
lupomic | 38:8121e7a79c0b | 131 | // RETURN: new_partial_rotation = new deg value in rotations |
lupomic | 38:8121e7a79c0b | 132 | float turn_absolut_deg(float end_deg, float current_rotations) |
lupomic | 37:05252c4a2d4e | 133 | { |
lupomic | 38:8121e7a79c0b | 134 | int full_rotations; |
lupomic | 38:8121e7a79c0b | 135 | if(current_rotations > 0) |
lupomic | 38:8121e7a79c0b | 136 | { |
lupomic | 38:8121e7a79c0b | 137 | full_rotations = round(current_rotations - 0.5); |
lupomic | 38:8121e7a79c0b | 138 | } |
lupomic | 38:8121e7a79c0b | 139 | else if(current_rotations < 0) |
lupomic | 38:8121e7a79c0b | 140 | { |
lupomic | 38:8121e7a79c0b | 141 | full_rotations = round(current_rotations + 0.5); |
lupomic | 38:8121e7a79c0b | 142 | } |
lupomic | 38:8121e7a79c0b | 143 | else |
lupomic | 38:8121e7a79c0b | 144 | { |
lupomic | 38:8121e7a79c0b | 145 | full_rotations = 0; |
lupomic | 38:8121e7a79c0b | 146 | } |
lupomic | 38:8121e7a79c0b | 147 | float new_partial_rotation = full_rotations - start_deg_arm/360 + end_deg/360; |
lupomic | 38:8121e7a79c0b | 148 | return new_partial_rotation; |
lupomic | 37:05252c4a2d4e | 149 | } |
lupomic | 33:70ea029a69e8 | 150 | |
lupomic | 37:05252c4a2d4e | 151 | //calculates position of arm when lift up has ended. |
lupomic | 37:05252c4a2d4e | 152 | //RETURN: end_deg = degree which the motor has to turn in order to reach end lift position. |
lupomic | 37:05252c4a2d4e | 153 | float calc_pos_end_lift() |
lupomic | 37:05252c4a2d4e | 154 | { |
lupomic | 37:05252c4a2d4e | 155 | float end_deg; |
lupomic | 38:8121e7a79c0b | 156 | end_deg = asin((dist_arm_ground-(dist_grappleratt_grappler_uk-dist_grappleratt_grappler_uk))/arm_length) + start_deg_arm; |
lupomic | 37:05252c4a2d4e | 157 | end_deg = end_deg * 180 / pi; |
lupomic | 37:05252c4a2d4e | 158 | return end_deg; |
lupomic | 37:05252c4a2d4e | 159 | } |
lupomic | 37:05252c4a2d4e | 160 | |
lupomic | 38:8121e7a79c0b | 161 | //*********************************************************************************************************************************************************** |
lupomic | 38:8121e7a79c0b | 162 | // important calculatet constant for Wall-E |
lupomic | 38:8121e7a79c0b | 163 | const double deg_up_from_horizon_to_stair = calc_arm_deg_for_height(height_stairs); |
lupomic | 38:8121e7a79c0b | 164 | |
lupomic | 38:8121e7a79c0b | 165 | // import functions from file mapping |
lupomic | 38:8121e7a79c0b | 166 | extern double powerx(double base, double pow2); |
lupomic | 38:8121e7a79c0b | 167 | extern double mapping (float adc_value_mV); |
lupomic | 38:8121e7a79c0b | 168 | |
lupomic | 38:8121e7a79c0b | 169 | // |
lupomic | 38:8121e7a79c0b | 170 | //simple check if there is an object in proximity |
lupomic | 38:8121e7a79c0b | 171 | //returns 0 if there is NO object present |
lupomic | 38:8121e7a79c0b | 172 | //returns 1 if there is an object present |
lupomic | 38:8121e7a79c0b | 173 | //returns 2 if the distance isn't in the expected range |
lupomic | 38:8121e7a79c0b | 174 | |
lupomic | 38:8121e7a79c0b | 175 | uint8_t nextStepDetection(double distanceCm,double setpointDistance){ |
lupomic | 38:8121e7a79c0b | 176 | double distance = distanceCm; |
lupomic | 38:8121e7a79c0b | 177 | double setpoint = setpointDistance; |
lupomic | 38:8121e7a79c0b | 178 | if(distance == 0){ |
lupomic | 38:8121e7a79c0b | 179 | return 10; //sensor value is outside the expected range |
lupomic | 38:8121e7a79c0b | 180 | } |
lupomic | 41:a8557360c15e | 181 | if((distance <= (setpoint + 2)) && (distance >= (setpoint - 2))){ |
lupomic | 38:8121e7a79c0b | 182 | return 3; //the distance to the next step is in ±1cm of the setpoint |
lupomic | 38:8121e7a79c0b | 183 | } |
lupomic | 38:8121e7a79c0b | 184 | if(distance < setpoint){ |
lupomic | 38:8121e7a79c0b | 185 | return 0; //the robot is to close to the step to rotate the arm unhindered |
lupomic | 38:8121e7a79c0b | 186 | } |
lupomic | 38:8121e7a79c0b | 187 | if(distance > setpoint){ |
lupomic | 38:8121e7a79c0b | 188 | return 1; //the robot is too far away from the next step |
lupomic | 38:8121e7a79c0b | 189 | } |
lupomic | 38:8121e7a79c0b | 190 | else{ |
lupomic | 38:8121e7a79c0b | 191 | return 2; |
lupomic | 38:8121e7a79c0b | 192 | } |
lupomic | 38:8121e7a79c0b | 193 | |
lupomic | 33:70ea029a69e8 | 194 | } |
lupomic | 38:8121e7a79c0b | 195 | //simple check if there is an object in proximity |
lupomic | 38:8121e7a79c0b | 196 | //returns 0 if there is NO object present |
lupomic | 38:8121e7a79c0b | 197 | //returns 1 if there is an object present |
lupomic | 38:8121e7a79c0b | 198 | //returns 2 if the distance isn't in the expected range |
lupomic | 38:8121e7a79c0b | 199 | uint8_t StepDetection_down(float sensor) |
lupomic | 37:05252c4a2d4e | 200 | |
lupomic | 37:05252c4a2d4e | 201 | { |
lupomic | 38:8121e7a79c0b | 202 | double d_valueMM = mapping(sensor*1.0e3f*3.3f); |
lupomic | 38:8121e7a79c0b | 203 | if(d_valueMM >= 4) return 0; |
lupomic | 38:8121e7a79c0b | 204 | else if( d_valueMM > 100 ) return 2; |
lupomic | 38:8121e7a79c0b | 205 | else if((d_valueMM < 4)||(d_valueMM==0)) return 1; |
lupomic | 38:8121e7a79c0b | 206 | |
lupomic | 38:8121e7a79c0b | 207 | else return 5; |
lupomic | 37:05252c4a2d4e | 208 | } |
lupomic | 33:70ea029a69e8 | 209 | |
lupomic | 37:05252c4a2d4e | 210 | // bring arm in starting position. Height of stairs. |
lupomic | 37:05252c4a2d4e | 211 | int set_arm_stair_height() |
lupomic | 38:8121e7a79c0b | 212 | |
lupomic | 37:05252c4a2d4e | 213 | { |
lupomic | 37:05252c4a2d4e | 214 | float diff; |
lupomic | 41:a8557360c15e | 215 | int gripper=StepDetection_down(ir_analog_in_Distance_L); |
lupomic | 38:8121e7a79c0b | 216 | |
lupomic | 38:8121e7a79c0b | 217 | |
lupomic | 41:a8557360c15e | 218 | if (desired_pos==0) { |
lupomic | 41:a8557360c15e | 219 | desired_pos=turn_relative_deg(startPos, positionController_M_Arm.getRotation()); |
lupomic | 41:a8557360c15e | 220 | positionController_M_Arm.setDesiredRotation(desired_pos, max_speed_rps_arm); // command to turn motor to desired deg. |
lupomic | 41:a8557360c15e | 221 | } |
lupomic | 37:05252c4a2d4e | 222 | |
lupomic | 38:8121e7a79c0b | 223 | diff =abs( desired_pos-(positionController_M_Arm.getRotation())); |
lupomic | 40:04b032b01dd5 | 224 | printf("Set arm Position ARM (rot): %3.3f Desired:%3.3f State:%d ToNextfunction:%d Diff:%3.3f\n", |
lupomic | 40:04b032b01dd5 | 225 | positionController_M_Arm.getRotation(), desired_pos, state, ToNextFunction, diff); |
lupomic | 41:a8557360c15e | 226 | |
lupomic | 41:a8557360c15e | 227 | |
lupomic | 41:a8557360c15e | 228 | if (gripper){ |
lupomic | 41:a8557360c15e | 229 | desired_pos=turn_relative_deg(0.0, positionController_M_Arm.getRotation() ); |
lupomic | 41:a8557360c15e | 230 | positionController_M_Arm.setDesiredRotation(desired_pos, max_speed_rps_arm); |
lupomic | 41:a8557360c15e | 231 | } |
lupomic | 41:a8557360c15e | 232 | |
lupomic | 41:a8557360c15e | 233 | if((diff<0.009)&&gripper){ |
lupomic | 37:05252c4a2d4e | 234 | return 1; |
lupomic | 37:05252c4a2d4e | 235 | } |
lupomic | 37:05252c4a2d4e | 236 | else { |
lupomic | 38:8121e7a79c0b | 237 | return NULL; |
lupomic | 38:8121e7a79c0b | 238 | } |
lupomic | 37:05252c4a2d4e | 239 | } |
lupomic | 37:05252c4a2d4e | 240 | |
lupomic | 37:05252c4a2d4e | 241 | //Drives forward into the next step |
lupomic | 37:05252c4a2d4e | 242 | //Prameter:distance in milimeter |
lupomic | 37:05252c4a2d4e | 243 | int drive_straight(float distance) |
lupomic | 37:05252c4a2d4e | 244 | { |
lupomic | 37:05252c4a2d4e | 245 | float diff_R; |
lupomic | 37:05252c4a2d4e | 246 | float diff_L; |
lupomic | 34:9f779e91168e | 247 | |
lupomic | 38:8121e7a79c0b | 248 | if (desired_pos==0) { |
lupomic | 41:a8557360c15e | 249 | desired_pos= distance/(wheel_diameter*pi); |
lupomic | 38:8121e7a79c0b | 250 | float relativ_turns_rightmotor = turn_relative_deg(desired_pos, positionController_M_right.getRotation()); |
lupomic | 38:8121e7a79c0b | 251 | float relativ_turns_leftmotor = turn_relative_deg(desired_pos, positionController_M_left.getRotation()); |
lupomic | 38:8121e7a79c0b | 252 | } |
lupomic | 38:8121e7a79c0b | 253 | |
lupomic | 38:8121e7a79c0b | 254 | positionController_M_right.setDesiredRotation(desired_pos, max_speed_rps_wheel); |
lupomic | 38:8121e7a79c0b | 255 | positionController_M_left.setDesiredRotation(desired_pos, max_speed_rps_wheel); |
lupomic | 33:70ea029a69e8 | 256 | |
lupomic | 37:05252c4a2d4e | 257 | |
lupomic | 38:8121e7a79c0b | 258 | diff_R= abs(desired_pos-(positionController_M_right.getRotation())); |
lupomic | 38:8121e7a79c0b | 259 | diff_L= abs(desired_pos-(positionController_M_left.getRotation())); |
lupomic | 41:a8557360c15e | 260 | printf("Drive Straight Position Right(rot): %3.3f; Position Left (rot): %3.3f Desired: %3.3f Diff:%3.3f State:%d ToNextfunction:%d\n", |
lupomic | 41:a8557360c15e | 261 | positionController_M_right.getRotation(),positionController_M_left.getRotation(),desired_pos,diff_L, state, ToNextFunction); |
lupomic | 38:8121e7a79c0b | 262 | if ((diff_R<=0.03) && (diff_L<=0.03)) |
lupomic | 37:05252c4a2d4e | 263 | { |
lupomic | 37:05252c4a2d4e | 264 | return 1; |
lupomic | 37:05252c4a2d4e | 265 | } |
lupomic | 37:05252c4a2d4e | 266 | else |
lupomic | 37:05252c4a2d4e | 267 | { |
lupomic | 37:05252c4a2d4e | 268 | return 0; |
lupomic | 37:05252c4a2d4e | 269 | } |
lupomic | 33:70ea029a69e8 | 270 | } |
lupomic | 33:70ea029a69e8 | 271 | |
lupomic | 38:8121e7a79c0b | 272 | //turns the arm until the robot is on the next step |
lupomic | 37:05252c4a2d4e | 273 | int lift_up() |
lupomic | 37:05252c4a2d4e | 274 | { |
lupomic | 37:05252c4a2d4e | 275 | float diff; |
lupomic | 38:8121e7a79c0b | 276 | if (desired_pos==0) { |
lupomic | 38:8121e7a79c0b | 277 | desired_pos = turn_relative_deg(liftPos,positionController_M_Arm.getRotation()); |
lupomic | 38:8121e7a79c0b | 278 | } |
lupomic | 38:8121e7a79c0b | 279 | |
lupomic | 38:8121e7a79c0b | 280 | |
lupomic | 37:05252c4a2d4e | 281 | |
lupomic | 38:8121e7a79c0b | 282 | positionController_M_Arm.setDesiredRotation(desired_pos, max_speed_rps_arm); |
lupomic | 38:8121e7a79c0b | 283 | |
lupomic | 38:8121e7a79c0b | 284 | diff=abs(desired_pos-positionController_M_Arm.getRotation()); |
lupomic | 40:04b032b01dd5 | 285 | // printf("Lift Up: Position ARM (rot): %3.3f Desired:%3.3f State:%d ToNextfunction:%d\n",positionController_M_Arm.getRotation(),desired_pos, state, ToNextFunction); |
lupomic | 38:8121e7a79c0b | 286 | if(diff<=0.03) |
lupomic | 37:05252c4a2d4e | 287 | { return 1; |
lupomic | 37:05252c4a2d4e | 288 | } |
lupomic | 37:05252c4a2d4e | 289 | else |
lupomic | 37:05252c4a2d4e | 290 | { return 0; |
lupomic | 37:05252c4a2d4e | 291 | } |
lupomic | 33:70ea029a69e8 | 292 | |
lupomic | 37:05252c4a2d4e | 293 | } |
lupomic | 37:05252c4a2d4e | 294 | //*********************************************************************************************************************************************************** |
lupomic | 37:05252c4a2d4e | 295 | |
lupomic | 37:05252c4a2d4e | 296 | // while loop gets executed every main_task_period_ms milliseconds |
lupomic | 37:05252c4a2d4e | 297 | 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 | 298 | 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 | 299 | //*********************************************************************************************************************************************************** |
lupomic | 37:05252c4a2d4e | 300 | |
lupomic | 33:70ea029a69e8 | 301 | int main(void) |
pmic | 23:26b3a25fc637 | 302 | { |
pmic | 24:86f1a63e35a0 | 303 | // attach button fall and rise functions to user button object |
lupomic | 37:05252c4a2d4e | 304 | user_button.fall(&user_button_pressed_fcn); |
lupomic | 37:05252c4a2d4e | 305 | user_button.rise(&user_button_released_fcn); |
lupomic | 38:8121e7a79c0b | 306 | mechanical_button.mode(PullDown); |
lupomic | 39:4c5e4ff386da | 307 | printf("test"); |
lupomic | 41:a8557360c15e | 308 | ToNextFunction = 0; |
lupomic | 37:05252c4a2d4e | 309 | while (true) |
lupomic | 37:05252c4a2d4e | 310 | { |
lupomic | 38:8121e7a79c0b | 311 | |
lupomic | 37:05252c4a2d4e | 312 | ir_distance_mm_L= mapping(ir_analog_in_Distance_L.read()*1.0e3f * 3.3f); |
lupomic | 38:8121e7a79c0b | 313 | |
lupomic | 38:8121e7a79c0b | 314 | |
lupomic | 38:8121e7a79c0b | 315 | if (ToNextFunction>=1||(mechanical_button.read()!=1)) |
lupomic | 38:8121e7a79c0b | 316 | { |
lupomic | 38:8121e7a79c0b | 317 | enable_motors=1; |
lupomic | 38:8121e7a79c0b | 318 | } |
lupomic | 41:a8557360c15e | 319 | |
lupomic | 41:a8557360c15e | 320 | |
lupomic | 37:05252c4a2d4e | 321 | switch (ToNextFunction) |
lupomic | 37:05252c4a2d4e | 322 | { |
lupomic | 41:a8557360c15e | 323 | |
lupomic | 38:8121e7a79c0b | 324 | case 0: while (mechanical_button.read()!=1) |
lupomic | 38:8121e7a79c0b | 325 | { |
lupomic | 38:8121e7a79c0b | 326 | positionController_M_Arm.setDesiredRotation(-1,0.5); |
lupomic | 38:8121e7a79c0b | 327 | |
lupomic | 38:8121e7a79c0b | 328 | } |
lupomic | 38:8121e7a79c0b | 329 | if (mechanical_button){ |
lupomic | 38:8121e7a79c0b | 330 | positionController_M_Arm.setDesiredRotation(positionController_M_Arm.getRotation()); |
lupomic | 34:9f779e91168e | 331 | |
lupomic | 38:8121e7a79c0b | 332 | } |
lupomic | 38:8121e7a79c0b | 333 | |
lupomic | 38:8121e7a79c0b | 334 | |
lupomic | 38:8121e7a79c0b | 335 | break; |
lupomic | 38:8121e7a79c0b | 336 | |
lupomic | 38:8121e7a79c0b | 337 | case 1: |
lupomic | 39:4c5e4ff386da | 338 | |
lupomic | 39:4c5e4ff386da | 339 | ToNextFunction +=1; |
lupomic | 39:4c5e4ff386da | 340 | state=0; |
lupomic | 39:4c5e4ff386da | 341 | |
lupomic | 38:8121e7a79c0b | 342 | |
lupomic | 37:05252c4a2d4e | 343 | break; |
pmic | 6:e1fa1a2d7483 | 344 | |
lupomic | 39:4c5e4ff386da | 345 | case 2: state=drive_straight(drive_straight_mm); |
lupomic | 38:8121e7a79c0b | 346 | |
lupomic | 37:05252c4a2d4e | 347 | if (state==1){ |
lupomic | 38:8121e7a79c0b | 348 | ToNextFunction += 1; |
lupomic | 38:8121e7a79c0b | 349 | state=0; |
lupomic | 38:8121e7a79c0b | 350 | desired_pos=0; |
lupomic | 38:8121e7a79c0b | 351 | |
lupomic | 37:05252c4a2d4e | 352 | } |
lupomic | 39:4c5e4ff386da | 353 | |
lupomic | 33:70ea029a69e8 | 354 | break; |
lupomic | 37:05252c4a2d4e | 355 | |
lupomic | 39:4c5e4ff386da | 356 | case 3: state=lift_up(); |
lupomic | 38:8121e7a79c0b | 357 | |
lupomic | 38:8121e7a79c0b | 358 | if (state==1){ |
lupomic | 37:05252c4a2d4e | 359 | ToNextFunction += 1; |
lupomic | 38:8121e7a79c0b | 360 | state=0; |
lupomic | 38:8121e7a79c0b | 361 | desired_pos=0; |
lupomic | 37:05252c4a2d4e | 362 | } |
lupomic | 39:4c5e4ff386da | 363 | break; |
lupomic | 39:4c5e4ff386da | 364 | |
lupomic | 41:a8557360c15e | 365 | case 4: state=nextStepDetection(ir_distance_mm_L,4); |
lupomic | 40:04b032b01dd5 | 366 | |
lupomic | 40:04b032b01dd5 | 367 | |
lupomic | 39:4c5e4ff386da | 368 | |
lupomic | 39:4c5e4ff386da | 369 | if (state==3){ |
lupomic | 40:04b032b01dd5 | 370 | nextStep=1; |
lupomic | 39:4c5e4ff386da | 371 | } |
lupomic | 41:a8557360c15e | 372 | printf("distance:%3.3f Output:%d NextStep:%d\n ", ir_distance_mm_L, nextStepDetection(ir_distance_mm_L,4), nextStep); |
lupomic | 39:4c5e4ff386da | 373 | |
lupomic | 40:04b032b01dd5 | 374 | ToNextFunction +=1; |
lupomic | 40:04b032b01dd5 | 375 | state=0; |
lupomic | 39:4c5e4ff386da | 376 | |
lupomic | 38:8121e7a79c0b | 377 | break; |
lupomic | 37:05252c4a2d4e | 378 | |
lupomic | 37:05252c4a2d4e | 379 | case 5: |
lupomic | 39:4c5e4ff386da | 380 | |
lupomic | 37:05252c4a2d4e | 381 | state=drive_straight(drive_back_mm); |
lupomic | 38:8121e7a79c0b | 382 | |
lupomic | 39:4c5e4ff386da | 383 | if (StepDetection_down(ir_analog_in_Lookdown_B) != 1) |
lupomic | 39:4c5e4ff386da | 384 | { |
lupomic | 37:05252c4a2d4e | 385 | ToNextFunction += 1; |
lupomic | 38:8121e7a79c0b | 386 | state=0; |
lupomic | 38:8121e7a79c0b | 387 | desired_pos=0; |
lupomic | 39:4c5e4ff386da | 388 | positionController_M_left.setDesiredRotation(positionController_M_left.getRotation()); |
lupomic | 39:4c5e4ff386da | 389 | positionController_M_right.setDesiredRotation(positionController_M_right.getRotation()); |
lupomic | 38:8121e7a79c0b | 390 | |
lupomic | 37:05252c4a2d4e | 391 | } |
lupomic | 33:70ea029a69e8 | 392 | break; |
lupomic | 37:05252c4a2d4e | 393 | |
lupomic | 37:05252c4a2d4e | 394 | case 6: |
lupomic | 38:8121e7a79c0b | 395 | state=set_arm_stair_height(); |
lupomic | 38:8121e7a79c0b | 396 | |
lupomic | 40:04b032b01dd5 | 397 | if ((state==1) && (nextStep)){ |
lupomic | 39:4c5e4ff386da | 398 | ToNextFunction = 1; |
lupomic | 38:8121e7a79c0b | 399 | state=0; |
lupomic | 38:8121e7a79c0b | 400 | desired_pos=0; |
lupomic | 40:04b032b01dd5 | 401 | nextStep=0; |
lupomic | 39:4c5e4ff386da | 402 | } |
lupomic | 40:04b032b01dd5 | 403 | if ((state==1) && (nextStep!=1)) { |
lupomic | 39:4c5e4ff386da | 404 | ToNextFunction=0; |
lupomic | 39:4c5e4ff386da | 405 | state=0; |
lupomic | 39:4c5e4ff386da | 406 | desired_pos=0; |
lupomic | 40:04b032b01dd5 | 407 | nextStep=0; |
lupomic | 37:05252c4a2d4e | 408 | } |
lupomic | 33:70ea029a69e8 | 409 | break; |
lupomic | 38:8121e7a79c0b | 410 | |
lupomic | 34:9f779e91168e | 411 | default: ; |
lupomic | 33:70ea029a69e8 | 412 | } |
lupomic | 33:70ea029a69e8 | 413 | } |
lupomic | 38:8121e7a79c0b | 414 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
lupomic | 38:8121e7a79c0b | 415 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
lupomic | 38:8121e7a79c0b | 416 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
lupomic | 38:8121e7a79c0b | 417 | return 0; |
pmic | 1:93d997d6b232 | 418 | } |
pmic | 6:e1fa1a2d7483 | 419 | |
lupomic | 33:70ea029a69e8 | 420 | |
lupomic | 37:05252c4a2d4e | 421 | |
pmic | 24:86f1a63e35a0 | 422 | void user_button_pressed_fcn() |
pmic | 25:ea1d6e27c895 | 423 | { |
pmic | 26:28693b369945 | 424 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 425 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 426 | } |
pmic | 6:e1fa1a2d7483 | 427 | |
lupomic | 37:05252c4a2d4e | 428 | void user_button_released_fcn() |
lupomic | 37:05252c4a2d4e | 429 | { |
pmic | 24:86f1a63e35a0 | 430 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 24:86f1a63e35a0 | 431 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 432 | user_button_timer.stop(); |
lupomic | 37:05252c4a2d4e | 433 | if (user_button_elapsed_time_ms > 200) |
lupomic | 37:05252c4a2d4e | 434 | { |
lupomic | 40:04b032b01dd5 | 435 | ToNextFunction =1; |
lupomic | 37:05252c4a2d4e | 436 | } |
lupomic | 37:05252c4a2d4e | 437 | } |