turtlebot matlab,Turtlebot与Matlab入门教程-创建UI界面控制turtlebot

说明:

介绍如何创建UI界面控制turtlebot

步骤:

在turtlebot端:

[turtlebot] 启动Turtlebot

roslaunch turtlebot_bringup minimal.launch

[turtlebot] 启动雷达

roslaunch rplidar_ros rplidar.launch

在Matlab端:

运行代码turtlebot_UI.m文件

点击按键“Forward”,“back”,“Left”,“Right”,控制turtlebot前进,后退,向左,向右

点击按键“Get Odom”,绘制一个新的里程计(蓝色)

点击按键“Get Scan”,绘制当前最新的扫描数据(黑色)

点击按键“Reset”,重置

点击按键“Clear”,清楚界面的数据

代码如下:

%Set Up the UI and Its Components

function turtlebot_UI

f = figure('Visible','off','Position',[50,50,900,570]);

hforward = uicontrol('Style','pushbutton',...

'String','Forward','Position',[770,440,70,25],...

'Callback',@forwardCB);

hleft = uicontrol('Style','pushbutton',...

'String','Left','Position',[730,400,70,25],...

'Callback',@leftCB);

hright = uicontrol('Style','pushbutton',...

'String','Right','Position',[815,400,70,25],...

'Callback',@rightCB);

hback = uicontrol('Style','pushbutton',...

'String','Back','Position',[770,360,70,25],...

'Callback',@backCB);

hgetOdom = uicontrol('Style','pushbutton',...

'String','Get Odom','Position',[730,320,70,25],...

'Callback',@getOdomCB);

hreset = uicontrol('Style','pushbutton',...

'String','Reset','Position',[815,320,70,25],...

'Callback',@resetCB);

hgetScan = uicontrol('Style','pushbutton',...

'String','Get Scan','Position',[730,280,70,25],...

'Callback',@getScanCB);

hclear = uicontrol('Style','pushbutton',...

'String','Clear','Position',[815,280,70,25],...

'Callback',@clearCB);

ha = axes('Units','pixels','Position',[50,60,600,400]);

% Make UI visible

f.Visible = 'on';

%Connect to Robot

ipaddress = '192.168.0.93'; % IP address of your robot

tbot = turtlebot(ipaddress);

odomList = [];

scanPts = [];

resetOdometry(tbot);

%Specify Callback Functions for UI Buttons

function forwardCB(source,eventdata)

setVelocity(tbot,0.25,0)

end

function leftCB(source,eventdata)

setVelocity(tbot,0,0.5)

end

function rightCB(source,eventdata)

setVelocity(tbot,0,-0.5)

end

function backCB(source,eventdata)

setVelocity(tbot,-0.25,0)

end

function getOdomCB(source,eventdata)

plotOdom

end

function resetCB(source,eventdata)

resetOdometry(tbot);

odomList = [];

plotOdom

end

function getScanCB(source,eventdata)

plotOdom

hold on

plotScan

hold off

end

function clearCB(source,eventdata)

scanPts = [];

plotOdom

end

function plotOdom

% Gets and plots current and saved odometry

odom = getOdometry(tbot);

odomList = [odomList; odom.Position(1) odom.Position(2)];

plot(odomList(:,1),odomList(:,2),'b')

hold on

quiver(odom.Position(1),odom.Position(2),...

cos(odom.Orientation(1)),sin(odom.Orientation(1)),'r',...

'LineWidth',2)

hold off

xlim([-5,5])

ylim([-5,5])

grid on

end

function plotScan

% Plots laser scan data on top of current odometry

[scan,scanMsg] = getLaserScan(tbot);

odom = getOdometry(tbot);

cart = readCartesian(scanMsg);

scanPts = [cart(:,1)+odom.Position(1) cart(:,2)+odom.Position(2)];

yaw = odom.Orientation(1);

R = [cos(yaw) -sin(yaw); sin(yaw) cos(yaw)];

scanPts = (R*scanPts')';

plot(scanPts(:,1),scanPts(:,2),'k')

xlim([-5,5])

ylim([-5,5])

grid on

end

end

效果如下:

44fddd7ed09511ab896c829fdc87bc77.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值