Example project for the Line Follower robot.
Dependencies: PM2_Libary Eigen
main.cpp@38:6d11788e14c0, 2022-05-05 (annotated)
- Committer:
- pmic
- Date:
- Thu May 05 18:01:37 2022 +0200
- Revision:
- 38:6d11788e14c0
- Parent:
- 34:702246639f02
- Child:
- 40:eb7f8dce5787
Development ongoing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 33:cff70742569d | 1 | #include <mbed.h> |
pmic | 33:cff70742569d | 2 | #include <math.h> |
pmic | 33:cff70742569d | 3 | |
pmic | 17:c19b471f05cb | 4 | #include "PM2_Libary.h" |
pmic | 6:e1fa1a2d7483 | 5 | |
pmic | 34:702246639f02 | 6 | # define M_PI 3.14159265358979323846 // number pi |
pmic | 33:cff70742569d | 7 | |
pmic | 24:86f1a63e35a0 | 8 | // logical variable main task |
pmic | 24:86f1a63e35a0 | 9 | 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 | 10 | |
pmic | 24:86f1a63e35a0 | 11 | // user button on nucleo board |
pmic | 24:86f1a63e35a0 | 12 | 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 | 13 | 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 | 14 | void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below |
pmic | 24:86f1a63e35a0 | 15 | void user_button_released_fcn(); |
pmic | 6:e1fa1a2d7483 | 16 | |
pmic | 24:86f1a63e35a0 | 17 | // while loop gets executed every main_task_period_ms milliseconds |
pmic | 34:702246639f02 | 18 | int main_task_period_ms = 10; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second |
pmic | 24:86f1a63e35a0 | 19 | 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 | 20 | |
pmic | 24:86f1a63e35a0 | 21 | // led on nucleo board |
pmic | 24:86f1a63e35a0 | 22 | DigitalOut user_led(LED1); // create DigitalOut object to command user led |
pmic | 17:c19b471f05cb | 23 | |
pmic | 38:6d11788e14c0 | 24 | // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor |
pmic | 38:6d11788e14c0 | 25 | float ir_distance_mV = 0.0f; // define variable to store measurement |
pmic | 38:6d11788e14c0 | 26 | AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 |
pmic | 38:6d11788e14c0 | 27 | |
pmic | 38:6d11788e14c0 | 28 | // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB |
pmic | 38:6d11788e14c0 | 29 | DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors |
pmic | 38:6d11788e14c0 | 30 | |
pmic | 38:6d11788e14c0 | 31 | float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motors |
pmic | 38:6d11788e14c0 | 32 | FastPWM pwm_M1(PB_13); // motor M1 is closed-loop speed controlled (angle velocity) |
pmic | 38:6d11788e14c0 | 33 | FastPWM pwm_M2(PA_9); // motor M2 is closed-loop speed controlled (angle velocity) |
pmic | 38:6d11788e14c0 | 34 | |
pmic | 38:6d11788e14c0 | 35 | EncoderCounter encoder_M1(PA_6, PC_7); // create encoder objects to read in the encoder counter values |
pmic | 38:6d11788e14c0 | 36 | EncoderCounter encoder_M2(PB_6, PB_7); |
pmic | 38:6d11788e14c0 | 37 | |
pmic | 38:6d11788e14c0 | 38 | // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box |
pmic | 38:6d11788e14c0 | 39 | float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack |
pmic | 38:6d11788e14c0 | 40 | float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio |
pmic | 38:6d11788e14c0 | 41 | float kn = 180.0f / 12.0f; // define motor constant in rpm per V |
pmic | 38:6d11788e14c0 | 42 | 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 | 38:6d11788e14c0 | 43 | float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 |
pmic | 38:6d11788e14c0 | 44 | |
pmic | 38:6d11788e14c0 | 45 | SpeedController speedController_M1(counts_per_turn, kn, max_voltage, pwm_M1, encoder_M1); // default 78.125:1 gear box with default contoller parameters |
pmic | 38:6d11788e14c0 | 46 | SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear box with default contoller parameters |
pmic | 38:6d11788e14c0 | 47 | // SpeedController speedController_M2(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M2, encoder_M2); // parameters adjusted to 100:1 gear |
pmic | 38:6d11788e14c0 | 48 | |
pmic | 38:6d11788e14c0 | 49 | // sparkfun line follower array |
pmic | 33:cff70742569d | 50 | I2C i2c(PB_9, PB_8); // I2C (PinName sda, PinName scl) |
pmic | 33:cff70742569d | 51 | SensorBar sensor_bar(i2c, 0.1175f); |
pmic | 20:7e7325edcf5c | 52 | |
pmic | 38:6d11788e14c0 | 53 | float r_wheel = 0.0358f / 2.0f; |
pmic | 38:6d11788e14c0 | 54 | float L_wheel = 0.143f; |
pmic | 38:6d11788e14c0 | 55 | // transform wheel to robot |
pmic | 38:6d11788e14c0 | 56 | float Crw[2][2] = {{r_wheel / 2.0f, r_wheel / 2.0f}, {r_wheel / L_wheel, -r_wheel / L_wheel}}; |
pmic | 38:6d11788e14c0 | 57 | // transform robot to wheel |
pmic | 38:6d11788e14c0 | 58 | float Cwr[2][2] = {{1.0f / r_wheel, L_wheel / (2.0f * r_wheel)}, {1.0f / r_wheel, -L_wheel / (2.0f * r_wheel)}}; |
pmic | 38:6d11788e14c0 | 59 | // float Test = Crw[0][] |
pmic | 38:6d11788e14c0 | 60 | |
pmic | 1:93d997d6b232 | 61 | int main() |
pmic | 23:26b3a25fc637 | 62 | { |
pmic | 24:86f1a63e35a0 | 63 | // attach button fall and rise functions to user button object |
pmic | 24:86f1a63e35a0 | 64 | user_button.fall(&user_button_pressed_fcn); |
pmic | 24:86f1a63e35a0 | 65 | user_button.rise(&user_button_released_fcn); |
pmic | 17:c19b471f05cb | 66 | |
pmic | 29:d6f1ccf42a31 | 67 | // start timer |
pmic | 24:86f1a63e35a0 | 68 | main_task_timer.start(); |
pmic | 6:e1fa1a2d7483 | 69 | |
pmic | 38:6d11788e14c0 | 70 | // enable hardwaredriver dc motors: 0 -> disabled, 1 -> enabled |
pmic | 38:6d11788e14c0 | 71 | enable_motors = 1; |
pmic | 6:e1fa1a2d7483 | 72 | |
pmic | 24:86f1a63e35a0 | 73 | while (true) { // this loop will run forever |
pmic | 6:e1fa1a2d7483 | 74 | |
pmic | 24:86f1a63e35a0 | 75 | main_task_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 76 | |
pmic | 24:86f1a63e35a0 | 77 | if (do_execute_main_task) { |
pmic | 34:702246639f02 | 78 | |
pmic | 38:6d11788e14c0 | 79 | // read analog input |
pmic | 38:6d11788e14c0 | 80 | ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f; |
pmic | 38:6d11788e14c0 | 81 | |
pmic | 38:6d11788e14c0 | 82 | speedController_M1.setDesiredSpeedRPS(0.5f); // set a desired speed for speed controlled dc motors M2 |
pmic | 38:6d11788e14c0 | 83 | speedController_M2.setDesiredSpeedRPS(0.5f); // set a desired speed for speed controlled dc motors M2 |
pmic | 38:6d11788e14c0 | 84 | |
pmic | 34:702246639f02 | 85 | /* |
pmic | 34:702246639f02 | 86 | uint8_t sensor_bar_raw_value = sensor_bar.getRaw(); |
pmic | 34:702246639f02 | 87 | for( int i = 7; i >= 0; i-- ) { |
pmic | 34:702246639f02 | 88 | printf("%d", (sensor_bar_raw_value >> i) & 0x01); |
pmic | 34:702246639f02 | 89 | } |
pmic | 34:702246639f02 | 90 | printf(", "); |
pmic | 34:702246639f02 | 91 | */ |
pmic | 34:702246639f02 | 92 | |
pmic | 34:702246639f02 | 93 | int8_t sensor_bar_binaryPosition = sensor_bar.getBinaryPosition(); |
pmic | 34:702246639f02 | 94 | printf("%d, ", sensor_bar_binaryPosition); |
pmic | 34:702246639f02 | 95 | |
pmic | 34:702246639f02 | 96 | uint8_t sensor_bar_nrOfLedsActive = sensor_bar.getNrOfLedsActive(); |
pmic | 34:702246639f02 | 97 | printf("%d, ", sensor_bar_nrOfLedsActive); |
pmic | 34:702246639f02 | 98 | |
pmic | 34:702246639f02 | 99 | float sensor_bar_angleRad = 0.0f; |
pmic | 34:702246639f02 | 100 | float sensor_bar_avgAngleRad = 0.0f; |
pmic | 34:702246639f02 | 101 | if (sensor_bar.isAnyLedActive()) { |
pmic | 34:702246639f02 | 102 | sensor_bar_angleRad = sensor_bar.getAngleRad(); |
pmic | 34:702246639f02 | 103 | sensor_bar_avgAngleRad = sensor_bar.getAvgAngleRad(); |
pmic | 34:702246639f02 | 104 | } |
pmic | 34:702246639f02 | 105 | printf("%f, ", sensor_bar_angleRad * 180.0f / M_PI); |
pmic | 34:702246639f02 | 106 | printf("%f\r\n", sensor_bar_avgAngleRad * 180.0f / M_PI); |
pmic | 34:702246639f02 | 107 | |
pmic | 1:93d997d6b232 | 108 | } else { |
pmic | 6:e1fa1a2d7483 | 109 | |
pmic | 38:6d11788e14c0 | 110 | ir_distance_mV = 0.0f; |
pmic | 38:6d11788e14c0 | 111 | |
pmic | 38:6d11788e14c0 | 112 | speedController_M1.setDesiredSpeedRPS(0.0f); |
pmic | 38:6d11788e14c0 | 113 | speedController_M2.setDesiredSpeedRPS(0.0f); |
pmic | 33:cff70742569d | 114 | } |
pmic | 6:e1fa1a2d7483 | 115 | |
pmic | 24:86f1a63e35a0 | 116 | user_led = !user_led; |
pmic | 24:86f1a63e35a0 | 117 | |
pmic | 24:86f1a63e35a0 | 118 | // do only output via serial what's really necessary (this makes your code slow) |
pmic | 33:cff70742569d | 119 | // printf("%d, %d\r\n", sensor_bar_raw_value_time_ms, sensor_bar_position_time_ms); |
pmic | 17:c19b471f05cb | 120 | |
pmic | 24:86f1a63e35a0 | 121 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
pmic | 24:86f1a63e35a0 | 122 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
pmic | 24:86f1a63e35a0 | 123 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
pmic | 1:93d997d6b232 | 124 | } |
pmic | 1:93d997d6b232 | 125 | } |
pmic | 6:e1fa1a2d7483 | 126 | |
pmic | 24:86f1a63e35a0 | 127 | void user_button_pressed_fcn() |
pmic | 25:ea1d6e27c895 | 128 | { |
pmic | 26:28693b369945 | 129 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 130 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 131 | } |
pmic | 6:e1fa1a2d7483 | 132 | |
pmic | 24:86f1a63e35a0 | 133 | void user_button_released_fcn() |
pmic | 6:e1fa1a2d7483 | 134 | { |
pmic | 24:86f1a63e35a0 | 135 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 24:86f1a63e35a0 | 136 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 137 | user_button_timer.stop(); |
pmic | 24:86f1a63e35a0 | 138 | if (user_button_elapsed_time_ms > 200) { |
pmic | 24:86f1a63e35a0 | 139 | do_execute_main_task = !do_execute_main_task; |
pmic | 8:9bb806a7f585 | 140 | } |
pmic | 6:e1fa1a2d7483 | 141 | } |