-
Notifications
You must be signed in to change notification settings - Fork 4
/
walls.c
220 lines (193 loc) · 5.5 KB
/
walls.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "walls.h"
#define SIDE_WALL_DETECTION (CELL_DIMENSION * 0.90)
#define FRONT_WALL_DETECTION (CELL_DIMENSION * 1.5)
#define SIDE_CALIBRATION_READINGS 20
#define DIAGONAL_MIN_DISTANCE 0.24
static volatile float distance[NUM_SENSOR];
static volatile float calibration_factor[NUM_SENSOR];
const float sensors_calibration_a[NUM_SENSOR] = {
SENSOR_SIDE_LEFT_A, SENSOR_SIDE_RIGHT_A, SENSOR_FRONT_LEFT_A,
SENSOR_FRONT_RIGHT_A};
const float sensors_calibration_b[NUM_SENSOR] = {
SENSOR_SIDE_LEFT_B, SENSOR_SIDE_RIGHT_B, SENSOR_FRONT_LEFT_B,
SENSOR_FRONT_RIGHT_B};
/**
* @brief Calculate and update the distance from each sensor.
*
* @note The distances are calculated from the center of the robot.
*/
void update_distance_readings(void)
{
uint8_t i = 0;
uint16_t on[NUM_SENSOR], off[NUM_SENSOR];
get_sensors_raw(on, off);
for (i = 0; i < NUM_SENSOR; i++) {
distance[i] =
(sensors_calibration_a[i] / sensors_raw_log(on[i], off[i]) -
sensors_calibration_b[i]);
if ((i == SENSOR_SIDE_LEFT_ID) || (i == SENSOR_SIDE_RIGHT_ID))
distance[i] -= calibration_factor[i];
}
}
/**
* @brief Get distance value from front left sensor.
*/
float get_front_left_distance(void)
{
return distance[SENSOR_FRONT_LEFT_ID];
}
/**
* @brief Get distance value from front right sensor.
*/
float get_front_right_distance(void)
{
return distance[SENSOR_FRONT_RIGHT_ID];
}
/**
* @brief Get distance value from side left sensor.
*/
float get_side_left_distance(void)
{
return distance[SENSOR_SIDE_LEFT_ID];
}
/**
* @brief Get distance value from side right sensor.
*/
float get_side_right_distance(void)
{
return distance[SENSOR_SIDE_RIGHT_ID];
}
/**
* @brief Calculate and return the side sensors error when object is too close.
*
* Taking into account that the walls are parallel to the robot, this function
* returns the distance that the robot is moved from the center of the
* corridor.
*/
float get_side_sensors_close_error(void)
{
float left_error;
float right_error;
left_error = distance[SENSOR_SIDE_LEFT_ID] - MIDDLE_MAZE_DISTANCE;
right_error = distance[SENSOR_SIDE_RIGHT_ID] - MIDDLE_MAZE_DISTANCE;
if ((left_error > 0.) && (right_error < 0.))
return right_error;
if ((right_error > 0.) && (left_error < 0.))
return -left_error;
return 0.;
}
/**
* @brief Calculate and return the side sensors error when object is too far.
*
* This is useful when the robot is too far away from a lateral wall on one
* side but there is no wall on the other side.
*/
float get_side_sensors_far_error(void)
{
float left_error;
float right_error;
left_error = distance[SENSOR_SIDE_LEFT_ID] - MIDDLE_MAZE_DISTANCE;
right_error = distance[SENSOR_SIDE_RIGHT_ID] - MIDDLE_MAZE_DISTANCE;
if ((left_error > 0.1) && (right_error < 0.04))
return right_error;
if ((right_error > 0.1) && (left_error < 0.04))
return -left_error;
return 0.;
}
/**
* @brief Calculate and return the front sensors error.
*
* Taking into account that robot is approaching to a perpendicular wall, this
* function returns the difference between the front sensors distances.
*
* If there is no front wall detected, it returns 0.
*/
float get_front_sensors_error(void)
{
if (!front_wall_detection())
return 0.;
return distance[SENSOR_FRONT_LEFT_ID] - distance[SENSOR_FRONT_RIGHT_ID];
}
/**
* @brief Calculate and return the diagonal sensors error.
*
* This function returns an error if the robot is too close to a pillar. The
* error is calculated as the difference between the sensed distance and a
* minimum distance threshold.
*/
float get_diagonal_sensors_error(void)
{
float left_error;
float right_error;
left_error = distance[SENSOR_FRONT_LEFT_ID] - DIAGONAL_MIN_DISTANCE;
right_error = distance[SENSOR_FRONT_RIGHT_ID] - DIAGONAL_MIN_DISTANCE;
if (right_error < 0.)
return right_error;
if (left_error < 0.)
return -left_error;
return 0.;
}
/**
* @brief Return the front wall distance, in meters.
*/
float get_front_wall_distance(void)
{
return (distance[SENSOR_FRONT_LEFT_ID] +
distance[SENSOR_FRONT_RIGHT_ID]) /
2.;
}
/**
* @brief Detect the existance or absence of the left wall.
*/
bool left_wall_detection(void)
{
return (distance[SENSOR_SIDE_LEFT_ID] < SIDE_WALL_DETECTION) ? true
: false;
}
/**
* @brief Detect the existance or absence of the right wall.
*/
bool right_wall_detection(void)
{
return (distance[SENSOR_SIDE_RIGHT_ID] < SIDE_WALL_DETECTION) ? true
: false;
}
/**
* @brief Detect the existance or absence of the front wall.
*/
bool front_wall_detection(void)
{
return ((distance[SENSOR_FRONT_LEFT_ID] < FRONT_WALL_DETECTION) &&
(distance[SENSOR_FRONT_RIGHT_ID] < FRONT_WALL_DETECTION))
? true
: false;
}
/**
* @brief Return left, front and right walls detection readings.
*/
struct walls_around read_walls(void)
{
struct walls_around walls_readings;
walls_readings.left = left_wall_detection();
walls_readings.front = front_wall_detection();
walls_readings.right = right_wall_detection();
return walls_readings;
}
/**
* @brief Calibration for side sensors.
*/
void side_sensors_calibration(void)
{
float right_temp = 0;
float left_temp = 0;
int i;
for (i = 0; i < SIDE_CALIBRATION_READINGS; i++) {
left_temp += distance[SENSOR_SIDE_LEFT_ID];
right_temp += distance[SENSOR_SIDE_RIGHT_ID];
sleep_ticks(SENSORS_SM_TICKS);
}
calibration_factor[SENSOR_SIDE_LEFT_ID] +=
(left_temp / SIDE_CALIBRATION_READINGS) - MIDDLE_MAZE_DISTANCE;
calibration_factor[SENSOR_SIDE_RIGHT_ID] +=
(right_temp / SIDE_CALIBRATION_READINGS) - MIDDLE_MAZE_DISTANCE;
}