blob: 055f20166a57b038108edc7e0f1ea651d99f440e [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
109// This is all the information sent from the Teensy to each camera.
110struct CameraCalibration {
Brian Silverman2eb89762019-02-17 15:16:37 -0800111 enum class CameraCommand : char {
Brian Silvermana3688802019-02-16 19:31:26 -0800112 // Stay in normal mode.
113 kNormal,
114 // Go to camera passthrough mode.
115 kCameraPassthrough,
116 // Go to being a useful USB device.
117 kUsb,
118 };
119
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?
Alex Perry3bf1bee2019-02-23 20:01:15 -0800142 // [0][0]: mount_angle
143 // [0][1]: focal_length
144 // [0][2]: barrel_mount
Brian Silverman1c0612e2019-01-26 17:26:08 -0800145 Eigen::Matrix<float, 3, 4> calibration;
Brian Silvermana3688802019-02-16 19:31:26 -0800146
147 // A local timestamp from the Teensy. This starts at 0 when the Teensy is
148 // powered on.
149 aos::monotonic_clock::time_point teensy_now;
150
151 // A realtime timestamp from the roboRIO. This will be min_time if the roboRIO
152 // has never sent anything.
153 aos::realtime_clock::time_point realtime_now;
154
155 // What mode the camera should transition into.
156 CameraCommand camera_command;
Brian Silverman1c0612e2019-01-26 17:26:08 -0800157};
158
159// This is all the information the Teensy sends to the RoboRIO.
160struct TeensyToRoborio {
Brian Silverman246cb222019-02-02 16:38:18 -0800161 bool operator==(const TeensyToRoborio &other) const {
162 if (other.frames != frames) {
163 return false;
164 }
165 return true;
166 }
167 bool operator!=(const TeensyToRoborio &other) const {
168 return !(*this == other);
169 }
170
Brian Silverman1c0612e2019-01-26 17:26:08 -0800171 // The newest frames received from up to three cameras. These will be the
172 // three earliest-received of all buffered frames.
173 aos::SizedArray<Frame, 3> frames;
174};
175
176// This is all the information the RoboRIO sends to the Teensy.
177struct RoborioToTeensy {
Brian Silverman246cb222019-02-02 16:38:18 -0800178 bool operator==(const RoborioToTeensy &other) const {
179 if (other.beacon_brightness != beacon_brightness) {
180 return false;
181 }
182 if (other.light_rings != light_rings) {
183 return false;
184 }
185 return true;
186 }
187 bool operator!=(const RoborioToTeensy &other) const {
188 return !(*this == other);
189 }
190
Brian Silverman1c0612e2019-01-26 17:26:08 -0800191 // Brightnesses for each of the beacon light channels. 0 is off, 255 is fully
192 // on.
193 std::array<uint8_t, 3> beacon_brightness;
194
195 // Whether the light ring for each camera should be on.
196 std::bitset<5> light_rings;
Brian Silvermana3688802019-02-16 19:31:26 -0800197
198 // The current time.
199 aos::realtime_clock::time_point realtime_now;
Brian Silverman1c0612e2019-01-26 17:26:08 -0800200};
201
202} // namespace jevois
203} // namespace frc971
204
205#endif // Y2019_JEVOIS_STRUCTURES_H_