blob: 6b86972771c8305840bf62ccb02863cb10c1fbc3 [file] [log] [blame]
Parker Schuh4d2978f2017-02-25 11:13:06 -08001#include <netdb.h>
2
Austin Schuhb6c5c852019-05-19 20:13:31 -07003#include "aos/events/shm-event-loop.h"
4#include "aos/init.h"
John Park33858a32018-09-28 23:05:48 -07005#include "aos/logging/logging.h"
6#include "aos/logging/queue_logging.h"
7#include "aos/time/time.h"
Parker Schuh4d2978f2017-02-25 11:13:06 -08008#include "aos/vision/events/udp.h"
Parker Schuhabb6b6c2017-03-11 16:31:24 -08009#include "y2017/vision/target_finder.h"
Parker Schuh4d2978f2017-02-25 11:13:06 -080010#include "y2017/vision/vision.q.h"
Parker Schuhd497ed62017-03-04 20:11:58 -080011#include "y2017/vision/vision_result.pb.h"
Parker Schuh4d2978f2017-02-25 11:13:06 -080012
Austin Schuh218a7552017-03-22 21:15:28 -070013namespace y2017 {
14namespace vision {
15
Parker Schuh4d2978f2017-02-25 11:13:06 -080016using aos::monotonic_clock;
17
Austin Schuh218a7552017-03-22 21:15:28 -070018int Main() {
Parker Schuh4d2978f2017-02-25 11:13:06 -080019 ::aos::events::RXUdpSocket recv(8080);
20 char raw_data[65507];
Parker Schuhabb6b6c2017-03-11 16:31:24 -080021 // TODO(parker): Have this pull in a config from somewhere.
22 TargetFinder finder;
Austin Schuhb6c5c852019-05-19 20:13:31 -070023 ::aos::ShmEventLoop event_loop;
24
25 ::aos::Sender<::y2017::vision::VisionStatus> vision_status_sender =
26 event_loop.MakeSender<::y2017::vision::VisionStatus>(
27 ".y2017.vision.vision_status");
Parker Schuh4d2978f2017-02-25 11:13:06 -080028
29 while (true) {
30 // TODO(austin): Don't malloc.
Parker Schuhd497ed62017-03-04 20:11:58 -080031 VisionResult target;
Parker Schuh4d2978f2017-02-25 11:13:06 -080032 int size = recv.Recv(raw_data, sizeof(raw_data));
33 monotonic_clock::time_point now = monotonic_clock::now();
34 auto target_time = now -
35 std::chrono::nanoseconds(target.send_timestamp() -
Austin Schuh218a7552017-03-22 21:15:28 -070036 target.image_timestamp()) -
Parker Schuh4d2978f2017-02-25 11:13:06 -080037 // It takes a bit to shoot a frame. Push the frame
38 // further back in time.
Austin Schuh218a7552017-03-22 21:15:28 -070039 std::chrono::milliseconds(60);
Parker Schuh4d2978f2017-02-25 11:13:06 -080040
41 if (!target.ParseFromArray(raw_data, size)) {
42 continue;
43 }
44
Austin Schuhb6c5c852019-05-19 20:13:31 -070045 auto new_vision_status = vision_status_sender.MakeMessage();
Parker Schuh4d2978f2017-02-25 11:13:06 -080046 new_vision_status->image_valid = target.has_target();
47 if (new_vision_status->image_valid) {
48 new_vision_status->target_time =
49 std::chrono::duration_cast<std::chrono::nanoseconds>(
50 target_time.time_since_epoch())
51 .count();
52
Parker Schuhabb6b6c2017-03-11 16:31:24 -080053 finder.GetAngleDist(
54 aos::vision::Vector<2>(target.target().x(), target.target().y()),
55 /* TODO: Insert down estimate here in radians: */ 0.0,
56 &new_vision_status->distance, &new_vision_status->angle);
Parker Schuh4d2978f2017-02-25 11:13:06 -080057 }
58
Austin Schuhf257f3c2019-10-27 21:00:43 -070059 AOS_LOG_STRUCT(DEBUG, "vision", *new_vision_status);
Parker Schuh4d2978f2017-02-25 11:13:06 -080060 if (!new_vision_status.Send()) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070061 AOS_LOG(ERROR, "Failed to send vision information\n");
Parker Schuh4d2978f2017-02-25 11:13:06 -080062 }
63 }
64}
Austin Schuh218a7552017-03-22 21:15:28 -070065
66} // namespace vision
67} // namespace y2017
68
69int main(int /*argc*/, char ** /*argv*/) {
70 ::aos::InitNRT();
71 ::y2017::vision::Main();
72}