forked from Rhoban/Metabot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavior.cpp
More file actions
182 lines (161 loc) · 4.1 KB
/
behavior.cpp
File metadata and controls
182 lines (161 loc) · 4.1 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
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
#include <math.h>
#include <terminal.h>
#include "imu.h"
#include "distance.h"
#include "motion.h"
#include "buzzer.h"
#include "behavior.h"
#include "leds.h"
TERMINAL_PARAMETER_INT(bhv, "Behavior", 0);
void behavior_set(uint8_t b)
{
bhv = b;
}
/**
* Here you can write down your own behaviour, the tick method (at the end of
* this file) is called with a given dt (in s) that you can use for temporal
* counters.
*
* You will find some examples that you can enable by uncommenting the method
* calls below. The buzzer, leds, motors and sensors are used in theses examples,
* allowing you to understand how to recover informations from the different
* robot hardware features.
*/
/**
* Behavior example
* Simply blinks the led green and blue (0.5ms each)
*/
void behavior_blink_leds(float dt)
{
static float t = 0;
t += dt;
if (t > 1) t -= 1;
if (t < 0.5) {
led_set_all(LED_G, true);
} else {
led_set_all(LED_B, true);
}
}
/**
* Behavior example
* Produce a +-30mm sinus on the height of the robot, it will
* then appear to breath
*/
void behavior_sinus_height(float dt)
{
static float t = 0;
t += dt;
motion_set_h(30*sin(t));
}
/**
* Behavior example
* The robot explore, it walks forward and turn around when there is an
* obstacle in the front of it
*/
void behavior_explore(float dt)
{
static int state = 0;
static float t = 0;
t += dt;
if (state == 0) {
// Walking forward
motion_set_x_speed(150);
motion_set_turn_speed(0);
// There is an obstacle, changing state
if (distance_get() < 30) {
t = 0;
state = 1;
// Produce a beep
buzzer_beep(440, 100);
}
} else if (state == 1) {
// Turning around
motion_set_x_speed(0);
motion_set_turn_speed(90);
// 2s have elapsed (at 90°/s), going back to explore
if (t > 2) {
state = 0;
}
}
}
/**
* Behavior example
* The robot walks forward. If it is grabbed and tilted more than 15°, it will
* stop walking and scream.
*/
void behavior_grab(float dt)
{
motion_set_x_speed(150);
if (fabs(imu_pitch()) > 15 || fabs(imu_roll()) > 15) {
buzzer_beep(200, 100);
motion_set_x_speed(0);
}
}
/**
* Behavior example
* The robot will turn around scanning for the longest distance, then it will
* walk a little in this direction and start again.
*/
void behavior_scan_space(float dt)
{
static int state = 0;
static float t = 0;
static float best_yaw = 0;
static float best_dist = 0;
t += dt;
if (state == 0) {
// Scanning around
motion_set_x_speed(0);
motion_set_turn_speed(90);
float dist = distance_get();
if (dist > best_dist) {
buzzer_beep(800, 50);
best_dist = dist;
best_yaw = imu_yaw();
}
if (t > 4) {
state = 1;
}
} else if (state == 1) {
// Aligning with the best seen yaw
float err = imu_yaw()-best_yaw;
float order = err;
if (order > 90) order = 90;
if (order > 0 && order < 30) order = 30;
if (order < -90) order = -90;
if (order < 0 && order > -30) order = -30;
motion_set_x_speed(0);
motion_set_turn_speed(order);
if (fabs(err) < 6) {
state = 2;
t = 0;
}
} else if (state == 2) {
// Walking a little
motion_set_x_speed(189);
motion_set_turn_speed(0);
if (t > 2) {
state = 0;
t = 0;
best_dist = 0;
}
}
}
void behavior_tick(float dt)
{
static int lastBhv = 0;
if (bhv == 1) behavior_blink_leds(dt);
if (bhv == 2) behavior_sinus_height(dt);
if (bhv == 3) behavior_explore(dt);
if (bhv == 4) behavior_grab(dt);
if (bhv == 5) behavior_scan_space(dt);
if (bhv == 0 && lastBhv != 0) {
leds_decustom();
motion_set_x_speed(0);
motion_set_y_speed(0);
motion_set_turn_speed(0);
motion_reset();
buzzer_stop();
}
lastBhv = bhv;
}