workshop 1
Dependencies: PM2_Libary Eigen
Fork of PM2_Example_Summer_School by
main.cpp@46:e5fb4dd7b531, 2022-05-18 (annotated)
- Committer:
- huels035
- Date:
- Wed May 18 09:29:01 2022 -0500
- Revision:
- 46:e5fb4dd7b531
- Parent:
- 45:42adc921bc66
- Child:
- 47:31726ce58a6d
Workshop 1 with motor working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 36:8c75783c1eca | 1 | #include <mbed.h> |
pmic | 17:c19b471f05cb | 2 | #include "PM2_Libary.h" |
pmic | 36:8c75783c1eca | 3 | #include "Eigen/Dense.h" |
pmic | 36:8c75783c1eca | 4 | |
pmic | 36:8c75783c1eca | 5 | # define M_PI 3.14159265358979323846 // number pi |
pmic | 6:e1fa1a2d7483 | 6 | |
huels035 | 46:e5fb4dd7b531 | 7 | |
huels035 | 45:42adc921bc66 | 8 | //Workshop 1 |
huels035 | 46:e5fb4dd7b531 | 9 | float ir_distance_mV2cm (float ir_distance_mV); |
pmic | 24:86f1a63e35a0 | 10 | // logical variable main task |
pmic | 24:86f1a63e35a0 | 11 | 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 | 12 | |
pmic | 24:86f1a63e35a0 | 13 | // user button on nucleo board |
pmic | 24:86f1a63e35a0 | 14 | 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 | 15 | 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 | 16 | void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below |
pmic | 24:86f1a63e35a0 | 17 | void user_button_released_fcn(); |
pmic | 6:e1fa1a2d7483 | 18 | |
pmic | 1:93d997d6b232 | 19 | int main() |
pmic | 23:26b3a25fc637 | 20 | { |
pmic | 36:8c75783c1eca | 21 | // while loop gets executed every main_task_period_ms milliseconds |
pmic | 41:8a63b01edd7e | 22 | const 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 | 36:8c75783c1eca | 23 | Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms |
huels035 | 46:e5fb4dd7b531 | 24 | //printf("here1"); |
pmic | 40:7e6b7aec3947 | 25 | // a coutner |
pmic | 40:7e6b7aec3947 | 26 | uint32_t main_task_cntr = 0; |
pmic | 40:7e6b7aec3947 | 27 | |
pmic | 36:8c75783c1eca | 28 | // led on nucleo board |
pmic | 36:8c75783c1eca | 29 | DigitalOut user_led(LED1); // create DigitalOut object to command user led |
pmic | 36:8c75783c1eca | 30 | |
huels035 | 46:e5fb4dd7b531 | 31 | DigitalOut enable_motors(PB_15); |
huels035 | 46:e5fb4dd7b531 | 32 | FastPWM pwm_M1(PB_13); |
huels035 | 46:e5fb4dd7b531 | 33 | FastPWM pwm_M2(PA_9); |
huels035 | 46:e5fb4dd7b531 | 34 | |
huels035 | 46:e5fb4dd7b531 | 35 | const float pwm_period_s = .00005f; |
huels035 | 46:e5fb4dd7b531 | 36 | pwm_M1.period(pwm_period_s); |
huels035 | 46:e5fb4dd7b531 | 37 | pwm_M2.period(pwm_period_s); |
huels035 | 46:e5fb4dd7b531 | 38 | //pwm_M1.write(0.5f); |
huels035 | 46:e5fb4dd7b531 | 39 | |
huels035 | 46:e5fb4dd7b531 | 40 | enable_motors = 1; |
huels035 | 46:e5fb4dd7b531 | 41 | |
huels035 | 46:e5fb4dd7b531 | 42 | AnalogIn ir_analog_in(PC_2); |
huels035 | 46:e5fb4dd7b531 | 43 | float ir_distance_mV = 0.0f; |
huels035 | 46:e5fb4dd7b531 | 44 | float ir_distance_cm = 0.0f; |
huels035 | 46:e5fb4dd7b531 | 45 | float distAxisToSensor = 0.12f; |
huels035 | 46:e5fb4dd7b531 | 46 | |
huels035 | 46:e5fb4dd7b531 | 47 | //I2C i2c(PB_9, PB_8); |
huels035 | 46:e5fb4dd7b531 | 48 | |
huels035 | 46:e5fb4dd7b531 | 49 | //SensorBar light_sensor(i2c, distAxisToSensor); |
huels035 | 46:e5fb4dd7b531 | 50 | |
pmic | 24:86f1a63e35a0 | 51 | // attach button fall and rise functions to user button object |
pmic | 24:86f1a63e35a0 | 52 | user_button.fall(&user_button_pressed_fcn); |
pmic | 24:86f1a63e35a0 | 53 | user_button.rise(&user_button_released_fcn); |
pmic | 17:c19b471f05cb | 54 | |
pmic | 29:d6f1ccf42a31 | 55 | // start timer |
pmic | 24:86f1a63e35a0 | 56 | main_task_timer.start(); |
pmic | 6:e1fa1a2d7483 | 57 | |
pmic | 24:86f1a63e35a0 | 58 | while (true) { // this loop will run forever |
pmic | 6:e1fa1a2d7483 | 59 | |
pmic | 24:86f1a63e35a0 | 60 | main_task_timer.reset(); |
huels035 | 46:e5fb4dd7b531 | 61 | //ir_distance_mV = ir_analog_in.read()* 3.3f * 1.0e3f; |
huels035 | 46:e5fb4dd7b531 | 62 | |
huels035 | 46:e5fb4dd7b531 | 63 | //ir_distance_cm = ir_distance_mV2cm(ir_distance_mV); |
pmic | 40:7e6b7aec3947 | 64 | |
pmic | 17:c19b471f05cb | 65 | |
huels035 | 46:e5fb4dd7b531 | 66 | //pwm_M1.write(.75f); |
huels035 | 46:e5fb4dd7b531 | 67 | if (do_execute_main_task) { |
huels035 | 46:e5fb4dd7b531 | 68 | pwm_M1.write(.75f); |
pmic | 1:93d997d6b232 | 69 | } else { |
huels035 | 46:e5fb4dd7b531 | 70 | pwm_M1.write(.5f); |
huels035 | 46:e5fb4dd7b531 | 71 | } |
huels035 | 46:e5fb4dd7b531 | 72 | |
pmic | 6:e1fa1a2d7483 | 73 | |
pmic | 41:8a63b01edd7e | 74 | // user_led is switching its state every second |
pmic | 41:8a63b01edd7e | 75 | if ( (main_task_cntr%(1000 / main_task_period_ms) == 0) && (main_task_cntr!=0) ) { |
pmic | 40:7e6b7aec3947 | 76 | user_led = !user_led; |
pmic | 40:7e6b7aec3947 | 77 | } |
pmic | 40:7e6b7aec3947 | 78 | main_task_cntr++; |
huels035 | 46:e5fb4dd7b531 | 79 | //printf("%f \r\n", light_sensor.getAngleRad()); |
huels035 | 46:e5fb4dd7b531 | 80 | //printf("IR sensor (mV): %f \r\n", ir_distance_mV); |
huels035 | 46:e5fb4dd7b531 | 81 | //printf("IR sensor (cm): %f \r\n", ir_distance_cm); |
huels035 | 46:e5fb4dd7b531 | 82 | |
huels035 | 46:e5fb4dd7b531 | 83 | |
huels035 | 46:e5fb4dd7b531 | 84 | |
pmic | 40:7e6b7aec3947 | 85 | |
pmic | 24:86f1a63e35a0 | 86 | // do only output via serial what's really necessary (this makes your code slow) |
pmic | 41:8a63b01edd7e | 87 | /* |
pmic | 41:8a63b01edd7e | 88 | printf("IR sensor (mV): %3.3f, IR sensor (cm): %3.3f, SensorBar angle (rad): %3.3f, Speed M1 (rps) %3.3f, Position M2 (rot): %3.3f\r\n", |
pmic | 24:86f1a63e35a0 | 89 | ir_distance_mV, |
pmic | 41:8a63b01edd7e | 90 | ir_distance_cm, |
pmic | 40:7e6b7aec3947 | 91 | sensor_bar_avgAngleRad, |
pmic | 40:7e6b7aec3947 | 92 | speedController_M1.getSpeedRPS(), |
pmic | 40:7e6b7aec3947 | 93 | positionController_M2.getRotation()); |
pmic | 41:8a63b01edd7e | 94 | */ |
pmic | 17:c19b471f05cb | 95 | |
pmic | 24:86f1a63e35a0 | 96 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
pmic | 24:86f1a63e35a0 | 97 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
pmic | 24:86f1a63e35a0 | 98 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
pmic | 1:93d997d6b232 | 99 | } |
pmic | 1:93d997d6b232 | 100 | } |
pmic | 6:e1fa1a2d7483 | 101 | |
pmic | 24:86f1a63e35a0 | 102 | void user_button_pressed_fcn() |
pmic | 25:ea1d6e27c895 | 103 | { |
pmic | 26:28693b369945 | 104 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 105 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 106 | } |
pmic | 6:e1fa1a2d7483 | 107 | |
pmic | 24:86f1a63e35a0 | 108 | void user_button_released_fcn() |
pmic | 6:e1fa1a2d7483 | 109 | { |
pmic | 24:86f1a63e35a0 | 110 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 24:86f1a63e35a0 | 111 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 112 | user_button_timer.stop(); |
pmic | 24:86f1a63e35a0 | 113 | if (user_button_elapsed_time_ms > 200) { |
pmic | 24:86f1a63e35a0 | 114 | do_execute_main_task = !do_execute_main_task; |
pmic | 8:9bb806a7f585 | 115 | } |
huels035 | 46:e5fb4dd7b531 | 116 | } |
huels035 | 46:e5fb4dd7b531 | 117 | |
huels035 | 46:e5fb4dd7b531 | 118 | float ir_distance_mV2cm (float ir_distance_mV) |
huels035 | 46:e5fb4dd7b531 | 119 | { |
huels035 | 46:e5fb4dd7b531 | 120 | |
huels035 | 46:e5fb4dd7b531 | 121 | // Coefficients (with 95% confidence bounds): |
huels035 | 46:e5fb4dd7b531 | 122 | static float a = 0.8255; //(-4.031, 5.682) |
huels035 | 46:e5fb4dd7b531 | 123 | static float c = 1.463e+04; //(1.224e+04, 1.702e+04) |
huels035 | 46:e5fb4dd7b531 | 124 | float f1 = c/(ir_distance_mV + 1) + a; |
huels035 | 46:e5fb4dd7b531 | 125 | return f1; |
pmic | 6:e1fa1a2d7483 | 126 | } |