Getting ros2 time to stamp the PoseStamped message

30 ビュー (過去 30 日間)
Samantha Kamel
Samantha Kamel 2022 年 7 月 1 日
回答済み: Josh Chen 2024 年 4 月 4 日
I work with a ros2 system in which my algorithm sends as an output a navigation message using the geometry_msgs/PoseStamped. The problem is that I want to assign the time stamp parameter with the value of ros2 time and the only function available for that is rostime("now") which works only with ros not ros2 that's why I get the error:
The global ROS node is not initialized. Use "rosinit" to start the global node and connect to a ROS network.
So I'm looking for a solution to get the time of ros2 or find a way to make the system time in a format of the time stamp of the output message.
That is the code I'm using to assign the output message:
msg= ros2message("geometry_msgs/PoseStamped");
msg.pose.position.x=pos(1); %pos is a variable assigned previously in the code
msg.pose.position.y=pos(2);
msg.pose.position.z=0;
msg.orientation.x=0;
msg.orientation.y=0;
msg.orientation.z=theta;
msg.orientation.w=1;
[s,ns]= getTime; %getTime Function
GNSSmsg.header.stamp.sec=s;
GNSSmsg.header.stamp.nanosec=ns;
%getTime Function:
function [s,ns] = getTime
t = rostime('now');
s = t.Sec;
ns = t.Nsec;
end

採用された回答

Josh Chen
Josh Chen 2024 年 4 月 4 日
Hi Samantha,
We introduced ros2time since R2022b.
If you are looking for a ROS 2 equivalent for getting timestamp and you are working with MATLAB/Simulink version at or after R2022b, I would recommand to use ros2time. This allows you to not only use system time, but also, time from "/use_sim_time" topic.
Thanks,
Josh

その他の回答 (1 件)

Cam Salzberger
Cam Salzberger 2022 年 8 月 4 日
Hello Samantha,
I don't have MATLAB in front of me to check, but if you want to use system time, I think using datetime and convertTo would be easier than starting a ROS Master:
currTime = datetime("now");
currTimePosix = convertTo(currTime, "posixtime");
sec = floor(currTimePosix);
nsec = round((currTimePosix-sec)*1e9);
msg.sec = sec;
msg.nsec = nsec
msg = struct with fields:
sec: 1.6596e+09 nsec: 834337950
-Cam

カテゴリ

Help Center および File ExchangeROS 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