Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 1 | #include <netdb.h> |
| 2 | #include <unistd.h> |
| 3 | |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 4 | #include <array> |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 5 | #include <atomic> |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 6 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cstdlib> |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 8 | #include <limits> |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <thread> |
| 11 | #include <vector> |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 12 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "aos/events/event_loop.h" |
| 14 | #include "aos/events/shm_event_loop.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 15 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 16 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 17 | #include "aos/time/time.h" |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 18 | #include "aos/vision/events/udp.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | #include "frc971/control_loops/drivetrain/drivetrain_status_generated.h" |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 20 | #include "y2016/constants.h" |
| 21 | #include "y2016/vision/stereo_geometry.h" |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 22 | #include "y2016/vision/vision_data.pb.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | #include "y2016/vision/vision_generated.h" |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 24 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 25 | namespace y2016::vision { |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 26 | |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 27 | namespace chrono = ::std::chrono; |
| 28 | using ::aos::monotonic_clock; |
| 29 | |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 30 | ::aos::vision::Vector<2> CreateCenterFromTarget(double lx, double ly, double rx, |
| 31 | double ry) { |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 32 | return ::aos::vision::Vector<2>((lx + rx) / 2.0, (ly + ry) / 2.0); |
| 33 | } |
| 34 | |
| 35 | double TargetWidth(double lx, double ly, double rx, double ry) { |
| 36 | double dx = lx - rx; |
| 37 | double dy = ly - ry; |
Austin Schuh | 098e487 | 2016-03-20 16:51:24 -0700 | [diff] [blame] | 38 | return ::std::hypot(dx, dy); |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void SelectTargets(const VisionData &left_target, |
| 42 | const VisionData &right_target, |
| 43 | ::aos::vision::Vector<2> *center_left, |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 44 | ::aos::vision::Vector<2> *center_right, double *angle_left, |
| 45 | double *angle_right) { |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 46 | // No good targets. Let the caller decide defaults. |
| 47 | if (right_target.target_size() == 0 || left_target.target_size() == 0) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // Only one option, we have to go with it. |
| 52 | if (right_target.target_size() == 1 && left_target.target_size() == 1) { |
| 53 | *center_left = |
| 54 | CreateCenterFromTarget(left_target.target(0).left_corner_x(), |
| 55 | left_target.target(0).left_corner_y(), |
| 56 | left_target.target(0).right_corner_x(), |
| 57 | left_target.target(0).right_corner_y()); |
| 58 | *center_right = |
| 59 | CreateCenterFromTarget(right_target.target(0).left_corner_x(), |
| 60 | right_target.target(0).left_corner_y(), |
| 61 | right_target.target(0).right_corner_x(), |
| 62 | right_target.target(0).right_corner_y()); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | // Now we have to make a decision. |
Austin Schuh | 9f59c8a | 2016-03-20 21:09:05 -0700 | [diff] [blame] | 67 | double min_angle = -1.0; |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 68 | int left_index = 0; |
| 69 | // First pick the widest target from the left. |
| 70 | for (int i = 0; i < left_target.target_size(); i++) { |
Austin Schuh | 9f59c8a | 2016-03-20 21:09:05 -0700 | [diff] [blame] | 71 | const double h = left_target.target(i).left_corner_y() - |
| 72 | left_target.target(i).right_corner_y(); |
| 73 | const double wid1 = TargetWidth(left_target.target(i).left_corner_x(), |
| 74 | left_target.target(i).left_corner_y(), |
| 75 | left_target.target(i).right_corner_x(), |
| 76 | left_target.target(i).right_corner_y()); |
| 77 | const double angle = h / wid1; |
| 78 | if (min_angle == -1.0 || ::std::abs(angle) < ::std::abs(min_angle)) { |
| 79 | min_angle = angle; |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 80 | *angle_left = angle; |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 81 | left_index = i; |
| 82 | } |
| 83 | } |
| 84 | // Calculate the angle of the bottom edge for the left. |
| 85 | double h = left_target.target(left_index).left_corner_y() - |
| 86 | left_target.target(left_index).right_corner_y(); |
Austin Schuh | 9f59c8a | 2016-03-20 21:09:05 -0700 | [diff] [blame] | 87 | |
| 88 | double good_ang = min_angle; |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 89 | double min_ang_err = -1.0; |
| 90 | int right_index = -1; |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 91 | // Now pick the bottom edge angle from the right that lines up best with the |
| 92 | // left. |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 93 | for (int j = 0; j < right_target.target_size(); j++) { |
| 94 | double wid2 = TargetWidth(right_target.target(j).left_corner_x(), |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 95 | right_target.target(j).left_corner_y(), |
| 96 | right_target.target(j).right_corner_x(), |
| 97 | right_target.target(j).right_corner_y()); |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 98 | h = right_target.target(j).left_corner_y() - |
| 99 | right_target.target(j).right_corner_y(); |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 100 | double ang = h / wid2; |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 101 | double ang_err = ::std::abs(good_ang - ang); |
| 102 | if (min_ang_err == -1.0 || min_ang_err > ang_err) { |
| 103 | min_ang_err = ang_err; |
| 104 | right_index = j; |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 105 | *angle_right = ang; |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | *center_left = |
| 110 | CreateCenterFromTarget(left_target.target(left_index).left_corner_x(), |
| 111 | left_target.target(left_index).left_corner_y(), |
| 112 | left_target.target(left_index).right_corner_x(), |
| 113 | left_target.target(left_index).right_corner_y()); |
| 114 | *center_right = |
| 115 | CreateCenterFromTarget(right_target.target(right_index).left_corner_x(), |
| 116 | right_target.target(right_index).left_corner_y(), |
| 117 | right_target.target(right_index).right_corner_x(), |
| 118 | right_target.target(right_index).right_corner_y()); |
| 119 | } |
| 120 | |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 121 | class CameraHandler { |
| 122 | public: |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 123 | void Received(const VisionData &target, monotonic_clock::time_point now) { |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 124 | if (current_.received) { |
| 125 | last_ = current_; |
| 126 | } |
| 127 | current_.target = target; |
| 128 | current_.rx_time = now; |
Austin Schuh | 6fe1d51 | 2016-04-24 19:12:04 -0700 | [diff] [blame] | 129 | current_.capture_time = now - |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 130 | chrono::nanoseconds(target.send_timestamp() - |
| 131 | target.image_timestamp()) + |
Austin Schuh | 6fe1d51 | 2016-04-24 19:12:04 -0700 | [diff] [blame] | 132 | // It takes a bit to shoot a frame. Push the frame |
| 133 | // further back in time. |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 134 | chrono::milliseconds(10); |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 135 | current_.received = true; |
| 136 | } |
| 137 | |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 138 | void CheckStale(monotonic_clock::time_point now) { |
| 139 | if (now > current_.rx_time + chrono::milliseconds(50)) { |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 140 | current_.received = false; |
| 141 | last_.received = false; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | bool received_both() const { return current_.received && last_.received; } |
| 146 | |
| 147 | bool is_valid() const { |
| 148 | return current_.target.target_size() > 0 && last_.target.target_size() > 0; |
| 149 | } |
| 150 | |
| 151 | const VisionData &target() const { return current_.target; } |
| 152 | const VisionData &last_target() const { return last_.target; } |
| 153 | |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 154 | monotonic_clock::time_point capture_time() const { |
| 155 | return current_.capture_time; |
| 156 | } |
| 157 | monotonic_clock::time_point last_capture_time() const { |
| 158 | return last_.capture_time; |
| 159 | } |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | struct TargetWithTimes { |
| 163 | VisionData target; |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 164 | monotonic_clock::time_point rx_time{monotonic_clock::epoch()}; |
| 165 | monotonic_clock::time_point capture_time{monotonic_clock::epoch()}; |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 166 | bool received = false; |
| 167 | }; |
| 168 | |
| 169 | TargetWithTimes current_; |
| 170 | TargetWithTimes last_; |
| 171 | }; |
| 172 | |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 173 | void CalculateFiltered(const CameraHandler &older, const CameraHandler &newer, |
| 174 | const ::aos::vision::Vector<2> &newer_center, |
| 175 | const ::aos::vision::Vector<2> &last_newer_center, |
| 176 | double angle, double last_angle, |
| 177 | ::aos::vision::Vector<2> *interpolated_result, |
| 178 | double *interpolated_angle) { |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 179 | const double age_ratio = |
| 180 | ::aos::time::DurationInSeconds(older.capture_time() - |
| 181 | newer.last_capture_time()) / |
| 182 | ::aos::time::DurationInSeconds(newer.capture_time() - |
| 183 | newer.last_capture_time()); |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 184 | interpolated_result->Set( |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 185 | newer_center.x() * age_ratio + (1 - age_ratio) * last_newer_center.x(), |
| 186 | newer_center.y() * age_ratio + (1 - age_ratio) * last_newer_center.y()); |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 187 | |
| 188 | *interpolated_angle = angle * age_ratio + (1 - age_ratio) * last_angle; |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 189 | } |
ben | 54dbccb | 2016-03-20 14:42:28 -0700 | [diff] [blame] | 190 | |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 191 | // Handles calculating drivetrain offsets. |
| 192 | class DrivetrainOffsetCalculator { |
| 193 | public: |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 194 | DrivetrainOffsetCalculator(::aos::EventLoop *event_loop) |
| 195 | : drivetrain_status_fetcher_( |
| 196 | event_loop |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 197 | ->MakeFetcher<::frc971::control_loops::drivetrain::Status>( |
| 198 | "/drivetrain")) {} |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 199 | |
| 200 | // Takes a vision status message with everything except |
| 201 | // drivetrain_{left,right}_position set and sets those. |
| 202 | // Returns false if it doesn't have enough data to fill them out. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 203 | bool CompleteVisionStatus(::y2016::vision::VisionStatusT *status) { |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 204 | while (drivetrain_status_fetcher_.FetchNext()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 205 | data_[data_index_].time = |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 206 | drivetrain_status_fetcher_.context().monotonic_event_time; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 207 | data_[data_index_].left = |
| 208 | drivetrain_status_fetcher_->estimated_left_position(); |
| 209 | data_[data_index_].right = |
| 210 | drivetrain_status_fetcher_->estimated_right_position(); |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 211 | ++data_index_; |
| 212 | if (data_index_ == data_.size()) data_index_ = 0; |
| 213 | if (valid_data_ < data_.size()) ++valid_data_; |
| 214 | } |
| 215 | |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 216 | if (valid_data_ == 0) return false; |
| 217 | |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 218 | const monotonic_clock::time_point capture_time = |
| 219 | monotonic_clock::time_point(chrono::nanoseconds(status->target_time)); |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 220 | DrivetrainData before, after; |
| 221 | FindBeforeAfter(&before, &after, capture_time); |
| 222 | |
| 223 | if (before.time == after.time) { |
| 224 | status->drivetrain_left_position = before.left; |
| 225 | status->drivetrain_right_position = before.right; |
| 226 | } else { |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 227 | const double age_ratio = |
| 228 | ::aos::time::DurationInSeconds(capture_time - before.time) / |
| 229 | ::aos::time::DurationInSeconds(after.time - before.time); |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 230 | status->drivetrain_left_position = |
| 231 | before.left * (1 - age_ratio) + after.left * age_ratio; |
| 232 | status->drivetrain_right_position = |
| 233 | before.right * (1 - age_ratio) + after.right * age_ratio; |
| 234 | } |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 239 | private: |
| 240 | struct DrivetrainData { |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 241 | monotonic_clock::time_point time; |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 242 | double left, right; |
| 243 | }; |
| 244 | |
| 245 | // Fills out before and after with the data surrounding capture_time. |
| 246 | // They might be identical if that's the closest approximation. |
| 247 | // Do not call this if valid_data_ is 0. |
| 248 | void FindBeforeAfter(DrivetrainData *before, DrivetrainData *after, |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 249 | monotonic_clock::time_point capture_time) { |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 250 | size_t location = 0; |
| 251 | while (true) { |
| 252 | // We hit the end of our data. Just fill them both out as the last data |
| 253 | // point. |
| 254 | if (location >= valid_data_) { |
| 255 | *before = *after = |
| 256 | data_[previous_index((valid_data_ + data_index_) % data_.size())]; |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // The index into data_ corresponding to location positions after |
| 261 | // (data_index_ - 1). |
| 262 | const size_t index = previous_index(location + data_index_); |
| 263 | |
| 264 | // If we've found the one we want. |
| 265 | if (data_[index].time > capture_time) { |
| 266 | *after = data_[index]; |
| 267 | if (location == 0) { |
| 268 | // If this is the first one and it's already after, just return the |
| 269 | // same thing for both. |
| 270 | *before = data_[index]; |
| 271 | } else { |
| 272 | *before = data_[previous_index(index)]; |
| 273 | } |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | ++location; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | size_t previous_index(size_t index) const { |
| 282 | if (index == 0) { |
| 283 | return data_.size() - 1; |
| 284 | } else { |
| 285 | return index - 1; |
| 286 | } |
| 287 | } |
| 288 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 289 | ::aos::Fetcher<::frc971::control_loops::drivetrain::Status> |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 290 | drivetrain_status_fetcher_; |
| 291 | |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 292 | ::std::array<DrivetrainData, 200> data_; |
| 293 | // The index into data_ the next data point is going at. |
| 294 | size_t data_index_ = 0; |
| 295 | // How many elemets of data_ are valid. |
| 296 | size_t valid_data_ = 0; |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 297 | }; |
| 298 | |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 299 | void Main() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 300 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 301 | aos::configuration::ReadConfig("aos_config.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 302 | |
| 303 | ::aos::ShmEventLoop event_loop(&config.message()); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 304 | |
| 305 | ::aos::Sender<::y2016::vision::VisionStatus> vision_status_sender = |
Tyler Chatow | 24b5db1 | 2020-01-06 21:16:56 -0800 | [diff] [blame] | 306 | event_loop.MakeSender<::y2016::vision::VisionStatus>("/vision"); |
Austin Schuh | 1bf8a21 | 2019-05-26 22:13:14 -0700 | [diff] [blame] | 307 | |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 308 | StereoGeometry stereo(constants::GetValues().vision_name); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 309 | AOS_LOG(INFO, "calibration: %s\n", |
| 310 | stereo.calibration().ShortDebugString().c_str()); |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 311 | |
Austin Schuh | bd0a40f | 2019-06-30 14:56:31 -0700 | [diff] [blame] | 312 | DrivetrainOffsetCalculator drivetrain_offset(&event_loop); |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 313 | |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 314 | CameraHandler left; |
| 315 | CameraHandler right; |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 316 | |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 317 | ::aos::events::RXUdpSocket recv(8080); |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 318 | char rawData[65507]; |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 319 | |
| 320 | while (true) { |
| 321 | // TODO(austin): Don't malloc. |
| 322 | VisionData target; |
| 323 | int size = recv.Recv(rawData, 65507); |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 324 | monotonic_clock::time_point now = monotonic_clock::now(); |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 325 | |
| 326 | if (target.ParseFromArray(rawData, size)) { |
| 327 | if (target.camera_index() == 0) { |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 328 | left.Received(target, now); |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 329 | } else { |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 330 | right.Received(target, now); |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 331 | } |
| 332 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 333 | AOS_LOG(ERROR, "oh noes: parse error\n"); |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 334 | continue; |
| 335 | } |
| 336 | |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 337 | left.CheckStale(now); |
| 338 | right.CheckStale(now); |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 339 | |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 340 | if (left.received_both() && right.received_both()) { |
| 341 | const bool left_image_valid = left.is_valid(); |
| 342 | const bool right_image_valid = right.is_valid(); |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 343 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 344 | auto builder = vision_status_sender.MakeBuilder(); |
| 345 | VisionStatusT new_vision_status; |
| 346 | new_vision_status.left_image_valid = left_image_valid; |
| 347 | new_vision_status.right_image_valid = right_image_valid; |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 348 | if (left_image_valid && right_image_valid) { |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 349 | ::aos::vision::Vector<2> center_left(0.0, 0.0); |
| 350 | ::aos::vision::Vector<2> center_right(0.0, 0.0); |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 351 | double angle_left; |
| 352 | double angle_right; |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 353 | SelectTargets(left.target(), right.target(), ¢er_left, |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 354 | ¢er_right, &angle_left, &angle_right); |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 355 | |
| 356 | // TODO(Ben): Remember this from last time instead of recalculating it |
| 357 | // each time. |
| 358 | ::aos::vision::Vector<2> last_center_left(0.0, 0.0); |
| 359 | ::aos::vision::Vector<2> last_center_right(0.0, 0.0); |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 360 | double last_angle_left; |
| 361 | double last_angle_right; |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 362 | SelectTargets(left.last_target(), right.last_target(), |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 363 | &last_center_left, &last_center_right, &last_angle_left, |
| 364 | &last_angle_right); |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 365 | |
| 366 | ::aos::vision::Vector<2> filtered_center_left(0.0, 0.0); |
| 367 | ::aos::vision::Vector<2> filtered_center_right(0.0, 0.0); |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 368 | double filtered_angle_left; |
| 369 | double filtered_angle_right; |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 370 | if (left.capture_time() < right.capture_time()) { |
| 371 | filtered_center_left = center_left; |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 372 | filtered_angle_left = angle_left; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 373 | new_vision_status.target_time = |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 374 | chrono::duration_cast<chrono::nanoseconds>( |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 375 | left.capture_time().time_since_epoch()) |
| 376 | .count(); |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 377 | CalculateFiltered(left, right, center_right, last_center_right, |
| 378 | angle_right, last_angle_right, |
| 379 | &filtered_center_right, &filtered_angle_right); |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 380 | } else { |
| 381 | filtered_center_right = center_right; |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 382 | filtered_angle_right = angle_right; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 383 | new_vision_status.target_time = |
Austin Schuh | fb5f7de | 2016-11-26 15:15:19 -0800 | [diff] [blame] | 384 | chrono::duration_cast<chrono::nanoseconds>( |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 385 | right.capture_time().time_since_epoch()) |
| 386 | .count(); |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 387 | CalculateFiltered(right, left, center_left, last_center_left, |
| 388 | angle_left, last_angle_left, &filtered_center_left, |
| 389 | &filtered_angle_left); |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 392 | double distance, horizontal_angle, vertical_angle; |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 393 | stereo.Process(filtered_center_left, filtered_center_right, &distance, |
| 394 | &horizontal_angle, &vertical_angle); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 395 | new_vision_status.left_image_timestamp = |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 396 | left.target().image_timestamp(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 397 | new_vision_status.right_image_timestamp = |
Austin Schuh | 1194574 | 2016-04-13 22:18:36 -0700 | [diff] [blame] | 398 | right.target().image_timestamp(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 399 | new_vision_status.left_send_timestamp = left.target().send_timestamp(); |
| 400 | new_vision_status.right_send_timestamp = |
Parker Schuh | 2cd173d | 2017-01-28 00:12:01 -0800 | [diff] [blame] | 401 | right.target().send_timestamp(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 402 | new_vision_status.horizontal_angle = horizontal_angle; |
| 403 | new_vision_status.vertical_angle = vertical_angle; |
| 404 | new_vision_status.distance = distance; |
| 405 | new_vision_status.angle = |
Austin Schuh | 6bf7305 | 2016-04-20 20:20:25 -0700 | [diff] [blame] | 406 | (filtered_angle_left + filtered_angle_right) / 2.0; |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 407 | } |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 408 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 409 | if (drivetrain_offset.CompleteVisionStatus(&new_vision_status)) { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 410 | if (builder.Send( |
| 411 | VisionStatus::Pack(*builder.fbb(), &new_vision_status)) != |
| 412 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 413 | AOS_LOG(ERROR, "Failed to send vision information\n"); |
Brian Silverman | bc83118 | 2016-04-16 02:06:09 -0400 | [diff] [blame] | 414 | } |
| 415 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 416 | AOS_LOG(WARNING, "vision without drivetrain"); |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 417 | } |
| 418 | } |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 419 | |
| 420 | if (target.camera_index() == 0) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 421 | AOS_LOG(DEBUG, "left_target: %s\n", |
| 422 | left.target().ShortDebugString().c_str()); |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 423 | } else { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 424 | AOS_LOG(DEBUG, "right_target: %s\n", |
| 425 | right.target().ShortDebugString().c_str()); |
Austin Schuh | c65b0ea | 2016-03-16 22:09:19 -0700 | [diff] [blame] | 426 | } |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 427 | } |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 428 | } |
| 429 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 430 | } // namespace y2016::vision |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 431 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 432 | int main(int argc, char **argv) { |
| 433 | ::aos::InitGoogle(&argc, &argv); |
Brian Silverman | 2ccf8c5 | 2016-03-15 00:22:26 -0400 | [diff] [blame] | 434 | ::y2016::vision::Main(); |
| 435 | } |