您的位置:首页 > 其它

使用北通手柄控制turtlesim运动

2016-09-06 16:20 471 查看
在自己的工作空间中创建package:

要使用手柄控制

cd ~/wry_ws/src
catkin_create_pkg learning_joy roscpp turtlesim joy
cd ..
catkin_make
以上是在自己的工作空间中创建功能包,并编译工作空间。

因为要使用手柄控制turtlesim运动,因此该功能包依赖有:joy功能包和turtlesim功能包。

1、创建turtle_teleop_joy

在learning_joy/src下,运用自己熟悉的编译器,创建并编辑文件:turtle_teleop_joy.cpp,编辑内容为:

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

class TeleopTurtle
{
public:
TeleopTurtle();

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

ros::NodeHandle nh_;

int linear_, angular_;
double l_scale_, a_scale_;
ros::Publisher vel_pub_;
ros::Subscriber joy_sub_;

};

TeleopTurtle::TeleopTurtle():linear_(1), angular_(0)
{
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_);

vel_pub_ = nh_.advertise<<span style="font-family:Arial, Helvetica, sans-serif;">geometry_msgs:</span>:Twist>("turtle1/cmd_vel", 1);

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;
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)
{
ros::init(argc, argv, "teleop_turtle");
TeleopTurtle teleop_turtle;

ros::spin();
}
注意:在hydro版本以后,发送turtlesim运动信息的topic:turtle/cmd_vel的类型为:geometry_msgs/Twist。

          在hydro版本之前,均使用turtlesim/Velocity的消息类型。

在上述程序中,需要控制turtlesim运动,需要订阅手柄信息(通过topic:joy),并将从joy获得的手柄信息,通过joycallback函数转换为twist类型的数据,发布出去(发布到topic:turtle1/cmd_vel)。

2、设置手柄

将手柄的USB接口接到笔记本上,运行指令:

ls /dev/input/


显示如下内容:

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


可以发现,手柄的接入端口为js0;

测试手柄:

sudo jstest /dev/input/js0


运行该命令后,如果显示如下,说明joystick的驱动是合适的:

Driver version is 2.1.0.
Joystick (Microsoft X-BOX 360 pad) has 8 axes (X, Y, Z, Rx, Ry, Rz, Hat0X, Hat0Y)
and 11 buttons (BtnX, BtnY, BtnTR, BtnTL2, BtnTR2, BtnSelect,<span style="font-family: Arial, Helvetica, sans-serif;">BtnThumbL, BtnThumbR, ?, ?, ?</span><span style="font-family: Arial, Helvetica, sans-serif;"> ).</span>
Testing ... (interrupt to exit)
运行roscore,设置系统对于手柄的默认端口:

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


3、创建launch文件:

<launch>

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

 <!-- joy node --><div>  <node respawn="true" pkg="joy" type="joy_node" name="joy_node" ></div>    <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"/><div>
</div><div>  <node pkg="learning_joy" type="turtle_teleop_joy" name="teleop_turtlesim" /></div><div></launch></div>
4、运行launch文件
roslaunch learning_joy turtle_joy.launch
即可用手柄控制turtlesim运行

5、运行rosrun rqt_graph rqt_graph 即可看到节点关系图。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ROS