Prototyp V2
Dependencies: PM2_Libary
main.cpp@33:70ea029a69e8, 2022-04-06 (annotated)
- Committer:
- lupomic
- Date:
- Wed Apr 06 12:38:20 2022 +0200
- Revision:
- 33:70ea029a69e8
- Parent:
- 32:bf35aeffc374
- Child:
- 34:9f779e91168e
basic functions
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 | 31:24081337c9ed | 4 | |
pmic | 24:86f1a63e35a0 | 5 | // logical variable main task |
pmic | 24:86f1a63e35a0 | 6 | 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 |
pmic | 17:c19b471f05cb | 7 | |
pmic | 24:86f1a63e35a0 | 8 | // user button on nucleo board |
pmic | 24:86f1a63e35a0 | 9 | 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 | 10 | 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 | 11 | void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below |
pmic | 24:86f1a63e35a0 | 12 | void user_button_released_fcn(); |
pmic | 6:e1fa1a2d7483 | 13 | |
pmic | 24:86f1a63e35a0 | 14 | // while loop gets executed every main_task_period_ms milliseconds |
pmic | 24:86f1a63e35a0 | 15 | int main_task_period_ms = 50; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second |
pmic | 24:86f1a63e35a0 | 16 | Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms |
pmic | 6:e1fa1a2d7483 | 17 | |
pmic | 24:86f1a63e35a0 | 18 | // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor |
pmic | 24:86f1a63e35a0 | 19 | float ir_distance_mV = 0.0f; // define variable to store measurement |
pmic | 24:86f1a63e35a0 | 20 | AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 |
pmic | 6:e1fa1a2d7483 | 21 | |
lupomic | 33:70ea029a69e8 | 22 | |
lupomic | 33:70ea029a69e8 | 23 | |
pmic | 24:86f1a63e35a0 | 24 | // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB |
pmic | 24:86f1a63e35a0 | 25 | DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors |
pmic | 17:c19b471f05cb | 26 | |
pmic | 24:86f1a63e35a0 | 27 | float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors |
lupomic | 33:70ea029a69e8 | 28 | //motor pin declaration |
lupomic | 33:70ea029a69e8 | 29 | FastPWM pwm_M_right(PA_9); |
lupomic | 33:70ea029a69e8 | 30 | FastPWM pwm_M_left(PB_13); |
lupomic | 33:70ea029a69e8 | 31 | FastPWM pwm_M_arm(PA_10); |
pmic | 17:c19b471f05cb | 32 | |
lupomic | 33:70ea029a69e8 | 33 | //Encoder pin declaration |
lupomic | 33:70ea029a69e8 | 34 | EncoderCounter encoder_M_right(PA_6, PC_7); //encoder pin decalaration for wheels right side |
lupomic | 33:70ea029a69e8 | 35 | EncoderCounter encoder_M_left(PB_6, PB_7); //encoder pin decalaration for wheels left side |
lupomic | 33:70ea029a69e8 | 36 | EncoderCounter encoder_M_arm(PA_0, PA_1); //encoder pin decalaration for arm |
pmic | 17:c19b471f05cb | 37 | |
pmic | 30:1e8295770bc1 | 38 | // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box |
pmic | 24:86f1a63e35a0 | 39 | float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack |
lupomic | 33:70ea029a69e8 | 40 | float counts_per_turn_wheels = 2000.0f * 100.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for wheels |
lupomic | 33:70ea029a69e8 | 41 | float counts_per_turn_arm = 40000.0f * 100.0f; // define counts per turn at gearbox end (counts/turn * gearratio) for arm |
pmic | 25:ea1d6e27c895 | 42 | float kn = 180.0f / 12.0f; // define motor constant in rpm per V |
pmic | 30:1e8295770bc1 | 43 | 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 |
pmic | 30:1e8295770bc1 | 44 | float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 |
pmic | 6:e1fa1a2d7483 | 45 | |
lupomic | 33:70ea029a69e8 | 46 | //motors for tracks |
lupomic | 33:70ea029a69e8 | 47 | 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 | 48 | 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 | 49 | //Arm Motor |
lupomic | 33:70ea029a69e8 | 50 | 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 | 51 | |
lupomic | 33:70ea029a69e8 | 52 | //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 |
lupomic | 33:70ea029a69e8 | 53 | // 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 | 54 | //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 |
pmic | 17:c19b471f05cb | 55 | |
pmic | 17:c19b471f05cb | 56 | |
pmic | 24:86f1a63e35a0 | 57 | // LSM9DS1 IMU, carefull: not all PES boards have an imu (chip shortage) |
pmic | 25:ea1d6e27c895 | 58 | // 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" |
pmic | 20:7e7325edcf5c | 59 | |
lupomic | 33:70ea029a69e8 | 60 | //Platzhalter Variabeln für die Positionierung |
lupomic | 33:70ea029a69e8 | 61 | int16_t PositionStair = 20; |
lupomic | 33:70ea029a69e8 | 62 | int16_t PositionBackOff = 100; |
lupomic | 33:70ea029a69e8 | 63 | int16_t degArmStart = 40; |
lupomic | 33:70ea029a69e8 | 64 | int16_t degArmLift = 18; |
lupomic | 33:70ea029a69e8 | 65 | int ToNextFunction = 1; |
lupomic | 33:70ea029a69e8 | 66 | |
lupomic | 33:70ea029a69e8 | 67 | int StartPosition(int16_t deg){ |
lupomic | 33:70ea029a69e8 | 68 | |
lupomic | 33:70ea029a69e8 | 69 | positionController_M_Arm.setDesiredRotation(deg); |
lupomic | 33:70ea029a69e8 | 70 | |
lupomic | 33:70ea029a69e8 | 71 | return NULL; |
lupomic | 33:70ea029a69e8 | 72 | } |
lupomic | 33:70ea029a69e8 | 73 | //Drives forward into the next step |
lupomic | 33:70ea029a69e8 | 74 | int Drive(int16_t dist){ |
lupomic | 33:70ea029a69e8 | 75 | int8_t i = 0; //prov condition variable |
lupomic | 33:70ea029a69e8 | 76 | |
lupomic | 33:70ea029a69e8 | 77 | int8_t distance = dist; //distance which will be driven in [mm] |
lupomic | 33:70ea029a69e8 | 78 | float factor = 1.0f; //factor for calculating the value in to the float which will be given to the setDesiredRotation function |
lupomic | 33:70ea029a69e8 | 79 | float distanceValue = float(distance)*factor; |
lupomic | 33:70ea029a69e8 | 80 | |
lupomic | 33:70ea029a69e8 | 81 | positionController_M_right.setDesiredRotation(distanceValue); |
lupomic | 33:70ea029a69e8 | 82 | positionController_M_left.setDesiredRotation(distanceValue); |
lupomic | 33:70ea029a69e8 | 83 | |
lupomic | 33:70ea029a69e8 | 84 | return 0; |
lupomic | 33:70ea029a69e8 | 85 | } |
lupomic | 33:70ea029a69e8 | 86 | |
lupomic | 33:70ea029a69e8 | 87 | //only turns the arm until the robot is on the next step |
lupomic | 33:70ea029a69e8 | 88 | //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 |
lupomic | 33:70ea029a69e8 | 89 | int LiftUp(int16_t deg){ |
lupomic | 33:70ea029a69e8 | 90 | int8_t rotation = deg; |
lupomic | 33:70ea029a69e8 | 91 | int8_t i = 0; //prov condition variable |
lupomic | 33:70ea029a69e8 | 92 | |
lupomic | 33:70ea029a69e8 | 93 | positionController_M_Arm.setDesiredRotation(deg); |
lupomic | 33:70ea029a69e8 | 94 | |
lupomic | 33:70ea029a69e8 | 95 | |
lupomic | 33:70ea029a69e8 | 96 | return 0; |
lupomic | 33:70ea029a69e8 | 97 | } |
lupomic | 33:70ea029a69e8 | 98 | //pow function is here so we dont have to use the math.h library |
lupomic | 33:70ea029a69e8 | 99 | //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 |
lupomic | 33:70ea029a69e8 | 100 | double powerx(double base, double pow2){ |
lupomic | 33:70ea029a69e8 | 101 | double result = -1; |
lupomic | 33:70ea029a69e8 | 102 | double power = pow2; |
lupomic | 33:70ea029a69e8 | 103 | double basis = base; |
lupomic | 33:70ea029a69e8 | 104 | result = 1; |
lupomic | 33:70ea029a69e8 | 105 | //handling negative exponents |
lupomic | 33:70ea029a69e8 | 106 | if(power<0){ |
lupomic | 33:70ea029a69e8 | 107 | for(double i=1; i<=(power*(-1.0)); i++) { |
lupomic | 33:70ea029a69e8 | 108 | result *= basis; |
lupomic | 33:70ea029a69e8 | 109 | } |
lupomic | 33:70ea029a69e8 | 110 | result = 1.0/result; |
lupomic | 33:70ea029a69e8 | 111 | } |
lupomic | 33:70ea029a69e8 | 112 | //handling positive exponents |
lupomic | 33:70ea029a69e8 | 113 | else{ |
lupomic | 33:70ea029a69e8 | 114 | for(double i=1; i<=power; i++){ |
lupomic | 33:70ea029a69e8 | 115 | result *= basis;}} |
lupomic | 33:70ea029a69e8 | 116 | |
lupomic | 33:70ea029a69e8 | 117 | return result; |
lupomic | 33:70ea029a69e8 | 118 | } |
lupomic | 33:70ea029a69e8 | 119 | |
lupomic | 33:70ea029a69e8 | 120 | double mapping(float adc_value_mV){ |
lupomic | 33:70ea029a69e8 | 121 | double distance = 0.0f; //distance in mm |
lupomic | 33:70ea029a69e8 | 122 | double infY =360 , supY = 2360; //Window for sensor values |
lupomic | 33:70ea029a69e8 | 123 | double voltage_mV = adc_value_mV; |
lupomic | 33:70ea029a69e8 | 124 | 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 |
lupomic | 33:70ea029a69e8 | 125 | if(voltage_mV > infY && voltage_mV < supY){ |
lupomic | 33:70ea029a69e8 | 126 | 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; |
lupomic | 33:70ea029a69e8 | 127 | } |
lupomic | 33:70ea029a69e8 | 128 | return (distance); |
lupomic | 33:70ea029a69e8 | 129 | } |
lupomic | 33:70ea029a69e8 | 130 | |
lupomic | 33:70ea029a69e8 | 131 | |
lupomic | 33:70ea029a69e8 | 132 | int main(void) |
pmic | 23:26b3a25fc637 | 133 | { |
pmic | 24:86f1a63e35a0 | 134 | // attach button fall and rise functions to user button object |
lupomic | 33:70ea029a69e8 | 135 | user_button.fall(&user_button_pressed_fcn); |
lupomic | 33:70ea029a69e8 | 136 | user_button.rise(&user_button_released_fcn); |
lupomic | 33:70ea029a69e8 | 137 | |
pmic | 24:86f1a63e35a0 | 138 | |
pmic | 6:e1fa1a2d7483 | 139 | |
lupomic | 33:70ea029a69e8 | 140 | while (true){ |
lupomic | 33:70ea029a69e8 | 141 | ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f; |
lupomic | 33:70ea029a69e8 | 142 | |
lupomic | 33:70ea029a69e8 | 143 | printf("test pow function 2 ^ 2 %lf\n",powerx(2,2)); |
lupomic | 33:70ea029a69e8 | 144 | printf("test mapping function %f\n", mapping(ir_distance_mV)); |
pmic | 6:e1fa1a2d7483 | 145 | |
lupomic | 33:70ea029a69e8 | 146 | printf("IR sensor (mV): %3.3f\n", ir_distance_mV); |
lupomic | 33:70ea029a69e8 | 147 | |
pmic | 6:e1fa1a2d7483 | 148 | |
lupomic | 33:70ea029a69e8 | 149 | switch (ToNextFunction) { |
lupomic | 33:70ea029a69e8 | 150 | case 1: StartPosition(degArmStart); |
lupomic | 33:70ea029a69e8 | 151 | printf("a"); |
lupomic | 33:70ea029a69e8 | 152 | // ToNextFunction+=1; |
lupomic | 33:70ea029a69e8 | 153 | break; |
lupomic | 33:70ea029a69e8 | 154 | case 2: Drive(PositionStair); |
lupomic | 33:70ea029a69e8 | 155 | printf("b"); |
lupomic | 33:70ea029a69e8 | 156 | // ToNextFunction+=1; |
lupomic | 33:70ea029a69e8 | 157 | break; |
lupomic | 33:70ea029a69e8 | 158 | case 3:// LiftUp(degArmLift); |
lupomic | 33:70ea029a69e8 | 159 | // ToNextFunction+=1; |
lupomic | 33:70ea029a69e8 | 160 | printf("c"); |
lupomic | 33:70ea029a69e8 | 161 | break; |
lupomic | 33:70ea029a69e8 | 162 | case 4: Drive(PositionBackOff); |
lupomic | 33:70ea029a69e8 | 163 | printf("d"); |
lupomic | 33:70ea029a69e8 | 164 | // ToNextFunction+=1; |
lupomic | 33:70ea029a69e8 | 165 | break; |
lupomic | 33:70ea029a69e8 | 166 | case 5: LiftUp(degArmStart); |
lupomic | 33:70ea029a69e8 | 167 | printf("d"); |
lupomic | 33:70ea029a69e8 | 168 | // ToNextFunction = 0; |
lupomic | 33:70ea029a69e8 | 169 | break; |
lupomic | 33:70ea029a69e8 | 170 | } |
pmic | 6:e1fa1a2d7483 | 171 | |
lupomic | 33:70ea029a69e8 | 172 | |
lupomic | 33:70ea029a69e8 | 173 | |
lupomic | 33:70ea029a69e8 | 174 | } |
lupomic | 33:70ea029a69e8 | 175 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
pmic | 24:86f1a63e35a0 | 176 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
pmic | 24:86f1a63e35a0 | 177 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
lupomic | 33:70ea029a69e8 | 178 | return 0; |
pmic | 1:93d997d6b232 | 179 | } |
pmic | 6:e1fa1a2d7483 | 180 | |
lupomic | 33:70ea029a69e8 | 181 | |
pmic | 24:86f1a63e35a0 | 182 | void user_button_pressed_fcn() |
pmic | 25:ea1d6e27c895 | 183 | { |
pmic | 26:28693b369945 | 184 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 185 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 186 | } |
pmic | 6:e1fa1a2d7483 | 187 | |
lupomic | 33:70ea029a69e8 | 188 | void user_button_released_fcn() { |
pmic | 24:86f1a63e35a0 | 189 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 24:86f1a63e35a0 | 190 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 191 | user_button_timer.stop(); |
pmic | 24:86f1a63e35a0 | 192 | if (user_button_elapsed_time_ms > 200) { |
lupomic | 33:70ea029a69e8 | 193 | ToNextFunction += 1;} |
lupomic | 33:70ea029a69e8 | 194 | } |