blob: f96a95e1026e67ea4f54065d4ff9505e2dc5acf7 [file] [log] [blame]
Parker Schuh4d2978f2017-02-25 11:13:06 -08001#include <netdb.h>
2
3#include "aos/common/logging/logging.h"
4#include "aos/common/logging/queue_logging.h"
5#include "aos/common/time.h"
6#include "aos/linux_code/init.h"
7#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
12using aos::monotonic_clock;
13
Parker Schuh4d2978f2017-02-25 11:13:06 -080014int main() {
15 using namespace y2017::vision;
16 ::aos::events::RXUdpSocket recv(8080);
17 char raw_data[65507];
Parker Schuhabb6b6c2017-03-11 16:31:24 -080018 // TODO(parker): Have this pull in a config from somewhere.
19 TargetFinder finder;
Parker Schuh4d2978f2017-02-25 11:13:06 -080020
21 while (true) {
22 // TODO(austin): Don't malloc.
Parker Schuhd497ed62017-03-04 20:11:58 -080023 VisionResult target;
Parker Schuh4d2978f2017-02-25 11:13:06 -080024 int size = recv.Recv(raw_data, sizeof(raw_data));
25 monotonic_clock::time_point now = monotonic_clock::now();
26 auto target_time = now -
27 std::chrono::nanoseconds(target.send_timestamp() -
28 target.image_timestamp()) +
29 // It takes a bit to shoot a frame. Push the frame
30 // further back in time.
31 std::chrono::milliseconds(10);
32
33 if (!target.ParseFromArray(raw_data, size)) {
34 continue;
35 }
36
37 auto new_vision_status = vision_status.MakeMessage();
38 new_vision_status->image_valid = target.has_target();
39 if (new_vision_status->image_valid) {
40 new_vision_status->target_time =
41 std::chrono::duration_cast<std::chrono::nanoseconds>(
42 target_time.time_since_epoch())
43 .count();
44
Parker Schuhabb6b6c2017-03-11 16:31:24 -080045 finder.GetAngleDist(
46 aos::vision::Vector<2>(target.target().x(), target.target().y()),
47 /* TODO: Insert down estimate here in radians: */ 0.0,
48 &new_vision_status->distance, &new_vision_status->angle);
Parker Schuh4d2978f2017-02-25 11:13:06 -080049 }
50
51 LOG_STRUCT(DEBUG, "vision", *new_vision_status);
52 if (!new_vision_status.Send()) {
53 LOG(ERROR, "Failed to send vision information\n");
54 }
55 }
56}