Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 1 | #include <fstream> |
| 2 | |
| 3 | #include "aos/logging/implementations.h" |
| 4 | #include "aos/logging/logging.h" |
| 5 | #include "aos/vision/blob/codec.h" |
| 6 | #include "aos/vision/blob/find_blob.h" |
| 7 | #include "aos/vision/events/socket_types.h" |
| 8 | #include "aos/vision/events/udp.h" |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 9 | #include "y2019/jevois/camera/image_stream.h" |
| 10 | #include "y2019/jevois/camera/reader.h" |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 11 | |
| 12 | #include "y2019/jevois/serial.h" |
Brian Silverman | ce4825f | 2019-02-17 18:28:39 -0800 | [diff] [blame] | 13 | #include "y2019/jevois/structures.h" |
| 14 | #include "y2019/jevois/uart.h" |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 15 | #include "y2019/vision/target_finder.h" |
| 16 | |
| 17 | using ::aos::events::DataSocket; |
| 18 | using ::aos::events::RXUdpSocket; |
| 19 | using ::aos::events::TCPServer; |
| 20 | using ::aos::vision::DataRef; |
| 21 | using ::aos::vision::Int32Codec; |
| 22 | using ::aos::monotonic_clock; |
| 23 | using ::y2019::jevois::open_via_terminos; |
| 24 | using aos::vision::Segment; |
| 25 | |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 26 | class CameraStream : public ::y2019::camera::ImageStreamEvent { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 27 | public: |
| 28 | CameraStream(::aos::vision::CameraParams params, const ::std::string &fname) |
| 29 | : ImageStreamEvent(fname, params) {} |
| 30 | |
| 31 | void ProcessImage(DataRef data, monotonic_clock::time_point monotonic_now) { |
| 32 | LOG(INFO, "got frame: %d\n", (int)data.size()); |
| 33 | |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 34 | if (on_frame_) on_frame_(data, monotonic_now); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 37 | void set_on_frame(const std::function< |
| 38 | void(DataRef, monotonic_clock::time_point)> &on_frame) { |
| 39 | on_frame_ = on_frame; |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | std::function<void(DataRef, monotonic_clock::time_point)> on_frame_; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | int open_terminos(const char *tty_name) { return open_via_terminos(tty_name); } |
| 47 | |
| 48 | std::string GetFileContents(const std::string &filename) { |
| 49 | std::ifstream in(filename, std::ios::in | std::ios::binary); |
| 50 | if (in) { |
| 51 | std::string contents; |
| 52 | in.seekg(0, std::ios::end); |
| 53 | contents.resize(in.tellg()); |
| 54 | in.seekg(0, std::ios::beg); |
| 55 | in.read(&contents[0], contents.size()); |
| 56 | in.close(); |
| 57 | return (contents); |
| 58 | } |
| 59 | fprintf(stderr, "Could not read file: %s\n", filename.c_str()); |
| 60 | exit(-1); |
| 61 | } |
| 62 | |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 63 | using aos::vision::ImageRange; |
| 64 | using aos::vision::RangeImage; |
| 65 | using aos::vision::ImageFormat; |
| 66 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 67 | int main(int argc, char **argv) { |
| 68 | (void)argc; |
| 69 | (void)argv; |
| 70 | using namespace y2019::vision; |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 71 | using frc971::jevois::CameraCommand; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 72 | // gflags::ParseCommandLineFlags(&argc, &argv, false); |
| 73 | ::aos::logging::Init(); |
| 74 | ::aos::logging::AddImplementation( |
| 75 | new ::aos::logging::StreamLogImplementation(stderr)); |
| 76 | |
| 77 | int itsDev = open_terminos("/dev/ttyS0"); |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 78 | frc971::jevois::CobsPacketizer<frc971::jevois::uart_to_camera_size()> cobs; |
| 79 | // Uncomment these to printf directly to stdout to get debug info... |
| 80 | // dup2(itsDev, 1); |
| 81 | // dup2(itsDev, 2); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 82 | |
| 83 | TargetFinder finder_; |
| 84 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 85 | aos::vision::CameraParams params0; |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 86 | params0.set_exposure(50); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 87 | params0.set_brightness(40); |
| 88 | params0.set_width(640); |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 89 | params0.set_fps(15); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 90 | params0.set_height(480); |
| 91 | |
| 92 | ::std::unique_ptr<CameraStream> camera0( |
| 93 | new CameraStream(params0, "/dev/video0")); |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 94 | camera0->set_on_frame([&](DataRef data, |
| 95 | monotonic_clock::time_point monotonic_now) { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 96 | aos::vision::ImageFormat fmt{640, 480}; |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 97 | aos::vision::BlobList imgs = aos::vision::FindBlobs( |
Brian Silverman | efde952 | 2019-03-23 22:02:40 -0700 | [diff] [blame] | 98 | aos::vision::FastYuyvYThreshold(fmt, data.data(), 120)); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 99 | finder_.PreFilter(&imgs); |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 100 | LOG(INFO, "Blobs: (%zu).\n", imgs.size()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 101 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 102 | constexpr bool verbose = false; |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 103 | ::std::vector<Polygon> raw_polys; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 104 | for (const RangeImage &blob : imgs) { |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 105 | // Convert blobs to contours in the corrected space. |
Brian Silverman | 86891e5 | 2019-03-23 22:02:52 -0700 | [diff] [blame] | 106 | ContourNode *contour = finder_.GetContour(blob); |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 107 | ::std::vector<::Eigen::Vector2f> unwarped_contour = |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 108 | finder_.UnWarpContour(contour); |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 109 | const Polygon polygon = |
| 110 | finder_.FindPolygon(::std::move(unwarped_contour), verbose); |
| 111 | if (!polygon.segments.empty()) { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 112 | raw_polys.push_back(polygon); |
| 113 | } |
| 114 | } |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 115 | LOG(INFO, "Polygons: (%zu).\n", raw_polys.size()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 116 | |
| 117 | // Calculate each component side of a possible target. |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 118 | ::std::vector<TargetComponent> target_component_list = |
| 119 | finder_.FillTargetComponentList(raw_polys, verbose); |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 120 | LOG(INFO, "Components: (%zu).\n", target_component_list.size()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 121 | |
| 122 | // Put the compenents together into targets. |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 123 | ::std::vector<Target> target_list = |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 124 | finder_.FindTargetsFromComponents(target_component_list, verbose); |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 125 | LOG(INFO, "Potential Target: (%zu).\n", target_list.size()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 126 | |
| 127 | // Use the solver to generate an intermediate version of our results. |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 128 | ::std::vector<IntermediateResult> results; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 129 | for (const Target &target : target_list) { |
| 130 | results.emplace_back(finder_.ProcessTargetToResult(target, verbose)); |
| 131 | } |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 132 | LOG(INFO, "Raw Results: (%zu).\n", results.size()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 133 | |
Alex Perry | bac3d3f | 2019-03-10 14:26:51 -0700 | [diff] [blame] | 134 | results = finder_.FilterResults(results, 30, verbose); |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 135 | LOG(INFO, "Results: (%zu).\n", results.size()); |
Brian Silverman | ce4825f | 2019-02-17 18:28:39 -0800 | [diff] [blame] | 136 | |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 137 | // TODO: Select top 3 (randomly?) |
| 138 | |
Brian Silverman | c41fb86 | 2019-03-02 21:14:46 -0800 | [diff] [blame] | 139 | frc971::jevois::CameraFrame frame{}; |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 140 | |
| 141 | for (size_t i = 0; i < results.size() && i < frame.targets.max_size(); |
| 142 | ++i) { |
| 143 | const auto &result = results[i].extrinsics; |
| 144 | frame.targets.push_back(frc971::jevois::Target{ |
| 145 | static_cast<float>(result.z), static_cast<float>(result.y), |
| 146 | static_cast<float>(result.r2), static_cast<float>(result.r1)}); |
| 147 | } |
| 148 | |
| 149 | frame.age = std::chrono::duration_cast<frc971::jevois::camera_duration>( |
| 150 | aos::monotonic_clock::now() - monotonic_now); |
| 151 | |
Brian Silverman | 86891e5 | 2019-03-23 22:02:52 -0700 | [diff] [blame] | 152 | // If we succeed in writing our delimiter, then write out the rest of |
| 153 | // the frame. If not, no point in continuing. |
Brian Silverman | ce4825f | 2019-02-17 18:28:39 -0800 | [diff] [blame] | 154 | if (write(itsDev, "\0", 1) == 1) { |
Brian Silverman | ce4825f | 2019-02-17 18:28:39 -0800 | [diff] [blame] | 155 | const auto serialized_frame = frc971::jevois::UartPackToTeensy(frame); |
| 156 | // We don't really care if this succeeds or not. If it fails for some |
| 157 | // reason, we'll just try again with the next frame, and the other end |
| 158 | // will find the new packet just fine. |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 159 | ssize_t n = |
| 160 | write(itsDev, serialized_frame.data(), serialized_frame.size()); |
| 161 | |
| 162 | if (n != (ssize_t)serialized_frame.size()) { |
| 163 | LOG(INFO, "Some problem happened"); |
| 164 | } |
Brian Silverman | ce4825f | 2019-02-17 18:28:39 -0800 | [diff] [blame] | 165 | } |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 166 | }); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 167 | |
| 168 | aos::events::EpollLoop loop; |
| 169 | |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 170 | while (true) { |
| 171 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 172 | camera0->ReadEvent(); |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 173 | |
| 174 | { |
| 175 | constexpr size_t kBufferSize = frc971::jevois::uart_to_teensy_size(); |
| 176 | char data[kBufferSize]; |
| 177 | ssize_t n = read(itsDev, &data[0], kBufferSize); |
| 178 | if (n >= 1) { |
| 179 | cobs.ParseData(gsl::span<const char>(&data[0], n)); |
| 180 | auto packet = cobs.received_packet(); |
| 181 | if (!packet.empty()) { |
| 182 | auto calibration_question = |
| 183 | frc971::jevois::UartUnpackToCamera(packet); |
| 184 | if (calibration_question) { |
| 185 | const auto &calibration = *calibration_question; |
Alex Perry | 3bf1bee | 2019-02-23 20:01:15 -0800 | [diff] [blame] | 186 | IntrinsicParams *intrinsics = finder_.mutable_intrinsics(); |
| 187 | intrinsics->mount_angle = calibration.calibration(0, 0); |
| 188 | intrinsics->focal_length = calibration.calibration(0, 1); |
| 189 | intrinsics->barrel_mount = calibration.calibration(0, 2); |
| 190 | |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 191 | switch (calibration.camera_command) { |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 192 | case CameraCommand::kNormal: |
Brian Silverman | bac7754 | 2019-03-03 13:57:00 -0800 | [diff] [blame] | 193 | case CameraCommand::kAs: |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 194 | break; |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 195 | case CameraCommand::kUsb: |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 196 | return 0; |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 197 | case CameraCommand::kCameraPassthrough: |
Parker Schuh | 5e8e3a5 | 2019-02-24 13:36:19 -0800 | [diff] [blame] | 198 | return system("touch /tmp/do_not_export_sd_card"); |
| 199 | } |
| 200 | } else { |
| 201 | printf("bad frame\n"); |
| 202 | } |
| 203 | cobs.clear_received_packet(); |
| 204 | } |
| 205 | } |
| 206 | } |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // TODO: Fix event loop on jevois: |
| 210 | // loop.Add(camera0.get()); |
| 211 | // loop.Run(); |
| 212 | } |