blob: c707d1f3b4334ae3a98eb0008f944620afa68b25 [file] [log] [blame]
Parker Schuh41d72732019-02-22 22:28:04 -08001#include "y2019/jevois/serial.h"
2
3#include "aos/logging/implementations.h"
4#include "aos/logging/logging.h"
5#include "y2019/jevois/cobs.h"
6#include "y2019/jevois/serial.h"
7#include "y2019/jevois/structures.h"
8#include "y2019/jevois/uart.h"
9
10#include <fcntl.h>
11#include <unistd.h>
12#include <chrono>
13#include <fstream>
14#include <iostream>
15#include <thread>
16
17namespace y2019 {
18namespace vision {
19
20void main(int argc, char **argv) {
21 (void)argc;
22 (void)argv;
23 using namespace y2019::vision;
24 using namespace frc971::jevois;
25 // gflags::ParseCommandLineFlags(&argc, &argv, false);
26 ::aos::logging::Init();
Tyler Chatow4b471e12020-01-05 20:19:36 -080027 ::aos::logging::SetImplementation(
Parker Schuh41d72732019-02-22 22:28:04 -080028 new ::aos::logging::StreamLogImplementation(stderr));
29
30 int flags = fcntl(0, F_GETFL, 0);
31 fcntl(0, F_SETFL, flags | O_NONBLOCK);
32
33 int itsDev = ::y2019::jevois::open_via_terminos("/dev/ttyUSB0");
34
35 CobsPacketizer<uart_to_teensy_size()> cobs;
36 while (true) {
37 {
38 constexpr size_t kBufferSize = uart_to_teensy_size();
39 char data[kBufferSize];
40 ssize_t n = read(itsDev, &data[0], kBufferSize);
41 if (n >= 1) {
42 cobs.ParseData(gsl::span<const char>(&data[0], n));
43 auto packet = cobs.received_packet();
44 if (!packet.empty()) {
45 // One we read data from the serial, Teensy code will return an
46 // optional with success if there is a new frame. unwrap that
47 // and print out any frames from inside.
48 auto frame_optional = UartUnpackToTeensy(packet);
49 if (frame_optional) {
50 printf("----------\n");
51 const auto &frame = *frame_optional;
52 for (const auto &target : frame.targets) {
53 printf("z: %g y: %g, r1: %g, r2: %g\n", target.distance / 0.0254,
54 target.height / 0.0254, target.skew, target.heading);
55 }
56 } else {
57 printf("bad frame\n");
58 }
59 cobs.clear_received_packet();
60 }
61 }
62 }
63 {
64 constexpr size_t kBufferSize = 1024;
65 char data[kBufferSize];
66 // read command char from stdin. 'p' = passthrough else usb.
67 ssize_t n = read(0, &data[0], kBufferSize);
68 if (n >= 1) {
69 CameraCalibration calibration{};
70 if (data[0] == 'p') {
Brian Silvermane9924fd2019-03-02 15:20:42 -080071 calibration.camera_command = CameraCommand::kCameraPassthrough;
Parker Schuh41d72732019-02-22 22:28:04 -080072 } else {
Brian Silvermane9924fd2019-03-02 15:20:42 -080073 calibration.camera_command = CameraCommand::kUsb;
Parker Schuh41d72732019-02-22 22:28:04 -080074 }
75 if (write(itsDev, "\0", 1) == 1) {
76 const auto out_data = frc971::jevois::UartPackToCamera(calibration);
77 // We don't really care if this succeeds or not. If it fails for some
78 // reason, we'll just try again with the next frame, and the other end
79 // will find the new packet just fine.
80 if (static_cast<int>(
81 write(itsDev, out_data.data(), out_data.size())) !=
82 static_cast<int>(out_data.size())) {
83 printf("ERROR!!! START OVER FROM FIRST PRINCIPLES!!\n");
84 }
85 }
86 }
87 }
88 std::this_thread::sleep_for(std::chrono::milliseconds(2));
89 }
90}
91
92} // namespace y2019
93} // namespace vision
94
95int main(int argc, char **argv) { y2019::vision::main(argc, argv); }