![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Our Programm to build up from the PES Board Example
Dependencies: PM2_Libary
main.cpp@24:b45ebc28cdf0, 2022-03-27 (annotated)
- Committer:
- oneelia01
- Date:
- Sun Mar 27 01:27:06 2022 +0100
- Revision:
- 24:b45ebc28cdf0
- Parent:
- 23:4a0912c80739
Grundger?st aufgebaut (switch, erste funktoinen eingepfl. ,
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
boro | 0:5d4d21d56334 | 1 | #include "mbed.h" |
pmic | 16:2de2a437afdc | 2 | #include "PM2_Libary.h" |
pmic | 16:2de2a437afdc | 3 | |
pmic | 16:2de2a437afdc | 4 | // logical variable main task |
pmic | 16:2de2a437afdc | 5 | 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 | 16:2de2a437afdc | 6 | |
pmic | 16:2de2a437afdc | 7 | // user button on nucleo board |
pmic | 16:2de2a437afdc | 8 | 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 | 16:2de2a437afdc | 9 | InterruptIn user_button(PC_13); // create InterruptIn interface object to evaluate user button falling and rising edge (no blocking code in ISR) |
pmic | 16:2de2a437afdc | 10 | void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below |
pmic | 16:2de2a437afdc | 11 | void user_button_released_fcn(); |
pmic | 16:2de2a437afdc | 12 | |
pmic | 16:2de2a437afdc | 13 | // while loop gets executed every main_task_period_ms milliseconds |
pmic | 16:2de2a437afdc | 14 | 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 | 16:2de2a437afdc | 15 | Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms |
pmic | 13:5d689f89d794 | 16 | |
pmic | 16:2de2a437afdc | 17 | // led on nucleo board |
pmic | 16:2de2a437afdc | 18 | DigitalOut user_led(LED1); // create DigitalOut object to command user led |
pmic | 16:2de2a437afdc | 19 | |
pmic | 16:2de2a437afdc | 20 | // additional Led |
pmic | 16:2de2a437afdc | 21 | DigitalOut extra_led(PB_9); // create DigitalOut object to command extra led (do add an aditional resistor, e.g. 220...500 Ohm) |
pmic | 16:2de2a437afdc | 22 | |
pmic | 16:2de2a437afdc | 23 | // mechanical button |
pmic | 16:2de2a437afdc | 24 | 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 | 16:2de2a437afdc | 25 | |
pmic | 16:2de2a437afdc | 26 | // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor |
pmic | 16:2de2a437afdc | 27 | float ir_distance_mV = 0.0f; // define variable to store measurement |
pmic | 20:c52e148d933f | 28 | // ??? // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 |
pmic | 16:2de2a437afdc | 29 | |
oneelia01 | 24:b45ebc28cdf0 | 30 | int schritt = 0; |
oneelia01 | 24:b45ebc28cdf0 | 31 | |
boro | 0:5d4d21d56334 | 32 | int main() |
boro | 0:5d4d21d56334 | 33 | { |
pmic | 16:2de2a437afdc | 34 | // attach button fall and rise functions to user button object |
pmic | 16:2de2a437afdc | 35 | user_button.fall(&user_button_pressed_fcn); |
pmic | 16:2de2a437afdc | 36 | user_button.rise(&user_button_released_fcn); |
pmic | 16:2de2a437afdc | 37 | |
pmic | 16:2de2a437afdc | 38 | // start timers |
pmic | 16:2de2a437afdc | 39 | main_task_timer.start(); |
pmic | 16:2de2a437afdc | 40 | |
pmic | 16:2de2a437afdc | 41 | // set pullup mode: add resistor between pin and 3.3 V, so that there is a defined potential |
pmic | 16:2de2a437afdc | 42 | mechanical_button.mode(PullUp); |
pmic | 16:2de2a437afdc | 43 | |
pmic | 16:2de2a437afdc | 44 | while (true) { // this loop will run forever |
pmic | 16:2de2a437afdc | 45 | |
pmic | 16:2de2a437afdc | 46 | main_task_timer.reset(); |
pmic | 16:2de2a437afdc | 47 | |
pmic | 16:2de2a437afdc | 48 | if (do_execute_main_task) { |
pmic | 13:5d689f89d794 | 49 | |
oneelia01 | 24:b45ebc28cdf0 | 50 | switch (schritt) { |
oneelia01 | 24:b45ebc28cdf0 | 51 | |
oneelia01 | 24:b45ebc28cdf0 | 52 | case 0: //initialsiserung |
oneelia01 | 24:b45ebc28cdf0 | 53 | initialisierung(); |
oneelia01 | 24:b45ebc28cdf0 | 54 | |
oneelia01 | 24:b45ebc28cdf0 | 55 | if(/*stütze oben und keine Treppe erkennbar und boden erkennbar (alle sensoren funktionieren)*/){ |
oneelia01 | 24:b45ebc28cdf0 | 56 | schritt = 1; |
oneelia01 | 24:b45ebc28cdf0 | 57 | reset_initialisierung; |
oneelia01 | 24:b45ebc28cdf0 | 58 | } |
oneelia01 | 24:b45ebc28cdf0 | 59 | break; |
oneelia01 | 24:b45ebc28cdf0 | 60 | |
oneelia01 | 24:b45ebc28cdf0 | 61 | case 1: //anfahren bis treppenanschlag |
oneelia01 | 24:b45ebc28cdf0 | 62 | break; |
pmic | 13:5d689f89d794 | 63 | |
oneelia01 | 24:b45ebc28cdf0 | 64 | case 2: //Mittelkörper hochfahren |
oneelia01 | 24:b45ebc28cdf0 | 65 | break; |
oneelia01 | 24:b45ebc28cdf0 | 66 | |
oneelia01 | 24:b45ebc28cdf0 | 67 | case 3: //vorwärtsfahren bis Mittelkörper auf der Treppe |
oneelia01 | 24:b45ebc28cdf0 | 68 | break; |
pmic | 14:db46f47b0480 | 69 | |
oneelia01 | 24:b45ebc28cdf0 | 70 | case 4: //Sttze hochfahren und trpee erkennen |
oneelia01 | 24:b45ebc28cdf0 | 71 | break; |
pmic | 16:2de2a437afdc | 72 | |
oneelia01 | 24:b45ebc28cdf0 | 73 | case 5: //Vorwärtsfahren und "abschalten" |
oneelia01 | 24:b45ebc28cdf0 | 74 | break; |
oneelia01 | 24:b45ebc28cdf0 | 75 | } |
oneelia01 | 24:b45ebc28cdf0 | 76 | } |
pmic | 16:2de2a437afdc | 77 | |
oneelia01 | 24:b45ebc28cdf0 | 78 | |
pmic | 13:5d689f89d794 | 79 | |
pmic | 16:2de2a437afdc | 80 | user_led = !user_led; |
pmic | 13:5d689f89d794 | 81 | |
oneelia01 | 24:b45ebc28cdf0 | 82 | |
pmic | 16:2de2a437afdc | 83 | |
pmic | 16:2de2a437afdc | 84 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
pmic | 16:2de2a437afdc | 85 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
pmic | 16:2de2a437afdc | 86 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
boro | 0:5d4d21d56334 | 87 | } |
boro | 0:5d4d21d56334 | 88 | } |
pmic | 13:5d689f89d794 | 89 | |
oneelia01 | 24:b45ebc28cdf0 | 90 | void user_button_pressed_fcn() //NH: Darf nicht gelöscht werden! |
pmic | 1:4e0e4d0363d9 | 91 | { |
pmic | 19:22e264bd960d | 92 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 93 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 94 | } |
pmic | 13:5d689f89d794 | 95 | |
oneelia01 | 24:b45ebc28cdf0 | 96 | void user_button_released_fcn() //NH: Darf nicht gelöscht werden! |
pmic | 1:4e0e4d0363d9 | 97 | { |
pmic | 16:2de2a437afdc | 98 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 16:2de2a437afdc | 99 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 1:4e0e4d0363d9 | 100 | user_button_timer.stop(); |
pmic | 16:2de2a437afdc | 101 | if (user_button_elapsed_time_ms > 200) { |
pmic | 16:2de2a437afdc | 102 | do_execute_main_task = !do_execute_main_task; |
pmic | 13:5d689f89d794 | 103 | } |
oneelia01 | 24:b45ebc28cdf0 | 104 | } |
oneelia01 | 24:b45ebc28cdf0 | 105 | |
oneelia01 | 24:b45ebc28cdf0 | 106 | |
oneelia01 | 24:b45ebc28cdf0 | 107 | void initialisierung(){ |
oneelia01 | 24:b45ebc28cdf0 | 108 | |
oneelia01 | 24:b45ebc28cdf0 | 109 | } |
oneelia01 | 24:b45ebc28cdf0 | 110 | |
oneelia01 | 24:b45ebc28cdf0 | 111 | void reset_initialisierung(){ |
oneelia01 | 24:b45ebc28cdf0 | 112 | |
oneelia01 | 24:b45ebc28cdf0 | 113 | } |
oneelia01 | 24:b45ebc28cdf0 | 114 | |
oneelia01 | 24:b45ebc28cdf0 | 115 | |
oneelia01 | 24:b45ebc28cdf0 | 116 | void prozessschritt_1(){ //initial |
oneelia01 | 24:b45ebc28cdf0 | 117 | |
oneelia01 | 24:b45ebc28cdf0 | 118 | } |
oneelia01 | 24:b45ebc28cdf0 | 119 | |
oneelia01 | 24:b45ebc28cdf0 | 120 | void reset_Prozessschritt_1(){ |
oneelia01 | 24:b45ebc28cdf0 | 121 | |
oneelia01 | 24:b45ebc28cdf0 | 122 | } |
oneelia01 | 24:b45ebc28cdf0 | 123 | |
oneelia01 | 24:b45ebc28cdf0 | 124 | void prozessschritt_2(){ |
oneelia01 | 24:b45ebc28cdf0 | 125 | |
oneelia01 | 24:b45ebc28cdf0 | 126 | } |
oneelia01 | 24:b45ebc28cdf0 | 127 | |
oneelia01 | 24:b45ebc28cdf0 | 128 | void reset_Prozessschritt_2(){ |
oneelia01 | 24:b45ebc28cdf0 | 129 | |
pmic | 6:6c1c38d4faa4 | 130 | } |