This commit is contained in:
trung09122002
2026-09-22 15:42:14 +07:00
parent 14ccf198d2
commit 2ef18312a8
16 changed files with 1502 additions and 3 deletions

View File

@@ -67,6 +67,13 @@ endif()
# duoc tren mot may chi co mang toi LiDAR, khong can build ca cay.
add_executable(vslam_lidar_probe vslam_lidar_probe.cc)
# vslam_joy_probe: in index truc/nut THAT cua tay cam. Thay cho `joy_debug_node`
# cua base_kit.
#
# Cung ly do nhu lidar_probe, khong phu thuoc lib nao cua du an: no phai chay
# duoc tren may chi co tay cam cam vao, truoc khi build ca cay.
add_executable(vslam_joy_probe vslam_joy_probe.cc)
# vslam_world_export: `.world` cua Gazebo -> luoi chiem dung .pgm + .yaml.
#
# Cho phep dung the gioi mo phong TU CHINH file world, thay vi tu mot .db da

133
tools/vslam_joy_probe.cc Normal file
View File

@@ -0,0 +1,133 @@
// 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 <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
namespace {
constexpr double kAxisScale = 1.0 / 32767.0;
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("\ngat 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] = ev.value * kAxisScale;
} 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;
}