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 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 12 | namespace y2017::vision { |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 13 | |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 14 | using aos::monotonic_clock; |
| 15 | |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 16 | int Main() { |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 17 | ::aos::events::RXUdpSocket recv(8080); |
| 18 | char raw_data[65507]; |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 19 | // TODO(parker): Have this pull in a config from somewhere. |
| 20 | TargetFinder finder; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
Austin Schuh | c5fa6d9 | 2022-02-25 14:36:28 -0800 | [diff] [blame] | 22 | aos::configuration::ReadConfig("aos_config.json"); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | |
| 24 | ::aos::ShmEventLoop event_loop(&config.message()); |
Austin Schuh | b6c5c85 | 2019-05-19 20:13:31 -0700 | [diff] [blame] | 25 | |
| 26 | ::aos::Sender<::y2017::vision::VisionStatus> vision_status_sender = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 27 | event_loop.MakeSender<::y2017::vision::VisionStatus>("/vision"); |
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 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 45 | auto builder = vision_status_sender.MakeBuilder(); |
| 46 | VisionStatus::Builder vision_status_builder = |
| 47 | builder.MakeBuilder<VisionStatus>(); |
| 48 | vision_status_builder.add_image_valid(target.has_target()); |
| 49 | if (target.has_target()) { |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 50 | vision_status_builder.add_target_time( |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 51 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 52 | target_time.time_since_epoch()) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 53 | .count()); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 54 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 55 | double distance = 0.0; |
| 56 | double angle = 0.0; |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 57 | finder.GetAngleDist( |
| 58 | aos::vision::Vector<2>(target.target().x(), target.target().y()), |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 59 | /* TODO: Insert down estimate here in radians: */ 0.0, &distance, |
| 60 | &angle); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 61 | vision_status_builder.add_distance(distance); |
| 62 | vision_status_builder.add_angle(angle); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 63 | } |
| 64 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 65 | if (builder.Send(vision_status_builder.Finish()) != |
| 66 | aos::RawSender::Error::kOk) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 67 | AOS_LOG(ERROR, "Failed to send vision information\n"); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | } |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 71 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 72 | } // namespace y2017::vision |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 73 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 74 | int main(int argc, char **argv) { |
| 75 | ::aos::InitGoogle(&argc, &argv); |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 76 | ::y2017::vision::Main(); |
| 77 | } |