Ubuntu环境下读取罗技G29方向盘信息
生活随笔
收集整理的這篇文章主要介紹了
Ubuntu环境下读取罗技G29方向盘信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Ubuntu環境下讀取羅技G29方向盤信息
引言
實驗室有這么酷的駕駛設備,來了一年還沒有實際操作過,早就蠢蠢欲試了,哈哈哈不過之前負責的師兄還在就一直沒敢用,現在他畢業了就可以為所欲為了
之前師兄好像都是在Windows下開發的,我覺得比較麻煩而且與現有的框架感覺兼容性不高,所以還是選擇了在Linux下開發
信息查看
首先要確定插入的設備哪一個是G29方向盤,下面兩個命令都可以
ls /dev/input 或 dmesg一般來說是event11或js0
要查看方向盤信息,在終端輸入
cat /dev/input/js0 | hexdump輸出信息如下
(base) redwall@redwall-desktop:~$ cat /dev/input/js0 | hexdump 0000000 d754 0053 0000 0081 d754 0053 0000 0181 0000010 d754 0053 0000 0281 d754 0053 0000 0381 0000020 d754 0053 0000 0481 d754 0053 0000 0581 0000030 d754 0053 0000 0681 d754 0053 0000 0781 0000040 d754 0053 0000 0881 d754 0053 0000 0981顯然沒什么可讀性
安裝操縱桿的校準工具:jstest-gtk
sudo aptitude install jstest-gtk建議大家熟悉使用aptitude而不是apt
安裝完成后在終端運行
jstest-gtk /dev/input/js0出現如下圖形界面,方向盤信息均在圖形界面中進行了顯示
結合ROS的joy包進行開發
安裝并編譯相關包,在終端輸入
sudo aptitude install ros-melodic-joy sudo aptitude install ros-melodic-joystick sudo aptitude install ros-melodic-joystick-drivers rosdep install joy rosmake joy在兩個終端中分別輸入
roscore rosrun joy joy_node查看現有的話題
(base) redwall@redwall-desktop:~/catkin_ws$ rostopic list /diagnostics /joy /joy/set_feedback /rosout /rosout_agg其中/joy話題包含了需要的方向盤信息,查看/joy話題的內容
(base) redwall@redwall-desktop:~$ rostopic echo /joy輸出
--- header: seq: 386stamp: secs: 1657894839nsecs: 565566079frame_id: "/dev/input/js0" axes: [0.648137629032135, 0.0, 0.0, 0.0, 0.0, 0.0] buttons: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] --- header: seq: 387stamp: secs: 1657894839nsecs: 567570108frame_id: "/dev/input/js0" axes: [0.6481055021286011, 0.0, 0.0, 0.0, 0.0, 0.0] buttons: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] --- header: seq: 388stamp: secs: 1657894839nsecs: 573687380frame_id: "/dev/input/js0" axes: [0.6480733752250671, 0.0, 0.0, 0.0, 0.0, 0.0] buttons: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]查看/joy話題的信息
(base) redwall@redwall-desktop:~$ rostopic info /joy Type: sensor_msgs/JoyPublishers: * /joy_node (http://redwall-desktop:41415/)Subscribers: None可以看到該話題是由/joy_node節點發布的,信息的數據類型為sensor_msgs/Joy
查看sensor_msgs/Joy的消息格式
(base) redwall@redwall-desktop:~$ rosmsg show sensor_msgs/Joy std_msgs/Header headeruint32 seqtime stampstring frame_id float32[] axes int32[] buttons- seq——消息序列
- stamp——消息的時間戳
- frame_id——消息的來源
- axes——方向盤中軸的信息,以數組的形式
- buttons——方向盤中按鍵的信息,以數組的形式
所以在正常使用中只要訂閱該話題,并對該話題發布的消息進行解析即可獲取G29方向盤的信息
編寫簡單的測試程序
#include <ros/ros.h> #include <sensor_msgs/Joy.h> #include <iostream> using namespace std;void steer_callback(const sensor_msgs::Joy::ConstPtr &msg) {cout << "Receive control message from:" << msg->header.frame_id << endl;for (int i = 0; i < msg->axes.size(); ++i){if (msg->axes[i] != 0.0){cout << "Axis " << i << " is not zero!" << endl;cout << "Axis " << i << " value:" << msg->axes[i] << endl;}}for (int i = 0; i < msg->buttons.size(); ++i){if (msg->buttons[i] != 0.0){cout << "Button " << i << " is not zero!" << endl;cout << "Button " << i << " value:" << msg->axes[i] << endl;}}cout << endl; }int main(int argc, char *argv[]) {ros::init(argc, argv, "logitech_steer");ros::NodeHandle nh;ros::Subscriber sub = nh.subscribe("/joy", 1, steer_callback);ros::spin();return 0; }編寫簡單的launch文件
<?xml version="1.0"?> <launch><node pkg="joy" type="joy_node" name="joy_node" output="screen" /><node pkg="steer_test" type="steer_test" name="steer_test" output="screen" /> </launch>運行輸出
(base) redwall@redwall-desktop:~/catkin_ws$ roslaunch steer_test steer_test.launch ... logging to /home/redwall/.ros/log/dbe33e8e-043a-11ed-a8dc-000babe43e9a/roslaunch-redwall-desktop-20515.log Checking log directory for disk usage. This may take a while. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB.started roslaunch server http://redwall-desktop:46371/SUMMARY ========PARAMETERS* /rosdistro: melodic* /rosversion: 1.14.13NODES/joy_node (joy/joy_node)steer_test (steer_test/steer_test)ROS_MASTER_URI=http://localhost:11311process[joy_node-1]: started with pid [20547] process[steer_test-2]: started with pid [20548] [ WARN] [1657895770.683714324]: Couldn't set gain on joystick force feedback: Bad file descriptor [ INFO] [1657895770.685046029]: Opened joystick: /dev/input/js0. deadzone_: 0.050000. Receive control message from:/dev/input/js0 Axis 0 is not zero! Axis 0 value:0.648041Receive control message from:/dev/input/js0 Axis 0 is not zero! Axis 0 value:0.648009Receive control message from:/dev/input/js0 Axis 0 is not zero! Axis 0 value:0.647977Receive control message from:/dev/input/js0 Axis 0 is not zero! Axis 0 value:0.647945確定按鍵與軸的對應關系
| 離合 | axes[1] |
| 剎車 | axes[3] |
| 油門 | axes[2] |
| 左撥片 | buttons[5] |
| 右撥片 | buttons[4] |
| 紅色圓環包圍的回車鍵 | buttons[23] |
| 加號鍵 | buttons[19] |
| 減號鍵 | buttons[20] |
| 方向上下鍵 | axes[5] |
| 方向左右鍵 | axes[4] |
| L2鍵 | buttons[7] |
| L3鍵 | buttons[11] |
| R2鍵 | buttons[6] |
| R3鍵 | buttons[10] |
- axes數組中數據的范圍在[-1,1],將[-32767,32767]進行了歸一化
- buttons數組中的值只有0和1,可以認為是一個布爾量,按一下則變為true
- 方向盤順時針為正,逆時針為負
- 油門、離合、剎車抬起時為-1,踩下時為1
- 方向上鍵為-1,下鍵為1,與正常印象中的相反,這和羅技手柄倒是一致的
- 方向左鍵為-1,右鍵為1,同樣與正常印象中的相反
參考博客
羅技G29方向盤linux下的開發
總結
以上是生活随笔為你收集整理的Ubuntu环境下读取罗技G29方向盘信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝桥杯-K好数
- 下一篇: [Leedcode][第十题][剑指of