blob: a658bd7433f19a29857e2c8e58fc27a5700d6eb1 [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);
Parker Schuh41d72732019-02-22 22:28:04 -080026
27 int flags = fcntl(0, F_GETFL, 0);
28 fcntl(0, F_SETFL, flags | O_NONBLOCK);
29
30 int itsDev = ::y2019::jevois::open_via_terminos("/dev/ttyUSB0");
31
32 CobsPacketizer<uart_to_teensy_size()> cobs;
33 while (true) {
34 {
35 constexpr size_t kBufferSize = uart_to_teensy_size();
36 char data[kBufferSize];
37 ssize_t n = read(itsDev, &data[0], kBufferSize);
38 if (n >= 1) {
Austin Schuhb72be802022-01-02 12:26:28 -080039 cobs.ParseData(absl::Span<const char>(&data[0], n));
Parker Schuh41d72732019-02-22 22:28:04 -080040 auto packet = cobs.received_packet();
41 if (!packet.empty()) {
42 // One we read data from the serial, Teensy code will return an
43 // optional with success if there is a new frame. unwrap that
44 // and print out any frames from inside.
45 auto frame_optional = UartUnpackToTeensy(packet);
46 if (frame_optional) {
47 printf("----------\n");
48 const auto &frame = *frame_optional;
49 for (const auto &target : frame.targets) {
50 printf("z: %g y: %g, r1: %g, r2: %g\n", target.distance / 0.0254,
51 target.height / 0.0254, target.skew, target.heading);
52 }
53 } else {
54 printf("bad frame\n");
55 }
56 cobs.clear_received_packet();
57 }
58 }
59 }
60 {
61 constexpr size_t kBufferSize = 1024;
62 char data[kBufferSize];
63 // read command char from stdin. 'p' = passthrough else usb.
64 ssize_t n = read(0, &data[0], kBufferSize);
65 if (n >= 1) {
66 CameraCalibration calibration{};
67 if (data[0] == 'p') {
Brian Silvermane9924fd2019-03-02 15:20:42 -080068 calibration.camera_command = CameraCommand::kCameraPassthrough;
Parker Schuh41d72732019-02-22 22:28:04 -080069 } else {
Brian Silvermane9924fd2019-03-02 15:20:42 -080070 calibration.camera_command = CameraCommand::kUsb;
Parker Schuh41d72732019-02-22 22:28:04 -080071 }
72 if (write(itsDev, "\0", 1) == 1) {
73 const auto out_data = frc971::jevois::UartPackToCamera(calibration);
74 // We don't really care if this succeeds or not. If it fails for some
75 // reason, we'll just try again with the next frame, and the other end
76 // will find the new packet just fine.
77 if (static_cast<int>(
78 write(itsDev, out_data.data(), out_data.size())) !=
79 static_cast<int>(out_data.size())) {
80 printf("ERROR!!! START OVER FROM FIRST PRINCIPLES!!\n");
81 }
82 }
83 }
84 }
85 std::this_thread::sleep_for(std::chrono::milliseconds(2));
86 }
87}
88
89} // namespace y2019
90} // namespace vision
91
92int main(int argc, char **argv) { y2019::vision::main(argc, argv); }