Prototyp V2
Dependencies: PM2_Libary
Diff: main.cpp
- Branch:
- michi
- Revision:
- 93:4661e91fdd50
- Parent:
- 92:06c871d9a6ad
- Parent:
- 88:cb8a18fc0391
- Child:
- 94:7cf17f2eaa28
--- a/main.cpp Mon May 02 15:33:11 2022 +0200 +++ b/main.cpp Tue May 03 18:39:55 2022 +0200 @@ -33,8 +33,8 @@ AnalogIn ir_analog_in_Distance_R(PC_3); AnalogIn ir_analog_in_Lookdown_B(PC_5); AnalogIn ir_analog_in_Lookdown_F(PB_1); +// create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 -// create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors @@ -74,6 +74,7 @@ const int drive_straight_mm = 200; // placeholder for testing drives amount forward const int drive_back_mm = -200; // placeholder for testing drives amount backwards int ToNextFunction = 0; // current state of the system (which function is beeing executed) +int state = 0; // definition variables for calculations const float pi = 2 * acos(0.0); // definiton of pi @@ -135,6 +136,17 @@ float new_partial_rotation = full_rotations - start_deg_arm/360 + end_deg/360; return new_partial_rotation; } + +//calculates position of arm when lift up has ended. +//RETURN: end_deg = degree which the motor has to turn in order to reach end lift position. +float calc_pos_end_lift() +{ + float end_deg; + end_deg = asin((dist_arm_ground-(dist_grappleratt_grappler_uk - dist_arm_attach_OK_griparea))/arm_length) + start_deg_arm; + end_deg = end_deg * 180 / pi; + return end_deg; +} + //*********************************************************************************************************************************************************** // important calculatet constant for Wall-E const double deg_up_from_horizon_to_stair = calc_arm_deg_for_height(height_stairs); @@ -157,19 +169,36 @@ } // bring arm in starting position. Height of stairs. -void set_arm_stair_height() + +int set_arm_stair_height() { float deg = deg_up_from_horizon_to_stair + start_deg_arm; + enable_motors = 1; + float diff; + 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 + if ((0.0 > deg) || (deg > 360.0)) + { + printf("Error in start_position: degree is out of bound for Start Position."); // error when desired reaching point is out of reach. + return 2; + } enable_motors = 1; positionController_M_Arm.setDesiredRotation(deg / 360.0, max_speed_rps_arm); // command to turn motor to desired deg. + diff = deg-(positionController_M_Arm.getRotation() * 360.0); + if (diff<=0.3){ + return 1; + } + else { + return NULL;} enable_motors = 0; } //Drives forward into the next step //Prameter:distance in milimeter -void drive_straight(float distance) +int drive_straight(float distance) { + float diff_R; + float diff_L; float deg_to_turn = wheel_dist_to_deg(distance); float relativ_turns_rightmotor = turn_relative_deg(deg_to_turn, positionController_M_right.getRotation()); @@ -179,22 +208,39 @@ positionController_M_right.setDesiredRotation(relativ_turns_rightmotor, max_speed_rps_wheel); positionController_M_left.setDesiredRotation(relativ_turns_leftmotor, max_speed_rps_wheel); enable_motors = 0; + + + diff_R= relativ_turns_rightmotor-positionController_M_right.getRotation(); + diff_L= relativ_turns_leftmotor-positionController_M_left.getRotation(); + if ((diff_R<=0.3) && (diff_L<=0.3)) + { + return 1; + } + else + { + return 0; + } + } //turns the arm until the robot is on the next step void lift_up() { + float diff; float absolut_pos_arm = turn_absolut_deg(end_pos_lift_deg, positionController_M_Arm.getRotation()-1); + float position_lift_end_deg = asin((-dist_arm_ground - (dist_grappleratt_grappler_uk )) / arm_length) - 90; // calculates the degree which has to be reached in order to get on top of next step + float relativ_turns_arm = turn_absolut_deg(position_lift_end_deg, positionController_M_Arm.getRotation()); enable_motors = 1; - positionController_M_Arm.setDesiredRotation(absolut_pos_arm, max_speed_rps_arm); + positionController_M_Arm.setDesiredRotation(relativ_turns_arm , max_speed_rps_arm); enable_motors = 0; + } //function to climbe one step up and get in to position to drive in to the next step //PARAM: None //RETURN: None -void lift_sequenze() +int lift_sequenze() { lift_up(); if ((StepDetection_down(ir_analog_in_Lookdown_B) == 1) && (StepDetection_down(ir_analog_in_Lookdown_F) == 1)) @@ -203,12 +249,39 @@ drive_straight(-grip_area_depth - 2); } set_arm_stair_height(); + + positionController_M_Arm.setDesiredRotation(0, max_speed_rps_arm); + + float diff=relativ_turns_arm-positionController_M_Arm.getRotation(); + if(diff<=0.3) + { return 1; + } + else + { return 0; + } + + } //*********************************************************************************************************************************************************** -//Function which checks if sensors and motors have been wired correctly and the expectet results will happen. otherwise Wall-E will ???. -void check_start() -{ +//Function which checks if sensors and motors have been wired correctly and the expectet results will happen. otherwise Wall-E will show with armmovement. +int check_start(); +int NextStep (float){ + return 1; +} + +// +//simple check if there is an object in proximity +//returns 0 if there is NO object present +//returns 1 if there is an object present +//returns 2 if the distance isn't in the expected range + +uint8_t StepDetection(double distance){ + double d_valueMM = distance; + if(d_valueMM >= 4) return 0; + if(d_valueMM < 4) return 1; + if(d_valueMM <= 0 || d_valueMM > 100 ) return 2; + else return 2; } @@ -223,21 +296,38 @@ user_button.fall(&user_button_pressed_fcn); user_button.rise(&user_button_released_fcn); + while (true) { + + ir_distance_mm_L= mapping(ir_analog_in_Distance_L.read()*1.0e3f * 3.3f); + ir_distance_mm_R= mapping(ir_analog_in_Distance_R.read()*1.0e3f * 3.3f); + float ir_distance_mm_Lookdown_B= mapping(ir_analog_in_Lookdown_B.read()*1.0e3f * 3.3f); + float ir_distance_mm_Lookdown_F= mapping(ir_analog_in_Lookdown_F.read()*1.0e3f * 3.3f); + + + switch (ToNextFunction) { - case 1: + set_arm_stair_height(); + + printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation()); + if (state==1){ + ToNextFunction += 1; + } break; case 2: drive_straight(drive_straight_mm); + printf("Case 2: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n", + positionController_M_right.getRotation(),positionController_M_left.getRotation()); break; case 3: lift_sequenze(); + printf("Case 3: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation()); break; case 4: @@ -245,7 +335,17 @@ ToNextFunction = 0; break; - default: ; + case 5: + state=drive_straight(drive_back_mm); + printf("Case 4: Position Right(rot): %3.3f; Position Left (rot): %3.3f\n", + positionController_M_right.getRotation(),positionController_M_left.getRotation()); + if ((state==1)&&(StepDetection(ir_distance_mm_Lookdown_B)!=1)){ + ToNextFunction += 1; + } + break; + + + default: ; } } // read timer and make the main thread sleep for the remaining time span (non blocking) @@ -255,6 +355,7 @@ } + void user_button_pressed_fcn() { user_button_timer.start(); @@ -268,6 +369,6 @@ user_button_timer.stop(); if (user_button_elapsed_time_ms > 200) { - ToNextFunction += 1; + ToNextFunction = 3; } } \ No newline at end of file