попытки сделать crsf, не взлетела часть с пультом
This commit is contained in:
102
ground/main.cpp
102
ground/main.cpp
@@ -1,58 +1,86 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
#include <csignal>
|
||||
#include <port/poller.h>
|
||||
#include <port/uart.h>
|
||||
#include <port/udp.h>
|
||||
#include <crsf.h>
|
||||
|
||||
#include "joystick-reader.h"
|
||||
#include "udp-driver.h"
|
||||
#include "port/poller.h"
|
||||
#include "port/uart.h"
|
||||
#include "port/udp.h"
|
||||
|
||||
static constexpr uint16_t UDP_PORT = 1067;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
// Парсим аргументы командной строки для частоты
|
||||
int frequency = 5;
|
||||
const char* sendAddress;
|
||||
if (argc == 1) {
|
||||
sendAddress = "127.0.0.1";
|
||||
} else if (argc == 2) {
|
||||
sendAddress = argv[1];
|
||||
} else if (argc == 3) {
|
||||
sendAddress = argv[1];
|
||||
frequency = std::atoi(argv[2]);
|
||||
} else {
|
||||
std::cerr << "Usage: " << argv[0] << " send.ip.addr.v4 [Frequency]" << std::endl;
|
||||
if (argc < 3 || argc > 4) {
|
||||
std::cerr << "Usage: " << argv[0]
|
||||
<< " serial_port remote.ip.addr.4 [control packets frequency]\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Starting joystick reader with frequency: " << frequency << " Hz" << std::endl;
|
||||
const std::string serialPort = argv[1];
|
||||
const std::string remoteIp = argv[2];
|
||||
const int controlFreq = (argc == 4) ? std::atoi(argv[3]) : 50;
|
||||
|
||||
JoystickReader reader;
|
||||
const int64_t timeInterval = 1000 / frequency;
|
||||
while (!reader.initialize()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
}
|
||||
// UART speed fixed at 400k per your earlier specification
|
||||
constexpr int UART_BAUD = 400000;
|
||||
|
||||
UDPSocket udp(sendAddress, 1066);
|
||||
std::vector<uint16_t> data;
|
||||
std::cout << "Opening UART: " << serialPort << " @ " << UART_BAUD << "\n";
|
||||
std::cout << "Remote CRSF UDP endpoint: " << remoteIp << ":14550\n";
|
||||
std::cout << "Control packets frequency: " << controlFreq << " Hz\n";
|
||||
|
||||
std::cout << "Sending data to " << sendAddress << ":1066" << std::endl;
|
||||
poller::PollWrapper poller;
|
||||
|
||||
int counter = 0;
|
||||
while (reader.readData(data)) {
|
||||
if (!udp.sendFrame(data)) {
|
||||
std::cerr << "Failed to send UDP packet" << std::endl;
|
||||
auto uart = std::make_shared<drivers::UartDriver>(serialPort, UART_BAUD);
|
||||
auto udp = std::make_shared<drivers::UdpDriver>(UDP_PORT);
|
||||
|
||||
poller.objects.push_back(uart);
|
||||
poller.objects.push_back(udp);
|
||||
|
||||
crsf::CrsfParser parser;
|
||||
// CrsfTxProcessor processor(udp, uart, remoteIp, UDP_PORT, controlFreq);
|
||||
|
||||
std::vector<uint8_t> tmpBuffer;
|
||||
|
||||
auto lastTick = std::chrono::steady_clock::now();
|
||||
|
||||
while (true) {
|
||||
poller.loop(1000);
|
||||
|
||||
if (uart->isPollHup()) {
|
||||
std::cerr << "UART disconnected!\n";
|
||||
break;
|
||||
}
|
||||
if (udp->isPollHup()) {
|
||||
std::cerr << "UDP socket error!\n";
|
||||
break;
|
||||
}
|
||||
|
||||
// Выводим прогресс каждые 100 пакетов
|
||||
if (++counter % 100 == 0) {
|
||||
std::cout << "Sent " << counter << " packets" << std::endl;
|
||||
bool eventReceived = false;
|
||||
|
||||
// UART input
|
||||
if (uart->isPollIn()) {
|
||||
eventReceived = true;
|
||||
size_t read = uart->readChunk(tmpBuffer);
|
||||
if (read > 0) {
|
||||
parser.parseBytes(tmpBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
// вычислим время для сна
|
||||
// должно быть время now+timeInterval
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(timeInterval));
|
||||
while (true) {
|
||||
auto pkt = parser.pullPacket();
|
||||
if (pkt == nullptr) {
|
||||
break;
|
||||
}
|
||||
pkt->writeToBuffer(tmpBuffer);
|
||||
udp->sendTo(std::span<const uint8_t>(tmpBuffer), remoteIp, UDP_PORT);
|
||||
}
|
||||
|
||||
if (!eventReceived) {
|
||||
std::cout << "[W]: No actions received in last 1s!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Exiting..." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user