добавил программу для воздуха, все вроде работает

This commit is contained in:
2025-11-14 13:31:55 +03:00
parent af351135a2
commit 8b7c7b71bc
4 changed files with 69 additions and 67 deletions

View File

@@ -3,8 +3,8 @@
#include <thread>
#include <chrono>
JoystickReader::JoystickReader(int frequency)
: joystick(nullptr), updateIntervalMs(1000 / frequency) {}
JoystickReader::JoystickReader()
: joystick(nullptr) {}
JoystickReader::~JoystickReader() {
if (joystick) {
@@ -50,29 +50,28 @@ bool JoystickReader::readData(std::vector<uint16_t>& data) {
SDL_JoystickUpdate();
data.resize(16, 1500); // Заполняем нейтральным значением
data.resize(24); // Заполняем нейтральным значением
for (auto& i: data) { i = 1500; }
// Читаем оси (обычно до 6 осей)
int axes = std::min(SDL_JoystickNumAxes(joystick), 6);
for (int i = 0; i < axes; ++i) {
int axes = std::min(SDL_JoystickNumAxes(joystick), 8);
for (int i = 0; i < std::min(8, axes); ++i) {
Sint16 axisValue = SDL_JoystickGetAxis(joystick, i);
// Преобразуем из [-32768, 32767] в [1000, 2000]
data[i] = static_cast<uint16_t>((axisValue + 32768) * 1000 / 65536 + 1000);
}
axes = 8;
// Читаем кнопки
int buttons = SDL_JoystickNumButtons(joystick);
for (int i = 0; i < buttons && i < data.size() - axes; ++i) {
Uint8 buttonState = SDL_JoystickGetButton(joystick, i);
data[axes + i] = buttonState ? 2000 : 1000;
auto buttonState = SDL_JoystickGetButton(joystick, i);
data[axes + i] = static_cast<uint16_t>(1000.0 + (buttonState * (1000.0 / 255.0)));
}
for (auto& i: data) {
if (i < 900) i = 900;
if (i > 2100) i = 2100;
if (i < 950) i = 950;
if (i > 2050) i = 2050;
}
std::this_thread::sleep_for(std::chrono::milliseconds(updateIntervalMs));
return true;
}

View File

@@ -13,7 +13,7 @@
class JoystickReader {
public:
JoystickReader(int frequency = 50);
JoystickReader();
~JoystickReader();
bool initialize();
@@ -22,7 +22,6 @@ public:
private:
SDL_Joystick* joystick;
int updateIntervalMs;
std::string joystickName;
};

View File

@@ -6,6 +6,12 @@
#include "joystick-reader.h"
#include "udp-driver.h"
int64_t milliseconds() {
timeval tv{};
gettimeofday(&tv,nullptr);
return ((tv.tv_sec * 1000000l) + tv.tv_usec) / 1000;
}
int main(int argc, char* argv[]) {
// Парсим аргументы командной строки для частоты
@@ -25,7 +31,8 @@ int main(int argc, char* argv[]) {
std::cout << "Starting joystick reader with frequency: " << frequency << " Hz" << std::endl;
JoystickReader reader(frequency);
JoystickReader reader;
const int64_t timeInterval = 1000 / frequency;
while (!reader.initialize()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
@@ -36,6 +43,7 @@ int main(int argc, char* argv[]) {
std::cout << "Sending data to " << sendAddress << ":1066" << std::endl;
int counter = 0;
auto now = milliseconds();
while (reader.readData(data)) {
if (!udp.sendFrame(data)) {
std::cerr << "Failed to send UDP packet" << std::endl;
@@ -45,6 +53,15 @@ int main(int argc, char* argv[]) {
if (++counter % 100 == 0) {
std::cout << "Sent " << counter << " packets" << std::endl;
}
// вычислим время для сна
// должно быть время now+timeInterval
auto targetTime = now + timeInterval;
auto nowTime = milliseconds();
if (targetTime > nowTime) {
std::this_thread::sleep_for(std::chrono::milliseconds(targetTime - nowTime));
}
now = milliseconds();
}
std::cout << "Exiting..." << std::endl;