Repository for verBOT robot project, hopefully featuring two branches: Dev/Test and Prod.

Dependencies:   PM2_Libary Eigen

Committer:
wesse162
Date:
Tue May 24 09:19:20 2022 +0200
Revision:
46:e9d929bae82d
Parent:
45:efab79252438
dummy comment.

Who changed what in which revision?

UserRevisionLine numberNew 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
wesse162 45:efab79252438 8 // adding the first comment of the semester
wesse162 46:e9d929bae82d 9 // adding a testing comment
wesse162 45:efab79252438 10
pmic 24:86f1a63e35a0 11 // logical variable main task
pmic 24:86f1a63e35a0 12 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 13
pmic 24:86f1a63e35a0 14 // user button on nucleo board
pmic 24:86f1a63e35a0 15 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 16 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 17 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 24:86f1a63e35a0 18 void user_button_released_fcn();
pmic 6:e1fa1a2d7483 19
pmic 1:93d997d6b232 20 int main()
pmic 23:26b3a25fc637 21 {
pmic 36:8c75783c1eca 22 // while loop gets executed every main_task_period_ms milliseconds
pmic 41:8a63b01edd7e 23 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 24 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 25
pmic 40:7e6b7aec3947 26 // a coutner
pmic 40:7e6b7aec3947 27 uint32_t main_task_cntr = 0;
pmic 40:7e6b7aec3947 28
pmic 36:8c75783c1eca 29 // led on nucleo board
pmic 36:8c75783c1eca 30 DigitalOut user_led(LED1); // create DigitalOut object to command user led
pmic 36:8c75783c1eca 31
wesse162 46:e9d929bae82d 32 AnalogIn ir_analog_in(PC_2); // create AnalogIn for IR sensor calibration
wesse162 46:e9d929bae82d 33 float ir_distance_mV = 0.0f;
wesse162 46:e9d929bae82d 34 float ir_distance_cm = 0.0f;
wesse162 46:e9d929bae82d 35
pmic 24:86f1a63e35a0 36 // attach button fall and rise functions to user button object
pmic 24:86f1a63e35a0 37 user_button.fall(&user_button_pressed_fcn);
pmic 24:86f1a63e35a0 38 user_button.rise(&user_button_released_fcn);
pmic 17:c19b471f05cb 39
pmic 29:d6f1ccf42a31 40 // start timer
pmic 24:86f1a63e35a0 41 main_task_timer.start();
pmic 6:e1fa1a2d7483 42
pmic 24:86f1a63e35a0 43 while (true) { // this loop will run forever
pmic 6:e1fa1a2d7483 44
pmic 24:86f1a63e35a0 45 main_task_timer.reset();
pmic 40:7e6b7aec3947 46
wesse162 46:e9d929bae82d 47 // reads IR sensor's raw value, AnalogIn/Out normalized to 1 (read between 0-3.3 V)
wesse162 46:e9d929bae82d 48 ir_distance_mV = ir_analog_in.read() * 3.3f * 1.0e3f;
wesse162 46:e9d929bae82d 49
wesse162 46:e9d929bae82d 50
pmic 24:86f1a63e35a0 51 if (do_execute_main_task) {
pmic 17:c19b471f05cb 52
pmic 1:93d997d6b232 53 } else {
pmic 40:7e6b7aec3947 54
pmic 1:93d997d6b232 55 }
pmic 6:e1fa1a2d7483 56
pmic 41:8a63b01edd7e 57 // user_led is switching its state every second
pmic 41:8a63b01edd7e 58 if ( (main_task_cntr%(1000 / main_task_period_ms) == 0) && (main_task_cntr!=0) ) {
pmic 40:7e6b7aec3947 59 user_led = !user_led;
pmic 40:7e6b7aec3947 60 }
pmic 40:7e6b7aec3947 61 main_task_cntr++;
pmic 40:7e6b7aec3947 62
wesse162 46:e9d929bae82d 63 printf("IR Sensor (mV): %f\r\n", ir_distance_mV);
wesse162 46:e9d929bae82d 64
pmic 24:86f1a63e35a0 65 // do only output via serial what's really necessary (this makes your code slow)
pmic 41:8a63b01edd7e 66 /*
pmic 41:8a63b01edd7e 67 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 68 ir_distance_mV,
pmic 41:8a63b01edd7e 69 ir_distance_cm,
pmic 40:7e6b7aec3947 70 sensor_bar_avgAngleRad,
pmic 40:7e6b7aec3947 71 speedController_M1.getSpeedRPS(),
pmic 40:7e6b7aec3947 72 positionController_M2.getRotation());
pmic 41:8a63b01edd7e 73 */
pmic 17:c19b471f05cb 74
pmic 24:86f1a63e35a0 75 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 24:86f1a63e35a0 76 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 24:86f1a63e35a0 77 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
pmic 1:93d997d6b232 78 }
pmic 1:93d997d6b232 79 }
pmic 6:e1fa1a2d7483 80
pmic 24:86f1a63e35a0 81 void user_button_pressed_fcn()
pmic 25:ea1d6e27c895 82 {
pmic 26:28693b369945 83 user_button_timer.start();
pmic 6:e1fa1a2d7483 84 user_button_timer.reset();
pmic 6:e1fa1a2d7483 85 }
pmic 6:e1fa1a2d7483 86
pmic 24:86f1a63e35a0 87 void user_button_released_fcn()
pmic 6:e1fa1a2d7483 88 {
pmic 24:86f1a63e35a0 89 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 24:86f1a63e35a0 90 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 6:e1fa1a2d7483 91 user_button_timer.stop();
pmic 24:86f1a63e35a0 92 if (user_button_elapsed_time_ms > 200) {
pmic 24:86f1a63e35a0 93 do_execute_main_task = !do_execute_main_task;
pmic 8:9bb806a7f585 94 }
pmic 6:e1fa1a2d7483 95 }