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