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