Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 1 | #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 Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 8 | #include "y2017/vision/target_finder.h" |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 9 | #include "y2017/vision/vision.q.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; |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 22 | |
| 23 | while (true) { |
| 24 | // TODO(austin): Don't malloc. |
Parker Schuh | d497ed6 | 2017-03-04 20:11:58 -0800 | [diff] [blame] | 25 | VisionResult target; |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 26 | int size = recv.Recv(raw_data, sizeof(raw_data)); |
| 27 | monotonic_clock::time_point now = monotonic_clock::now(); |
| 28 | auto target_time = now - |
| 29 | std::chrono::nanoseconds(target.send_timestamp() - |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 30 | target.image_timestamp()) - |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 31 | // It takes a bit to shoot a frame. Push the frame |
| 32 | // further back in time. |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 33 | std::chrono::milliseconds(60); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 34 | |
| 35 | if (!target.ParseFromArray(raw_data, size)) { |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | auto new_vision_status = vision_status.MakeMessage(); |
| 40 | new_vision_status->image_valid = target.has_target(); |
| 41 | if (new_vision_status->image_valid) { |
| 42 | new_vision_status->target_time = |
| 43 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 44 | target_time.time_since_epoch()) |
| 45 | .count(); |
| 46 | |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame] | 47 | finder.GetAngleDist( |
| 48 | aos::vision::Vector<2>(target.target().x(), target.target().y()), |
| 49 | /* TODO: Insert down estimate here in radians: */ 0.0, |
| 50 | &new_vision_status->distance, &new_vision_status->angle); |
Parker Schuh | 4d2978f | 2017-02-25 11:13:06 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | LOG_STRUCT(DEBUG, "vision", *new_vision_status); |
| 54 | if (!new_vision_status.Send()) { |
| 55 | LOG(ERROR, "Failed to send vision information\n"); |
| 56 | } |
| 57 | } |
| 58 | } |
Austin Schuh | 218a755 | 2017-03-22 21:15:28 -0700 | [diff] [blame] | 59 | |
| 60 | } // namespace vision |
| 61 | } // namespace y2017 |
| 62 | |
| 63 | int main(int /*argc*/, char ** /*argv*/) { |
| 64 | ::aos::InitNRT(); |
| 65 | ::y2017::vision::Main(); |
| 66 | } |