blob: aa4fc079e697e91d93f65b88642070f951963d11 [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.
88struct Frame {
Brian Silverman246cb222019-02-02 16:38:18 -080089 bool operator==(const Frame &other) const {
90 if (other.targets != targets) {
91 return false;
92 }
93 if (other.age != age) {
94 return false;
95 }
96 return true;
97 }
98 bool operator!=(const Frame &other) const {
99 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 Silvermane9924fd2019-03-02 15:20:42 -0800109enum class CameraCommand : char {
110 // Stay in normal mode.
111 kNormal,
112 // Go to camera passthrough mode.
113 kCameraPassthrough,
114 // Go to being a useful USB device.
115 kUsb,
116};
117
Brian Silverman1c0612e2019-01-26 17:26:08 -0800118// This is all the information sent from the Teensy to each camera.
119struct CameraCalibration {
Brian Silverman246cb222019-02-02 16:38:18 -0800120 bool operator==(const CameraCalibration &other) const {
121 if (other.calibration != calibration) {
122 return false;
123 }
Brian Silverman2eb89762019-02-17 15:16:37 -0800124 if (other.teensy_now != teensy_now) {
125 return false;
126 }
127 if (other.realtime_now != realtime_now) {
128 return false;
129 }
130 if (other.camera_command != camera_command) {
131 return false;
132 }
Brian Silverman246cb222019-02-02 16:38:18 -0800133 return true;
134 }
135 bool operator!=(const CameraCalibration &other) const {
136 return !(*this == other);
137 }
138
Brian Silverman1c0612e2019-01-26 17:26:08 -0800139 // The calibration matrix. This defines where the camera is pointing.
140 //
Brian Silverman246cb222019-02-02 16:38:18 -0800141 // TODO(Parker): What are the details on how this is defined?
Brian Silverman1c0612e2019-01-26 17:26:08 -0800142 Eigen::Matrix<float, 3, 4> calibration;
Brian Silvermana3688802019-02-16 19:31:26 -0800143
144 // A local timestamp from the Teensy. This starts at 0 when the Teensy is
145 // powered on.
146 aos::monotonic_clock::time_point teensy_now;
147
148 // A realtime timestamp from the roboRIO. This will be min_time if the roboRIO
149 // has never sent anything.
150 aos::realtime_clock::time_point realtime_now;
151
152 // What mode the camera should transition into.
153 CameraCommand camera_command;
Brian Silverman1c0612e2019-01-26 17:26:08 -0800154};
155
156// This is all the information the Teensy sends to the RoboRIO.
157struct TeensyToRoborio {
Brian Silverman246cb222019-02-02 16:38:18 -0800158 bool operator==(const TeensyToRoborio &other) const {
159 if (other.frames != frames) {
160 return false;
161 }
162 return true;
163 }
164 bool operator!=(const TeensyToRoborio &other) const {
165 return !(*this == other);
166 }
167
Brian Silverman1c0612e2019-01-26 17:26:08 -0800168 // The newest frames received from up to three cameras. These will be the
169 // three earliest-received of all buffered frames.
170 aos::SizedArray<Frame, 3> frames;
171};
172
173// This is all the information the RoboRIO sends to the Teensy.
174struct RoborioToTeensy {
Brian Silverman246cb222019-02-02 16:38:18 -0800175 bool operator==(const RoborioToTeensy &other) const {
176 if (other.beacon_brightness != beacon_brightness) {
177 return false;
178 }
179 if (other.light_rings != light_rings) {
180 return false;
181 }
182 return true;
183 }
184 bool operator!=(const RoborioToTeensy &other) const {
185 return !(*this == other);
186 }
187
Brian Silverman1c0612e2019-01-26 17:26:08 -0800188 // Brightnesses for each of the beacon light channels. 0 is off, 255 is fully
189 // on.
190 std::array<uint8_t, 3> beacon_brightness;
191
192 // Whether the light ring for each camera should be on.
193 std::bitset<5> light_rings;
Brian Silvermana3688802019-02-16 19:31:26 -0800194
195 // The current time.
196 aos::realtime_clock::time_point realtime_now;
Brian Silvermane9924fd2019-03-02 15:20:42 -0800197
198 // A command to send to all the cameras.
199 CameraCommand camera_command;
Brian Silverman1c0612e2019-01-26 17:26:08 -0800200};
201
202} // namespace jevois
203} // namespace frc971
204
205#endif // Y2019_JEVOIS_STRUCTURES_H_