XBOX连ROS车

https://blog.csdn.net/weixin_34409357/article/details/86127202

 ROS中的joy包提供了游戏手柄的驱动,并且包含joy_node节点,这一节点可以发布包含手柄按钮和轴信息的Joy消息。在终端中输入下面命令,安装joy包:

$ sudo apt-get install ros-kinetic-joy

Configuring the Joystick

  安装好之后,将用USB线将手柄连上电脑,然后输入下面指令看看Linux能否找到设备:

$ ls /dev/input/

  通常会输出下面类似的信息:

by-id    event0  event2  event4  event6  event8  mouse0  mouse2  uinput
by-path  event1  event3  event5  event7  js0     mice    mouse1

  游戏手柄的名字一般为jsX,这里我们的手柄为js0,可以使用jstest命令来测试手柄:

$ sudo jstest /dev/input/js0

  按下手柄上不同的按钮或者移动摇杆,可以看到相应数据发生变化:

  接下来要让ROS节点joy_node能获取手柄信息,先列出手柄的权限( ls -l: 以长格式的形式查看当前目录下所有可见文件的详细属性):

$ ls -l /dev/input/js0

  将会看到下面类似的输出:

crw-rw-XX- 1 root dialout 188, 0 2009-08-14 12:04 /dev/input/jsX

  c表示该文件是一个字符设备文件(character),一般置于/dev目录下,一次传输一个字节的设备被称为字符设备,如键盘、字符终端等,传输数据的最小单位为一个字节。r代表具有可读权限;w代表具有可写权限;x代表具有可执行权限,- 代表无权限。其中前三个表示文件属主的权限,中间三个表示组用户权限,最后三个表示其他用户权限。

   如果XX为rw,表示js设备配置正确;如果XX为--表示设备没有配置正确,需要添加权限:

$ sudo chmod a+rw /dev/input/js0

Starting the Joy Node

  为了能发布joy消息我们需要运行joy_node节点,并且指定设备名,默认为js0(tell the joy node which joystick device to use the default is js0):

$ roscore
$ rosparam set joy_node/dev "/dev/input/js0"

  然后就可以运行joy_node节点:

$ rosrun joy joy_node

  如果运行成功,端中会显示下面的信息:

[ INFO] [1513180661.775001483]: Opened joystick: /dev/input/js0. deadzone_: 0.050000. 

  打开一个新终端,输入下面命令查看具体的joy消息:

$ rostopic echo joy

  sensor_msgs/Joy消息格式如下: 

# Reports the state of a joysticks axes and buttons.
Header header           # timestamp in the header is the time the data is received from the joystick
float32[] axes          # the axes measurements from a joystick
int32[] buttons         # the buttons measurements from a joystick 

 蓝牙无线连接

  打开Ubuntu16.04的蓝牙,查找到Xbox Wireless Controller后进行连接:

  不过在连接过程中遇到了一些问题,蓝牙一连上就掉了。下面这个方法可以解决这一问题:

  1. install 

sudo apt install sysfsutils

  2. 以管理员权限用gedit打开sysfs.conf文件

sudo gedit /etc/sysfs.conf

  3. 在文件中加入下面一行:

module/bluetooth/parameters/disable_ertm = 1

  4. 保存文件然后重启电脑。开机后再次连接:

  待手柄上的指示灯常亮,用ls命令查看设备,可以看出js0已经安装好:

测试与校准

  JStest is a simple and useful tool that you can use to calibrate your controllers’ axis.

sudo apt-get install jstest-gtk

  输入下面命令打开jstest的GUI界面,选择已经连接上的Xbox One手柄

$ jstest-gtk

  点击Properties属性按钮,可以进行手柄测试:

Writing a Teleoperation Node for a Linux-Supported Joystick

  在工作空间中创建一个测试package:

$ cd ~/catkin_ws/src
$ catkin_create_pkg learning_joy roscpp turtlesim joy
$ cd ~/catkin_ws/
$ catkin_make

  然后在package的src文件夹中创建turtle_teleop_joy.cpp源文件

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <sensor_msgs/Joy.h>

// create the TeleopTurtle class and define the joyCallback function that will take a joy msg
class TeleopTurtle
{
public:
  TeleopTurtle();

private:
  void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);

  ros::NodeHandle nh_;

  int linear_, angular_;   // used to define which axes of the joystick will control our turtle
  double l_scale_, a_scale_;
  ros::Publisher vel_pub_;
  ros::Subscriber joy_sub_;

};


