This commit is contained in:
trung09122002
2026-09-22 17:10:03 +07:00
parent 83e20a64d9
commit caf080649f
6 changed files with 105 additions and 34 deletions

View File

@@ -28,6 +28,7 @@
#include <sys/ioctl.h>
#include <unistd.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
@@ -38,6 +39,19 @@ namespace {
constexpr double kAxisScale = 1.0 / 32767.0;
// ⚠️ DAO DAU, GIONG HET GamepadReader.
//
// joydev tho quy uoc LEN va TRAI la AM; `joy_node` cua ROS dao dau, va moi
// gia tri trong `system.yaml gamepad` deu theo quy uoc da dao do.
//
// Cong cu nay PHAI in cung quy uoc voi node. In so tho thi nguoi do se doc
// dau o day roi dat config nguoc lai -- va phat hien ra bang cach thay robot
// lui khi day can gat len.
double NormalizeAxis(int16_t raw) {
const double v = -(static_cast<double>(raw) * kAxisScale);
return std::max(-1.0, std::min(1.0, v));
}
void PrintState(const std::vector<double>& axes,
const std::vector<int>& buttons) {
std::printf("\n=== TAY CAM ===\n");
@@ -91,7 +105,9 @@ int main(int argc, char** argv) {
std::printf("thiet bi : %s\n", dev.c_str());
std::printf("ten : %s\n", name);
std::printf("layout : %u truc, %u nut\n", n_axes, n_buttons);
std::printf("\ngat can va bam nut; Ctrl-C de thoat.\n");
std::printf("\ntruc in theo DUNG quy uoc node dung (day len / gat trai ="
" duong).\n");
std::printf("gat can va bam nut; Ctrl-C de thoat.\n");
std::vector<double> axes(n_axes, 0.0);
std::vector<int> buttons(n_buttons, 0);
@@ -119,7 +135,7 @@ int main(int argc, char** argv) {
// Bo co INIT nhung VAN ap gia tri: do la trang thai hien tai luc vua mo.
const uint8_t type = ev.type & ~JS_EVENT_INIT;
if (type == JS_EVENT_AXIS && ev.number < axes.size()) {
axes[ev.number] = ev.value * kAxisScale;
axes[ev.number] = NormalizeAxis(ev.value);
} else if (type == JS_EVENT_BUTTON && ev.number < buttons.size()) {
buttons[ev.number] = ev.value ? 1 : 0;
} else {