ROS2, MATLAB, GAZEBO.

10 views (last 30 days)
Robson Cardoso
Robson Cardoso on 15 Apr 2024
Answered: Sanchari on 11 Jul 2024
I’m using ros2/Matlab and need the tranformations in a chart. How could I do that.
  2 Comments
Robson Cardoso
Robson Cardoso on 15 Apr 2024
on a chart in Matlab
Jaswanth
Jaswanth on 12 Jun 2024
Can you explain the specific workflow/use case you are looking to implement?

Sign in to comment.

Answers (1)

Sanchari
Sanchari on 11 Jul 2024
Hello Robson,
I'm assuming that the need is to visualize transformations in a chart using ROS 2 and MATLAB. Below is a overview of the steps to be taken:
  1. Set Up ROS 2 in MATLAB: Ensure that the necessary ROS 2 setup is present in MATLAB.
  2. Subscribe to TF Messages: Subscribe to the transformation messages (usually "/tf" or "/tf_static" topics) to receive transformation data.
  3. Extract Transformation Data: Extract the transformation data from the received messages.
  4. Plot the Transformations: Use MATLAB plotting functions to visualize the transformations in a chart.
Below is a detailed step-by-step guide and example code to achieve this:
Step 1. Set Up ROS 2 in MATLAB: Make sure that the ROS Toolbox is installed and set up for ROS 2. Please check the setup using below code:
ros2 node list;
Step 2. Create a ROS 2 Node and Subscriber: Create a ROS 2 node and a subscriber for the transformation topic.
% Create a ROS 2 node
node = ros2node("/matlab_node");
% Create a subscriber for the '/tf' topic
tfSub = ros2subscriber(node, '/tf', 'geometry_msgs/TransformStamped', @tfCallback);
% Create a subscriber for the /tf_static topic (if needed)
tfStaticSub = ros2subscriber(node, '/tf_static', 'geometry_msgs/TransformStamped', @tfCallback);
Step 3. Callback Function to Handle Transform Messages: Define a callback function to handle the transformation messages and store the data.
% Initialize a global variable to store transformations
global tfData;
tfData = [];
% Callback function to handle /tf messages
function tfCallback(~, msg)
global tfData;
% Extract the transformation data
tf = msg.transforms;
for i = 1:length(tf)
% Extract translation
trans = tf(i).transform.translation;
x = trans.x;
y = trans.y;
z = trans.z;
% Extract rotation (quaternion)
rot = tf(i).transform.rotation;
qx = rot.x;
qy = rot.y;
qz = rot.z;
qw = rot.w;
% Store the transformation data
tfData = [tfData; x, y, z, qx, qy, qz, qw];
end
end
Step 4. Plot the Transformations: Use MATLAB plotting functions to visualize the transformations.
% Plot the transformations
figure;
hold on;
grid on;
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Transformations');
% Plot in a loop to continuously update the chart
while true
global tfData;
if ~isempty(tfData)
% Extract translation data
x = tfData(:, 1);
y = tfData(:, 2);
z = tfData(:, 3);
% Plot the translation data
scatter3(x, y, z, 'filled');
% Clear the data to avoid re-plotting
tfData = [];
end
drawnow;
pause(0.1); % Adjust the pause duration as needed
end
By following these steps, you can subscribe to transformation messages in ROS 2, extract the transformation data, and visualize it in a chart using MATLAB.
Please refer the following links for more details on:
  1. ros2subscriber (MathWorks Documentation): https://www.mathworks.com/help/ros/ref/ros2subscriber.html?searchHighlight=ros2subscriber&s_tid=srchtitle_support_results_1_ros2subscriber#d126e48764
  2. ros2tf (MathWorks Documentation): https://www.mathworks.com/help/ros/ref/ros2tf.html
  3. Get started with ROS 2 (MathWorks Documentation): https://www.mathworks.com/help/ros/ug/get-started-with-ros-2.html?searchHighlight=ROS%202%20MATLAB&s_tid=srchtitle_support_results_10_ROS%202%20MATLAB
  4. ros2 (MathWorks Documentation): https://www.mathworks.com/help/ros/ref/ros2.html?searchHighlight=ROS%202%20MATLAB&s_tid=srchtitle_support_results_4_ROS%202%20MATLAB
  5. ROS Toolbox (MathWorks Documentation): https://www.mathworks.com/help/ros/index.html?searchHighlight=ROS%202%20MATLAB&s_tid=srchtitle_support_results_1_ROS%202%20MATLAB
  6. Access the tf Transformation Tree in ROS 2 (MathWorks Documentation): https://www.mathworks.com/help/ros/ug/access-the-tf-transformation-in-ros-2.html?searchHighlight=ROS%202%20MATLAB%20transformations&s_tid=srchtitle_support_results_2_ROS%202%20MATLAB%20transformations
  7. SE(2) (MathWorks Documentation): https://www.mathworks.com/help/ros/ref/se2.html?searchHighlight=ROS%202%20MATLAB%20transformations&s_tid=srchtitle_support_results_3_ROS%202%20MATLAB%20transformations
  8. tform (MathWorks Documentation): https://www.mathworks.com/help/ros/ref/se2.tform.html?searchHighlight=ROS%202%20MATLAB%20transformations&s_tid=srchtitle_support_results_10_ROS%202%20MATLAB%20transformations
Hope this helps!

Categories

Find more on Specialized Messages in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!