blob: b6db24e923e3851591c6f9f4827dc90b56d84a9d [file] [log] [blame]
Brian Silverman1c0612e2019-01-26 17:26:08 -08001#ifndef Y2019_JEVOIS_STRUCTURES_H_
2#define Y2019_JEVOIS_STRUCTURES_H_
3
4#include <stdint.h>
5
6#include <array>
7#include <bitset>
8#include <chrono>
9
10#include "Eigen/Dense"
11
12#include "aos/containers/sized_array.h"
Brian Silvermana3688802019-02-16 19:31:26 -080013#include "aos/time/time.h"
Brian Silvermanfdfb3132019-02-24 15:27:27 -080014#include "third_party/GSL/include/gsl/gsl"
Brian Silverman1c0612e2019-01-26 17:26:08 -080015
16namespace frc971 {
17namespace jevois {
18
19// The overall flow to get data to the roboRIO consists of:
20// 1. Camera captures a frame and grabs an absolute timestamp.
21// 2. Camera processes the frame.
22// 3. Camera grabs another absolute timestamp and subtracts to get a
23// camera_duration.
24// 4. Camera sends the frame via UART to the Teensy.
25// 5. Teensy grabs an absolute timestamp for the first character received.
26// 6. Teensy buffers at most one frame from each camera.
27// 7. roboRIO toggles the CS line.
28// 8. Teensy grabs an absolute timestamp for CS being asserted.
29// 9. Teensy pulls together up to three frames and adds the time each one
30// spent in its queue to the timestamps, and queues them in its SPI
31// peripheral. This all happens before the roboRIO has enough time to start
32// actually moving data.
33// 10. roboRIO transfers the frames, and sends back light/status commands.
34
35using camera_duration = std::chrono::duration<uint8_t, std::milli>;
36
37// This file declares the shared datastructures for the JeVois-based image
38// system.
39//
40// Note that floating-point numbers are represented with floats. This is because
41// a float has more bits than we need for anything, and the Teensy can't handle
42// doubles very quickly. It would probably be quick enough, but it's easier to
43// just use floats and not worry about it.
44
45struct Target {
Brian Silverman246cb222019-02-02 16:38:18 -080046 bool operator==(const Target &other) const {
47 if (other.distance != distance) {
48 return false;
49 }
50 if (other.height != height) {
51 return false;
52 }
53 if (other.heading != heading) {
54 return false;
55 }
56 if (other.skew != skew) {
57 return false;
58 }
59 return true;
60 }
61 bool operator!=(const Target &other) const {
62 return !(*this == other);
63 }
64
Brian Silverman1c0612e2019-01-26 17:26:08 -080065 // Distance to the target in meters. Specifically, the distance from the
66 // center of the camera's image plane to the center of the target.
67 float distance;
68
69 // Height of the target in meters. Specifically, the distance from the floor
70 // to the center of the target.
71 float height;
72
73 // Heading of the center of the target in radians. Zero is straight out
74 // perpendicular to the camera's image plane. Images to the left (looking at a
75 // camera image) are at a positive angle.
76 float heading;
77
78 // The angle between the target and the camera's image plane. This is
79 // projected so both are assumed to be perpendicular to the floor. Parallel
80 // targets have a skew of zero. Targets rotated such that their left edge
81 // (looking at a camera image) is closer are at a positive angle.
82 float skew;
83};
84
85// The information extracted from a single camera frame.
86//
87// This is all the information sent from each camera to the Teensy.
Brian Silvermanc41fb862019-03-02 21:14:46 -080088struct CameraFrame {
89 bool operator==(const CameraFrame &other) const {
Brian Silverman246cb222019-02-02 16:38:18 -080090 if (other.targets != targets) {
91 return false;
92 }
93 if (other.age != age) {
94 return false;
95 }
96 return true;
97 }
Brian Silvermanc41fb862019-03-02 21:14:46 -080098 bool operator!=(const CameraFrame &other) const {
Brian Silverman246cb222019-02-02 16:38:18 -080099 return !(*this == other);
100 }
101
Brian Silverman1c0612e2019-01-26 17:26:08 -0800102 // The top most interesting targets found in this frame.
103 aos::SizedArray<Target, 3> targets;
104
105 // How long ago from the current time this frame was captured.
106 camera_duration age;
107};
108
Brian Silvermanc41fb862019-03-02 21:14:46 -0800109// The information extracted from a single camera frame, from a given camera.
110struct RoborioFrame {
111 bool operator==(const RoborioFrame &other) const {
112 if (other.targets != targets) {
113 return false;
114 }
115 if (other.age != age) {
116 return false;
117 }
118 if (other.camera_index != camera_index) {
119 return false;
120 }
121 return true;
122 }
123 bool operator!=(const RoborioFrame &other) const {
124 return !(*this == other);
125 }
126
127 // The top most interesting targets found in this frame.
128 aos::SizedArray<Target, 3> targets;
129
130 // How long ago from the current time this frame was captured.
131 camera_duration age;
132 // Which camera this is from (which position on the robot, not a serial
133 // number).
134 int camera_index;
135};
136
Brian Silvermane9924fd2019-03-02 15:20:42 -0800137enum class CameraCommand : char {
138 // Stay in normal mode.
139 kNormal,
140 // Go to camera passthrough mode.
141 kCameraPassthrough,
142 // Go to being a useful USB device.
143 kUsb,
144};
145
Brian Silverman1c0612e2019-01-26 17:26:08 -0800146// This is all the information sent from the Teensy to each camera.
147struct CameraCalibration {
Brian Silverman246cb222019-02-02 16:38:18 -0800148 bool operator==(const CameraCalibration &other) const {
149 if (other.calibration != calibration) {
150 return false;
151 }
Brian Silverman2eb89762019-02-17 15:16:37 -0800152 if (other.teensy_now != teensy_now) {
153 return false;
154 }
155 if (other.realtime_now != realtime_now) {
156 return false;
157 }
158 if (other.camera_command != camera_command) {
159 return false;
160 }
Brian Silverman246cb222019-02-02 16:38:18 -0800161 return true;
162 }
163 bool operator!=(const CameraCalibration &other) const {
164 return !(*this == other);
165 }
166
Brian Silverman1c0612e2019-01-26 17:26:08 -0800167 // The calibration matrix. This defines where the camera is pointing.
168 //
Brian Silverman246cb222019-02-02 16:38:18 -0800169 // TODO(Parker): What are the details on how this is defined?
Alex Perry3bf1bee2019-02-23 20:01:15 -0800170 // [0][0]: mount_angle
171 // [0][1]: focal_length
172 // [0][2]: barrel_mount
Brian Silverman1c0612e2019-01-26 17:26:08 -0800173 Eigen::Matrix<float, 3, 4> calibration;
Brian Silvermana3688802019-02-16 19:31:26 -0800174
175 // A local timestamp from the Teensy. This starts at 0 when the Teensy is
176 // powered on.
177 aos::monotonic_clock::time_point teensy_now;
178
179 // A realtime timestamp from the roboRIO. This will be min_time if the roboRIO
180 // has never sent anything.
181 aos::realtime_clock::time_point realtime_now;
182
183 // What mode the camera should transition into.
184 CameraCommand camera_command;
Brian Silverman1c0612e2019-01-26 17:26:08 -0800185};
186
187// This is all the information the Teensy sends to the RoboRIO.
188struct TeensyToRoborio {
Brian Silverman246cb222019-02-02 16:38:18 -0800189 bool operator==(const TeensyToRoborio &other) const {
190 if (other.frames != frames) {
191 return false;
192 }
193 return true;
194 }
195 bool operator!=(const TeensyToRoborio &other) const {
196 return !(*this == other);
197 }
198
Brian Silverman1c0612e2019-01-26 17:26:08 -0800199 // The newest frames received from up to three cameras. These will be the
200 // three earliest-received of all buffered frames.
Brian Silvermanc41fb862019-03-02 21:14:46 -0800201 aos::SizedArray<RoborioFrame, 3> frames;
Brian Silverman1c0612e2019-01-26 17:26:08 -0800202};
203
204// This is all the information the RoboRIO sends to the Teensy.
205struct RoborioToTeensy {
Brian Silverman246cb222019-02-02 16:38:18 -0800206 bool operator==(const RoborioToTeensy &other) const {
207 if (other.beacon_brightness != beacon_brightness) {
208 return false;
209 }
210 if (other.light_rings != light_rings) {
211 return false;
212 }
Brian Silvermanc41fb862019-03-02 21:14:46 -0800213 if (other.realtime_now != realtime_now) {
214 return false;
215 }
216 if (other.camera_command != camera_command) {
217 return false;
218 }
Brian Silverman246cb222019-02-02 16:38:18 -0800219 return true;
220 }
221 bool operator!=(const RoborioToTeensy &other) const {
222 return !(*this == other);
223 }
224
Brian Silverman1c0612e2019-01-26 17:26:08 -0800225 // Brightnesses for each of the beacon light channels. 0 is off, 255 is fully
226 // on.
227 std::array<uint8_t, 3> beacon_brightness;
228
229 // Whether the light ring for each camera should be on.
230 std::bitset<5> light_rings;
Brian Silvermana3688802019-02-16 19:31:26 -0800231
232 // The current time.
233 aos::realtime_clock::time_point realtime_now;
Brian Silvermane9924fd2019-03-02 15:20:42 -0800234
235 // A command to send to all the cameras.
236 CameraCommand camera_command;
Brian Silverman1c0612e2019-01-26 17:26:08 -0800237};
238
239} // namespace jevois
240} // namespace frc971
241
242#endif // Y2019_JEVOIS_STRUCTURES_H_