Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 1 | #include <fcntl.h> |
| 2 | #include <unistd.h> |
| 3 | |
| 4 | #include <chrono> |
| 5 | #include <fstream> |
| 6 | #include <iostream> |
| 7 | #include <thread> |
Parker Schuh | 41d7273 | 2019-02-22 22:28:04 -0800 | [diff] [blame] | 8 | |
| 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 Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 16 | namespace y2019::vision { |
Parker Schuh | 41d7273 | 2019-02-22 22:28:04 -0800 | [diff] [blame] | 17 | |
| 18 | void 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 Schuh | 41d7273 | 2019-02-22 22:28:04 -0800 | [diff] [blame] | 24 | |
| 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 Schuh | b72be80 | 2022-01-02 12:26:28 -0800 | [diff] [blame] | 37 | cobs.ParseData(absl::Span<const char>(&data[0], n)); |
Parker Schuh | 41d7273 | 2019-02-22 22:28:04 -0800 | [diff] [blame] | 38 | 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 Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 66 | calibration.camera_command = CameraCommand::kCameraPassthrough; |
Parker Schuh | 41d7273 | 2019-02-22 22:28:04 -0800 | [diff] [blame] | 67 | } else { |
Brian Silverman | e9924fd | 2019-03-02 15:20:42 -0800 | [diff] [blame] | 68 | calibration.camera_command = CameraCommand::kUsb; |
Parker Schuh | 41d7273 | 2019-02-22 22:28:04 -0800 | [diff] [blame] | 69 | } |
| 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 Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 87 | } // namespace y2019::vision |
Parker Schuh | 41d7273 | 2019-02-22 22:28:04 -0800 | [diff] [blame] | 88 | |
| 89 | int main(int argc, char **argv) { y2019::vision::main(argc, argv); } |