From 05adb18909d9be8021a6b697cd5e0337ea5d0642 Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Fri, 16 Jan 2026 14:53:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BD=D1=8F=D1=82=D1=8C=20=D0=BF=D1=80=D0=BE=D0=B8?= =?UTF-8?q?=D1=81=D1=85=D0=BE=D0=B4=D1=8F=D1=89=D0=B5=D0=B5=20=D0=B8=20?= =?UTF-8?q?=D1=83=D1=81=D1=82=D1=80=D0=B0=D0=BD=D0=B8=D1=82=D1=8C=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- air/main.cpp | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/air/main.cpp b/air/main.cpp index 0b0d476..59b74cd 100644 --- a/air/main.cpp +++ b/air/main.cpp @@ -23,7 +23,7 @@ inline int64_t milliseconds() { return ((tv.tv_sec * 1000000l) + tv.tv_usec) / 1000; } -constexpr int64_t SBUS_SEND_FRAME_INTERVAL = 15; +constexpr int64_t SBUS_SEND_FRAME_INTERVAL = 50; constexpr int64_t SBUS_RXLOSS_INTERVAL = 500; class UDPServer { @@ -186,7 +186,7 @@ public: // Открытие последовательного порта fd = ::open(port_path.c_str(), O_RDWR | O_NOCTTY | O_SYNC); if (fd < 0) { - std::cerr << "Failed to open serial port " << port_path << ": " << std::strerror(errno) << std::endl; + std::cout << "Failed to open serial port " << port_path << ": " << std::strerror(errno) << std::endl; return false; } struct termios2 tio{}; @@ -208,7 +208,7 @@ public: tio.c_cflag |= (BOTHER|CREAD|CLOCAL); if (ioctl(fd, TCSETS2, &tio) != 0) { - std::cerr << "Failed to set termios2 attributes: " << std::strerror(errno) << std::endl; + std::cout << "Failed to set termios2 attributes: " << std::strerror(errno) << std::endl; close(fd); return false; } @@ -248,13 +248,13 @@ public: ssize_t written = write(fd, data.data(), data.size()); if (written < 0) { - std::cerr << "Failed to write to serial port " << strerror(errno) << std::endl; + std::cout << "Failed to write to serial port " << strerror(errno) << std::endl; return false; } // Принудительная отправка данных if (drain() < 0) { - std::cerr << "Failed to flush data to serial port" << strerror(errno) << std::endl; + std::cout << "Failed to flush data to serial port" << strerror(errno) << std::endl; return false; } return true; @@ -263,7 +263,7 @@ public: int main(int argc, char* argv[]) { // Парсим аргументы командной строки - std::string serial_port = "/dev/ttyUSB0"; + std::string serial_port = "/dev/ttyPS0"; if (argc > 1) { serial_port = argv[1]; } @@ -289,14 +289,23 @@ int main(int argc, char* argv[]) { int totalPacketCount = 0; int serialErrors = 0; int64_t lastStatisticsShow = milliseconds(); + int64_t _lastLoopNow = lastStatisticsShow; while (true) { pollfd udpFd{.fd = udp_server.sockfd, .events = POLLIN, .revents = 0}; poll(&udpFd, 1, SBUS_SEND_FRAME_INTERVAL / 4); + auto now = milliseconds(); + if (std::abs(now - _lastLoopNow) > SBUS_SEND_FRAME_INTERVAL) { + std::cout << "Warning: Loop freeze, reset timers. now time:" << now << std::endl; + lastSbusWrite = 0; + lastSbusRecv = 0; + lastStatisticsShow = 0; + } + _lastLoopNow = now; if (udpFd.revents & POLLIN) { // Прием UDP пакета std::vector data = udp_server.receive(); - lastSbusRecv = milliseconds(); + lastSbusRecv = now; if (!data.empty()) { packetCount++; @@ -320,7 +329,7 @@ int main(int argc, char* argv[]) { } } - const auto now = milliseconds(); + std::cout << "from loop: current time " << now << std::endl; if (now - lastSbusWrite >= SBUS_SEND_FRAME_INTERVAL && now - lastSbusRecv <= SBUS_RXLOSS_INTERVAL) { lastSbusWrite = now; if (!serial.writeBuffer(sb.binaryBuffer)) { @@ -331,7 +340,7 @@ int main(int argc, char* argv[]) { // после 50 ошибок дальше не будем пытаться что-то писать, ибо это бесполезно if (serialErrors >= 50) { - std::cerr << "FATAL: 50 errors on serial port write operation, exit" << std::endl; + std::cout << "FATAL: 50 errors on serial port write operation, exit" << std::endl; break; } } @@ -345,7 +354,7 @@ int main(int argc, char* argv[]) { } } catch (const std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; + std::cout << "Error: " << e.what() << std::endl; return 1; }