< >
Home » Turtlebot3-Matlab教程 » Turtlebot3与Matlab入门教程-避开障碍物

Turtlebot3与Matlab入门教程-避开障碍物

说明:

  • 如何使得Turtlebot3向前移动并且当存在障碍物时改变其方向

步骤:

在turtlebot3端:

  • [turtlebot3] 启动Turtlebot3
roslaunch turtlebot3_bringup turtlebot3_robot.launch

在Matlab端:

  • 运行avoiding_obstacles.m文件

  • 运行循环以向前移动机器人并计算与机器人最近的障碍物。 当障碍物在distanceThreshold的界限内时,机器人转动。 该循环在运行时间20秒后停止。

  • 代码如下:

rosshutdown
ipaddress = '192.168.0.93';
rosinit(ipaddress);
robot = rospublisher('/cmd_vel');
velmsg = rosmessage(robot);
laser = rossubscriber('/scan');
scan = receive(laser, 3);
figure;
plot(scan);

spinVelocity = 0.6;
forwardVelocity = 0.1;
backwardVelocity = -0.02
distanceThreshold = 0.6;

tic;
while toc < 20
    % Collect information from laser scan
    scan = receive(laser);
    plot(scan);
    data = readCartesian(scan);
    x = data(:,1);
    y = data(:,2);
    % Compute distance of the closest
    dist = sqrt(x.^2 + y.^2);
    minDist = min(dist);
    % Command robot action
    if minDist < distanceThreshold
        % If close to obstacle, back up slightly and spin
        velmsg.Angular.Z = spinVelocity;
        velmsg.Linear.X = backwardVelocity;
    else
        % Continue on forward path
        velmsg.Linear.X = forwardVelocity;
        velmsg.Angular.Z = 0;
    end
    send(robot,velmsg);
end

clear
rosshutdown

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: none