Main Content

与Turtlebot沟通

此示例介绍了TurtleBot®平台以及Matlab®用户可以与之交互的方式。具体而言,该示例中的代码演示了如何将消息发布到TurtleBot(例如速度)以及如何订阅Turtlebot发布的主题(例如odometry)。

必须为此示例运行TurtleBot以工作。

先决条件:Get Started with Gazebo and a Simulated TurtleBotorGet Started with a Real TurtleBot

Connect to the TurtleBot

Turtlebot必须运行如果您使用的是真正的turtlebot并遵循硬件设置步骤Get Started with a Real TurtleBot,机器人正在运行。如果您正在模拟中使用TurtleBot并遵循设置步骤Get Started with Gazebo and a Simulated TurtleBot那launch one of the Gazebo® worlds from the desktop (凉亭办公室那for instance).

In your MATLAB instance on the host computer, run the following command. ReplaceIP地址with the IP address of the TurtleBot. This line initializes ROS and connects to the TurtleBot.

IP地址=“http://192.168.178.133:11311”;罗斯尼特(ipaddress)
Initializing global node /matlab_global_node_88195 with NodeURI http://192.168.178.1:59934/

If the network you are using to connect to the TurtleBot is not your default network adapter, you can manually specify the IP address of the adapter that is used to connect to the robot. This might happen if you use a Wireless network, but also have an active Ethernet connection. Replaceip_of_turtlebot.with the IP address of the TurtleBot andIP_OF_HOST_COMPUTER使用用于连接机器人的主机适配器的IP地址:

rosinit(“ip_of_turtlebot”“nodehost”“ip_of_host_computer”);

Display all the available ROS topics using:

rostopic列表

If you do not see any topics, then the network has not been set up properly. Refer to the beginning of this document for network setup steps.

移动机器人

You can control the movement of the TurtleBot by publishing a message to the/ cmd_vel.话题。消息必须是类型geometry_msgs/Twist,其中包含指定所需的线性和角速度的数据。Turtlebot的动作可以通过两个不同的值来控制:沿着沿线的线性速度X-axis控制前后运动和周围的角速度Z.-axis控制机器人基座的旋转速度。

设置变量velocityto use for a brief TurtleBot movement.

velocity = 0.1;% meters per second

为此创建一个发布者/ cmd_vel.主题和包含速度值的相应消息。使用具有结构格式消息的发布者以获得更好的性能。

robotcmd = rospublisher("/cmd_vel"“dataformat”“结构”);velmsg = rosmessage(robotcmd);

设置前向速度(沿着X-axis) of the robot based on thevelocity变量并将命令发布到机器人。让它移动一下,然后把它带到停止。

velMsg.Linear.X = velocity; send(robotCmd,velMsg) pause(4) velMsg.Linear.X = 0; send(robotCmd,velMsg)

要查看Velocity主题发布的消息类型,请执行以下操作:

rostopic类型/ cmd_vel.
geometry_msgs/Twist

The topic expects messages of typegeometry_msgs/Twist那which is exactly the type of thevelMsg以上创建。

To view which nodes are publishing and subscribing to a given topic, use the command:rostopic信息TOPICNAME。以下命令列出了速度主题的发布者和订阅者。Matlab被列为出版商之一。

rostopic信息/ cmd_vel.
Type: geometry_msgs/Twist Publishers: * /matlab_global_node_88195 (http://192.168.178.1:59934/) Subscribers: * /gazebo (http://192.168.178.133:39227/)

接收机器人位置和方向

turtlebot使用了/odom主题发布当前位置和方向(集体表示为姿势)。由于Turtlebot没有配备GPS系统,因此姿势将相对于机器人第一次打开时的姿势。

为测量消息创建一个订阅者

OdomSub = Rossubscriber(“/ odom”“dataformat”“结构”);

等待订阅者返回数据,然后提取数据并将其分配给变量Xy那andZ.

odomMsg = receive(odomSub,3); pose = odomMsg.Pose.Pose; x = pose.Position.X; y = pose.Position.Y; z = pose.Position.Z;

笔记:如果你看到错误,那么很可能收到命令超时。确保正在发布OCOMORY,并且您的网络已正确设置。

显示Xy那andZ.values

[x y z]
ANS =.1×3-2.6176 1.0007 -0.0010

turtlebot的方向被储存为四元数方向财产姿势。采用quat2eul(机器人系统工具箱)to convert into the more convenient representation of Euler angles. To display the current orientation,θ.那of the robot in degrees, execute the following lines.

quat = pose.Orientation; angles = quat2eul([quat.W quat.X quat.Y quat.Z]); theta = rad2deg(angles(1))
θ.= 0.1556

Receive Lidar Data

Subscribe to the lidar topic:

lidarsub = Rossubscriber(“/扫描”“dataformat”“结构”);

After subscribing to the lidar topic, wait for the data and then display it with rosPlot.

scanmsg =接收(lidarsub);图ROSPOT(SCANMSG)

要在机器人转到短时间内时,要连续显示更新激光乐队扫描,请使用以下循环:

velMsg.Angular.Z = velocity; send(robotCmd,velMsg) ticwhiletoc <20 scanmsg =接收(lidarsub);ROSPLOT(SCANMSG)结尾

velMsg.Angular.Z = 0; send(robotCmd,velMsg)

Disconnect from the Robot

使用它们清除发布商,订阅者和其他与其他ROS相关对象的工作空间。

clear

采用Rosshutdown.一旦完成了ROS网络。关闭全球节点并断开来自Turtlebot的连接。

Rosshutdown.
Shutting down global node /matlab_global_node_88195 with NodeURI http://192.168.178.1:59934/

下一步

Refer to the next example:探索Turtlebot的基本行为