добавил программу для воздуха, все вроде работает
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user