How to use 'Timer' function to send ROS messages?

 採用された回答

Koundinya
Koundinya 2018 年 9 月 10 日
編集済み: Koundinya 2019 年 2 月 7 日

0 投票

You can use MATLAB timers in this way :
% Create a timer object
t=timer;
Set execution mode of the timer object to fixedRate, fixedDelay or fixedSpacing to execute your call back repeatedly
set(t,'executionMode','fixedRate');
Specify the function handle or the command you want to be executed. In your case, for sending messages over a ROS topic , use send :
set(t,'TimerFcn','send(pub,msg)');
Set the frequency or period(in seconds),at which the timer callback function is called
% To publish the message at a rate of 50 times per second
set(t,'Period', 0.02);
% Start the timer
start(t);
ROS message msg is then sent periodically(every 0.02 seconds). To stop the timer:
stop(t);
Delete the timer object after usage
delete(t);

5 件のコメント

JAI PRAKASH
JAI PRAKASH 2018 年 9 月 10 日
Thanks @Poluri
If 'msg' is varying with time then?
Koundinya
Koundinya 2018 年 9 月 10 日
Then you can include the code that generates/modifies msg in a function, and pass it to the timer object :
set(t,'TimerFcn',@sendROSMessage);
function sendROSMessage(obj, event)
% modify msg
% msg=getSensorReading();
% msg=msg+1;
% send
send(pub,msg);
end
Refer to the documentation on MATLAB timer callback functions for more details
JAI PRAKASH
JAI PRAKASH 2018 年 9 月 10 日
msg.Data=getSensorReading();
Arun Prasanth Soungyan Gokul
Arun Prasanth Soungyan Gokul 2019 年 2 月 6 日
Hi Koundinya,
I'm trying to use the timer object which you suggested. I already created the publisher and it's respective message(pub,msg). But when I set set(t,'TimerFcn','send(pub,msg)'), and the other attributes
and run start(t); it says undefined function or variable msg,undefined function or variable pub.
Can you please help me out from this?
Koundinya
Koundinya 2019 年 2 月 7 日
編集済み: Koundinya 2019 年 2 月 7 日
Hi Arun,
You need to first create a ROS publisher object(named 'pub' in this case) and then define a message ('msg') that is to be published. Take a look at the documentation of rospublisher and send for examples.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangePublishers and Subscribers についてさらに検索

製品

リリース

R2018a

タグ

質問済み:

2018 年 9 月 7 日

編集済み:

2019 年 2 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by