Files
vslam_cpp/tools/vslam_joy_probe.cc
trung09122002 f17dbdce2b update
2026-09-22 17:18:02 +07:00

148 lines
4.8 KiB
C++

// In trang thai tho cua tay cam. Port cua `joy_debug_node` trong
// base_kit/src/joy_control.
//
// ================== VI SAO CAN CONG CU NAY ==================
// `system.yaml gamepad` noi "can trai Y la axes[1]". Con so do dung voi
// Logitech F710/F310 o mode XInput, va SAI voi gan nhu moi bo tay cam khac --
// tay PS, tay F710 o mode DirectInput (cong gat X/D o lung), tay tau, hay cung
// mot tay cam nhung qua driver khac.
//
// Doan index la cach chac chan nhat de tao ra mot con robot lai nguoc ma khong
// ai hieu vi sao. Cong cu nay khong doan: no IN RA cai dang co.
//
// ⚠️ Cong cu nay KHONG lam robot chuyen dong. No chi doc /dev/input/jsN.
//
// ================== CACH DUNG ==================
// build/tools/vslam_joy_probe [/dev/input/js0]
//
// Gat tung can, bam tung nut, ghi lai so hien ra, roi dat vao system.yaml.
// Chu y hai thu hay bi sai:
// * COC (LT/RT) o XInput nghi o +1.0 chu khong phai 0, va TRUOC lan bam dau
// tien thi doc ra 0.0.
// * D-pad co the la TRUC (axes 6/7) hoac la NUT, tuy driver.
#include <errno.h>
#include <fcntl.h>
#include <linux/joystick.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
namespace {
constexpr double kAxisScale = 1.0 / 32767.0;
// KHONG dao dau -- giong het GamepadReader::NormalizeJoydevAxis.
//
// Cong cu nay PHAI in cung quy uoc voi node. Neu mot ben dao dau con ben kia
// khong, 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. Da xay ra that.
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");
std::printf("Nut (%zu tong):", buttons.size());
bool any = false;
for (size_t i = 0; i < buttons.size(); ++i) {
if (buttons[i] != 0) {
std::printf(" buttons[%zu]=1", i);
any = true;
}
}
if (!any) std::printf(" (khong nut nao)");
std::printf("\n");
std::printf("Truc (%zu tong):\n", axes.size());
for (size_t i = 0; i < axes.size(); ++i) {
// In CA truc dang o 0: mot truc luon 0 la thong tin (driver khong xuat
// no), va mot truc nghi o +1.0 thi phai thay duoc ngay -- do la coc.
std::printf(" axes[%zu] = %+.3f%s\n", i, axes[i],
std::abs(axes[i]) > 0.05 ? " <-- dang gat" : "");
}
std::printf("===============\n");
}
} // namespace
int main(int argc, char** argv) {
const std::string dev = (argc > 1) ? argv[1] : "/dev/input/js0";
const int fd = ::open(dev.c_str(), O_RDONLY);
if (fd < 0) {
std::fprintf(stderr, "khong mo duoc %s: %s\n", dev.c_str(),
std::strerror(errno));
std::fprintf(stderr,
"\n Kiem tra:\n"
" ls -l /dev/input/js* thiet bi co ton tai khong\n"
" groups co trong nhom `input` khong\n");
return 1;
}
uint8_t n_axes = 0;
uint8_t n_buttons = 0;
char name[128] = {};
if (::ioctl(fd, JSIOCGAXES, &n_axes) < 0) n_axes = 0;
if (::ioctl(fd, JSIOCGBUTTONS, &n_buttons) < 0) n_buttons = 0;
if (::ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0) {
std::snprintf(name, sizeof(name), "(khong doc duoc ten)");
}
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("\ntruc in DUNG con so ma node nhan duoc (khong dao dau).\n");
std::printf("Day can len ma ra AM -> dat invert_linear: true trong"
" system.yaml.\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);
for (;;) {
pollfd pfd{};
pfd.fd = fd;
pfd.events = POLLIN;
const int rc = ::poll(&pfd, 1, 200);
if (rc < 0) {
if (errno == EINTR) continue;
std::fprintf(stderr, "poll(): %s\n", std::strerror(errno));
break;
}
if (rc == 0) continue;
if ((pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) != 0) {
std::fprintf(stderr, "thiet bi bien mat\n");
break;
}
js_event ev{};
const ssize_t n = ::read(fd, &ev, sizeof(ev));
if (n != static_cast<ssize_t>(sizeof(ev))) continue;
// 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] = NormalizeAxis(ev.value);
} else if (type == JS_EVENT_BUTTON && ev.number < buttons.size()) {
buttons[ev.number] = ev.value ? 1 : 0;
} else {
continue;
}
PrintState(axes, buttons);
}
::close(fd);
return 0;
}