如何在matlab中绘制障碍物,Turtlebot与Matlab入门教程-避开障碍物

说明:

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

步骤:

在turtlebot端:

[turtlebot] 启动Turtlebot

roslaunch turtlebot_bringup minimal.launch

[turtlebot] 启动雷达

roslaunch rplidar_ros rplidar.launch

在Matlab端:

运行avoiding_obstacles.m文件

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

代码如下:

rosshutdown

ipaddress = '192.168.0.14';

rosinit(ipaddress);

robot = rospublisher('/mobile_base/commands/velocity');

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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值