Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 1 | #include <netdb.h> |
| 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #include "aos/events/shm_event_loop.h" |
Austin Schuh | b6c5c85 | 2019-05-19 20:13:31 -0700 | [diff] [blame] | 4 | #include "aos/init.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 5 | #include "aos/logging/logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/time/time.h" |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 7 | #include "aos/vision/events/udp.h" |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 8 | #include "y2017/vision/target_finder.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include "y2017/vision/vision_generated.h" |
Parker Schuh | d497ed6 | 2017-03-04 20:11:58 -0800 | [diff] [blame] | 10 | #include "y2017/vision/vision_result.pb.h" |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 11 | |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 12 | namespace y2017 { |
| 13 | namespace vision { |
| 14 | |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 15 | using aos::monotonic_clock; |
| 16 | |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 17 | int Main() { |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 18 | ::aos::events::RXUdpSocket recv(8080); |
| 19 | char raw_data[65507]; |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 20 | // TODO(parker): Have this pull in a config from somewhere. |
| 21 | TargetFinder finder; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 22 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 23 | aos::configuration::ReadConfig("aos_config.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | |
| 25 | ::aos::ShmEventLoop event_loop(&config.message()); |
Austin Schuh | b6c5c85 | 2019-05-19 20:13:31 -0700 | [diff] [blame] | 26 | |
| 27 | ::aos::Sender<::y2017::vision::VisionStatus> vision_status_sender = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 28 | event_loop.MakeSender<::y2017::vision::VisionStatus>("/vision"); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 29 | |
| 30 | while (true) { |
| 31 | // TODO(austin): Don't malloc. |
Parker Schuh | d497ed6 | 2017-03-04 20:11:58 -0800 | [diff] [blame] | 32 | VisionResult target; |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 33 | int size = recv.Recv(raw_data, sizeof(raw_data)); |
| 34 | monotonic_clock::time_point now = monotonic_clock::now(); |
| 35 | auto target_time = now - |
| 36 | std::chrono::nanoseconds(target.send_timestamp() - |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 37 | target.image_timestamp()) - |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 38 | // It takes a bit to shoot a frame. Push the frame |
| 39 | // further back in time. |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 40 | std::chrono::milliseconds(60); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 41 | |
| 42 | if (!target.ParseFromArray(raw_data, size)) { |
| 43 | continue; |
| 44 | } |
| 45 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 46 | auto builder = vision_status_sender.MakeBuilder(); |
| 47 | VisionStatus::Builder vision_status_builder = |
| 48 | builder.MakeBuilder<VisionStatus>(); |
| 49 | vision_status_builder.add_image_valid(target.has_target()); |
| 50 | if (target.has_target()) { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 51 | vision_status_builder.add_target_time( |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 52 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 53 | target_time.time_since_epoch()) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 54 | .count()); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 55 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 56 | double distance = 0.0; |
| 57 | double angle = 0.0; |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 58 | finder.GetAngleDist( |
| 59 | aos::vision::Vector<2>(target.target().x(), target.target().y()), |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 60 | /* TODO: Insert down estimate here in radians: */ 0.0, &distance, |
| 61 | &angle); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 62 | vision_status_builder.add_distance(distance); |
| 63 | vision_status_builder.add_angle(angle); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 64 | } |
| 65 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 66 | if (builder.Send(vision_status_builder.Finish()) != |
| 67 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 68 | AOS_LOG(ERROR, "Failed to send vision information\n"); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 72 | |
| 73 | } // namespace vision |
| 74 | } // namespace y2017 |
| 75 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 76 | int main(int argc, char **argv) { |
| 77 | ::aos::InitGoogle(&argc, &argv); |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 78 | ::y2017::vision::Main(); |
| 79 | } |