MATLAB Examples

Transform Laser Scan Data From A ROS Network

Transform laser scan data using a ROS transformation tree. When working with laser scan data, your sensor might not be mounted in the center of the robot. Many algorithms make this assumption, so that you might need to transform your data so it is relative to the robot center. This example uses a ROS transformation tree to receive the transformations between different coordinate frames. To transform the sensor data, you must be connected to a ROS network and have transformations available.

Setup and connect to a ROS network. Specify the IP address of the ROS device.

Initializing global node /matlab_global_node_43056 with NodeURI

Create the ROS transformation tree using rostf. Get the transform between the '/camera_link' and '/base_link' coordinate frames. These coordinate frame names are dependent on your robot configuration.

tftree = rostf;
tf = getTransform(tftree,'/camera_link','/base_link',rostime('now'));

Extract the rotation and translation matrices from the transform.

quat = [tf.Transform.Rotation.W,...
rotm = quat2rotm(quat);
trvec = [tf.Transform.Translation.X,...
         tf.Transform.Translation.Y ...

Create a homogeneous transform by combining the translation and rotation matrices.

tform = trvec2tform(trvec);
tform(1:3,1:3) = rotm(1:3,1:3);

Set up a subscriber to get laser scan data. Get the laser scan data as Cartesian points. Pad the points with zeros for the z-axis and convert them to homogeneous coordinates.

scansub = rossubscriber('/scan');
scan = receive(scansub)
cartScanData = scan.readCartesian;
cartScanData(:,3) = 0;
homScanData = cart2hom(cartScanData);
scan = 

  ROS LaserScan message with properties:

       MessageType: 'sensor_msgs/LaserScan'
            Header: [1×1 Header]
          AngleMin: -0.5216
          AngleMax: 0.5243
    AngleIncrement: 0.0016
     TimeIncrement: 0
          ScanTime: 0.0330
          RangeMin: 0.4500
          RangeMax: 10
            Ranges: [640×1 single]
       Intensities: [0×1 single]

  Use showdetails to show the contents of the message

Apply the homogeneous transform and convert scan data back to Cartesian points.

trPts = tform*homScanData';
cartScanDataTransformed = hom2cart(trPts');

Get the polar angles and ranges from the Cartesian Points.

[angles,ranges] = cart2pol(cartScanDataTransformed(:,1),...

Shutdown ROS network.

Shutting down global node /matlab_global_node_43056 with NodeURI