blob: 3c0c70523b5b3b1ec90cbed2bc25376d0d3522e2 [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"
Alex Perrye9775c32019-03-09 10:48:57 -080015#include "y2019/vision/image_writer.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -080016#include "y2019/vision/target_finder.h"
17
18using ::aos::events::DataSocket;
19using ::aos::events::RXUdpSocket;
20using ::aos::events::TCPServer;
21using ::aos::vision::DataRef;
22using ::aos::vision::Int32Codec;
23using ::aos::monotonic_clock;
24using ::y2019::jevois::open_via_terminos;
25using aos::vision::Segment;
26
Parker Schuh5e8e3a52019-02-24 13:36:19 -080027class CameraStream : public ::y2019::camera::ImageStreamEvent {
Parker Schuh2a1447c2019-02-17 00:25:29 -080028 public:
29 CameraStream(::aos::vision::CameraParams params, const ::std::string &fname)
30 : ImageStreamEvent(fname, params) {}
31
32 void ProcessImage(DataRef data, monotonic_clock::time_point monotonic_now) {
33 LOG(INFO, "got frame: %d\n", (int)data.size());
34
Parker Schuh5e8e3a52019-02-24 13:36:19 -080035 if (on_frame_) on_frame_(data, monotonic_now);
Parker Schuh2a1447c2019-02-17 00:25:29 -080036 }
37
Parker Schuh5e8e3a52019-02-24 13:36:19 -080038 void set_on_frame(const std::function<
39 void(DataRef, monotonic_clock::time_point)> &on_frame) {
40 on_frame_ = on_frame;
41 }
42
43 private:
44 std::function<void(DataRef, monotonic_clock::time_point)> on_frame_;
Parker Schuh2a1447c2019-02-17 00:25:29 -080045};
46
47int open_terminos(const char *tty_name) { return open_via_terminos(tty_name); }
48
49std::string GetFileContents(const std::string &filename) {
50 std::ifstream in(filename, std::ios::in | std::ios::binary);
51 if (in) {
52 std::string contents;
53 in.seekg(0, std::ios::end);
54 contents.resize(in.tellg());
55 in.seekg(0, std::ios::beg);
56 in.read(&contents[0], contents.size());
57 in.close();
58 return (contents);
59 }
60 fprintf(stderr, "Could not read file: %s\n", filename.c_str());
61 exit(-1);
62}
63
Parker Schuh5e8e3a52019-02-24 13:36:19 -080064using aos::vision::ImageRange;
65using aos::vision::RangeImage;
66using aos::vision::ImageFormat;
67
Parker Schuh2a1447c2019-02-17 00:25:29 -080068int main(int argc, char **argv) {
69 (void)argc;
70 (void)argv;
71 using namespace y2019::vision;
Brian Silvermane9924fd2019-03-02 15:20:42 -080072 using frc971::jevois::CameraCommand;
Parker Schuh2a1447c2019-02-17 00:25:29 -080073 // gflags::ParseCommandLineFlags(&argc, &argv, false);
74 ::aos::logging::Init();
75 ::aos::logging::AddImplementation(
76 new ::aos::logging::StreamLogImplementation(stderr));
77
78 int itsDev = open_terminos("/dev/ttyS0");
Parker Schuh5e8e3a52019-02-24 13:36:19 -080079 frc971::jevois::CobsPacketizer<frc971::jevois::uart_to_camera_size()> cobs;
80 // Uncomment these to printf directly to stdout to get debug info...
81 // dup2(itsDev, 1);
82 // dup2(itsDev, 2);
Parker Schuh2a1447c2019-02-17 00:25:29 -080083
84 TargetFinder finder_;
Alex Perrye9775c32019-03-09 10:48:57 -080085 ImageWriter writer_;
Parker Schuh2a1447c2019-02-17 00:25:29 -080086
Parker Schuh2a1447c2019-02-17 00:25:29 -080087 aos::vision::CameraParams params0;
Ben Fredricksona8c3d552019-03-03 14:14:53 -080088 params0.set_exposure(50);
Parker Schuh2a1447c2019-02-17 00:25:29 -080089 params0.set_brightness(40);
90 params0.set_width(640);
Ben Fredricksona8c3d552019-03-03 14:14:53 -080091 params0.set_fps(15);
Parker Schuh2a1447c2019-02-17 00:25:29 -080092 params0.set_height(480);
93
94 ::std::unique_ptr<CameraStream> camera0(
95 new CameraStream(params0, "/dev/video0"));
Parker Schuh5e8e3a52019-02-24 13:36:19 -080096 camera0->set_on_frame([&](DataRef data,
97 monotonic_clock::time_point monotonic_now) {
Parker Schuh2a1447c2019-02-17 00:25:29 -080098 aos::vision::ImageFormat fmt{640, 480};
Brian Silverman37b15b32019-03-10 13:30:18 -070099 aos::vision::BlobList imgs = aos::vision::FindBlobs(
100 aos::vision::SlowYuyvYThreshold(fmt, data.data(), 120));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800101 finder_.PreFilter(&imgs);
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800102 LOG(INFO, "Blobs: (%zu).\n", imgs.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800103
Austin Schuh32ffac22019-03-09 22:42:02 -0800104 constexpr bool verbose = false;
Austin Schuh6e56faf2019-03-10 14:04:57 -0700105 ::std::vector<Polygon> raw_polys;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800106 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800107 // Convert blobs to contours in the corrected space.
108 ContourNode* contour = finder_.GetContour(blob);
Austin Schuh6e56faf2019-03-10 14:04:57 -0700109 ::std::vector<::Eigen::Vector2f> unwarped_contour =
Austin Schuhe5015972019-03-09 17:47:34 -0800110 finder_.UnWarpContour(contour);
Austin Schuh6e56faf2019-03-10 14:04:57 -0700111 const Polygon polygon =
112 finder_.FindPolygon(::std::move(unwarped_contour), verbose);
113 if (!polygon.segments.empty()) {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800114 raw_polys.push_back(polygon);
115 }
116 }
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800117 LOG(INFO, "Polygons: (%zu).\n", raw_polys.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800118
119 // Calculate each component side of a possible target.
Austin Schuh32ffac22019-03-09 22:42:02 -0800120 ::std::vector<TargetComponent> target_component_list =
121 finder_.FillTargetComponentList(raw_polys, verbose);
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800122 LOG(INFO, "Components: (%zu).\n", target_component_list.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800123
124 // Put the compenents together into targets.
Austin Schuh32ffac22019-03-09 22:42:02 -0800125 ::std::vector<Target> target_list =
Parker Schuh2a1447c2019-02-17 00:25:29 -0800126 finder_.FindTargetsFromComponents(target_component_list, verbose);
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800127 LOG(INFO, "Potential Target: (%zu).\n", target_list.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800128
129 // Use the solver to generate an intermediate version of our results.
Austin Schuh32ffac22019-03-09 22:42:02 -0800130 ::std::vector<IntermediateResult> results;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800131 for (const Target &target : target_list) {
132 results.emplace_back(finder_.ProcessTargetToResult(target, verbose));
133 }
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800134 LOG(INFO, "Raw Results: (%zu).\n", results.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800135
Alex Perrybac3d3f2019-03-10 14:26:51 -0700136 results = finder_.FilterResults(results, 30, verbose);
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800137 LOG(INFO, "Results: (%zu).\n", results.size());
Brian Silvermance4825f2019-02-17 18:28:39 -0800138
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800139 // TODO: Select top 3 (randomly?)
140
Brian Silvermanc41fb862019-03-02 21:14:46 -0800141 frc971::jevois::CameraFrame frame{};
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800142
143 for (size_t i = 0; i < results.size() && i < frame.targets.max_size();
144 ++i) {
145 const auto &result = results[i].extrinsics;
146 frame.targets.push_back(frc971::jevois::Target{
147 static_cast<float>(result.z), static_cast<float>(result.y),
148 static_cast<float>(result.r2), static_cast<float>(result.r1)});
149 }
150
151 frame.age = std::chrono::duration_cast<frc971::jevois::camera_duration>(
152 aos::monotonic_clock::now() - monotonic_now);
153
Brian Silvermance4825f2019-02-17 18:28:39 -0800154 // If we succeed in writing our delimiter, then write out the rest of the
155 // frame. If not, no point in continuing.
156 if (write(itsDev, "\0", 1) == 1) {
Brian Silvermance4825f2019-02-17 18:28:39 -0800157 const auto serialized_frame = frc971::jevois::UartPackToTeensy(frame);
158 // We don't really care if this succeeds or not. If it fails for some
159 // reason, we'll just try again with the next frame, and the other end
160 // will find the new packet just fine.
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800161 ssize_t n =
162 write(itsDev, serialized_frame.data(), serialized_frame.size());
163
164 if (n != (ssize_t)serialized_frame.size()) {
165 LOG(INFO, "Some problem happened");
166 }
Brian Silvermance4825f2019-02-17 18:28:39 -0800167 }
Alex Perrye9775c32019-03-09 10:48:57 -0800168
169 writer_.ProcessImage(data, results.size());
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800170 });
Parker Schuh2a1447c2019-02-17 00:25:29 -0800171
172 aos::events::EpollLoop loop;
173
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800174 while (true) {
175 std::this_thread::sleep_for(std::chrono::milliseconds(1));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800176 camera0->ReadEvent();
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800177
178 {
179 constexpr size_t kBufferSize = frc971::jevois::uart_to_teensy_size();
180 char data[kBufferSize];
181 ssize_t n = read(itsDev, &data[0], kBufferSize);
182 if (n >= 1) {
Alex Perryadb5ad42019-03-06 20:51:40 -0800183 LOG(INFO, "Serial bytes: %zd", n);
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800184 cobs.ParseData(gsl::span<const char>(&data[0], n));
185 auto packet = cobs.received_packet();
186 if (!packet.empty()) {
187 auto calibration_question =
188 frc971::jevois::UartUnpackToCamera(packet);
189 if (calibration_question) {
190 const auto &calibration = *calibration_question;
Alex Perry3bf1bee2019-02-23 20:01:15 -0800191 IntrinsicParams *intrinsics = finder_.mutable_intrinsics();
192 intrinsics->mount_angle = calibration.calibration(0, 0);
193 intrinsics->focal_length = calibration.calibration(0, 1);
194 intrinsics->barrel_mount = calibration.calibration(0, 2);
195
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800196 switch (calibration.camera_command) {
Brian Silvermane9924fd2019-03-02 15:20:42 -0800197 case CameraCommand::kNormal:
Brian Silvermanbac77542019-03-03 13:57:00 -0800198 case CameraCommand::kAs:
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800199 break;
Brian Silvermane9924fd2019-03-02 15:20:42 -0800200 case CameraCommand::kUsb:
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800201 return 0;
Brian Silvermane9924fd2019-03-02 15:20:42 -0800202 case CameraCommand::kCameraPassthrough:
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800203 return system("touch /tmp/do_not_export_sd_card");
204 }
205 } else {
206 printf("bad frame\n");
207 }
208 cobs.clear_received_packet();
209 }
210 }
211 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800212 }
213
214 // TODO: Fix event loop on jevois:
215 // loop.Add(camera0.get());
216 // loop.Run();
217}