# ROS2, MATLAB, GAZEBO.

조회 수: 8 (최근 30일)
Robson Cardoso 2024년 4월 15일
답변: Sanchari 2024년 7월 11일 7:07
I’m using ros2/Matlab and need the tranformations in a chart. How could I do that.
##### 댓글 수: 2없음 표시없음 숨기기
Robson Cardoso 2024년 4월 15일
on a chart in Matlab
Jaswanth 2024년 6월 12일
Can you explain the specific workflow/use case you are looking to implement?

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Sanchari 2024년 7월 11일 7:07
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.
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!

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Network Connection and Exploration에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by