Repository navigation
Expand file tree
/
Copy pathmain_serial_stream.cpp
More file actions
127 lines (99 loc) · 4.78 KB
/
Copy pathmain_serial_stream.cpp
File metadata and controls
127 lines (99 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "mbed.h"
// pes board pin map
#include "PESBoardPinMap.h"
// drivers
#include "DebounceIn.h"
#include "SerialStream.h"
bool do_execute_main_task = false; // this variable will be toggled via the user button (blue button) and
// decides whether to execute the main task or not
bool do_reset_all_once = false; // this variable is used to reset certain variables and objects and
// shows how you can run a code segment only once
// objects for user button (blue button) handling on nucleo board
DebounceIn user_button(BUTTON1); // create DebounceIn to evaluate the user button
void toggle_do_execute_main_fcn(); // custom function which is getting executed when user
// button gets pressed, definition at the end
// main runs as an own thread
int main()
{
// attach button fall function address to user button object
user_button.fall(&toggle_do_execute_main_fcn);
// while loop gets executed every main_task_period_ms milliseconds, this is a
// simple approach to repeatedly execute main
const int main_task_period_ms = 20; // define main task period time in ms e.g. 20 ms, therefore
// the main task will run 50 times per second
Timer main_task_timer; // create Timer object which we use to run the main task
// every main_task_period_ms
// led on nucleo board
DigitalOut user_led(LED1);
// additional led
// create DigitalOut object to command extra led, you need to add an additional resistor, e.g. 220...500 Ohm
// a led has an anode (+) and a cathode (-), the cathode needs to be connected to ground via the resistor
DigitalOut led1(PB_9);
// --- adding variables and objects and applying functions starts here ---
// serial stream to send data over uart
SerialStream serialStream(PB_UNUSED_UART_TX /*PB_10*/,
PB_UNUSED_UART_RX /*PC_5 */);
int cntr_1 = 0;
int cntr_2 = 0;
// additional timer to measure time elapsed since last call
Timer logging_timer;
logging_timer.start();
microseconds time_previous_us{0};
// start timer
main_task_timer.start();
// this loop will run forever
while (true) {
main_task_timer.reset();
// --- code that runs every cycle at the start goes here ---
// measure delta time
const microseconds time_us = logging_timer.elapsed_time();
const float dtime_us = duration_cast<microseconds>(time_us - time_previous_us).count();
time_previous_us = time_us;
if (do_execute_main_task) {
// --- code that runs when the blue button was pressed goes here ---
// visual feedback that the main task is executed, setting this once would actually be enough
led1 = 1;
// increment counter
cntr_1++; // add 1 to counter 1
cntr_2 += 10; // add 10 to counter 2
} else {
// the following code block gets executed only once
if (do_reset_all_once) {
do_reset_all_once = false;
// --- variables and objects that should be reset go here ---
// reset variables and objects
cntr_1 = 0;
cntr_2 = 0;
// serialStream.reset();
led1 = 0;
}
}
// toggling the user led
user_led = !user_led;
// --- code that runs every cycle at the end goes here ---
// print to the serial terminal
if ((cntr_1 % 50 == 0) && (cntr_1 != 0))
printf("Counter 1: %d, Counter 2: %d\n", cntr_1, cntr_2);
if (serialStream.startByteReceived()) {
// send data over serial stream
serialStream.write( dtime_us ); // 0 delta time in us
serialStream.write((float)(cntr_1)); // 1 counter 1 value
serialStream.write((float)(cntr_2)); // 2 counter 3 value
serialStream.send();
}
// read timer and make the main thread sleep for the remaining time span (non blocking)
int main_task_elapsed_time_ms = duration_cast<milliseconds>(main_task_timer.elapsed_time()).count();
if (main_task_period_ms - main_task_elapsed_time_ms < 0)
printf("Warning: Main task took longer than main_task_period_ms\n");
else
thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
}
}
void toggle_do_execute_main_fcn()
{
// toggle do_execute_main_task if the button was pressed
do_execute_main_task = !do_execute_main_task;
// set do_reset_all_once to true if do_execute_main_task changed from false to true
if (do_execute_main_task)
do_reset_all_once = true;
}