[openrtm-users 01427] Re: Using Coil::timer function

Ando Noriaki n-ando @ aist.go.jp
2010年 10月 8日 (金) 01:22:26 JST


Hello Tony

> Hi all,
> I found a timer class in the coil library and am interested to use it for my
> components in timing my outputs.
> I have looked through the website and information I have, but still couldn't
> really figure out how to declare a listener function and use it with the
> timer class.
> I was wondering if any of you can point me in the right direction or give me
> a simple example?
>
> Thanks heaps in advance.
> Tony

See the following simple example.
You can add a listener object of a class which inherits ListenerBase
pure-virtual class -> (1).
And you can also add any kind of listener objects by using template
function -> (2)

#include <iostream>
#include <coil/Timer.h>
#include <coil/Listener.h>
#include <coil/TimeValue.h>

class MyListener
 : public ListenerBase
{
public:
  virtual void invoke()
  {
    std::cout << "Hello" << std::endl;
  }
};

class MyClass
{
public:
  void hoge()
  {
    std::cout << "HOGE" << std::endl;
  }
};

int main(void)
{
  coil::TimeValue timerinterval(0, 10000);
  coil::Timer timer(timerinterval); // Timer interval is 0.01sec
  MyListener listener;
  MyClass myobj;

  coil::TimeValue interval(1, 0);
  timer.registerListener(&listener, interval); // (1) listener is
ListenerBase's subclass
  timer.registerListenerObj(&myobj, &MyClass::hoge, interval/10); //
(2) any kind class member function can be added by this template
function
  timer.start();

  while (1) ;

}

Then, you will get

HOGE
HOGE
HOGE
HOGE
HOGE
HOGE
HOGE
HOGE
HOGE
HOGE
Hello
HOGE
HOGE

Best Regards,
Noriaki Ando






-- 
Noriaki Ando, Ph.D.
    Senior Research Scientist, RT-Synthesis R.G., ISRI, AIST
    AIST Tsukuba Central 2, Tsukuba, Ibaraki 305-8568 JAPAN
    e-mail: n-ando @ aist.go.jp, web: http://staff.aist.go.jp/n-ando
    OpenRTM-aist: http://www.openrtm.org




openrtm-users メーリングリストの案内