Main Content

探索Turtlebot的基本行为

此示例可帮助您使用Turtlebot®探索基本自动系统。所描述的行为将机器人向前推动并在存在障碍时改变其方向。您将订阅激光扫描主题并发布速度主题以控制TurtleBot。

先决条件:与Turtlebot沟通

Connect to the TurtleBot

Make sure you have a TurtleBot running either in simulation through Gazebo® or on real hardware. Refer to开始凉亭和模拟的turtlebotorGet Started with a Real TurtleBotfor the startup procedure. This example uses the Gazebo-simulated Turtlebot.

In the downloaded virtual machine, click theGazebo House捷径启动世界。

Initialize ROS. Connect to the TurtleBot by replacingIP地址使用Turtlebot的IP地址。

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

为机器人的速度创建发布者并为该主题创建一条消息。使用结构格式消息进行通信以提高性能。

robot = rospublisher("/cmd_vel"“dataformat”“结构”);velmsg = rosmessage(机器人);

接收扫描数据

如果您正在使用Real Turtlebot Hardware,请确保您启动激光器和相机。启动LIDAR和相机的命令是:

roslaunchturtlebot3_bringup.turtlebot3_core.launch.roslaunchturtlebot3_bringup.turtlebot3_lidar.launch.roslaunchturtlebot3_bringup.turtlebot3_rpicamera.launch.

You must execute the command in a terminal on the TurtleBot. The TurtleBot uses the LDS-01 Lidar to construct a laser scan that is published on the/scan话题。对于这个例子的其余部分,这个术语激光扫描是指在本主题上发布的数据。

订阅主题/scan

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

等待一个激光扫描消息到达,然后显示它。

扫描=receive(laser,3)
扫描=结构与字段:MessageType:'sensor_msgs / laserscan'标题:[1×1 struct] angleemin:0 anglemax:6.2832角度算法:0.0175临时扫描:0扫描:0 rangemin:0.1200 rangemax:3.5000范围:[360×1单身]强度:[360×1单身的]
图ROSPOT(扫描);

If you see an error, it is possible that the laser scan topic is not receiving any data. If you are running in simulation, try restarting Gazebo. If you are using hardware, make sure that you started the lidar and camera properly.

运行下面的代码行,情节激光扫描feed for ten seconds. Move an object in front of the TurtleBot and bring it close enough until it no longer shows up in the plot window. The laser scan has a limited range because of hardware limitations. The LDS-01 lidar has a minimum sensing range of 0.12 meters and a maximum range of 3.5 meters. Any objects outside these limits will not be detected by the sensor.

Tic;whiletoc < 10 scan = receive(laser,3); rosPlot(scan);结尾

Simple Obstacle Avoidance

Based on the distance readings from the laser scan, you can implement a simple obstacle avoidance algorithm. You can use a simplewhileloop to implement this behavior.

设置将在处理循环中使用的一些参数。您可以为不同的行为修改这些值。

spinVelocity = 0.6;%角速度(Rad / s)forwardvelocity = 0.1;%线性速度(m / s)反向velocity = -0.02;%线性速度(反向)(m / s)横向划线= 0.6;转动的%距离阈值(m)

Run a loop to move the robot forward and compute the closest obstacles to the robot. When an obstacle is within the limits of the横向划线,机器人转身。在运行时间20秒后,该循环停止。Ctrl + C(或Mac上的控制+ C)也停止此循环。

Tic;whileTOC <20% Collect information from laser scan扫描=接收(激光);ROSPOT(扫描);数据= rosreadcartesian(扫描);x =数据(:,1);y =数据(:,2);最接近障碍物的%计算距离dist = sqrt(x。^ 2 + y。^ 2);mindist = min(dist);%命令机器人动作如果mindist % If close to obstacle, back up slightly and spinvelmsg.angular.z =扫描仪;velmsg.linear.x =反向velocity;别的%继续前进路径velmsg.linear.x =转发velocity;velmsg.angular.z = 0;结尾send(robot,velmsg);结尾

Disconnect from the Robot

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

clear

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

Rosshutdown.
Shutting down global node /matlab_global_node_05319 with NodeURI http://192.168.178.1:51558/

更多信息

The laser scan has a minimum range at which it no longer sees objects in its way. That minimum is somewhere around 0.12 meters from the lidar.

The laser scan cannot detect glass walls. Following is an image from the camera:

Here is the corresponding laser scan:

The trash can is visible, but you cannot see the glass wall. When you use the TurtleBot in areas with windows or walls that the TurtleBot might not be able to detect, be aware of the limitations of the laser scan.

下一步

Refer to the next example:用遥操作控制Turtlebot