ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here are some complement

The retrieve_value method is called in my __init__ function which is

def __init__(self, ):
    super().__init__(__class__.__name__)

    self.__var = None
    self.__listener_cb_grp = ReentrantCallbackGroup()
    self.retrieve_value()

    # I want __var set here
    self.get_logger().info(f"init: var = {self.__var}")

What I understand, is that the callback will be run only once the spin method is called, so calling a time.sleep in this init method makes this init never ends.

Let's say, my init defines only the callback group and move the retrieved+"use of __var" in another methods work

def __init__(self, ):
    super().__init__(__class__.__name__)

    self.__var = None
    self.__listener_cb_grp = ReentrantCallbackGroup()

def work(self,):
    self.get_logger().info("work IN !")
    self.retrieve_value()
    # I want __var set here
    self.get_logger().info(f"work: var = {self.__var}")
    self.get_logger().info("work OUT !")

And the main will init the node, spin the executor and last call the work method

node = TestCB()  # my node

exe = MultiThreadedExecutor()
exe.add_node(node)
exe.spin()

node.get_logger().info("init done, work now !")
node.work()

node.destroy_node()
rclpy.shutdown()

This time the node is stuck right after the init. I suspect everything has to be done in callbacks and methods can't be called by ROS2 design, is that right ?