TeleopTurtle::TeleopTurtle(): linear_(1), angular_(2)
{
  //  initialize some parameters
  nh_.param("axis_linear", linear_, linear_);  
  nh_.param("axis_angular", angular_, angular_);
  nh_.param("scale_angular", a_scale_, a_scale_);
  nh_.param("scale_linear", l_scale_, l_scale_);

  // create a publisher that will advertise on the command_velocity topic of the turtle
  vel_pub_ = nh_.advertise<geometry_msgs::Twist>("turtle1/cmd_vel", 1);

  // subscribe to the joystick topic for the input to drive the turtle
  joy_sub_ = nh_.subscribe<sensor_msgs::Joy>("joy", 10, &TeleopTurtle::joyCallback, this);
}

void TeleopTurtle::joyCallback(const sensor_msgs::Joy::ConstPtr& joy)
{
  geometry_msgs::Twist twist;

// take the data from the joystick and manipulate it by scaling it and using independent axes to control the linear and angular velocities of the turtle
  twist.angular.z = a_scale_*joy->axes[angular_];
  twist.linear.x = l_scale_*joy->axes[linear_];

  vel_pub_.publish(twist); 
}


int main(int argc, char** argv)
{
  // initialize our ROS node, create a teleop_turtle, and spin our node until Ctrl-C is pressed
  ros::init(argc, argv, "teleop_turtle");
  TeleopTurtle teleop_turtle;

  ros::spin();
}

  在CMakeLists中加入下面这两行:

add_executable(turtle_teleop_joy src/turtle_teleop_joy.cpp)
target_link_libraries(turtle_teleop_joy ${catkin_LIBRARIES})

  按照前面的操作连接并配置好手柄以后,在learning_joy目录下新建一个launch文件夹并在其中创建turtle_joy.launch文件(注意joystick设备名称要写对):

<launch>
 <!-- Turtlesim Node-->
  <node pkg="turtlesim" type="turtlesim_node" name="sim"/>

 <!-- joy node -->
  <node pkg="joy" type="joy_node" name="turtle_joy" respawn="true">
    <param name="dev" type="string" value="/dev/input/js0" />
    <param name="deadzone" value="0.12" />
  </node>

 <!-- Axes -->
  <param name="axis_linear" value="1" type="int"/>
  <param name="axis_angular" value="0" type="int"/>
  <param name="scale_linear" value="2" type="double"/>
  <param name="scale_angular" value="2" type="double"/>
  <node pkg="learning_joy" type="turtle_teleop_joy" name="teleop"/>
</launch>

   编译好之后执行下面的命令,可以开启3个节点(turtlesim_node海龟仿真节点、joy_node发布手柄消息节点、turtle_teleop_joy订阅手柄消息并发布turtle1/cmd_vel消息节点),实现用手柄控制海龟运动:

roslaunch learning_joy turtle_joy.launch 

   用手柄上的左摇杆(下图中的1)来控制海龟:Up/Down Axis控制前进和后退;Left/Right Axis控制左转和右转

 

 

 

 

参考:

joy

teleop_twist_joy

WIN10怎样校准和设置游戏手柄

如何将 Xbox One 无线控制器连接到 Windows PC

Get to know your Xbox One Wireless Controller

How to Set Up an Xbox One Controller in Ubuntu

How do I get an Xbox One controller to work with 16.04

Configuring and Using a Linux-Supported Joystick with ROS

Writing a Teleoperation Node for a Linux-Supported Joystick

ROS(机器人操作系统)是一款成熟的机器人操作系统,具有完备的生态体系,未来的发展意义可以成为机器人届中的”Linux、Android“。机器人的开发学习要综合软硬件的协同开发,硬件开发有些部分倾向于底层的设计和使用。软件开发可以依托C++、PYTHON等高级语言进行ROS接口应用开发,或者兼容ROS系统。本系列的ROS开发课程包含下位机开发、上位机开发、基于MBD(基于模型设计的开发)等。下位机作为机器人设计的基础部分是学习机器人操作系统必经之路,下位机我们通常会选择Arduino(适合学习不适合工程,代码执行效率差)、STM32系列产品(工程应用广泛,适合学习和工程开发,代码针对性强),当然还有NXP系列、51系列、TI DSP等,后期可以根据产品的性能和成本要求去考虑;上位机部分,主要是基于工控机、树莓派Raspberry、英伟达Jetson等可以运行操作系统的嵌入式设备再基于ROS操作系统进行实训学习。网络上对于ROS类的教学比较多,但是系统化从底层向高阶层层递升的教学方法偏少,知识碎片化严重,对机器人开发工程师深远的发展有负面影响,基于此本人通过多年自身的学习和工程实践,将机器人开发课程系统化、具象化、模块化地引导式学习,每节课程都有相应的课件和代码引导。对致力于机器人事业的学生有推动作用,且增强信心,系统化自己的机器人知识。为自己的职业规划和事业发展奠定坚实基础。最后,你们的支持,就是老师不断创作的动力!老师会不断更新机器人类相关知识,希望”与子同裳“。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值