blob: 3ccb1dd9f116a648f4e781e247ff119856c94f2a [file] [log] [blame]
Brian Silverman2ccf8c52016-03-15 00:22:26 -04001#include <stdlib.h>
2#include <netdb.h>
3#include <unistd.h>
4
5#include <vector>
6#include <memory>
Brian Silvermanbc831182016-04-16 02:06:09 -04007#include <array>
8#include <thread>
9#include <atomic>
10#include <limits>
Brian Silverman2ccf8c52016-03-15 00:22:26 -040011
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 Silvermanbc831182016-04-16 02:06:09 -040017#include "aos/common/mutex.h"
18
19#include "frc971/control_loops/drivetrain/drivetrain.q.h"
Brian Silverman2ccf8c52016-03-15 00:22:26 -040020
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
26namespace y2016 {
27namespace vision {
28
Austin Schuh11945742016-04-13 22:18:36 -070029::aos::vision::Vector<2> CreateCenterFromTarget(double lx, double ly, double rx,
30 double ry) {
ben54dbccb2016-03-20 14:42:28 -070031 return ::aos::vision::Vector<2>((lx + rx) / 2.0, (ly + ry) / 2.0);
32}
33
34double TargetWidth(double lx, double ly, double rx, double ry) {
35 double dx = lx - rx;
36 double dy = ly - ry;
Austin Schuh098e4872016-03-20 16:51:24 -070037 return ::std::hypot(dx, dy);
ben54dbccb2016-03-20 14:42:28 -070038}
39
40void SelectTargets(const VisionData &left_target,
41 const VisionData &right_target,
42 ::aos::vision::Vector<2> *center_left,
Austin Schuh6bf73052016-04-20 20:20:25 -070043 ::aos::vision::Vector<2> *center_right, double *angle_left,
44 double *angle_right) {
ben54dbccb2016-03-20 14:42:28 -070045 // 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 Schuh9f59c8a2016-03-20 21:09:05 -070066 double min_angle = -1.0;
ben54dbccb2016-03-20 14:42:28 -070067 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 Schuh9f59c8a2016-03-20 21:09:05 -070070 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 Schuh6bf73052016-04-20 20:20:25 -070079 *angle_left = angle;
ben54dbccb2016-03-20 14:42:28 -070080 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 Schuh9f59c8a2016-03-20 21:09:05 -070086
87 double good_ang = min_angle;
ben54dbccb2016-03-20 14:42:28 -070088 double min_ang_err = -1.0;
89 int right_index = -1;
Austin Schuh11945742016-04-13 22:18:36 -070090 // Now pick the bottom edge angle from the right that lines up best with the
91 // left.
ben54dbccb2016-03-20 14:42:28 -070092 for (int j = 0; j < right_target.target_size(); j++) {
93 double wid2 = TargetWidth(right_target.target(j).left_corner_x(),
Austin Schuh11945742016-04-13 22:18:36 -070094 right_target.target(j).left_corner_y(),
95 right_target.target(j).right_corner_x(),
96 right_target.target(j).right_corner_y());
ben54dbccb2016-03-20 14:42:28 -070097 h = right_target.target(j).left_corner_y() -
98 right_target.target(j).right_corner_y();
Austin Schuh11945742016-04-13 22:18:36 -070099 double ang = h / wid2;
ben54dbccb2016-03-20 14:42:28 -0700100 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 Schuh6bf73052016-04-20 20:20:25 -0700104 *angle_right = ang;
ben54dbccb2016-03-20 14:42:28 -0700105 }
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 Schuh11945742016-04-13 22:18:36 -0700120class 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 Schuh6bf73052016-04-20 20:20:25 -0700165void 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 Schuh11945742016-04-13 22:18:36 -0700171 const double age_ratio =
172 (older.capture_time() - newer.last_capture_time()).ToSeconds() /
173 (newer.capture_time() - newer.last_capture_time()).ToSeconds();
Austin Schuh6bf73052016-04-20 20:20:25 -0700174 interpolated_result->Set(
Austin Schuh11945742016-04-13 22:18:36 -0700175 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 Schuh6bf73052016-04-20 20:20:25 -0700177
178 *interpolated_angle = angle * age_ratio + (1 - age_ratio) * last_angle;
Austin Schuh11945742016-04-13 22:18:36 -0700179}
ben54dbccb2016-03-20 14:42:28 -0700180
Brian Silvermanbc831182016-04-16 02:06:09 -0400181// Handles calculating drivetrain offsets.
182class 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 Silverman2ccf8c52016-03-15 00:22:26 -0400290void Main() {
291 StereoGeometry stereo(constants::GetValues().vision_name);
292 LOG(INFO, "calibration: %s\n",
293 stereo.calibration().ShortDebugString().c_str());
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400294
Brian Silvermanbc831182016-04-16 02:06:09 -0400295 DrivetrainOffsetCalculator drivetrain_offset;
296 ::std::thread drivetrain_offset_thread(::std::ref(drivetrain_offset));
297
Austin Schuh11945742016-04-13 22:18:36 -0700298 CameraHandler left;
299 CameraHandler right;
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700300
301 ::aos::vision::RXUdpSocket recv(8080);
302 char rawData[65507];
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700303
304 while (true) {
305 // TODO(austin): Don't malloc.
306 VisionData target;
307 int size = recv.Recv(rawData, 65507);
Austin Schuh11945742016-04-13 22:18:36 -0700308 ::aos::time::Time now = ::aos::time::Time::Now();
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700309
310 if (target.ParseFromArray(rawData, size)) {
311 if (target.camera_index() == 0) {
Austin Schuh11945742016-04-13 22:18:36 -0700312 left.Received(target, now);
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700313 } else {
Austin Schuh11945742016-04-13 22:18:36 -0700314 right.Received(target, now);
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400315 }
316 } else {
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700317 LOG(ERROR, "oh noes: parse error\n");
318 continue;
319 }
320
Austin Schuh11945742016-04-13 22:18:36 -0700321 left.CheckStale(now);
322 right.CheckStale(now);
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400323
Austin Schuh11945742016-04-13 22:18:36 -0700324 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 Schuhc65b0ea2016-03-16 22:09:19 -0700327
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400328 auto new_vision_status = vision_status.MakeMessage();
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700329 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 Schuh11945742016-04-13 22:18:36 -0700332 ::aos::vision::Vector<2> center_left(0.0, 0.0);
333 ::aos::vision::Vector<2> center_right(0.0, 0.0);
Austin Schuh6bf73052016-04-20 20:20:25 -0700334 double angle_left;
335 double angle_right;
Austin Schuh11945742016-04-13 22:18:36 -0700336 SelectTargets(left.target(), right.target(), &center_left,
Austin Schuh6bf73052016-04-20 20:20:25 -0700337 &center_right, &angle_left, &angle_right);
Austin Schuh11945742016-04-13 22:18:36 -0700338
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 Schuh6bf73052016-04-20 20:20:25 -0700343 double last_angle_left;
344 double last_angle_right;
Austin Schuh11945742016-04-13 22:18:36 -0700345 SelectTargets(left.last_target(), right.last_target(),
Austin Schuh6bf73052016-04-20 20:20:25 -0700346 &last_center_left, &last_center_right,
347 &last_angle_left, &last_angle_right);
Austin Schuh11945742016-04-13 22:18:36 -0700348
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 Schuh6bf73052016-04-20 20:20:25 -0700351 double filtered_angle_left;
352 double filtered_angle_right;
Austin Schuh11945742016-04-13 22:18:36 -0700353 if (left.capture_time() < right.capture_time()) {
354 filtered_center_left = center_left;
Austin Schuh6bf73052016-04-20 20:20:25 -0700355 filtered_angle_left = angle_left;
Austin Schuh11945742016-04-13 22:18:36 -0700356 new_vision_status->target_time = left.capture_time().ToNSec();
Austin Schuh6bf73052016-04-20 20:20:25 -0700357 CalculateFiltered(left, right, center_right, last_center_right,
358 angle_right, last_angle_right,
359 &filtered_center_right, &filtered_angle_right);
Austin Schuh11945742016-04-13 22:18:36 -0700360 } else {
361 filtered_center_right = center_right;
Austin Schuh6bf73052016-04-20 20:20:25 -0700362 filtered_angle_right = angle_right;
Austin Schuh11945742016-04-13 22:18:36 -0700363 new_vision_status->target_time = right.capture_time().ToNSec();
Austin Schuh6bf73052016-04-20 20:20:25 -0700364 CalculateFiltered(right, left, center_left, last_center_left,
365 angle_left, last_angle_left, &filtered_center_left,
366 &filtered_angle_left);
Austin Schuh11945742016-04-13 22:18:36 -0700367 }
368
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700369 double distance, horizontal_angle, vertical_angle;
Austin Schuh11945742016-04-13 22:18:36 -0700370 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 Schuhc65b0ea2016-03-16 22:09:19 -0700378 new_vision_status->horizontal_angle = horizontal_angle;
379 new_vision_status->vertical_angle = vertical_angle;
380 new_vision_status->distance = distance;
Austin Schuh6bf73052016-04-20 20:20:25 -0700381 new_vision_status->angle =
382 (filtered_angle_left + filtered_angle_right) / 2.0;
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700383 }
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400384
Brian Silvermanbc831182016-04-16 02:06:09 -0400385 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 Silverman2ccf8c52016-03-15 00:22:26 -0400392 }
393 }
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700394
395 if (target.camera_index() == 0) {
Austin Schuh11945742016-04-13 22:18:36 -0700396 LOG(DEBUG, "left_target: %s\n", left.target().ShortDebugString().c_str());
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700397 } else {
Austin Schuh11945742016-04-13 22:18:36 -0700398 LOG(DEBUG, "right_target: %s\n",
399 right.target().ShortDebugString().c_str());
Austin Schuhc65b0ea2016-03-16 22:09:19 -0700400 }
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400401 }
Brian Silvermanbc831182016-04-16 02:06:09 -0400402
403 drivetrain_offset.Quit();
404 drivetrain_offset_thread.join();
Brian Silverman2ccf8c52016-03-15 00:22:26 -0400405}
406
407} // namespace vision
408} // namespace y2016
409
410int main(int /*argc*/, char ** /*argv*/) {
411 ::aos::InitNRT();
412 ::y2016::vision::Main();
413}