Main Content

探索海龟机器人的基本行为

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

先决条件:与海龟机器人交流

Connect to the TurtleBot

Make sure you have a TurtleBot running either in simulation through Gazebo® or on real hardware. Refer to开始使用凉亭和模拟海龟机器人orGet 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地址带有海龟机器人的IP地址。

IP地址=“ http://192.168.178.133:11311”;Rosinit(ipaddress)
初始化全局节点/matlab_global_node_05319带有nodeuri http://192.168.178.1:51558/

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

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

接收扫描数据

如果您正在使用真正的海龟硬件,请确保启动LiDar和相机。启动LiDAR和相机的命令是:

罗斯拉切turtlebot3_bringupturtlebot3_core.launch罗斯拉切turtlebot3_bringupturtlebot3_lidar.launch罗斯拉切turtlebot3_bringupturtlebot3_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”,,,,“结构”);

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

扫描=接收(激光,3)
扫描=带有字段的结构:MSSAGETYPE:“ Sensor_msgs/Laserscan”标题:[1×1结构] genglemin:0 anglemax:6.2832 AngleIncrement:0.0175时间缩短:0 Scantime:0 Scantime:0 rangemin:0.1200 Rangemax:0.1200 Rangemax:360×1个单次单次rangeS rangeS:[360×1个单次] Intensities:[360×1 [360×1 [[360×1 [[[360×1] [[[360×1]单身的]
图Rosplot(扫描);

如果你看到一个错误,可能是激光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.

运行以下代码行,这些代码绘制活激光扫描馈电十秒钟。将物体移动到海龟机器人面前,并将其足够近,直到不再出现在地块窗口中。由于硬件限制,激光扫描范围有限。LDS-01激光雷达的最小感测范围为0.12米,最大范围为3.5米。传感器将无法检测到这些限制之外的任何对象。

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

Simple Obstacle Avoidance

根据激光扫描的距离读数,您可以实现一种简单的避免算法。您可以使用简单whileloop to implement this behavior.

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

spinVelocity = 0.6;%角速度(rad/s)远前= 0.1;线性速度%(M/s)落后= -0.02;线性速度%(反向)(m/s)Distancethreshold = 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 theDistancethreshold,机器人转身。该循环在运行时间20秒后停止。CTRL+C(或Mac上的对照+C)也停止了此循环。

tic;whileTOC <20% Collect information from laser scan扫描=接收(激光);rosplot(扫描);数据= RosreadCartesian(扫描);x = data(:,1);y = data(:,2);%计算最接近障碍的距离dist = sqrt(x。^2 + y。^2);Mindist = min(Dist);%命令机器人动作如果思想%如果靠近障碍,请稍微备份并旋转velmsg.angular.z =闪闪发光;velmsg.linear.x =落后;别的%继续前进道路velmsg.linear.x = forwardvelocity;velmsg.angular.z = 0;结尾send(robot,velmsg);结尾

Disconnect from the Robot

完成后,请清除发布者,订阅者和其他与ROS相关的对象的工作空间。

clear

利用Rosshutdown一旦完成了ROS网络。关闭全球节点并与海龟机器人断开连接。

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:用遥控器控制海龟机器人