Workshop 1
Dependencies: PM2_Libary Eigen
main.cpp@39:c6475c899b61, 2022-05-16 (annotated)
- Committer:
- pmic
- Date:
- Mon May 16 11:05:58 2022 +0200
- Revision:
- 39:c6475c899b61
- Parent:
- 38:8cf86a20f0fe
- Child:
- 40:7e6b7aec3947
Changed main period
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 36:8c75783c1eca | 1 | #include <mbed.h> |
pmic | 36:8c75783c1eca | 2 | |
pmic | 17:c19b471f05cb | 3 | #include "PM2_Libary.h" |
pmic | 36:8c75783c1eca | 4 | #include "Eigen/Dense.h" |
pmic | 36:8c75783c1eca | 5 | |
pmic | 36:8c75783c1eca | 6 | # define M_PI 3.14159265358979323846 // number pi |
pmic | 6:e1fa1a2d7483 | 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 | 1:93d997d6b232 | 17 | int main() |
pmic | 23:26b3a25fc637 | 18 | { |
pmic | 36:8c75783c1eca | 19 | // while loop gets executed every main_task_period_ms milliseconds |
pmic | 39:c6475c899b61 | 20 | const 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 | 36:8c75783c1eca | 21 | Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms |
pmic | 36:8c75783c1eca | 22 | |
pmic | 36:8c75783c1eca | 23 | // led on nucleo board |
pmic | 36:8c75783c1eca | 24 | DigitalOut user_led(LED1); // create DigitalOut object to command user led |
pmic | 36:8c75783c1eca | 25 | |
pmic | 36:8c75783c1eca | 26 | // additional Led |
pmic | 36:8c75783c1eca | 27 | DigitalOut extra_led(PB_9); // create DigitalOut object to command extra led (do add an aditional resistor, e.g. 220...500 Ohm) |
pmic | 36:8c75783c1eca | 28 | |
pmic | 36:8c75783c1eca | 29 | // mechanical button |
pmic | 36:8c75783c1eca | 30 | DigitalIn mechanical_button(PC_5); // create DigitalIn object to evaluate extra mechanical button, you need to specify the mode for proper usage, see below |
pmic | 36:8c75783c1eca | 31 | |
pmic | 36:8c75783c1eca | 32 | // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor |
pmic | 36:8c75783c1eca | 33 | float ir_distance_mV = 0.0f; // define variable to store measurement |
pmic | 36:8c75783c1eca | 34 | AnalogIn ir_analog_in(PC_2); // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 |
pmic | 36:8c75783c1eca | 35 | |
pmic | 36:8c75783c1eca | 36 | // 78:1, 100:1, ... Metal Gearmotor 20Dx44L mm 12V CB |
pmic | 36:8c75783c1eca | 37 | DigitalOut enable_motors(PB_15); // create DigitalOut object to enable dc motors |
pmic | 36:8c75783c1eca | 38 | |
pmic | 36:8c75783c1eca | 39 | const float pwm_period_s = 0.00005f; // define pwm period time in seconds and create FastPWM objects to command dc motor M1 |
pmic | 36:8c75783c1eca | 40 | FastPWM pwm_M1(PB_13); // motor M1 is used open loop |
pmic | 36:8c75783c1eca | 41 | FastPWM pwm_M2(PA_9); // motor M2 is closed-loop speed controlled (angle velocity) |
pmic | 36:8c75783c1eca | 42 | FastPWM pwm_M3(PA_10); // motor M3 is closed-loop position controlled (angle controlled) |
pmic | 36:8c75783c1eca | 43 | |
pmic | 36:8c75783c1eca | 44 | EncoderCounter encoder_M1(PA_6, PC_7); // create encoder objects to read in the encoder counter values |
pmic | 36:8c75783c1eca | 45 | EncoderCounter encoder_M2(PB_6, PB_7); |
pmic | 36:8c75783c1eca | 46 | EncoderCounter encoder_M3(PA_0, PA_1); |
pmic | 36:8c75783c1eca | 47 | |
pmic | 36:8c75783c1eca | 48 | // create SpeedController and PositionController objects, default parametrization is for 78.125:1 gear box |
pmic | 36:8c75783c1eca | 49 | const float max_voltage = 12.0f; // define maximum voltage of battery packs, adjust this to 6.0f V if you only use one batterypack |
pmic | 36:8c75783c1eca | 50 | const float counts_per_turn = 20.0f * 78.125f; // define counts per turn at gearbox end: counts/turn * gearratio |
pmic | 36:8c75783c1eca | 51 | const float kn = 180.0f / 12.0f; // define motor constant in rpm per V |
pmic | 36:8c75783c1eca | 52 | 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 |
pmic | 36:8c75783c1eca | 53 | const float kp = 0.1f; // define custom kp, this is the default speed controller gain for gear box 78.125:1 |
pmic | 36:8c75783c1eca | 54 | |
pmic | 36:8c75783c1eca | 55 | // SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwm_M2, encoder_M2); // default 78.125:1 gear box with default contoller parameters |
pmic | 36:8c75783c1eca | 56 | SpeedController speedController_M2(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M2, encoder_M2); // parameters adjusted to 100:1 gear |
pmic | 36:8c75783c1eca | 57 | |
pmic | 36:8c75783c1eca | 58 | 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 |
pmic | 36:8c75783c1eca | 59 | // PositionController positionController_M3(counts_per_turn, kn, max_voltage, pwm_M3, encoder_M3); // default 78.125:1 gear with default contoller parameters |
pmic | 36:8c75783c1eca | 60 | PositionController positionController_M3(counts_per_turn * k_gear, kn / k_gear, max_voltage, pwm_M3, encoder_M3); // parameters adjusted to 100:1 gear, we need a different speed controller gain here |
pmic | 36:8c75783c1eca | 61 | positionController_M3.setSpeedCntrlGain(kp * k_gear); |
pmic | 36:8c75783c1eca | 62 | positionController_M3.setMaxVelocityRPS(max_speed_rps); |
pmic | 36:8c75783c1eca | 63 | |
pmic | 36:8c75783c1eca | 64 | // Futaba Servo S3001 20mm 3kg Analog |
pmic | 36:8c75783c1eca | 65 | Servo servo_S1(PB_2); // create servo objects |
pmic | 36:8c75783c1eca | 66 | Servo servo_S2(PC_8); |
pmic | 36:8c75783c1eca | 67 | float servo_S1_angle = 0; // servo S1 normalized angle |
pmic | 36:8c75783c1eca | 68 | float servo_S2_angle = 0; // servo S2 normalized angle |
pmic | 36:8c75783c1eca | 69 | const int servo_period_mus = 20000; // define servo period time in mus |
pmic | 36:8c75783c1eca | 70 | |
pmic | 36:8c75783c1eca | 71 | int servo_counter = 0; // define servo counter, this is an additional variable to make the servos move |
pmic | 36:8c75783c1eca | 72 | const int loops_per_seconds = static_cast<int>(ceilf(1.0f/(0.001f*(float)main_task_period_ms))); // define loops per second |
pmic | 36:8c75783c1eca | 73 | |
pmic | 36:8c75783c1eca | 74 | // Groove Ultrasonic Ranger V2.0 |
pmic | 36:8c75783c1eca | 75 | float us_distance_cm = 0.0f; // define variable to store measurement |
pmic | 36:8c75783c1eca | 76 | RangeFinder us_range_finder(PB_12, 5782.0f, 0.02f, 17500); // create range finder object (ultra sonic distance sensor), 20 Hz parametrization |
pmic | 36:8c75783c1eca | 77 | // RangeFinder us_range_finder(PB_12, 5782.0f, 0.02f, 7000); // create range finder object (ultra sonic distance sensor), 50 Hz parametrization |
pmic | 36:8c75783c1eca | 78 | |
pmic | 36:8c75783c1eca | 79 | // LSM9DS1 IMU, carefull: not all PES boards have an imu (chip shortage) |
pmic | 36:8c75783c1eca | 80 | // 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 | 36:8c75783c1eca | 81 | |
pmic | 24:86f1a63e35a0 | 82 | // attach button fall and rise functions to user button object |
pmic | 24:86f1a63e35a0 | 83 | user_button.fall(&user_button_pressed_fcn); |
pmic | 24:86f1a63e35a0 | 84 | user_button.rise(&user_button_released_fcn); |
pmic | 17:c19b471f05cb | 85 | |
pmic | 29:d6f1ccf42a31 | 86 | // start timer |
pmic | 24:86f1a63e35a0 | 87 | main_task_timer.start(); |
pmic | 6:e1fa1a2d7483 | 88 | |
pmic | 24:86f1a63e35a0 | 89 | // set pullup mode: add resistor between pin and 3.3 V, so that there is a defined potential |
pmic | 24:86f1a63e35a0 | 90 | mechanical_button.mode(PullUp); |
pmic | 24:86f1a63e35a0 | 91 | |
pmic | 24:86f1a63e35a0 | 92 | // enable hardwaredriver dc motors: 0 -> disabled, 1 -> enabled |
pmic | 10:c5d85e35758c | 93 | enable_motors = 1; |
pmic | 17:c19b471f05cb | 94 | |
pmic | 24:86f1a63e35a0 | 95 | // motor M1 is used open-loop, we need to initialize the pwm and set pwm output to zero at the beginning, range: 0...1 -> u_min...u_max: 0.5 -> 0 V |
pmic | 25:ea1d6e27c895 | 96 | pwm_M1.period(pwm_period_s); |
pmic | 30:1e8295770bc1 | 97 | pwm_M1.write(0.5f); |
pmic | 9:f10b974d01e0 | 98 | |
pmic | 31:1b2a1bd1bccb | 99 | // set the soft pwm period for the servo objects |
pmic | 31:1b2a1bd1bccb | 100 | servo_S1.SetPeriod(servo_period_mus); |
pmic | 31:1b2a1bd1bccb | 101 | servo_S2.SetPeriod(servo_period_mus); |
pmic | 6:e1fa1a2d7483 | 102 | |
pmic | 24:86f1a63e35a0 | 103 | while (true) { // this loop will run forever |
pmic | 6:e1fa1a2d7483 | 104 | |
pmic | 24:86f1a63e35a0 | 105 | main_task_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 106 | |
pmic | 24:86f1a63e35a0 | 107 | if (do_execute_main_task) { |
pmic | 17:c19b471f05cb | 108 | |
pmic | 24:86f1a63e35a0 | 109 | // read analog input |
pmic | 24:86f1a63e35a0 | 110 | ir_distance_mV = 1.0e3f * ir_analog_in.read() * 3.3f; |
pmic | 6:e1fa1a2d7483 | 111 | |
pmic | 24:86f1a63e35a0 | 112 | // command dc motors if mechanical button is pressed |
pmic | 29:d6f1ccf42a31 | 113 | if (mechanical_button.read()) { |
pmic | 30:1e8295770bc1 | 114 | pwm_M1.write(0.75f); // write output voltage to motor M1 |
pmic | 24:86f1a63e35a0 | 115 | speedController_M2.setDesiredSpeedRPS(0.5f); // set a desired speed for speed controlled dc motors M2 |
pmic | 36:8c75783c1eca | 116 | positionController_M3.setDesiredRotation(1.5f); // set a desired rotation for position controlled dc motors M3 |
pmic | 24:86f1a63e35a0 | 117 | } else { |
pmic | 30:1e8295770bc1 | 118 | pwm_M1.write(0.5f); |
pmic | 24:86f1a63e35a0 | 119 | speedController_M2.setDesiredSpeedRPS(0.0f); |
pmic | 36:8c75783c1eca | 120 | positionController_M3.setDesiredRotation(0.0f); |
pmic | 24:86f1a63e35a0 | 121 | } |
pmic | 6:e1fa1a2d7483 | 122 | |
pmic | 30:1e8295770bc1 | 123 | // check if servos are enabled |
pmic | 31:1b2a1bd1bccb | 124 | if (!servo_S1.isEnabled()) servo_S1.Enable(); |
pmic | 31:1b2a1bd1bccb | 125 | if (!servo_S2.isEnabled()) servo_S2.Enable(); |
pmic | 24:86f1a63e35a0 | 126 | // command servo position, this needs to be calibrated |
pmic | 30:1e8295770bc1 | 127 | servo_S1.SetPosition(servo_S1_angle); |
pmic | 31:1b2a1bd1bccb | 128 | if (servo_S1_angle < 1.0f & servo_counter%loops_per_seconds == 0 & servo_counter != 0) { |
pmic | 30:1e8295770bc1 | 129 | servo_S1_angle += 0.01f; |
pmic | 8:9bb806a7f585 | 130 | } |
pmic | 30:1e8295770bc1 | 131 | servo_S2.SetPosition(servo_S2_angle); |
pmic | 31:1b2a1bd1bccb | 132 | if (servo_S2_angle < 1.0f & servo_counter%loops_per_seconds == 0 & servo_counter != 0) { |
pmic | 30:1e8295770bc1 | 133 | servo_S2_angle += 0.01f; |
pmic | 8:9bb806a7f585 | 134 | } |
pmic | 10:c5d85e35758c | 135 | servo_counter++; |
pmic | 6:e1fa1a2d7483 | 136 | |
pmic | 24:86f1a63e35a0 | 137 | // read ultra sonic distance sensor |
pmic | 24:86f1a63e35a0 | 138 | us_distance_cm = us_range_finder.read_cm(); |
pmic | 11:af0f165f8761 | 139 | |
pmic | 24:86f1a63e35a0 | 140 | // visual feedback that the main task is executed |
pmic | 24:86f1a63e35a0 | 141 | extra_led = 1; |
pmic | 9:f10b974d01e0 | 142 | |
pmic | 1:93d997d6b232 | 143 | } else { |
pmic | 6:e1fa1a2d7483 | 144 | |
pmic | 24:86f1a63e35a0 | 145 | ir_distance_mV = 0.0f; |
pmic | 1:93d997d6b232 | 146 | |
pmic | 30:1e8295770bc1 | 147 | pwm_M1.write(0.5f); |
pmic | 17:c19b471f05cb | 148 | speedController_M2.setDesiredSpeedRPS(0.0f); |
pmic | 36:8c75783c1eca | 149 | positionController_M3.setDesiredRotation(0.0f); |
pmic | 6:e1fa1a2d7483 | 150 | |
pmic | 30:1e8295770bc1 | 151 | servo_S1_angle = 0; |
pmic | 30:1e8295770bc1 | 152 | servo_S2_angle = 0; |
pmic | 30:1e8295770bc1 | 153 | // servo_S1.SetPosition(servo_S1_angle); |
pmic | 30:1e8295770bc1 | 154 | // servo_S2.SetPosition(servo_S2_angle); |
pmic | 30:1e8295770bc1 | 155 | if (servo_S1.isEnabled()) servo_S1.Disable(); |
pmic | 30:1e8295770bc1 | 156 | if (servo_S2.isEnabled()) servo_S2.Disable(); |
pmic | 17:c19b471f05cb | 157 | |
pmic | 24:86f1a63e35a0 | 158 | us_distance_cm = 0.0f; |
pmic | 6:e1fa1a2d7483 | 159 | |
pmic | 24:86f1a63e35a0 | 160 | extra_led = 0; |
pmic | 1:93d997d6b232 | 161 | } |
pmic | 6:e1fa1a2d7483 | 162 | |
pmic | 24:86f1a63e35a0 | 163 | user_led = !user_led; |
pmic | 24:86f1a63e35a0 | 164 | |
pmic | 24:86f1a63e35a0 | 165 | // do only output via serial what's really necessary (this makes your code slow) |
pmic | 30:1e8295770bc1 | 166 | printf("IR sensor (mV): %3.3f, Encoder M1: %3d, Speed M2 (rps) %3.3f, Position M3 (rot): %3.3f, Servo S1 angle (normalized): %3.3f, Servo S2 angle (normalized): %3.3f, US sensor (cm): %3.3f\r\n", |
pmic | 24:86f1a63e35a0 | 167 | ir_distance_mV, |
pmic | 17:c19b471f05cb | 168 | encoder_M1.read(), |
pmic | 17:c19b471f05cb | 169 | speedController_M2.getSpeedRPS(), |
pmic | 17:c19b471f05cb | 170 | positionController_M3.getRotation(), |
pmic | 30:1e8295770bc1 | 171 | servo_S1_angle, |
pmic | 30:1e8295770bc1 | 172 | servo_S2_angle, |
pmic | 24:86f1a63e35a0 | 173 | us_distance_cm); |
pmic | 6:e1fa1a2d7483 | 174 | |
pmic | 24:86f1a63e35a0 | 175 | // read out the imu, the actual frames of the sensor reading needs to be figured out |
pmic | 24:86f1a63e35a0 | 176 | // imu.updateGyro(); |
pmic | 24:86f1a63e35a0 | 177 | // imu.updateAcc(); |
pmic | 24:86f1a63e35a0 | 178 | // imu.updateMag(); |
pmic | 24:86f1a63e35a0 | 179 | // printf("%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f\r\n", imu.readGyroX(), imu.readGyroY(), imu.readGyroZ(), |
pmic | 24:86f1a63e35a0 | 180 | // imu.readAccX(), imu.readAccY(), imu.readAccZ(), imu.readMagX(), imu.readMagY(), imu.readMagZ()); |
pmic | 17:c19b471f05cb | 181 | |
pmic | 24:86f1a63e35a0 | 182 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
pmic | 24:86f1a63e35a0 | 183 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
pmic | 24:86f1a63e35a0 | 184 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
pmic | 1:93d997d6b232 | 185 | } |
pmic | 1:93d997d6b232 | 186 | } |
pmic | 6:e1fa1a2d7483 | 187 | |
pmic | 24:86f1a63e35a0 | 188 | void user_button_pressed_fcn() |
pmic | 25:ea1d6e27c895 | 189 | { |
pmic | 26:28693b369945 | 190 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 191 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 192 | } |
pmic | 6:e1fa1a2d7483 | 193 | |
pmic | 24:86f1a63e35a0 | 194 | void user_button_released_fcn() |
pmic | 6:e1fa1a2d7483 | 195 | { |
pmic | 24:86f1a63e35a0 | 196 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 24:86f1a63e35a0 | 197 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 198 | user_button_timer.stop(); |
pmic | 24:86f1a63e35a0 | 199 | if (user_button_elapsed_time_ms > 200) { |
pmic | 24:86f1a63e35a0 | 200 | do_execute_main_task = !do_execute_main_task; |
pmic | 8:9bb806a7f585 | 201 | } |
pmic | 6:e1fa1a2d7483 | 202 | } |