Ask Your Question
0

How can I communicate over USB in ROS 2?

asked 2022-05-24 03:47:00 -0500

hondaser gravatar image

Hi, I am trying to convert a ROS 1-package into a ROS 2-package. The purpose of the package is to communicate with a robot over USB. Therefore for ROS 1 I used the file serial.h to connect to USB and to write to USB. Is there a similar way in ROS 2 to communicate over USB?

Thanks in advance.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2022-05-28 04:53:53 -0500

808brick gravatar image

If your robot is running off a small microcontroller and you want to communicate over ROS2 via serial you can utilize micro-ROS as found here: https://micro.ros.org/

In ROS1 there is also the rosserial package, which encompasses the popular rosserial_arduino package that can also do the same. They have a tutorials page on the ROS1 wiki here

Again this would mainly apply if your robot is running off a microcontroller, which is the most common use case as to why you would run serial operations. If you are instead using a small computer (not q microcontroller) outfitted with Ethernet or Wi-Fi, this would be a more preferred way to communicate over ROS.

edit flag offensive delete link more

Comments

Keep in mind that micro-ROS does not support 8bit microcontrollers, where ROS1 serial did.

Joe28965 gravatar image Joe28965  ( 2022-05-30 04:26:10 -0500 )edit
1

answered 2022-05-27 10:40:28 -0500

Ranjit Kathiriya gravatar image

updated 2022-05-27 10:41:29 -0500

You can make your own node in ROS 2 either in python or CPP,

I have worked with Python, below is the code for that, you can use library serial in python, and you need to convert it to ros2 code based on your requirements.

import serial

ser = serial.Serial("/dev/ttyUSB0", baudrate=9600, parity=serial.PARITY_NONE, bytesize=serial.EIGHTBITS,
                            stopbits=serial.STOPBITS_ONE, timeout=10000)
if ser.is_open == True:
            return True
        else:
            return False

while (ser.is_open == True):
            line = ser.read_until(b'#')

In case of any ques, feel free to drop commet.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-05-24 01:10:20 -0500

Seen: 181 times

Last updated: May 28