Ubuntu 16.04环境中读取XBOX 360手柄信息

设备驱动安装

Ubuntu16.04 ROS(Kinetic)环境为例,安装Xbox360手柄驱动:

sudo apt-get install ros-kinetic-joystick-drivers

设备配对

刚插入设备USB,手柄的灯闪烁,进入匹配状态,几秒之后自动匹配好,手柄西瓜键灯会亮1号灯

获取设备的端口

在插入设备时,在/dev/input目录下 ,操作系统会为每一个设备分配event号,先插上的为event0,后插上的为event1。
查找设备的设备号用如下指令:

cat /proc/bus/input/devices

获得如下输出

I: Bus=0003 Vendor=80ee Product=0021 Version=0110
N: Name="VirtualBox USB Tablet"
P: Phys=usb-0000:00:06.0-1/input0
S: Sysfs=/devices/pci0000:00/0000:00:06.0/usb1/1-1/1-1:1.0/0003:80EE:0021.0001/input/input6
U: Uniq=
H: Handlers=mouse1 event5 js0 
B: PROP=0
B: EV=1f
B: KEY=1f0000 0 0 0 0
B: REL=1940
B: ABS=3
B: MSC=10

I: Bus=0001 Vendor=80ee Product=cafe Version=0601
N: Name="VirtualBox mouse integration"
P: Phys=
S: Sysfs=/devices/pci0000:00/0000:00:04.0/input/input7
U: Uniq=
H: Handlers=event6 js1 
B: PROP=0
B: EV=b
B: KEY=10000 0 0 0 0
B: ABS=3

I: Bus=0003 Vendor=045e Product=028e Version=0110
N: Name="Microsoft X-Box 360 pad"
P: Phys=usb-0000:00:06.0-2/input0
S: Sysfs=/devices/pci0000:00/0000:00:06.0/usb1/1-2/1-2:1.0/input/input8
U: Uniq=
H: Handlers=event7 js2 
B: PROP=0
B: EV=20000b
B: KEY=7cdb000000000000 0 0 0 0
B: ABS=3003f
B: FF=107030000 0

在端口处找到设备名字“Microsoft X-Box 360 pad”,event7/js2是手柄的设备号。

如果找不到,可能是手柄中的版本不对,根据产品说明书需要由X-input转换到D-input模式。(长按home键3秒)X -input适用于XBOX360移植到PC上的即插即玩的游戏,D-input适用于早期单机游戏如极品飞车1-12等。

读取设备数据

查看端口号:

ls /dev/input

查看端口输出

cat /dev/input/js2 | hexdump

或者

cat /dev/input/enent7 | hexdump

或者

jstest /dev/input/js2

得到一些16进制的数字,当手柄按下的时候会有数字变化

ke@ubnt20:/dev/input$ ls /dev/input/
by-id    event0  event2  event4  event6  js0  js2   mouse0
by-path  event1  event3  event5  event7  js1  mice  mouse1
ke@ubnt20:/dev/input$ cat /dev/input/js2 | hexdump 
0000000 c6d8 0003 0000 0081 c6d8 0003 0000 0181
0000010 c6d8 0003 0000 0281 c6d8 0003 0000 0381
0000020 c6d8 0003 0000 0481 c6d8 0003 0000 0581
0000030 c6d8 0003 0000 0681 c6d8 0003 0000 0781
0000040 c6d8 0003 0000 0881 c6d8 0003 0000 0981
0000050 c6d8 0003 0000 0a81 c6d8 0003 0000 0082
0000060 c6d8 0003 0000 0182 c6d8 0003 8001 0282
0000070 c6d8 0003 0000 0382 c6d8 0003 0000 0482
0000080 c6d8 0003 8001 0582 c6d8 0003 0000 0682
0000090 c6d8 0003 0000 0782 d274 0003 0001 0501
00000a0 d2e4 0003 0000 0501 d5c0 0003 8efa 0102
00000b0 d5e8 0003 8001 0102 d718 0003 0000 0102
00000c0 d820 0003 8001 0102 d8c4 0003 0000 0102
00000d0 d960 0003 7fff 0002 da4c 0003 0000 0002
00000e0 dbe4 0003 7fff 0602 dce4 0003 0000 0602
00000f0 ddb8 0003 7fff 0602 de90 0003 0000 0602
0000100 df84 0003 8001 0602 e034 0003 0000 0602
0000110 ead0 0003 8001 0602 ebb4 0003 0000 0602
0000120 ec64 0003 7fff 0702 ec9c 0003 7fff 0602
0000130 ecc4 0003 0000 0702 ed68 0003 0000 0602
0000140 f0c0 0003 8001 0602 f1b8 0003 0000 0602
0000150 f258 0003 7fff 0602 f364 0003 0000 0602
0000160 f3f4 0003 8001 0602 f504 0003 0000 0602
0000170 f568 0003 7fff 0602 f684 0003 0000 0602
0000180 f958 0003 7fff 0602 fa44 0003 0000 0602
0000190 fa5c 0003 8001 0702 fa70 0003 8001 0602
00001a0 fa80 0003 0000 0702 fb9c 0003 0000 0602
00001b0 fdf8 0003 7fff 0702 fee0 0003 0000 0702
00001c0 ffbc 0003 8001 0702 0094 0004 0000 0702
00001d0 0178 0004 7fff 0602 0278 0004 0000 0602
00001e0 02e0 0004 8001 0602 03d4 0004 0000 0602
00001f0 0444 0004 7fff 0702 0490 0004 0000 0702

ROS 中测试

首先,在新终端启动 roscore

roscore

然后设置设备并启动 joy 节点

rosparam set joy_node/dev "/dev/input/js2"
rosrun joy joy_node

最后在新的终端显示 joy 主题,移动手柄可以查看到相关的主题

rostopic echo joy

参考

Ubuntu16.04环境下读取USB手柄/方向盘信息
ROS笔记(38) Xbox360手柄控制移动

posted @ 2021-05-01 14:12  采男孩的小蘑菇  阅读(862)  评论(0编辑  收藏  举报