主要内容

跟踪并遵循一个对象Using a TurtleBot

此示例显示如何使用通过ROS网络连接的Turtlebot®机器人跟踪基于颜色的对象。ROS工具箱支持包用于T金宝appurtlebot的机器人,您可以捕获图像以在环境中查找对象,并发送速度命令以导航到对象。要遵循对象,请使用getColorImagesetVelocityfunctions along with image processing techniques.

连接到机器人

使用特定的IP地址连接到您的Turtlebot机器人。您的模拟或realCurtlebot必须与运行MATLAB®的计算机相同的ROS网络。

ipaddress ='192.168.192.130';% IP address of your robottbot = turtlebot(ipaddress,11311); tbot.Velocity.TopicName ='/cmd_vel';

跟踪对象

To detect the example object, a blue ball, you must specify some of its properties. You use these properties to process the images you capture. This example uses Image Processing Toolbox™ to run color and blob detection algorithms on each image to find the ball.

指定所示的球的属性,替换为为环境调整的值。给出的值对于凉亭模拟进行了最佳选择开始凉亭和模拟的龟毡.

BlueballParams.Bluemax = 120;纯蓝色最大允许偏差BlueballParams..darkMin = 30;%最低可接受的暗度值

Get an image off the TurtleBot.

liquimg = getcolorimage(tbot);

Use these two example helper functions to get and plot the location of the ball in the image. These functions use地区普通from Image Processing Toolbox to get the location and size of the ball. The first image is the output from the TurtleBot with the object location overlaid on it. The second image is the binary image,ballImage,用于基于图像中的图像检测蓝色BlueballParams.. If the ball does not show up as white with limited background in this binary image, try tuning theBlueballParams.values.

[位置,〜,弹性贴图] = examplehelperturtlebotfindblueball(qualemg,blueballparams,false);examplehelperturtlebotplotobject(liquimg,ballimage,位置);

Follow Object Using Velocity Commands

After you tune the tracking of the object in the image, you can set up a basic control algorithm for sending commands to the TurtleBot. This example uses basic logic with singular linear and angular velocity commands to get the robot to follow the ball. For a more advanced control scheme, see跟踪并遵循一个对象.

Get a new image and store the image size. Recalculate the position and size of the object.

liquimg = getcolorimage(tbot);[height,width] = size(latestImg); [position,~] = exampleHelperTurtleBotFindBlueBall(latestImg,blueBallParams,false);

Generate angular and linear velocities based on the position and size of the ball. If no object is found, spin and search for the ball (positive angular velocity only).

  • If the ball is on the far left side of the screen, the robot needs to turn left (positive angular velocity). The same is true for the right side (negative angular velocity).

  • If the ball is too small in the image, the robot must move closer (positive linear velocity). If the ball is too large, the robot must move farther away (negative linear velocity).

最后,发送线性和角速度的使用setVelocity暂停等待命令发送。迭代100个周期以测试跟踪。

球位置和尺寸的%参数TriconalTolerance = 20;sizegoal = 70;sizeTolerance = 5;fori = 1:100% Get latest image, ball postion, and ball size.liquimg = getcolorimage(tbot);[height,width] = size(latestImg); [position,ballSize] = exampleHelperTurtleBotFindBlueBall(latestImg,blueBallParams,false);%初始化速度为零。linearvel = 0;angularVel = 0;%左右控制如果Isempty(位置)Angularvel = 0.5;linearvel = 0;elseif(位置(1)>(宽/ 2) -  horizo​​ntaltolerance)Angularvel = 0.2;elseif(position(1) < (width/2)+horizontalTolerance) angularVel = -0.2;end% Forward and back control如果Isempty(球化)Angularvel = 0.5;linearvel = 0;elseifballSize > sizeGoal + sizeTolerance linearVel = - 0.1;elseifballSize < sizeGoal - sizeTolerance linearVel = 0.1;end%发送速度命令并等待发送命令。SetVelocity(TBOT,Linearvel,Angularvel)暂停(0.2)end

To verify the algorithm is successful, capture an image and use the help functions to track and plot the position once more. The TurtleBot focuses on the ball, assuming it found the ball during its search. Based on the Gazebo simulated world in this example, a final image capture shows the ball right in front of the robot.

liquimg = getcolorimage(tbot);[c,球化,弹性镜] = examplehelperturtlebotfindblueball(quoullimg,blueballparams,false);examplehelperturtlebotplotobject(liquimg,ballimage,c);

改进建议

This example uses simple sensor feedback and control parameters for tracking and following an object. It works well in a simulated environment due to its low level of risk to hardware and real environments. However, it does not factor in the bump or clip sensors, which can help with collisions or terrain changes. Dynamically adjusting the velocity commands and factoring in previous states can improve the movements of the TurtleBot.

为了改进现实世界应用的这个例子,考虑使用更高级的传感和控制方法,用于具有更强大的传感器数据的真实机器人。看跟踪并遵循一个对象对于使用PID控制器和其他机器人传感器的更高级示例。

看Also

||

相关话题