blob: 259e5fd018d102f40537b0075f129b5ec013c5e3 [file] [log] [blame]
Parker Schuh4d2978f2017-02-25 11:13:06 -08001#include <netdb.h>
2
John Park33858a32018-09-28 23:05:48 -07003#include "aos/logging/logging.h"
4#include "aos/logging/queue_logging.h"
5#include "aos/time/time.h"
John Park398c74a2018-10-20 21:17:39 -07006#include "aos/init.h"
Parker Schuh4d2978f2017-02-25 11:13:06 -08007#include "aos/vision/events/udp.h"
Parker Schuhabb6b6c2017-03-11 16:31:24 -08008#include "y2017/vision/target_finder.h"
Parker Schuh4d2978f2017-02-25 11:13:06 -08009#include "y2017/vision/vision.q.h"
Parker Schuhd497ed62017-03-04 20:11:58 -080010#include "y2017/vision/vision_result.pb.h"
Parker Schuh4d2978f2017-02-25 11:13:06 -080011
Austin Schuh218a7552017-03-22 21:15:28 -070012namespace y2017 {
13namespace vision {
14
Parker Schuh4d2978f2017-02-25 11:13:06 -080015using aos::monotonic_clock;
16
Austin Schuh218a7552017-03-22 21:15:28 -070017int Main() {
Parker Schuh4d2978f2017-02-25 11:13:06 -080018 ::aos::events::RXUdpSocket recv(8080);
19 char raw_data[65507];
Parker Schuhabb6b6c2017-03-11 16:31:24 -080020 // TODO(parker): Have this pull in a config from somewhere.
21 TargetFinder finder;
Parker Schuh4d2978f2017-02-25 11:13:06 -080022
23 while (true) {
24 // TODO(austin): Don't malloc.
Parker Schuhd497ed62017-03-04 20:11:58 -080025 VisionResult target;
Parker Schuh4d2978f2017-02-25 11:13:06 -080026 int size = recv.Recv(raw_data, sizeof(raw_data));
27 monotonic_clock::time_point now = monotonic_clock::now();
28 auto target_time = now -
29 std::chrono::nanoseconds(target.send_timestamp() -
Austin Schuh218a7552017-03-22 21:15:28 -070030 target.image_timestamp()) -
Parker Schuh4d2978f2017-02-25 11:13:06 -080031 // It takes a bit to shoot a frame. Push the frame
32 // further back in time.
Austin Schuh218a7552017-03-22 21:15:28 -070033 std::chrono::milliseconds(60);
Parker Schuh4d2978f2017-02-25 11:13:06 -080034
35 if (!target.ParseFromArray(raw_data, size)) {
36 continue;
37 }
38
39 auto new_vision_status = vision_status.MakeMessage();
40 new_vision_status->image_valid = target.has_target();
41 if (new_vision_status->image_valid) {
42 new_vision_status->target_time =
43 std::chrono::duration_cast<std::chrono::nanoseconds>(
44 target_time.time_since_epoch())
45 .count();
46
Parker Schuhabb6b6c2017-03-11 16:31:24 -080047 finder.GetAngleDist(
48 aos::vision::Vector<2>(target.target().x(), target.target().y()),
49 /* TODO: Insert down estimate here in radians: */ 0.0,
50 &new_vision_status->distance, &new_vision_status->angle);
Parker Schuh4d2978f2017-02-25 11:13:06 -080051 }
52
53 LOG_STRUCT(DEBUG, "vision", *new_vision_status);
54 if (!new_vision_status.Send()) {
55 LOG(ERROR, "Failed to send vision information\n");
56 }
57 }
58}
Austin Schuh218a7552017-03-22 21:15:28 -070059
60} // namespace vision
61} // namespace y2017
62
63int main(int /*argc*/, char ** /*argv*/) {
64 ::aos::InitNRT();
65 ::y2017::vision::Main();
66}