0
$\begingroup$

I need to set up a ROS2 python serial device driver that also subscribes to a ROS message. For serial drivers the only publish messages I set up the serial input in a select call like this. Here, the va500.serial_handler(line) is a method called after the select when serial data arrives. It deals with the serial data, and then publishes a message.

 def main(args = None):
    rclpy.init(args=args)
    va500 = va500Driver()
    while True:
        try:
            rfds, wfds, efds = select.select([va500.comm.fileno()], [], [], 1.)
            if rfds.__len__() > 0 and rfds[0] == va500.comm.fileno():
                line = va500.comm.readline()
                va500.serial_handler(line)
        except KeyboardInterrupt:
            print('Received shutdown command.')
            exit()

if __name__ == '__main__':
      main()

For nodes that just publish and subscribe to ROS messages we have it set up like this. These will sit in the .spin() and handle ros events as they happen using message callbacks defined elsewhere.

def main(args = None):
    rclpy.init(args=args)
    driver = ActuatorDriver()
    rclpy.spin(driver)
    driver.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

I'd like to do both of these, but if I use the .spin() to handle the ros message callbacks I can't also set up the select to sit on the serial port. I'm guessing the MultiThreadedExecutor is the answer, but maybe I'm missing something.

$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.