first commit

Dependencies:   PM2_Libary

Committer:
lupomic
Date:
Wed May 18 11:28:25 2022 +0200
Branch:
lupo
Revision:
39:4c5e4ff386da
Parent:
38:8121e7a79c0b
Child:
40:04b032b01dd5
testing

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 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 38:8121e7a79c0b 58 float startPos = -0.525; //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 37:05252c4a2d4e 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 38:8121e7a79c0b 82 const int drive_straight_mm = 2; // placeholder for testing drives amount forward
lupomic 38:8121e7a79c0b 83 const int drive_back_mm = -2; // 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 39:4c5e4ff386da 88 bool nextStep=false;
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 38:8121e7a79c0b 111 float wheel_dist_to_deg(int distance)
lupomic 38:8121e7a79c0b 112 {
lupomic 38:8121e7a79c0b 113 float deg_wheel = distance / (wheel_diameter * pi) * 360;
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 38:8121e7a79c0b 181 if((distance <= (setpoint + 1)) && (distance >= (setpoint - 1))){
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 38:8121e7a79c0b 215 if (desired_pos==0) {
lupomic 38:8121e7a79c0b 216 desired_pos=turn_relative_deg(startPos, positionController_M_Arm.getRotation());
lupomic 37:05252c4a2d4e 217 }
lupomic 38:8121e7a79c0b 218
lupomic 38:8121e7a79c0b 219
lupomic 38:8121e7a79c0b 220 positionController_M_Arm.setDesiredRotation(desired_pos, max_speed_rps_arm); // command to turn motor to desired deg.
lupomic 37:05252c4a2d4e 221
lupomic 38:8121e7a79c0b 222
lupomic 38:8121e7a79c0b 223
lupomic 38:8121e7a79c0b 224
lupomic 37:05252c4a2d4e 225
lupomic 38:8121e7a79c0b 226 diff =abs( desired_pos-(positionController_M_Arm.getRotation()));
lupomic 39:4c5e4ff386da 227 // printf("Set arm Position ARM (rot): %3.3f Desired:%3.3f State:%d ToNextfunction:%d Diff:%3.3f\n",
lupomic 39:4c5e4ff386da 228 // positionController_M_Arm.getRotation(), desired_pos, state, ToNextFunction, diff);
lupomic 38:8121e7a79c0b 229 if (diff<=0.009){
lupomic 37:05252c4a2d4e 230 return 1;
lupomic 37:05252c4a2d4e 231 }
lupomic 37:05252c4a2d4e 232 else {
lupomic 38:8121e7a79c0b 233 return NULL;
lupomic 38:8121e7a79c0b 234 }
lupomic 37:05252c4a2d4e 235 }
lupomic 37:05252c4a2d4e 236
lupomic 37:05252c4a2d4e 237 //Drives forward into the next step
lupomic 37:05252c4a2d4e 238 //Prameter:distance in milimeter
lupomic 37:05252c4a2d4e 239 int drive_straight(float distance)
lupomic 37:05252c4a2d4e 240 {
lupomic 37:05252c4a2d4e 241 float diff_R;
lupomic 37:05252c4a2d4e 242 float diff_L;
lupomic 34:9f779e91168e 243
lupomic 38:8121e7a79c0b 244 if (desired_pos==0) {
lupomic 38:8121e7a79c0b 245 desired_pos= wheel_dist_to_deg(distance);
lupomic 38:8121e7a79c0b 246 float relativ_turns_rightmotor = turn_relative_deg(desired_pos, positionController_M_right.getRotation());
lupomic 38:8121e7a79c0b 247 float relativ_turns_leftmotor = turn_relative_deg(desired_pos, positionController_M_left.getRotation());
lupomic 38:8121e7a79c0b 248 }
lupomic 38:8121e7a79c0b 249
lupomic 38:8121e7a79c0b 250 positionController_M_right.setDesiredRotation(desired_pos, max_speed_rps_wheel);
lupomic 38:8121e7a79c0b 251 positionController_M_left.setDesiredRotation(desired_pos, max_speed_rps_wheel);
lupomic 33:70ea029a69e8 252
lupomic 37:05252c4a2d4e 253
lupomic 38:8121e7a79c0b 254 diff_R= abs(desired_pos-(positionController_M_right.getRotation()));
lupomic 38:8121e7a79c0b 255 diff_L= abs(desired_pos-(positionController_M_left.getRotation()));
lupomic 39:4c5e4ff386da 256 // 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 39:4c5e4ff386da 257 // positionController_M_right.getRotation(),positionController_M_left.getRotation(),desired_pos,diff_L, state, ToNextFunction);
lupomic 38:8121e7a79c0b 258 if ((diff_R<=0.03) && (diff_L<=0.03))
lupomic 37:05252c4a2d4e 259 {
lupomic 37:05252c4a2d4e 260 return 1;
lupomic 37:05252c4a2d4e 261 }
lupomic 37:05252c4a2d4e 262 else
lupomic 37:05252c4a2d4e 263 {
lupomic 37:05252c4a2d4e 264 return 0;
lupomic 37:05252c4a2d4e 265 }
lupomic 33:70ea029a69e8 266 }
lupomic 33:70ea029a69e8 267
lupomic 38:8121e7a79c0b 268 //turns the arm until the robot is on the next step
lupomic 37:05252c4a2d4e 269 int lift_up()
lupomic 37:05252c4a2d4e 270 {
lupomic 37:05252c4a2d4e 271 float diff;
lupomic 38:8121e7a79c0b 272 if (desired_pos==0) {
lupomic 38:8121e7a79c0b 273 desired_pos = turn_relative_deg(liftPos,positionController_M_Arm.getRotation());
lupomic 38:8121e7a79c0b 274 }
lupomic 38:8121e7a79c0b 275
lupomic 38:8121e7a79c0b 276
lupomic 37:05252c4a2d4e 277
lupomic 38:8121e7a79c0b 278 positionController_M_Arm.setDesiredRotation(desired_pos, max_speed_rps_arm);
lupomic 38:8121e7a79c0b 279
lupomic 38:8121e7a79c0b 280 diff=abs(desired_pos-positionController_M_Arm.getRotation());
lupomic 39:4c5e4ff386da 281 // 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 282 if(diff<=0.03)
lupomic 37:05252c4a2d4e 283 { return 1;
lupomic 37:05252c4a2d4e 284 }
lupomic 37:05252c4a2d4e 285 else
lupomic 37:05252c4a2d4e 286 { return 0;
lupomic 37:05252c4a2d4e 287 }
lupomic 33:70ea029a69e8 288
lupomic 37:05252c4a2d4e 289 }
lupomic 37:05252c4a2d4e 290 //***********************************************************************************************************************************************************
lupomic 37:05252c4a2d4e 291
lupomic 37:05252c4a2d4e 292 // while loop gets executed every main_task_period_ms milliseconds
lupomic 37:05252c4a2d4e 293 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 294 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 295 //***********************************************************************************************************************************************************
lupomic 37:05252c4a2d4e 296
lupomic 33:70ea029a69e8 297 int main(void)
pmic 23:26b3a25fc637 298 {
pmic 24:86f1a63e35a0 299 // attach button fall and rise functions to user button object
lupomic 37:05252c4a2d4e 300 user_button.fall(&user_button_pressed_fcn);
lupomic 37:05252c4a2d4e 301 user_button.rise(&user_button_released_fcn);
lupomic 38:8121e7a79c0b 302 mechanical_button.mode(PullDown);
lupomic 39:4c5e4ff386da 303 printf("test");
lupomic 37:05252c4a2d4e 304
lupomic 37:05252c4a2d4e 305 while (true)
lupomic 37:05252c4a2d4e 306 {
lupomic 38:8121e7a79c0b 307
lupomic 37:05252c4a2d4e 308 ir_distance_mm_L= mapping(ir_analog_in_Distance_L.read()*1.0e3f * 3.3f);
lupomic 38:8121e7a79c0b 309
lupomic 38:8121e7a79c0b 310
lupomic 38:8121e7a79c0b 311 if (ToNextFunction>=1||(mechanical_button.read()!=1))
lupomic 38:8121e7a79c0b 312 {
lupomic 38:8121e7a79c0b 313 enable_motors=1;
lupomic 38:8121e7a79c0b 314 }
pmic 24:86f1a63e35a0 315
pmic 6:e1fa1a2d7483 316
lupomic 37:05252c4a2d4e 317 switch (ToNextFunction)
lupomic 37:05252c4a2d4e 318 {
lupomic 38:8121e7a79c0b 319
lupomic 38:8121e7a79c0b 320 case 0: while (mechanical_button.read()!=1)
lupomic 38:8121e7a79c0b 321 {
lupomic 38:8121e7a79c0b 322 positionController_M_Arm.setDesiredRotation(-1,0.5);
lupomic 38:8121e7a79c0b 323
lupomic 38:8121e7a79c0b 324 }
lupomic 38:8121e7a79c0b 325 if (mechanical_button){
lupomic 38:8121e7a79c0b 326 positionController_M_Arm.setDesiredRotation(positionController_M_Arm.getRotation());
lupomic 34:9f779e91168e 327
lupomic 38:8121e7a79c0b 328 }
lupomic 38:8121e7a79c0b 329
lupomic 38:8121e7a79c0b 330
lupomic 38:8121e7a79c0b 331 break;
lupomic 38:8121e7a79c0b 332
lupomic 38:8121e7a79c0b 333 case 1:
lupomic 39:4c5e4ff386da 334
lupomic 39:4c5e4ff386da 335 ToNextFunction +=1;
lupomic 39:4c5e4ff386da 336 state=0;
lupomic 39:4c5e4ff386da 337
lupomic 38:8121e7a79c0b 338
lupomic 37:05252c4a2d4e 339 break;
pmic 6:e1fa1a2d7483 340
lupomic 39:4c5e4ff386da 341 case 2: state=drive_straight(drive_straight_mm);
lupomic 38:8121e7a79c0b 342
lupomic 37:05252c4a2d4e 343 if (state==1){
lupomic 38:8121e7a79c0b 344 ToNextFunction += 1;
lupomic 38:8121e7a79c0b 345 state=0;
lupomic 38:8121e7a79c0b 346 desired_pos=0;
lupomic 38:8121e7a79c0b 347
lupomic 37:05252c4a2d4e 348 }
lupomic 39:4c5e4ff386da 349
lupomic 33:70ea029a69e8 350 break;
lupomic 37:05252c4a2d4e 351
lupomic 39:4c5e4ff386da 352 case 3: state=lift_up();
lupomic 38:8121e7a79c0b 353
lupomic 38:8121e7a79c0b 354 if (state==1){
lupomic 37:05252c4a2d4e 355 ToNextFunction += 1;
lupomic 38:8121e7a79c0b 356 state=0;
lupomic 38:8121e7a79c0b 357 desired_pos=0;
lupomic 37:05252c4a2d4e 358 }
lupomic 39:4c5e4ff386da 359 break;
lupomic 39:4c5e4ff386da 360
lupomic 39:4c5e4ff386da 361 case 4: state=nextStepDetection(ir_distance_mm_L,10);
lupomic 39:4c5e4ff386da 362 printf("distance:%3.3f Output:%d\n", ir_distance_mm_L, nextStepDetection(ir_distance_mm_L,10));
lupomic 39:4c5e4ff386da 363 nextStep=false;
lupomic 39:4c5e4ff386da 364
lupomic 39:4c5e4ff386da 365 if (state==3){
lupomic 39:4c5e4ff386da 366 nextStep=true;
lupomic 39:4c5e4ff386da 367 }
lupomic 39:4c5e4ff386da 368
lupomic 39:4c5e4ff386da 369
lupomic 39:4c5e4ff386da 370 // ToNextFunction +=1;
lupomic 39:4c5e4ff386da 371 // state=0;
lupomic 39:4c5e4ff386da 372
lupomic 38:8121e7a79c0b 373 break;
lupomic 37:05252c4a2d4e 374
lupomic 37:05252c4a2d4e 375 case 5:
lupomic 39:4c5e4ff386da 376
lupomic 37:05252c4a2d4e 377 state=drive_straight(drive_back_mm);
lupomic 38:8121e7a79c0b 378
lupomic 39:4c5e4ff386da 379 if (StepDetection_down(ir_analog_in_Lookdown_B) != 1)
lupomic 39:4c5e4ff386da 380 {
lupomic 37:05252c4a2d4e 381 ToNextFunction += 1;
lupomic 38:8121e7a79c0b 382 state=0;
lupomic 38:8121e7a79c0b 383 desired_pos=0;
lupomic 39:4c5e4ff386da 384 positionController_M_left.setDesiredRotation(positionController_M_left.getRotation());
lupomic 39:4c5e4ff386da 385 positionController_M_right.setDesiredRotation(positionController_M_right.getRotation());
lupomic 38:8121e7a79c0b 386
lupomic 37:05252c4a2d4e 387 }
lupomic 33:70ea029a69e8 388 break;
lupomic 37:05252c4a2d4e 389
lupomic 37:05252c4a2d4e 390 case 6:
lupomic 38:8121e7a79c0b 391 state=set_arm_stair_height();
lupomic 38:8121e7a79c0b 392
lupomic 39:4c5e4ff386da 393 if ((state==1)&&(nextStep)){
lupomic 39:4c5e4ff386da 394 ToNextFunction = 1;
lupomic 38:8121e7a79c0b 395 state=0;
lupomic 38:8121e7a79c0b 396 desired_pos=0;
lupomic 39:4c5e4ff386da 397 nextStep=false;
lupomic 39:4c5e4ff386da 398 }
lupomic 39:4c5e4ff386da 399 if ((state==1)&&(nextStep!=1)) {
lupomic 39:4c5e4ff386da 400 ToNextFunction=0;
lupomic 39:4c5e4ff386da 401 state=0;
lupomic 39:4c5e4ff386da 402 desired_pos=0;
lupomic 39:4c5e4ff386da 403 nextStep=false;
lupomic 37:05252c4a2d4e 404 }
lupomic 33:70ea029a69e8 405 break;
lupomic 38:8121e7a79c0b 406
lupomic 34:9f779e91168e 407 default: ;
lupomic 33:70ea029a69e8 408 }
lupomic 33:70ea029a69e8 409 }
lupomic 38:8121e7a79c0b 410 // read timer and make the main thread sleep for the remaining time span (non blocking)
lupomic 38:8121e7a79c0b 411 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
lupomic 38:8121e7a79c0b 412 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
lupomic 38:8121e7a79c0b 413 return 0;
pmic 1:93d997d6b232 414 }
pmic 6:e1fa1a2d7483 415
lupomic 33:70ea029a69e8 416
lupomic 37:05252c4a2d4e 417
pmic 24:86f1a63e35a0 418 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 419 {
pmic 26:28693b369945 420 user_button_timer.start();
pmic 6:e1fa1a2d7483 421 user_button_timer.reset();
pmic 6:e1fa1a2d7483 422 }
pmic 6:e1fa1a2d7483 423
lupomic 37:05252c4a2d4e 424 void user_button_released_fcn()
lupomic 37:05252c4a2d4e 425 {
pmic 24:86f1a63e35a0 426 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 427 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 428 user_button_timer.stop();
lupomic 37:05252c4a2d4e 429 if (user_button_elapsed_time_ms > 200)
lupomic 37:05252c4a2d4e 430 {
lupomic 39:4c5e4ff386da 431 ToNextFunction =4;
lupomic 37:05252c4a2d4e 432 }
lupomic 37:05252c4a2d4e 433 }