-
Notifications
You must be signed in to change notification settings - Fork 145
/
main_ws2_p2.cpp
191 lines (152 loc) · 7.56 KB
/
main_ws2_p2.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "mbed.h"
// pes board pin map
#include "pm2_drivers/PESBoardPinMap.h"
// drivers
#include "pm2_drivers/DebounceIn.h"
#include "pm2_drivers/Servo.h"
#include "pm2_drivers/UltrasonicSensor.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(USER_BUTTON); // create DebounceIn object to evaluate the user button
// falling and rising edge
void toggle_do_execute_main_fcn(); // custom function which is getting executed when user
// button gets pressed, definition below
// main runs as an own thread
int main()
{
// set up states for state machine
enum RobotState {
INITIAL,
EXECUTION,
SLEEP,
EMERGENCY
} robot_state = RobotState::INITIAL;
// attach button fall function address to user button object, button has a pull-up resistor
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, there for
// 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(USER_LED);
// additional led
// create DigitalOut object to command extra led, you need to add an aditional resistor, e.g. 220...500 Ohm
// a led has an anode (+) and a cathode (-), the cathode needs to be connected to ground via a resistor
DigitalOut led1(PB_9);
// mechanical button
DigitalIn mechanical_button(PC_5); // create DigitalIn object to evaluate mechanical button, you
// need to specify the mode for proper usage, see below
mechanical_button.mode(PullUp); // sets pullup between pin and 3.3 V, so that there
// is a defined potential
// ultra sonic sensor
UltrasonicSensor us_sensor(PB_D3);
float us_distance_cm = 0.0f;
// min and max ultra sonic sensor reading
float us_distance_min = 6.0f;
float us_distance_max = 40.0f;
// servo
Servo servo_D0(PB_D0);
Servo servo_D1(PB_D1);
// minimal pulse width and maximal pulse width obtained from the servo calibration process
// futuba S3001
float servo_D0_ang_min = 0.0150f;
float servo_D0_ang_max = 0.1150f;
// reely S0090
float servo_D1_ang_min = 0.0325f;
float servo_D1_ang_max = 0.1175f;
// servo.setNormalisedPulseWidth: before calibration (0,1) -> (min pwm, max pwm)
// servo.setNormalisedPulseWidth: after calibration (0,1) -> (servo_D0_ang_min, servo_D0_ang_max)
servo_D0.calibratePulseMinMax(servo_D0_ang_min, servo_D0_ang_max);
servo_D1.calibratePulseMinMax(servo_D1_ang_min, servo_D1_ang_max);
// variables to move the servo, this is just an example
float servo_input = 0.0f;
// start timer
main_task_timer.start();
// this loop will run forever
while (true) {
main_task_timer.reset();
if (do_execute_main_task) {
// visual feedback that the main task is executed, setting this once would actually be enough
led1 = 1;
// read us sensor distance, only valid measurements will update us_distance_cm
const float us_distance_cm_candidate = us_sensor.read();
if (us_distance_cm_candidate > 0.0f) {
us_distance_cm = us_distance_cm_candidate;
}
// state machine
switch (robot_state) {
case RobotState::INITIAL:
printf("INITIAL\n");
// enable the servo
if (!servo_D0.isEnabled())
servo_D0.enable();
robot_state = RobotState::EXECUTION;
break;
case RobotState::EXECUTION:
printf("EXECUTION\n");
// function to map the distance to the servo movement (us_distance_min, us_distance_max) -> (0.0f, 1.0f)
servo_input = (us_distance_cm - us_distance_min) / (us_distance_max - us_distance_min);
// values smaller than 0.0f or bigger than 1.0f ar constrained to the range (0.0f, 1.0f) in setNormalisedPulseWidth
servo_D0.setNormalisedPulseWidth(servo_input);
// if the measurement is outside the min or max limit go to SLEEP
if ((us_distance_cm < us_distance_min) || (us_distance_cm > us_distance_max)) {
robot_state = RobotState::SLEEP;
}
// if the mechanical button is pressed go to EMERGENCY
if (mechanical_button.read()) {
robot_state = RobotState::EMERGENCY;
}
break;
case RobotState::SLEEP:
printf("SLEEP\n");
// if the measurement is within the min and max limits go to EXECUTION
if ((us_distance_cm > us_distance_min) && (us_distance_cm < us_distance_max)) {
robot_state = RobotState::EXECUTION;
}
// if the mechanical button is pressed go to EMERGENCY
if (mechanical_button.read()) {
robot_state = RobotState::EMERGENCY;
}
break;
case RobotState::EMERGENCY:
printf("EMERGENCY\n");
// the transition to the emergency state causes the execution of the commands contained
// in the outer else statement scope, and since do_reset_all_once is true the system undergoes a reset
toggle_do_execute_main_fcn();
break;
default:
break; // do nothing
}
} else {
// the following code block gets executed only once
if (do_reset_all_once) {
do_reset_all_once = false;
// reset variables and objects
led1 = 0;
servo_D0.disable();
us_distance_cm = 0.0f;
robot_state = RobotState::INITIAL;
}
}
// toggling the user led
user_led = !user_led;
// print to the serial terminal
printf("US distance cm: %f \n", us_distance_cm);
// read timer and make the main thread sleep for the remaining time span (non blocking)
int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
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;
}