从MATLAB发布makers到Rviz中(转)

Publishing markers in RViz from MATLAB

Testing basic shape example

This example demonstrates how to publish different types of markers from MATLAB to RViz. In order to publish ROS messages, we use Robotics System Toolbox which is supported in MATLAB version above R2015a.

从MATLAB发布makers到Rviz中(转)

  • We first run roscore and rviz in a computer running Ubuntu as follows:
    roscore
    rosrun rviz rviz
  • In the computer running ROS, we publish a tf for a fixed frame, named my_frame as follows:
    rosrun tf static_transform_publisher 0.0 0.0 0.0 0.0 0.0 0.0 map my_frame 100
  • In matlab, we run following script:
%=== set the address for the connection
% IP address of the computer running roscore (ros server)
setenv('ROS_MASTER_URI','http://IP_address:11311'); 
rosinit; % connect to the ros server      
%=== specify data to publish
markerPub = rospublisher('/visualization_marker','visualization_msgs/Marker');
marker = rosmessage(markerPub);
   
%- set the frame information and names of marker
marker.Header.FrameId = 'my_frame';
marker.Ns = 'basic_shapes';
marker.Text = 'cube';
   
%- set the time
marker.Header.Stamp = rostime('now','system');
   
%- set the ID of the shape
marker.Id = 0;   
%- set the scale of the shape
marker.Scale.X = 1;
marker.Scale.Y = 1;
marker.Scale.Z = 1;
   
%- set position and orientation
Pos = rosmessage('geometry_msgs/Point');
Pos.X = 0;
Pos.Y = 0;
Pos.Z = 0;                
Ori = rosmessage('geometry_msgs/Quaternion');
Ori.W = 1;
marker.Pose.Position = Pos; 
marker.Pose.Orientation = Ori;  
%- set color
Color = rosmessage('std_msgs/ColorRGBA');
Color.R = 1;
Color.G = 0.5;
Color.B = 0;
Color.A = 1;
marker.Color = Color;  
%- set the type of the shape
type = [1 2 3]; % 1: cube, 2: sphere, 3: cylinder
   
while (1)
  for i=1:3
      %- set the time
      marker.Header.Stamp = rostime('now','system');
      marker.Type = type(i); 
      send(markerPub,marker);
      pause(1);
      disp(i)
  end            
end
  • In rviz, we add a marker and set the Fixed Frame as follows:
    从MATLAB发布makers到Rviz中(转)
  • Then you will be able to see the marker displayed in RViz like below:

从MATLAB发布makers到Rviz中(转)

Hope this example will be helpful for making visualization in ROS easier! If you have any problems or questions, please leave comments!

来源:http://www.whatisrobot.com/?p=28&i=1