Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 1 | #include <netdb.h> |
| 2 | |
Austin Schuh | b6c5c85 | 2019-05-19 20:13:31 -0700 | [diff] [blame] | 3 | #include "aos/events/shm-event-loop.h" |
| 4 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
| 6 | #include "aos/logging/queue_logging.h" |
| 7 | #include "aos/time/time.h" |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 8 | #include "aos/vision/events/udp.h" |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 9 | #include "y2017/vision/target_finder.h" |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 10 | #include "y2017/vision/vision.q.h" |
Parker Schuh | d497ed6 | 2017-03-04 20:11:58 -0800 | [diff] [blame] | 11 | #include "y2017/vision/vision_result.pb.h" |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 12 | |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 13 | namespace y2017 { |
| 14 | namespace vision { |
| 15 | |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 16 | using aos::monotonic_clock; |
| 17 | |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 18 | int Main() { |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 19 | ::aos::events::RXUdpSocket recv(8080); |
| 20 | char raw_data[65507]; |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 21 | // TODO(parker): Have this pull in a config from somewhere. |
| 22 | TargetFinder finder; |
Austin Schuh | b6c5c85 | 2019-05-19 20:13:31 -0700 | [diff] [blame] | 23 | ::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 Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 28 | |
| 29 | while (true) { |
| 30 | // TODO(austin): Don't malloc. |
Parker Schuh | d497ed6 | 2017-03-04 20:11:58 -0800 | [diff] [blame] | 31 | VisionResult target; |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 32 | 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 Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 36 | target.image_timestamp()) - |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 37 | // It takes a bit to shoot a frame. Push the frame |
| 38 | // further back in time. |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 39 | std::chrono::milliseconds(60); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 40 | |
| 41 | if (!target.ParseFromArray(raw_data, size)) { |
| 42 | continue; |
| 43 | } |
| 44 | |
Austin Schuh | b6c5c85 | 2019-05-19 20:13:31 -0700 | [diff] [blame] | 45 | auto new_vision_status = vision_status_sender.MakeMessage(); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 46 | 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 Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 53 | 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 Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 59 | AOS_LOG_STRUCT(DEBUG, "vision", *new_vision_status); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 60 | if (!new_vision_status.Send()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame^] | 61 | AOS_LOG(ERROR, "Failed to send vision information\n"); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | } |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 65 | |
| 66 | } // namespace vision |
| 67 | } // namespace y2017 |
| 68 | |
| 69 | int main(int /*argc*/, char ** /*argv*/) { |
| 70 | ::aos::InitNRT(); |
| 71 | ::y2017::vision::Main(); |
| 72 | } |