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