-
Notifications
You must be signed in to change notification settings - Fork 4
/
lunatic.h
384 lines (305 loc) · 13 KB
/
lunatic.h
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
aurora/LUNATIC: pronounced "lun-AT-ic" (definitely not "lun-A-tic")
Logically UNcoupled Architecture Technology for Intra-robot Communication
This file manages data exchange between all on-robot
software components, including these programs:
vision manages the realsense camera
Reads color and depth frames from the camera
Passes color to aruco to look for computer vision markers
Any detected markers create position estimates for the localizer
Passes depth to obstacle detector subsystem to look for rocks
Any detected obstacles get written to the obstacle grid
localizer integrates the robot position
Reads drive encoder counts from the backend
Reads aruco data from the vision subsystem
Reads camera pointing from the stepper motor
Publishes coordinates used by all other components
stepper talks to the camera pointing stepper motor
Reads requested view angle from backend
Publishes current view angle to the localizat
path planner computes an obstacle-free drive path
Reads the robot location from the localizer
Reads the target location from the backend
Reads obstacle grid from the obstacle detector
Publishes drive commands to the backend
backend talks to the robot, using two subsystems:
BAC: Basic Autonomy Control
manages the autonomy state machine and pilot comms (over UDP)
KEND: Keep Electronics Not Dying
talks to the Arduino, and restarts it if needed.
This is the project Interface Control Document (ICD).
*/
#ifndef __AURORA_LUNATIC_H
#define __AURORA_LUNATIC_H
#include <stdio.h>
#include <array>
#include "../aurora/data_exchange.h"
#include "../aurora/coords.h"
#include "../vision/grid.hpp"
#include "../../firmware/nano_net.h"
namespace aurora {
/* --------------- Nano Net ------------
Low level microcontroller communication.
*/
struct nano_net_data {
nano_net::nano_net_setup setup[nano_net::n_nanos];
nano_net::nano_net_command command[nano_net::n_nanos];
nano_net::nano_net_sensors sensor[nano_net::n_nanos];
void print(int n=0,FILE *f=stdout, const char *terminator="\n") const {
fprintf(f,"nano.setup[%d]: { motorMode: [",n);
for (int m=0;m<nano_net::n_motors;m++)
fprintf(f,"'%c' ",setup[n].motorMode[m]);
fprintf(f,"], sensorMode: [");
for (int s=0;s<nano_net::n_sensors;s++)
fprintf(f,"'%c' ",setup[n].sensorMode[s]);
fprintf(f,"]}%s",terminator);
fprintf(f,"nano.command[%d]: { %s %s %s, speed: [",
n,
command[n].stop?"STOP":"run",
command[n].torque?"TORQUE":"speed",
command[n].LED?"LED":"dark");
for (int m=0;m<nano_net::n_motors;m++)
fprintf(f,"%4d ",(int)command[n].speed[m]);
fprintf(f,"]}%s",terminator);
fprintf(f,"nano.sensor[%d]: ok:%d, heartbeat:%d, [", n,
(int)sensor[n].ok,(int)sensor[n].heartbeat);
for (int s=0;s<nano_net::n_sensors;s++)
{
fprintf(f,"{%s%s %3d}, ",
((n<nano_net::n_motors) && ((sensor[n].stall>>s)&1))?"STALL":" ",
((sensor[n].raw>>s)&1)?"#":"_",
(int)sensor[n].counts[s]);
}
fprintf(f,"]}%s",terminator);
}
};
/* This macro declares the variable used to communicate with the nano_net
Setup and commands written by the bac part of the backend
Sensors written by the kend modules for each nano
*/
#define MAKE_exchange_nano_net() aurora::data_exchange<aurora::nano_net_data> exchange_nano_net("nano.net")
/* -------------- Drive Command ----------------
Track speed commands, for the left and right tracks.
Values are speed percent.
speed=0 means stop.
speed=+100 means full speed forward.
speed=-100 means full speed reverse.
E.g., left=+100; right=0; makes a right pivot around the right track.
*/
struct drive_commands {
float left;
float right;
// Set left and right power to zero
void stop(void) {
left=right=0.0f;
}
// Return true if this value makes sense and obeys the limits
bool is_sane(void) const {
const float limit=150.0f;
if (left >= -limit && left <= limit
&& right >= -limit && right <= limit)
return true;
else
return false;
}
void print(FILE *f=stdout, const char *terminator="\n") const {
fprintf(f,
"drive: %5.1f L, %5.1f R%s",
left, right,
terminator);
}
};
/* This macro declares the variable used to
send the backend a drive command:
Written by the path planner
Read by the backend to send to the motors when in autonomous drive mode
Read by the localizer to predict future motion
*/
#define MAKE_exchange_drive_commands() aurora::data_exchange<aurora::drive_commands> exchange_drive_commands("backend.drive")
/* -------------- Drive Encoders --------------
Wheel encoder ticks: these capture the total forward motion
taken by the left and right tracks.
These are total motion since startup, not incremental.
Units are centimeters.
E.g., at startup, left=right=0.0;
After driving backwards by 0.5 meters, left=right=-50.0;
After driving backwards another 0.5 meters, left=right=-100.0;
After driving forwards by 1.5 meters, left=right=+50.0;
*/
struct drive_encoders {
typedef double real_t;
real_t left;
real_t right;
// Subtract two sets of encoder values (typically current-previous)
// to get relative motion.
drive_encoders operator-(const drive_encoders other) const {
drive_encoders ret;
ret.left=left - other.left;
ret.right=right - other.right;
return ret;
}
void print(FILE *f=stdout, const char *terminator="\n") const {
fprintf(f,
"encoders: %5.2f L, %5.2f R%s",
(float)left, (float)right,
terminator);
}
};
/* This macro declares the variable used to
report the current drive track encoders:
Written by the backend
Read by the localizer to track current motion
*/
#define MAKE_exchange_drive_encoders() aurora::data_exchange<aurora::drive_encoders> exchange_drive_encoders("backend.encoders")
/* -------------- Camera Pointing via Stepper Motor --------------
This same struct is used to request or report the stepper motor position.
*/
struct stepper_pointing {
float angle; // robot-relative camera pointing angle, in degrees. 0 == robot forward
int32_t stable; // 0: on break, homing, etc. 1: should be steady.
void print(FILE *f=stdout, const char *terminator="\n") const {
fprintf(f,
"stepper: %5.0f angle, %s%s",
angle, stable?"stable":"unstable",
terminator);
}
};
/* This macro declares the variable used to
request a new stepper motor position.
Written by the backend
Read by the stepper motor controller
*/
#define MAKE_exchange_stepper_request() aurora::data_exchange<aurora::stepper_pointing> exchange_stepper_request("stepper_request.angle")
/* This macro declares the variable used to
report the current stepper motor position:
Written by the stepper motor controller
Read by the localizer to bake camera coordinate transforms
*/
#define MAKE_exchange_stepper_report() aurora::data_exchange<aurora::stepper_pointing> exchange_stepper_report("stepper_report.angle")
/* -------------- Robot Localization and Navigation Coordinates --------------
See aurora/coords.h for robot_loc2D and robot_coord3D.
*/
/* This macro declares the variable used to
report the robot position for path planning:
Written by the localizer
Read by the path planner
Coordinate system: absolute field coordinates
*/
#define MAKE_exchange_plan_current() aurora::data_exchange<aurora::robot_loc2D> exchange_plan_current("plan_current.loc2D")
/* This macro declares the variable used to
target a new drive position for path planning:
Written by the backend when it wants to drive somewhere new
Read by the path planner
Coordinate system: absolute field coordinates
*/
#define MAKE_exchange_plan_target() aurora::data_exchange<aurora::robot_navtarget> exchange_plan_target("plan_target.loc2D")
#define MAKE_exchange_T265() aurora::data_exchange<aurora::robot_coord3D> exchange_T265("t265.loc3D")
/* This macro declares the variable used to
project depth camera obstacles into field coordinates:
Written by the localizer as the robot and camera coordinates change
Read by the obstacle detection subsystem
Coordinate system: absolute field coordinates
*/
#define MAKE_exchange_obstacle_view() aurora::data_exchange<aurora::robot_coord3D> exchange_obstacle_view("obstacle_view.loc3D")
// This is one report of a computer vision marker currently in-view.
// Coordinate system: camera-relative coordinates, scaled to unit marker size.
struct vision_marker_report {
robot_coord3D coords; // camera-relative coordinates of marker
int32_t markerID; // 0 if invalid, or an Aruco marker ID
// Return true if we're a valid report
inline bool is_valid() const { return markerID!=0; }
enum {max_count=2};
vision_marker_report() :markerID(0) {}
vision_marker_report(float x, float y, float angle, int32_t id){
markerID = id;
coords.origin.x=x;
coords.origin.y=y;
coords.origin.z=0.0;
coords.X = aurora::vec3_from_angle(angle);
coords.Y = aurora::vec3_from_angle(angle+90.0);
coords.Z.x = 0.0;
coords.Z.y = 0.0;
coords.Z.z = 0.0;
}
void print(FILE *f=stdout, const char *terminator="\n") const {
fprintf(f,"marker%d: ",markerID);
coords.print(f,terminator);
}
};
//******This must be defined in the file using the #define Make_exchange*****
// This is an array of reports for all currently visible markers
typedef std::array<vision_marker_report, vision_marker_report::max_count> vision_marker_reports;
/* This macro declares the variable used to
report a computer vision derived marker position to the localizer:
Written by the computer vision system when it sees an aruco marker
Read by the localizer
Coordinate system: camera-relative coordinates
*/
#define MAKE_exchange_marker_reports() aurora::data_exchange<aurora::vision_marker_reports> exchange_marker_reports("vision_marker.reports")
/* -------------- Robot Obstacle Detection for Navigation --------------
Stores a rasterized top-down view of the robot field.
grid_pixel: datatype at each pixel
*/
template <typename grid_pixel>
class field_raster {
public:
enum {GRIDSIZE=obstacle_grid::GRIDSIZE}; // cm per pixel
enum {GRIDX=obstacle_grid::GRIDX}; // xy pixel dimensions
enum {GRIDY=obstacle_grid::GRIDY};
enum {GRIDTOTAL=GRIDX*GRIDY}; // total pixel
grid_pixel raster[GRIDTOTAL];
grid_pixel &at(int x,int y) { return raster[y*GRIDX + x]; }
const grid_pixel &at(int x,int y) const { return raster[y*GRIDX + x]; }
/* Return true if this point has data (in range, and count >0) */
bool in_bounds(int x,int y) const {
if (x<0 || x>=GRIDX || y<0 || y>=GRIDY) return false;
return true;
}
/* Assign this value everywhere */
void clear(const grid_pixel &value) {
for (size_t i=0;i<GRIDTOTAL;i++) raster[i]=value;
}
};
/*
For each point on the field, store an unsigned char
indicating what state this part of the field is in:
*/
enum {
field_unknown=0, // we have no data (uninitialized)
field_fixed=10, // we expect this part of the field to be occupied (e.g., bin)
field_mined=20, // we dug deep with our mining head into this area
field_toolow=30, // looks like a crater here
field_sloped=50, // slopes seem too high
field_toohigh=80, // we saw a tall obstacle here
field_driveable = 100, // below this: not driveable. Above this: driveable
field_flat=120, // seen and looks flat(enough)
field_driven=250 // we have driven here before (definitely safe)
};
typedef field_raster<unsigned char> field_drivable;
/* This macro declares the variable used to
store the field grid of obstacle / drivable locations:
Written by the cartographer
Read by the pathplanner
*/
#define MAKE_exchange_field_drivable() aurora::data_exchange<aurora::field_drivable> exchange_field_drivable("field_drivable.grid")
/* This macro declares the variable used to
store the field grid of obstacle / drivable locations:
Written by the computer vision system
Read by the cartographer
*/
// #define MAKE_exchange_field_raw() aurora::data_exchange<aurora::field_drivable> exchange_field_raw("field_raw.grid")
#define MAKE_exchange_field_raw() aurora::data_exchange<obstacle_grid> exchange_field_raw("field_raw.grid")
/* ----------- Path Planning debugging ---------- */
class path_plan {
public:
// Target of the last planned path, or (0,0) if none.
robot_navtarget target;
// Positions along the currently planned path
unsigned short plan_len;
enum {max_path_len=30};
typedef robot_loc2D path_t;
path_t path_plan[max_path_len];
};
#define MAKE_exchange_path_plan() aurora::data_exchange<aurora::path_plan> exchange_path_plan("path_plan.path")
}; // end namespace
#endif