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" |
| 8 | #include "y2017/vision/vision.q.h" |
| 9 | #include "y2017/vision/vision_data.pb.h" |
| 10 | |
| 11 | using aos::monotonic_clock; |
| 12 | |
| 13 | namespace y2017 { |
| 14 | namespace vision { |
| 15 | |
| 16 | void ComputeDistanceAngle(const Target &target, double *distance, |
| 17 | double *angle) { |
| 18 | // TODO: fix this. |
| 19 | *distance = target.y(); |
| 20 | *angle = target.x(); |
| 21 | } |
| 22 | |
| 23 | } // namespace vision |
| 24 | } // namespace y2017 |
| 25 | |
| 26 | int main() { |
| 27 | using namespace y2017::vision; |
| 28 | ::aos::events::RXUdpSocket recv(8080); |
| 29 | char raw_data[65507]; |
| 30 | |
| 31 | while (true) { |
| 32 | // TODO(austin): Don't malloc. |
| 33 | VisionData target; |
| 34 | int size = recv.Recv(raw_data, sizeof(raw_data)); |
| 35 | monotonic_clock::time_point now = monotonic_clock::now(); |
| 36 | auto target_time = now - |
| 37 | std::chrono::nanoseconds(target.send_timestamp() - |
| 38 | target.image_timestamp()) + |
| 39 | // It takes a bit to shoot a frame. Push the frame |
| 40 | // further back in time. |
| 41 | std::chrono::milliseconds(10); |
| 42 | |
| 43 | if (!target.ParseFromArray(raw_data, size)) { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | auto new_vision_status = vision_status.MakeMessage(); |
| 48 | new_vision_status->image_valid = target.has_target(); |
| 49 | if (new_vision_status->image_valid) { |
| 50 | new_vision_status->target_time = |
| 51 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 52 | target_time.time_since_epoch()) |
| 53 | .count(); |
| 54 | |
| 55 | ComputeDistanceAngle(target.target(), &new_vision_status->distance, |
| 56 | &new_vision_status->angle); |
| 57 | } |
| 58 | |
| 59 | LOG_STRUCT(DEBUG, "vision", *new_vision_status); |
| 60 | if (!new_vision_status.Send()) { |
| 61 | LOG(ERROR, "Failed to send vision information\n"); |
| 62 | } |
| 63 | } |
| 64 | } |