How to create ROS message header time stamp?
16 次查看(过去 30 天)
显示 更早的评论
Trying to write a Matlab Function to retrieve ROS time and then write this time in Sec and Nsec in a ROS message. Matlab function intended to run as part of a larger Simulink model.
Tried this function:
function msg = assignString(blankMessage)
coder.extrinsic ('rostime');
coder.extrinsic ('now');
t=rostime('now'); %get rostime
blankMessage.Header.Stamp.Sec = t.Sec; %populate blank header
blankMessage.Header.Stamp.Nsec = t.Nsec; %populate blank header
msg = blankMessage;
But get error: Attempt to extract field 'Sec' from 'mxArray'.
Function 'MATLAB Function' (#260.161.162), line 7, column 33: "t" Launch diagnostic report. Component:MATLAB Function | Category:Coder error Attempt to extract field 'Nsec' from 'mxArray'.
Function 'MATLAB Function' (#260.224.225), line 8, column 34: "t" Launch diagnostic report. Component:MATLAB Function | Category:Coder error
Any help would be great! Apologies, I am fairly new to Matlab and Simulink...
0 个评论
采纳的回答
Sebastian Castro
2017-10-9
编辑:Sebastian Castro
2017-10-9
I got this working with 2 updates.
1. The error message you have is because the ROS time object is being returned as this mxArray format, which is a bridge between interpreted calls and C/C++ code. I had success sticking the whole unpacking of time into a separate function
coder.extrinsic ('getTime');
[s,ns] = getTime; %get rostime
where the getTime function is defined in a separate file as
function [s,ns] = getTime
t = rostime('now');
s = t.Sec;
ns = t.Nsec;
end
2. I had to explicitly define the input/output data types for the MATLAB Function block. If you look at the toolstrip while you have the MATLAB Function code open, there will be an "Edit Data" option. Here, you can set data types for the inputs and outputs.
For example, I tried your code on a message type of geometry_msgs/TwistStamped, which creates a Bus object in the workspace. The data type you have to use is then Bus: SL_Bus_testRosMsg_geometry_msgs_TwistStamped, which you can select as shown in the screenshot below. (If you don't see the bus data types created by the "Blank Message" block, hit the "Refresh data types" option in the drop-down).
- Sebastian
2 个评论
Sebastian Castro
2017-10-9
Sure. These are R2017b models, but you can change your Simulink preferences to open models from newer versions if needed.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Publishers and Subscribers 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!