# ROS2 Composition

**URL:** <https://discourse.openrobotics.org/t/ros2-composition/2469>\
**Category:** ROS General\
**Created:** [August 18, 2017, 10:25pm UTC](https://discourse.openrobotics.org/t/ros2-composition/2469 "2017-08-18T22:25:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrutyu1987](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@mrutyu1987](https://discourse.openrobotics.org/u/mrutyu1987)\
**Post date:** [August 18, 2017, 10:25pm UTC](https://discourse.openrobotics.org/t/ros2-composition/2469/1 "2017-08-18T22:25:26Z")

</div>

I’m new to ROS2. How Can I spin the node using composition techniques. In the case of publishers, examples shows use of timer to bind the callback function. Is there any other way of continuously sniping the node without binding it to timer.

In composition example.:  
// Use a timer to schedule periodic message publishing.  
timer\_ = create\_wall\_timer(1s, std::bind(&Talker::on\_timer, this));

My use case application is creating a video/image (OpenCV) publisher.

Regards,  
MJay

---

<div class="post-metadata">

**Author:** ![dirk-thomas](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/dirk-thomas/32/4328_2.png) [@dirk-thomas](https://discourse.openrobotics.org/u/dirk-thomas)\
**Post date:** [August 18, 2017, 10:42pm UTC](https://discourse.openrobotics.org/t/ros2-composition/2469/2 "2017-08-18T22:42:28Z")

</div>

In the composition case none of the nodes is in charge of the main function / main thread. Therefore you either have to use a timer to get notified in regular intervals or start your own thread (with all the implications for locking / thread safety).

---

<div class="post-metadata">

**Author:** ![sagniknitr](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/sagniknitr/32/1025_2.png) [@sagniknitr](https://discourse.openrobotics.org/u/sagniknitr)\
**Post date:** [March 12, 2018, 2:20pm UTC](https://discourse.openrobotics.org/t/ros2-composition/2469/3 "2018-03-12T14:20:40Z")

</div>

@dirk-thomas Just a query regarding future composition related work…Will there be anything like the onInit() function (like that of nodelets) in future ros 2 releases..Or will all composed nodes in ros 2 be initialised at the constructor of the class ??

---

<div class="post-metadata">

**Author:** ![dirk-thomas](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/dirk-thomas/32/4328_2.png) [@dirk-thomas](https://discourse.openrobotics.org/u/dirk-thomas)\
**Post date:** [March 12, 2018, 5:48pm UTC](https://discourse.openrobotics.org/t/ros2-composition/2469/4 "2018-03-12T17:48:09Z")

</div>

You might want to consider to use a managed node. The current implement matches the design document which describes how the life cycle of managed nodes works: [Managed nodes](http://design.ros2.org/articles/node_lifecycle.html)

There are currently no plans for changing anything in the life cycle itself. The configure step might be the right callback to perform your initialization.

---

<div class="post-metadata">

**Author:** ![sagniknitr](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/sagniknitr/32/1025_2.png) [@sagniknitr](https://discourse.openrobotics.org/u/sagniknitr)\
**Post date:** [March 22, 2018, 6:01am UTC](https://discourse.openrobotics.org/t/ros2-composition/2469/5 "2018-03-22T06:01:55Z")

</div>

Thanks @dirk-thomas. I was wondering how composition would support runtime polymorphism (like those supported by pcl\_nodelets in pcl\_ros) if all initialization were done at the constructor , instead of the the virtual onInit() function given in the nodelets.

---

<div class="post-metadata">

**Author:** ![dirk-thomas](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.openrobotics.org/dirk-thomas/32/4328_2.png) [@dirk-thomas](https://discourse.openrobotics.org/u/dirk-thomas)\
**Post date:** [April 3, 2018, 11:26pm UTC](https://discourse.openrobotics.org/t/ros2-composition/2469/6 "2018-04-03T23:26:58Z")

</div>

> [@sagniknitr](#):
>
> I was wondering how composition would support runtime polymorphism (like those supported by pcl\_nodelets in pcl\_ros)

I am sorry - I don’t understand the question. Maybe you can elaborate on it and provide more context.
