blob: 52b81adf92389d81b0e928e872fcfeffe27a58f9 [file] [log] [blame]
Parker Schuh2a1447c2019-02-17 00:25:29 -08001#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 Schuh5e8e3a52019-02-24 13:36:19 -08009#include "y2019/jevois/camera/image_stream.h"
10#include "y2019/jevois/camera/reader.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -080011
12#include "y2019/jevois/serial.h"
Brian Silvermance4825f2019-02-17 18:28:39 -080013#include "y2019/jevois/structures.h"
14#include "y2019/jevois/uart.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -080015#include "y2019/vision/target_finder.h"
16
17using ::aos::events::DataSocket;
18using ::aos::events::RXUdpSocket;
19using ::aos::events::TCPServer;
20using ::aos::vision::DataRef;
21using ::aos::vision::Int32Codec;
22using ::aos::monotonic_clock;
23using ::y2019::jevois::open_via_terminos;
24using aos::vision::Segment;
25
Parker Schuh5e8e3a52019-02-24 13:36:19 -080026class CameraStream : public ::y2019::camera::ImageStreamEvent {
Parker Schuh2a1447c2019-02-17 00:25:29 -080027 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 Schuh5e8e3a52019-02-24 13:36:19 -080034 if (on_frame_) on_frame_(data, monotonic_now);
Parker Schuh2a1447c2019-02-17 00:25:29 -080035 }
36
Parker Schuh5e8e3a52019-02-24 13:36:19 -080037 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 Schuh2a1447c2019-02-17 00:25:29 -080044};
45
46int open_terminos(const char *tty_name) { return open_via_terminos(tty_name); }
47
48std::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 Schuh5e8e3a52019-02-24 13:36:19 -080063using aos::vision::ImageRange;
64using aos::vision::RangeImage;
65using aos::vision::ImageFormat;
66
Parker Schuh2a1447c2019-02-17 00:25:29 -080067int main(int argc, char **argv) {
68 (void)argc;
69 (void)argv;
70 using namespace y2019::vision;
Brian Silvermane9924fd2019-03-02 15:20:42 -080071 using frc971::jevois::CameraCommand;
Parker Schuh2a1447c2019-02-17 00:25:29 -080072 // 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 Schuh5e8e3a52019-02-24 13:36:19 -080078 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 Schuh2a1447c2019-02-17 00:25:29 -080082
83 TargetFinder finder_;
84
Parker Schuh2a1447c2019-02-17 00:25:29 -080085 aos::vision::CameraParams params0;
Ben Fredricksona8c3d552019-03-03 14:14:53 -080086 params0.set_exposure(50);
Parker Schuh2a1447c2019-02-17 00:25:29 -080087 params0.set_brightness(40);
88 params0.set_width(640);
Ben Fredricksona8c3d552019-03-03 14:14:53 -080089 params0.set_fps(15);
Parker Schuh2a1447c2019-02-17 00:25:29 -080090 params0.set_height(480);
91
92 ::std::unique_ptr<CameraStream> camera0(
93 new CameraStream(params0, "/dev/video0"));
Parker Schuh5e8e3a52019-02-24 13:36:19 -080094 camera0->set_on_frame([&](DataRef data,
95 monotonic_clock::time_point monotonic_now) {
Parker Schuh2a1447c2019-02-17 00:25:29 -080096 aos::vision::ImageFormat fmt{640, 480};
Brian Silverman37b15b32019-03-10 13:30:18 -070097 aos::vision::BlobList imgs = aos::vision::FindBlobs(
Brian Silvermanefde9522019-03-23 22:02:40 -070098 aos::vision::FastYuyvYThreshold(fmt, data.data(), 120));
Parker Schuh2a1447c2019-02-17 00:25:29 -080099 finder_.PreFilter(&imgs);
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800100 LOG(INFO, "Blobs: (%zu).\n", imgs.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800101
Austin Schuh32ffac22019-03-09 22:42:02 -0800102 constexpr bool verbose = false;
Austin Schuh6e56faf2019-03-10 14:04:57 -0700103 ::std::vector<Polygon> raw_polys;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800104 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800105 // Convert blobs to contours in the corrected space.
Brian Silverman86891e52019-03-23 22:02:52 -0700106 ContourNode *contour = finder_.GetContour(blob);
Austin Schuh6e56faf2019-03-10 14:04:57 -0700107 ::std::vector<::Eigen::Vector2f> unwarped_contour =
Austin Schuhe5015972019-03-09 17:47:34 -0800108 finder_.UnWarpContour(contour);
Austin Schuh6e56faf2019-03-10 14:04:57 -0700109 const Polygon polygon =
110 finder_.FindPolygon(::std::move(unwarped_contour), verbose);
111 if (!polygon.segments.empty()) {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800112 raw_polys.push_back(polygon);
113 }
114 }
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800115 LOG(INFO, "Polygons: (%zu).\n", raw_polys.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800116
117 // Calculate each component side of a possible target.
Austin Schuh32ffac22019-03-09 22:42:02 -0800118 ::std::vector<TargetComponent> target_component_list =
119 finder_.FillTargetComponentList(raw_polys, verbose);
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800120 LOG(INFO, "Components: (%zu).\n", target_component_list.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800121
122 // Put the compenents together into targets.
Austin Schuh32ffac22019-03-09 22:42:02 -0800123 ::std::vector<Target> target_list =
Parker Schuh2a1447c2019-02-17 00:25:29 -0800124 finder_.FindTargetsFromComponents(target_component_list, verbose);
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800125 LOG(INFO, "Potential Target: (%zu).\n", target_list.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800126
127 // Use the solver to generate an intermediate version of our results.
Austin Schuh32ffac22019-03-09 22:42:02 -0800128 ::std::vector<IntermediateResult> results;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800129 for (const Target &target : target_list) {
130 results.emplace_back(finder_.ProcessTargetToResult(target, verbose));
131 }
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800132 LOG(INFO, "Raw Results: (%zu).\n", results.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800133
Alex Perrybac3d3f2019-03-10 14:26:51 -0700134 results = finder_.FilterResults(results, 30, verbose);
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800135 LOG(INFO, "Results: (%zu).\n", results.size());
Brian Silvermance4825f2019-02-17 18:28:39 -0800136
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800137 // TODO: Select top 3 (randomly?)
138
Brian Silvermanc41fb862019-03-02 21:14:46 -0800139 frc971::jevois::CameraFrame frame{};
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800140
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 Silverman86891e52019-03-23 22:02:52 -0700152 // If we succeed in writing our delimiter, then write out the rest of
153 // the frame. If not, no point in continuing.
Brian Silvermance4825f2019-02-17 18:28:39 -0800154 if (write(itsDev, "\0", 1) == 1) {
Brian Silvermance4825f2019-02-17 18:28:39 -0800155 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 Schuh5e8e3a52019-02-24 13:36:19 -0800159 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 Silvermance4825f2019-02-17 18:28:39 -0800165 }
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800166 });
Parker Schuh2a1447c2019-02-17 00:25:29 -0800167
168 aos::events::EpollLoop loop;
169
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800170 while (true) {
171 std::this_thread::sleep_for(std::chrono::milliseconds(1));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800172 camera0->ReadEvent();
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800173
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 Perry3bf1bee2019-02-23 20:01:15 -0800186 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 Schuh5e8e3a52019-02-24 13:36:19 -0800191 switch (calibration.camera_command) {
Brian Silvermane9924fd2019-03-02 15:20:42 -0800192 case CameraCommand::kNormal:
Brian Silvermanbac77542019-03-03 13:57:00 -0800193 case CameraCommand::kAs:
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800194 break;
Brian Silvermane9924fd2019-03-02 15:20:42 -0800195 case CameraCommand::kUsb:
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800196 return 0;
Brian Silvermane9924fd2019-03-02 15:20:42 -0800197 case CameraCommand::kCameraPassthrough:
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800198 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 Schuh2a1447c2019-02-17 00:25:29 -0800207 }
208
209 // TODO: Fix event loop on jevois:
210 // loop.Add(camera0.get());
211 // loop.Run();
212}