Calling setModuleInitProc

9 posts / 0 new
Last post
gjuhasz86
Offline
Last seen: 11 years 5 days ago
Joined: 2013-03-18 02:47
Calling setModuleInitProc

Hi,

When creating an RT component, I have to do something similar to:

 void MyModuleInit(RTC::Manager* manager) {
  // ...
 }
 // ...
 manager->setModuleInitProc(MyModuleInit);
 // ...

What if the MyModuleInit function has more arguments? Say it looks like this: void MyModuleInit(RTC::Manager* manager, MyCustomClass userData);

I tried using functors and boost::bind without success. Is there a way to do this?

In the end what I'm trying to achieve is to pass data from the main function to the component itself.

Thanks

Undefined
Ando Noriaki
Offline
Last seen: 1 year 9 months ago
Joined: 2011-09-04 17:20
[openrtm-beginners:00420] Calling setModuleInitProc

Hello,

To what do you want to give "userData"?
If you want to give"userData" to RTC instances, how about the following way?

==========================

void MyModuleInit(RTC::Manager* manager)
{
ConsoleInInit(manager);
RTC::RtcBase* comp;

// Create a component
std::cout << "Creating a component: \"ConsoleIn\"....";
comp = manager->createComponent("ConsoleIn?your_param0=your_value0&your_param1=your_value1");
==========================

Here, createComponent() is called in MyModuleInit() function, but you can call
createComponent() anywhere after manager->activateManager().
MyModuleInit() function is usually used only to load RTC's shared object.

After creating your RTC, you can get, your_param0, your_param1 like this,

==========================

ReturnCode_t onInitialize(ec)
{
std::cout << "your_param0: " << getProperties()["your_param0"] << std::endl;
std::cout << "your_param1: " << getProperties()["your_param1"] << std::endl;
}
==========================

You can get given parameters in all the onXXXX() call-back functions.
But, you cannot
get them in the constructor.

Best regards,
Noriaki Ando

2013/4/14 :
> Hi, When creating an RT component, I have to do something similar to: void
> MyModuleInit(RTC::Manager* manager) { // ... } // ...
> manager->setModuleInitProc(MyModuleInit); // ... What if the MyModuleInit
> function has more arguments? Say it looks like this: void
> MyModuleInit(RTC::Manager* manager, MyCustomClass userData); I tried using
> functors and boost::bind without success. Is there a way to do this? In the
> end what I'm trying to achieve is to pass data from the main function to the
> component itself. Thanks
>
> _______________________________________________
> openrtm-beginners mailing list
> openrtm-beginners@openrtm.org
> http://www.openrtm.org/mailman/listinfo/openrtm-beginners
_______________________________________________
openrtm-beginners mailing list
openrtm-beginners@openrtm.org
http://www.openrtm.org/mailman/listinfo/openrtm-beginners

gjuhasz86
Offline
Last seen: 11 years 5 days ago
Joined: 2013-03-18 02:47
[openrtm-beginners:00420] Calling setModuleInitProc

Thank you for your quick reply. As you guessed right, I would like to pass user data to the RTC component instance itself. And I don't necessary need it in the constructor, it's fine to have it in the onInitialize method.

At the time of writing my question I thought I was able to pass a custom function to manager->registerFactory so I thought if I can get custom data to MyModuleInit, I would be able to pass that to the RTC. But it turned out it didn't work, and I was facing the same problem.

So, back to your suggestion, that is definitely something I can use. But if I'm not mistaken I can only pass data in text format. The problem is that the data I would like to pass is a class instance (it's complex, and quite hard to serialize).

The only way I can think of working around it, is to have a class with static methods mapping ids to userData. I can pass the id as a text to the component, wich then calls the static method to get the userData. This is clearly an "ugly but working" solution, but definitely better then using the name of the component as a key of the map.

So there are no way of passing an object directly to the RTC?

Thanks,

Gabor Juhasz

gbiggs
Offline
Last seen: 6 years 9 months ago
Joined: 2010-08-02 07:51
[openrtm-beginners:00422] (無題)

If you look in the ModuleInit() function, you can see the component instance being created by a call to the createComponent() function of the manager. This returns a pointer to the component. If you cast this pointer (using a type-safe dynamic_cast) to your component's type, then you can call its functions. You could add a function to the component to initialise your user data and call that after calling createComponent().
GeoffOn 15 April 2013 17:07, <jaysicks.reg@gmail.com> wrote:

Thank you for your quick reply. As you guessed right, I would like to pass
user data to the RTC component instance itself. And I don't necessary need it
in the constructor, it's fine to have it in the onInitialize method. At the
time of writing my question I thought I was able to pass a custom function to
manager->registerFactory so I thought if I can get custom data to
MyModuleInit, I would be able to pass that to the RTC. But it turned out it
didn't work, and I was facing the same problem. So, back to your suggestion,
that is definitely something I can use. But if I'm not mistaken I can only
pass data in text format. The problem is that the data I would like to pass
is a class instance (it's complex, and quite hard to serialize). The only way
I can think of working around it, is to have a class with static methods
mapping ids to userData. I can pass the id as a text to the component, wich
then calls the static method to get the userData. This is clearly an "ugly
but working" solution, but definitely better then using the name of the
component as a key of the map. So there are no way of passing an object
directly to the RTC? Thanks, Gabor Juhasz

_______________________________________________
openrtm-beginners mailing list
openrtm-beginners@openrtm.org
http://www.openrtm.org/mailman/listinfo/openrtm-beginners

gjuhasz86
Offline
Last seen: 11 years 5 days ago
Joined: 2013-03-18 02:47
[openrtm-beginners:00422] (無題)

Hi,

If I'm not mistaken onInitalize() is called from createComponent(). Actually I'm trying to register ports based on user data. And I beleive I have to do that in the onInitialize() method at the latest.

Thanks, Gabor

Ando Noriaki
Offline
Last seen: 1 year 9 months ago
Joined: 2011-09-04 17:20
[openrtm-beginners:00424] (無題)

Hi,

You can call addOutPort/addInPort in other onXXX call-back functions.
So you can realize a RTC component with dynamic Data-Port.

Best regards,
Noriaki Ando

2013/4/15 :
> Hi, If I'm not mistaken onInitalize() is called from createComponent().
> Actually I'm trying to register ports based on user data. And I beleive I
> have to do that in the onInitialize() method at the latest. Thanks, Gabor
>
>
> _______________________________________________
> openrtm-beginners mailing list
> openrtm-beginners@openrtm.org
> http://www.openrtm.org/mailman/listinfo/openrtm-beginners

gbiggs
Offline
Last seen: 6 years 9 months ago
Joined: 2010-08-02 07:51
[openrtm-beginners:00425] (無題)

Ports can be added and removed at any time. I have used this a few times
to produce dynamically-reconfigurable components.

Remember that the changes won't show up in RTSystemEditor unless you
remove the component from the system and re-add it.

Geoff

On 15/04/13 20:13, jaysicks.reg@gmail.com wrote:
> Hi, If I'm not mistaken onInitalize() is called from createComponent().
> Actually I'm trying to register ports based on user data. And I beleive I
> have to do that in the onInitialize() method at the latest. Thanks, Gabor
_______________________________________________
openrtm-beginners mailing list
openrtm-beginners@openrtm.org
http://www.openrtm.org/mailman/listinfo/openrtm-beginners

gjuhasz86
Offline
Last seen: 11 years 5 days ago
Joined: 2013-03-18 02:47
[openrtm-beginners:00425] (無題)

Oh, I didn't know that! When I tried I think I just assumed it didn't work, because I didn't see the port appearing in system editor. But that's a great news. Thanks.

Now if I leave out the call for manager->setModuleInitProc() completely, and refactor the code as below, it works.

It is also safe to do this way, right?

 coil::Properties profile(comp_spec);
 
 RTC::Manager* manager;
 RTC::RtcBase* comp;
 
 manager = RTC::Manager::init(argc, argv);
 manager->init(argc, argv);
 manager->registerFactory(profile, RTC::Create<Component>, RTC::Delete<Component>);
 manager->activateManager();
         
 comp = manager->createComponent("MyComp");
 if (comp == NULL) {
    std::cerr << "Component create failed." << std::endl;
    abort();
 }
 
 manager->runManager(!block);

Now I'm also curious about: Is manager thread-safe? If I want to run multiple component from the same process, can I run multiple managers? I noticed a section called "RTC daemon" in the tutorial, and that sounds like it would do exactly this, but it seems the page hasn't been written yet.

Thanks,

Gabor

Ando Noriaki
Offline
Last seen: 1 year 9 months ago
Joined: 2011-09-04 17:20
[openrtm-beginners:00427] (無題)

Hi,

> Oh, I didn't know that! When I tried I think I just assumed it didn't work,
> because I didn't see the port appearing in system editor. But that's a great

Please reload (= delete from editor and DnD RTC from nameservice view
again :-)your RTCs on the system editor. You can see new Data Port on it.

From OpenRTM-aist-1.1, I've experimentally added ComponentObserverConsumer
to OpenRTM in order to notify component's event to RTSystemEditor. If you
compile and load it to your RTC process, port-add event would be notified
to RTSystemEditor, and its system editor view will be updated automatically.

http://svn.openrtm.org/OpenRTM-aist/tags/RELEASE_1_1_0/OpenRTM-aist/src/ext/sdo/observer/

> news. Thanks. Now if I leave out the call for manager->setModuleInitProc()
> completely, and refactor the code as below, it works. It is also safe to do
> this way, right?
>
> coil::Properties profile(comp_spec);
> RTC::Manager* manager;
> RTC::RtcBase* comp; manager = RTC::Manager::init(argc, argv);
> manager->init(argc, argv);
> manager->registerFactory(profile, RTC::Create, RTC::Delete);
> manager->activateManager(); comp = manager->createComponent("MyComp");
> if (comp == NULL) { std::cerr << "Component create failed." << std::endl; abort(); }
> manager->runManager(!block);
>
> Now I'm also curious about: Is manager
> thread-safe? If I want to run multiple component from the same process, can
> I run multiple managers?

Manager is singleton object, and its instance that are obtained by
Manager::instance()
function is always same.

Of course you can create multiple RTCs in one manager (=process).

RTC::Manager* manager;
manager = RTC::Manager::init(argc, argv);
manager->activateManager();

manager->load("MyComponentA.so", "MyComponentAInit");
manager->load("MyComponentB.so", "MyComponentBInit");
manager->load("MyComponentC.so", "MyComponentCInit");
manager->runManager(!block); // block = false

comp = manager->createComponent("MyComponentA");
if (comp == NULL) { std::cerr << "Component create failed." <<
std::endl; abort(); }
comp = manager->createComponent("MyComponentB");
if (comp == NULL) { std::cerr << "Component create failed." <<
std::endl; abort(); }
comp = manager->createComponent("MyComponentC");
if (comp == NULL) { std::cerr << "Component create failed." <<
std::endl; abort(); }

while (1) {
// do my task
sleep(1);
}

manager->shutdown();
return 0;

This code load MyComponent[A-C].so shared objects, and
create three component from them.

If you just only create multiple component in one proces, you can use
manager.modules.preload:
manager.components.precreate:
options to load and instantiate RTCs.

http://svn.openrtm.org/OpenRTM-aist/tags/RELEASE_1_1_0/OpenRTM-aist/etc/rtc.conf.sample

> I noticed a section called "RTC daemon" in the
> tutorial, and that sounds like it would do exactly this, but it seems the
> page hasn't been written yet. Thanks, Gabor

I'm sorry. That page is still under construction. I'll write it as
soon as possible.

Best regards,
Noriaki Ando
_______________________________________________
openrtm-beginners mailing list
openrtm-beginners@openrtm.org
http://www.openrtm.org/mailman/listinfo/openrtm-beginners

Log in or register to post comments

Download

latest Releases : 2.0.0-RELESE

2.0.0-RELESE Download page

Number of Projects

Choreonoid

Motion editor/Dynamics simulator

OpenHRP3

Dynamics simulator

OpenRTP

Integrated Development Platform

AIST RTC collection

RT-Components collection by AIST

TORK

Tokyo Opensource Robotics Association

DAQ-Middleware

Middleware for DAQ (Data Aquisition) by KEK