Prototyp V2
Dependencies: PM2_Libary
Diff: main.cpp
- Branch:
- michi
- Revision:
- 41:4a4978d1a578
- Parent:
- 40:e32c57763d92
- Child:
- 42:6e7ab1136354
--- a/main.cpp Wed Apr 13 09:10:19 2022 +0200 +++ b/main.cpp Mon Apr 18 11:28:04 2022 +0200 @@ -1,9 +1,24 @@ #include "mbed.h" #include "PM2_Libary.h" +#include <cmath> #include <cstdint> +#include <cstdio> +#include "math.h" -// logical variable main task -bool do_execute_main_task = false; // this variable will be toggled via the user button (blue button) to or not to execute the main task +//******************************************************************************************************************************************************************* +// Defined Variables in mm coming from Hardware-team. Need to be updated +float wheel_diameter = 30; // diameter of wheel with caterpillar to calculate mm per wheel turn (4) +float arm_length = 118.5; // lenght of arm from pivotpoint to pivotpoint (3) +float dist_arm_attach_distsensor = 20; // distance between pivot point arm on body to start distancesensor on top in horizontal (6) +float dist_distsensors = 200; // distance between the two distancesensors on top of Wall-E (9) +float dist_arm_ground = 51; // distance between pivotpoint arm and ground (5) +float gripper_area_height = 16 ; // Height of Grappler cutout to grapple Stair (8) +float dist_grappleratt_grappler_uk = 33; // distance between pivotpoint Grappler and bottom edge (?) + +float height_stairs = 100; // height to top of stairs in mm + +//*********************************************************************************************************************************************************** +// declaration of Input - Output pins // user button on nucleo board 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) @@ -11,18 +26,12 @@ void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below void user_button_released_fcn(); -// while loop gets executed every main_task_period_ms milliseconds -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 -Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms - // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor float ir_distance_mV = 0.0f; // define variable to store measurement from infrared distancesensor in mVolt AnalogIn ir_analog_in(PC_2); // 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 //motor pin declaration FastPWM pwm_M_right(PB_13); @@ -33,13 +42,15 @@ EncoderCounter encoder_M_right(PA_6, PC_7); //encoder pin decalaration for wheels right side EncoderCounter encoder_M_left(PB_6, PB_7); //encoder pin decalaration for wheels left side EncoderCounter encoder_M_arm(PA_0, PA_1); //encoder pin decalaration for arm +//*********************************************************************************************************************************************************** +// Hardware controll functions and setup // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack -float counts_per_turn_wheels = 2000.0f * 100.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels -float counts_per_turn_arm = 2000.0f * 100.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm +float counts_per_turn_wheels = 20.0f * 78.125f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels +float counts_per_turn_arm = 20.0f * 78.125f * 10.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm float kn = 180.0f / 12.0f; // define motor constant in rpm per V -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 256'000 turns for 360°) +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°) float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 //motors for tracks @@ -48,28 +59,46 @@ //Arm Motor 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 -//float max_speed_rps = 0.5f; not sure if needed // define maximum speed that the position controller is changig the speed, has to be smaller or equal to kn * max_voltage // PositionController positionController_M3(counts_per_turn, kn, max_voltage, pwm_M3, encoder_M3); // default 78.125:1 gear with default contoller parameters //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 +// LSM9DS1 IMU, carefull: not all PES boards have an imu (chip shortage) +// LSM9DS1 imu(PC_9, PA_8); // create LSM9DS1 comunication object, if you want to be able to use the imu you need to #include "LSM9DS1_i2c.h" +//*********************************************************************************************************************************************************** //Platzhalter Variabeln für die Positionierung float PositionStair = 0.2; float PositionBackOff = -0.5; -float degArmStart = 0.5; +float degArmStart = 0.0; float degArmLift = -0.5; -int ToNextFunction = 0; -float max_speed_rps = 0.5f; +int ToNextFunction = 0; // current state of the system (which function is beeing executed) +float max_speed_rps = 0.5f; // define maximum speed that the position controller is changig the speed, has to be smaller or equal to kn * max_voltage -int StartPosition(float deg){ +int StartPosition(float deg) +{ positionController_M_Arm.setDesiredRotation(deg); return NULL; } +// calculates the deg which the arm has to take to reach a certain height (the input height will be the height of OK Gripper area) +double calc_arm_deg_for_height(int height) +{ + if ((height - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height)) > arm_length) + { + printf("Error in calc_arm_deg_for_height: desireed height is bigger than Wall-E arm lenght."); // error message when desired height is not reachable. + } + float height_arm = height - dist_arm_ground - (dist_grappleratt_grappler_uk - gripper_area_height); + double deg_arm_rad = asin(height / arm_length); // deg in radians + double pi = 2 * acos(0.0); // definiton of pi + double deg_arm = deg_arm_rad * 180/pi; // deg in degrees + return deg_arm; +} //Drives forward into the next step -int Drive(float dist){ +// calculatioin of acctual distance with wheels is needed +int Drive(float dist) +{ float distance; distance=dist; positionController_M_right.setDesiredRotation(distance,max_speed_rps); @@ -79,7 +108,8 @@ //only turns the arm until the robot is on the next step //not yet clear if the motor controler function drives to a absolute poition or if it drives the given distance relative to the current position -int LiftUp(float deg){ +int LiftUp(float deg) +{ int8_t i = 0; //prov condition variable positionController_M_Arm.setDesiredRotation(deg); return 0; @@ -87,36 +117,53 @@ //pow function is here so we dont have to use the math.h library //it takes 2 arguments the base can be any negative or positive floating point number the power has to be a hos to be an "integer" defined as a double -double powerx(double base, double pow2){ +double powerx(double base, double pow2) +{ double result = -1; double power = pow2; double basis = base; result = 1; //handling negative exponents - if(power<0){ - for(double i=1; i<=(power*(-1.0)); i++) { + if(power<0) + { + for(double i=1; i<=(power*(-1.0)); i++) + { result *= basis; } result = 1.0/result; } //handling positive exponents - else{ - for(double i=1; i<=power; i++){ - result *= basis;}} + else + { + for(double i=1; i<=power; i++) + { + result *= basis; + } + } return result; - } +} -double mapping(float adc_value_mV){ +double mapping(float adc_value_mV) +{ double distance = 0.0f; //distance in mm double infY =360 , supY = 2360; //Window for sensor values double voltage_mV = adc_value_mV; double p1 = -1.127*powerx(10,-14), p2 = 8.881*powerx(10,-11), p3 = -2.76*powerx(10,-7), p4 = 0.0004262, p5 = -0.3363, p6 = 120.1 ; //faktoren für polynomkurve -> von matlab exportiert - if(voltage_mV > infY && voltage_mV < supY){ + if(voltage_mV > infY && voltage_mV < supY) + { distance = p1*powerx(voltage_mV,5) + p2*powerx(voltage_mV,4) + p3*powerx(voltage_mV,3) + p4*powerx(voltage_mV,2) + p5*voltage_mV + p6; } return (distance); } +// logical variable main task +bool do_execute_main_task = false; // this variable will be toggled via the user button (blue button) to or not to execute the main task + +// while loop gets executed every main_task_period_ms milliseconds +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 +Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms + + int main(void) { @@ -126,13 +173,13 @@ - while (true){ + while (true) + { enable_motors = 1; - ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f; - - switch (ToNextFunction) { + switch (ToNextFunction) + { case 1: StartPosition(degArmStart); printf("Case 1: Position ARM (rot): %3.3f\n",positionController_M_Arm.getRotation()); // ToNextFunction+=1;