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

Calling client based on a timer

asked 2023-04-08 15:02:52 -0500

Paul S gravatar image

I have created a node to read serial data from an Arduino in the form of bytes. I had to do this because the Arduino does not support micro-ros or rosserial. I had to run the callback that reads this data on a timer using self.create_timer. Every time data is properly received, I am using a service to decode this data and return it. My problem is that when I create a service and call it every time data is received the service completes its entire task, but nothing is ever returned.

Here is a stripped down version of my node.

class SerialReader(Node): 
    def __init__(self):
        super().__init__('serial_read_node') 

        # Declare parameters
        # Initialize serial connection

        self.cli = self.create_client(
            DecodeMessage,
            'decode_serial_message'
        )
        while not self.cli.wait_for_service(timeout_sec=1.0):
            self.get_logger().info('service not available, waiting again...')

        self.req = DecodeMessage.Request()
        self.create_timer(1, self.receiveData)

    def receiveData(self):
        # Receive bytes from Arduino as 'message'
        result = self.sendRequest(reading_bytes)

    def sendRequest(self, message): #call client for data
        self.req.message_received = message
        self.future = self.cli.call(self.req)
        rclpy.spin_until_future_complete(self, self.future)
        self.get_logger().info(f'Future = {self.future}')
        return self.future.result()

def main():
    rclpy.init()
    node = SerialReader()
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == "__main__":
    main()

My question are:

  1. Is my problem that the client is not its own node?
  2. Is there a way to use rclpy.spin_until_future_complete() on a callback function?
  3. Is multithreading and using executors a path to solving my problem?

I did not include my service as I have found that I can print to the logger the line before the return statement with everything working properly, but nothing here gets past the line rclpy.spin_until_future_complete(self, self.future).

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-12 15:45:57 -0500

Paul S gravatar image

Update: I have found this tutorial very helpful (How to use callback groups in ROS2). Most of my problems were solved by implementing multi threading and having separate callback groups for the timer, service, and client. I'm actively trying to get Parameters to work in an executable with two nodes.

edit flag offensive delete link more

Comments

Just to link to it: that content has been merged with the official documentation:

gvdhoorn gravatar image gvdhoorn  ( 2023-04-13 02:49:42 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2023-04-08 15:02:52 -0500

Seen: 104 times

Last updated: Apr 12 '23