matlab控制turtlebot,Turtlebot与Matlab入门教程-控制机器人

说明:

介绍如何控制机器人

步骤:

在turtlebot端

启动turtlebot

$ roslaunch turtlebot_bringup minimal.launch

启动雷达

$ roslaunch rplidar_ros rplidar.launch

在Matlab端

运行myTeleop.m文件

输入ROS_MASTERR_URI,http://192.168.0.93:11311

输入话题,/mobile_base/commands/velocity

按键前,后,左,右,控制机器人运动

代码如下:

function varargout = myTeleop(varargin)

% MYTELEOP MATLAB code for myTeleop.fig

% MYTELEOP, by itself, creates a new MYTELEOP or raises the existing

% singleton*.

%

% H = MYTELEOP returns the handle to a new MYTELEOP or the handle to

% the existing singleton*.

%

% MYTELEOP('CALLBACK',hObject,eventData,handles,...) calls the local

% function named CALLBACK in MYTELEOP.M with the given input arguments.

%

% MYTELEOP('Property','Value',...) creates a new MYTELEOP or raises the

% existing singleton*. Starting from the left, property value pairs are

% applied to the GUI before myTeleop_OpeningFcn gets called. An

% unrecognized property name or invalid value makes property application

% stop. All inputs are passed to myTeleop_OpeningFcn via varargin.

%

% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one

% instance to run (singleton)".

%

% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help myTeleop

% Last Modified by GUIDE v2.5 20-Jun-2017 15:24:11

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name', mfilename, ...

'gui_Singleton', gui_Singleton, ...

'gui_OpeningFcn', @myTeleop_OpeningFcn, ...

'gui_OutputFcn', @myTeleop_OutputFcn, ...

'gui_LayoutFcn', [] , ...

'gui_Callback', []);

if nargin && ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1});

end

if nargout

[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

else

gui_mainfcn(gui_State, varargin{:});

end

% End initialization code - DO NOT EDIT

% --- Executes just before myTeleop is made visible.

function myTeleop_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% varargin command line arguments to myTeleop (see VARARGIN)

% Choose default command line output for myTeleop

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

% UIWAIT makes myTeleop wait for user response (see UIRESUME)

% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.

function varargout = myTeleop_OutputFcn(hObject, eventdata, handles)

% varargout cell array for returning output args (see VARARGOUT);

% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure

varargout{1} = handles.output;

% 声明一些全局变量

% ROS Master URI和Topic name

global rosMasterUri

global teleopTopicName

rosMasterUri = 'http://192.168.1.202:11311';

teleopTopicName = '/cmd_vel';

% 机器人的运行速度

global leftVelocity

global rightVelocity

global forwardVelocity

global backwardVelocity

leftVelocity = 1; % 角速度 (rad/s)

rightVelocity = -1; % 角速度 (rad/s)

forwardVelocity = 0.2; % 线速度 (m/s)

backwardVelocity = -0.2; % 线速度 (m/s)

% 设置ROS Master URI

function URIEdit_Callback(hObject, eventdata, handles)

% hObject handle to URIEdit (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of URIEdit as text

% str2double(get(hObject,'String')) returns contents of URIEdit as a double

global rosMasterUri

rosMasterUri = get(hObject,'String')

% --- Executes during object creation, after setting all properties.

function URIEdit_CreateFcn(hObject, eventdata, handles)

% hObject handle to URIEdit (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

% See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

set(hObject,'BackgroundColor','white');

end

% 设置Topic name

function TopicEdit_Callback(hObject, eventdata, handles)

% hObject handle to TopicEdit (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of TopicEdit as text

% str2double(get(hObject,'String')) returns contents of TopicEdit as a double

global teleopTopicName

teleopTopicName = get(hObject,'String')

% --- Executes during object creation, after setting all properties.

function TopicEdit_CreateFcn(hObject, eventdata, handles)

% hObject handle to TopicEdit (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.

% See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

set(hObject,'BackgroundColor','white');

end

% 建立连接并初始化ROS publisher

% --- Executes on button press in ConnectButton.

function ConnectButton_Callback(hObject, eventdata, handles)

% hObject handle to ConnectButton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global rosMasterUri

global teleopTopicName

global robot

global velmsg

setenv('ROS_MASTER_URI',rosMasterUri)

rosinit

robot = rospublisher(teleopTopicName,'geometry_msgs/Twist');

velmsg = rosmessage(robot);

% 断开连接,关闭ROS

% --- Executes on button press in DisconnectButton.

function DisconnectButton_Callback(hObject, eventdata, handles)

% hObject handle to DisconnectButton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

rosshutdown

% 向前

% --- Executes on button press in ForwardButton.

function ForwardButton_Callback(hObject, eventdata, handles)

% hObject handle to ForwardButton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global velmsg

global robot

global teleopTopicName

global forwardVelocity

velmsg.Angular.Z = 0;

velmsg.Linear.X = forwardVelocity;

send(robot,velmsg);

latchpub = rospublisher(teleopTopicName, 'IsLatching', true);

%向左

% --- Executes on button press in LeftButton.

function LeftButton_Callback(hObject, eventdata, handles)

% hObject handle to LeftButton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global velmsg

global robot

global teleopTopicName

global leftVelocity

velmsg.Angular.Z = leftVelocity;

velmsg.Linear.X = 0;

send(robot,velmsg);

latchpub = rospublisher(teleopTopicName, 'IsLatching', true);

% 向右

% --- Executes on button press in RightButton.

function RightButton_Callback(hObject, eventdata, handles)

% hObject handle to RightButton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global velmsg

global robot

global teleopTopicName

global rightVelocity

velmsg.Angular.Z = rightVelocity;

velmsg.Linear.X = 0;

send(robot,velmsg);

latchpub = rospublisher(teleopTopicName, 'IsLatching', true);

% 向后

% --- Executes on button press in BackwardButton.

function BackwardButton_Callback(hObject, eventdata, handles)

% hObject handle to BackwardButton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global velmsg

global robot

global teleopTopicName

global backwardVelocity

velmsg.Angular.Z = 0;

velmsg.Linear.X = backwardVelocity;

send(robot,velmsg);

latchpub = rospublisher(teleopTopicName, 'IsLatching', true);

clear;

rosshutdown;

效果如下:

29200be9bc5e2129ccb5613fd3d00f74.png

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值