Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 1 | #ifndef Y2019_JEVOIS_STRUCTURES_H_ |
| 2 | #define Y2019_JEVOIS_STRUCTURES_H_ |
| 3 | |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 4 | #include <array> |
| 5 | #include <bitset> |
| 6 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cstdint> |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 8 | |
| 9 | #include "Eigen/Dense" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 11 | #include "aos/containers/sized_array.h" |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 12 | #include "aos/time/time.h" |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 13 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 14 | namespace frc971::jevois { |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 15 | |
| 16 | // The overall flow to get data to the roboRIO consists of: |
| 17 | // 1. Camera captures a frame and grabs an absolute timestamp. |
| 18 | // 2. Camera processes the frame. |
| 19 | // 3. Camera grabs another absolute timestamp and subtracts to get a |
| 20 | // camera_duration. |
| 21 | // 4. Camera sends the frame via UART to the Teensy. |
| 22 | // 5. Teensy grabs an absolute timestamp for the first character received. |
| 23 | // 6. Teensy buffers at most one frame from each camera. |
| 24 | // 7. roboRIO toggles the CS line. |
| 25 | // 8. Teensy grabs an absolute timestamp for CS being asserted. |
| 26 | // 9. Teensy pulls together up to three frames and adds the time each one |
| 27 | // spent in its queue to the timestamps, and queues them in its SPI |
| 28 | // peripheral. This all happens before the roboRIO has enough time to start |
| 29 | // actually moving data. |
| 30 | // 10. roboRIO transfers the frames, and sends back light/status commands. |
| 31 | |
| 32 | using camera_duration = std::chrono::duration<uint8_t, std::milli>; |
| 33 | |
| 34 | // This file declares the shared datastructures for the JeVois-based image |
| 35 | // system. |
| 36 | // |
| 37 | // Note that floating-point numbers are represented with floats. This is because |
| 38 | // a float has more bits than we need for anything, and the Teensy can't handle |
| 39 | // doubles very quickly. It would probably be quick enough, but it's easier to |
| 40 | // just use floats and not worry about it. |
| 41 | |
| 42 | struct Target { |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 43 | bool operator==(const Target &other) const { |
| 44 | if (other.distance != distance) { |
| 45 | return false; |
| 46 | } |
| 47 | if (other.height != height) { |
| 48 | return false; |
| 49 | } |
| 50 | if (other.heading != heading) { |
| 51 | return false; |
| 52 | } |
| 53 | if (other.skew != skew) { |
| 54 | return false; |
| 55 | } |
| 56 | return true; |
| 57 | } |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 58 | bool operator!=(const Target &other) const { return !(*this == other); } |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 59 | |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 60 | // Distance to the target in meters. Specifically, the distance from the |
| 61 | // center of the camera's image plane to the center of the target. |
| 62 | float distance; |
| 63 | |
James Kuszmaul | 81df16a | 2019-03-03 17:17:34 -0800 | [diff] [blame] | 64 | // Height of the target in meters. Specifically, the distance from the camera |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 65 | // to the center of the target. |
| 66 | float height; |
| 67 | |
| 68 | // Heading of the center of the target in radians. Zero is straight out |
| 69 | // perpendicular to the camera's image plane. Images to the left (looking at a |
| 70 | // camera image) are at a positive angle. |
| 71 | float heading; |
| 72 | |
| 73 | // The angle between the target and the camera's image plane. This is |
| 74 | // projected so both are assumed to be perpendicular to the floor. Parallel |
| 75 | // targets have a skew of zero. Targets rotated such that their left edge |
| 76 | // (looking at a camera image) is closer are at a positive angle. |
| 77 | float skew; |
| 78 | }; |
| 79 | |
| 80 | // The information extracted from a single camera frame. |
| 81 | // |
| 82 | // This is all the information sent from each camera to the Teensy. |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 83 | struct CameraFrame { |
| 84 | bool operator==(const CameraFrame &other) const { |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 85 | if (other.targets != targets) { |
| 86 | return false; |
| 87 | } |
| 88 | if (other.age != age) { |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
| 92 | } |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 93 | bool operator!=(const CameraFrame &other) const { return !(*this == other); } |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 94 | |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 95 | // The top most interesting targets found in this frame. |
| 96 | aos::SizedArray<Target, 3> targets; |
| 97 | |
| 98 | // How long ago from the current time this frame was captured. |
| 99 | camera_duration age; |
| 100 | }; |
| 101 | |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 102 | // The information extracted from a single camera frame, from a given camera. |
| 103 | struct RoborioFrame { |
| 104 | bool operator==(const RoborioFrame &other) const { |
| 105 | if (other.targets != targets) { |
| 106 | return false; |
| 107 | } |
| 108 | if (other.age != age) { |
| 109 | return false; |
| 110 | } |
| 111 | if (other.camera_index != camera_index) { |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 116 | bool operator!=(const RoborioFrame &other) const { return !(*this == other); } |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 117 | |
| 118 | // The top most interesting targets found in this frame. |
| 119 | aos::SizedArray<Target, 3> targets; |
| 120 | |
| 121 | // How long ago from the current time this frame was captured. |
| 122 | camera_duration age; |
| 123 | // Which camera this is from (which position on the robot, not a serial |
| 124 | // number). |
| 125 | int camera_index; |
| 126 | }; |
| 127 | |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 128 | enum class CameraCommand : char { |
| 129 | // Stay in normal mode. |
| 130 | kNormal, |
| 131 | // Go to camera passthrough mode. |
| 132 | kCameraPassthrough, |
| 133 | // Go to being a useful USB device. |
| 134 | kUsb, |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 135 | // Send As, which triggers the bootstrap script to drop directly into USB |
| 136 | // mode. |
| 137 | kAs, |
Austin Schuh | 4e2629d | 2019-03-28 14:44:37 -0700 | [diff] [blame] | 138 | // Log camera images |
| 139 | kLog, |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 142 | // This is all the information sent from the Teensy to each camera. |
| 143 | struct CameraCalibration { |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 144 | bool operator==(const CameraCalibration &other) const { |
| 145 | if (other.calibration != calibration) { |
| 146 | return false; |
| 147 | } |
Brian Silverman | 2eb8976 | 2019-02-17 15:16:37 -0800 | [diff] [blame] | 148 | if (other.teensy_now != teensy_now) { |
| 149 | return false; |
| 150 | } |
| 151 | if (other.realtime_now != realtime_now) { |
| 152 | return false; |
| 153 | } |
| 154 | if (other.camera_command != camera_command) { |
| 155 | return false; |
| 156 | } |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | bool operator!=(const CameraCalibration &other) const { |
| 160 | return !(*this == other); |
| 161 | } |
| 162 | |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 163 | // The calibration matrix. This defines where the camera is pointing. |
| 164 | // |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 165 | // TODO(Parker): What are the details on how this is defined? |
Alex Perry | 3bf1bee | 2019-02-23 20:01:15 -0800 | [diff] [blame] | 166 | // [0][0]: mount_angle |
| 167 | // [0][1]: focal_length |
| 168 | // [0][2]: barrel_mount |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 169 | Eigen::Matrix<float, 3, 4> calibration; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 170 | |
| 171 | // A local timestamp from the Teensy. This starts at 0 when the Teensy is |
| 172 | // powered on. |
| 173 | aos::monotonic_clock::time_point teensy_now; |
| 174 | |
| 175 | // A realtime timestamp from the roboRIO. This will be min_time if the roboRIO |
| 176 | // has never sent anything. |
| 177 | aos::realtime_clock::time_point realtime_now; |
| 178 | |
| 179 | // What mode the camera should transition into. |
| 180 | CameraCommand camera_command; |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | // This is all the information the Teensy sends to the RoboRIO. |
| 184 | struct TeensyToRoborio { |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 185 | bool operator==(const TeensyToRoborio &other) const { |
| 186 | if (other.frames != frames) { |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | bool operator!=(const TeensyToRoborio &other) const { |
| 192 | return !(*this == other); |
| 193 | } |
| 194 | |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 195 | // The newest frames received from up to three cameras. These will be the |
| 196 | // three earliest-received of all buffered frames. |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 197 | aos::SizedArray<RoborioFrame, 3> frames; |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | // This is all the information the RoboRIO sends to the Teensy. |
| 201 | struct RoborioToTeensy { |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 202 | bool operator==(const RoborioToTeensy &other) const { |
| 203 | if (other.beacon_brightness != beacon_brightness) { |
| 204 | return false; |
| 205 | } |
| 206 | if (other.light_rings != light_rings) { |
| 207 | return false; |
| 208 | } |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 209 | if (other.realtime_now != realtime_now) { |
| 210 | return false; |
| 211 | } |
| 212 | if (other.camera_command != camera_command) { |
| 213 | return false; |
| 214 | } |
Brian Silverman | 246cb22 | 2019-02-02 16:38:18 -0800 | [diff] [blame] | 215 | return true; |
| 216 | } |
| 217 | bool operator!=(const RoborioToTeensy &other) const { |
| 218 | return !(*this == other); |
| 219 | } |
| 220 | |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 221 | // Brightnesses for each of the beacon light channels. 0 is off, 255 is fully |
| 222 | // on. |
| 223 | std::array<uint8_t, 3> beacon_brightness; |
| 224 | |
| 225 | // Whether the light ring for each camera should be on. |
| 226 | std::bitset<5> light_rings; |
Brian Silverman | a368880 | 2019-02-16 19:31:26 -0800 | [diff] [blame] | 227 | |
| 228 | // The current time. |
| 229 | aos::realtime_clock::time_point realtime_now; |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 230 | |
| 231 | // A command to send to all the cameras. |
| 232 | CameraCommand camera_command; |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 233 | }; |
| 234 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 235 | } // namespace frc971::jevois |
Brian Silverman | 1c0612e | 2019-01-26 17:26:08 -0800 | [diff] [blame] | 236 | |
| 237 | #endif // Y2019_JEVOIS_STRUCTURES_H_ |