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