MATLAB与ROS开始(1)

1、通过MATLAB控制ROS中的简单机器人运动。

    参考:https://blog.csdn.net/mountzf/article/details/51712459

具体操作:1)打开虚拟机---打开一个终端---输入:roscore;运行turtle的简单例子:rosrun turtlesim turtlesim_node

                      

                  2)MATLAB端,为避免有rosmaster运行,先输入:rosshutdown,在输入:rosinit('192.168.244.129')  %虚拟机IP,在虚拟机终端输入:ifconfig可查。

                    

                3)MATLAB中查看rostopic list(输入rostopic list即可),查看topic数据类型等信息如:rostopic info /turtle1/cmd_vel

                4)MATLAB代码控制虚拟机机器人运动:(直接在MATLAB命令窗口复制粘贴运行)

cmdpub = rospublisher('/turtle1/cmd_vel',rostype.geometry_msgs_Twist)
 
pause(3)% Wait to ensure publisher is setup
 
cmdmsg = rosmessage(cmdpub);
 
cmdmsg.Linear.X = 10;
 
cmdmsg.Angular.Z = 15;
 
send(cmdpub,cmdmsg)
 
chatterpub = rospublisher('/chatter',rostype.std_msgs_String)
 
pause(3)% Wait to ensure publisher is setup
 
chattermsg = rosmessage(chatterpub);
 
chattermsg.Data = 'hello world'
 
send(chatterpub,chattermsg)
 
pause(5)

             5)改变了几次cmdmsg.Linear.X = 10; 和cmdmsg.Angular.Z = 15;的数值结果如下

                                         

2、为了直接使用mathwork官网介绍的案例,尽量保证与其一直性,在官网下载了对应的文件。

    参考:https://www.mathworks.com/help/robotics/examples/get-started-with-gazebo-and-a-simulated-turtlebot.html

  主要的安装步骤:Windows版本,因为官网推荐的是用VMware®Player,但我已经安装了VMware®workstation,所以可以不要这一步。俩者的区别就是兄弟关系,VMware®Player占用空间小,不用重启系统等。所以在装了VMware®workstation直接在第二步,点击archive,下载ROS Indigo Gazebo v3并解压。打开VMware®workstation----选择打开虚拟机---到解压ROS Indigo Gazebo v3的目录下----选择ros_indigo_gazebo_v3.vmx----确定---如果打开一个窗口询问您是否复制或移动了虚拟机,请按复制它即可。

以下步骤为在VMware®Player中安装的步骤。

参考:https://www.mathworks.com/supportfiles/robotics/ros/virtual_machines/v3/installation_instructions.htm

                                     1)下载并安装VMware®Player软件(许可证---可选)
                                     2) 下载包含虚拟机的存档:Download the archive containing the virtual machine

                                     3) 将存档解压缩到硬盘驱动器上的某个位置
                                     4)启动VMware Player 
                                     5) 在VMware Player中,按“打开虚拟机”
                                     6)浏览到Ubuntu图像的位置,选择ROS Indigo Gazebo v3并按OK
                                     7)现在,虚拟机已添加到您的库中
                                     8) 在VMware Player中,启动虚拟机
                                     9)如果打开一个窗口询问您是否复制或移动了虚拟机,请按复制它

3、使用:

1)双击打开gazebo turtlebot world。

       

2)下载案例:

https://www.mathworks.com/matlabcentral/fileexchange/64534-getting-started-with-robot-operating-system-ros?s_eid=PSM_15028

简单使用:打开gazeboExampleMATLAB.m,修改ip为虚拟机的IP:192.168.244.130

代码:运行

%% Object Detection Example: Gazebo
% Copyright 2017 The MathWorks, Inc.

%% SETUP
% Connect to ROS master
rosshutdown;
gazeboIp = '192.168.244.130';
rosinit(gazeboIp);
% Create ROS subscribers and publishers
imgSub = rossubscriber('/camera/rgb/image_raw');
receive(imgSub,10); % Wait to receive first message
[velPub,velMsg] = rospublisher('/mobile_base/commands/velocity');
% Create video player for visualization
vidPlayer = vision.DeployableVideoPlayer;
% Load control parameters
params = controlParamsGazebo;

%% LOOP
while(1) 
    %% SENSE
    % Grab images
    img = readImage(imgSub.LatestMessage);
    
    %% PROCESS
    % Object detection algorithm
    resizeScale = 0.5;
    [centerX,centerY,circleSize] = detectCircle(img,resizeScale);
    % Object tracking algorithm
    [v,w] = trackCircle(centerX,circleSize,size(img,2),params);
    
    %% CONTROL
    % Package ROS message and send to the robot
    velMsg.Linear.X = v;
    velMsg.Angular.Z = w;
    send(velPub,velMsg);
    
    %% VISUALIZE
    % Annotate image and update the video player
    img = insertShape(img,'Circle',[centerX centerY circleSize/2],'LineWidth',2);
    step(vidPlayer,img);
        
end

结果:

                                       

参考:https://blogs.mathworks.com/racing-lounge/2017/11/08/matlab-simulink-ros/

         https://www.mathworks.com/help/robotics/examples/test-autonomy-in-simulation.html?s_tid=blogs_rc_6

         https://www.mathworks.com/help/robotics/examples/communicate-with-the-turtlebot.html

         https://www.mathworks.com/help/robotics/examples/get-started-with-gazebo-and-a-simulated-turtlebot.html

         https://www.mathworks.com/help/robotics/examples.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JianRobSim

嘤嘤其名,求其友声!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值