Connecting ROS to Arduino Serial Port
Hi,
I'm using ROS-Indigo on Ubuntu 14.04. I have ROS properly installed, sourced and dependencies configured properly (wiki.ros.org/indigo/Installation/Ubuntu). I have arduino installed and ROS libraries setup perfectly (https://www.arduino.cc/en/Guide/Linux) (wiki.ros.org/rosserialarduino/Tutorials/Arduino%20IDE%20Setup) (wiki.ros.org/rosserialarduino/Tutorials).
Now, my arduino stand-alone works perfectly, I tested that with a servo motor.
I did the following steps:
- Dumped the example servo motor program into arduino without any changes.
- Started roscore.
When I tried connecting ROS with arduino using this command:
rosrun rosserial_python serial_node.py /dev/ttyUSB0
I get this error:
[INFO] [WallTime: 1528924023.535404] ROS Serial Python Node
[INFO] [WallTime: 1528924023.543175] Connecting to /dev/ttyUSB0 at 9600 baud
Traceback (most recent call last):
File "/home/prasanth/catkin_ws/src/rosserial/rosserial_python/nodes/serial_node.py", line 88, in <module>
client = SerialClient(port_name, baud, fix_pyserial_for_test=fix_pyserial_for_test)
File "/home/prasanth/catkin_ws/src/rosserial/rosserial_python/src/rosserial_python/SerialClient.py", line 358, in __init__
self.port = Serial(port, baud, timeout=self.timeout, write_timeout=10)
TypeError: __init__() got an unexpected keyword argument 'write_timeout'
I tried using this alternate command:
rosrun rosserial_python serial_node.py /dev/ttyACM0
and adding different baud rates to the end like:
rosrun rosserial_python serial_node.py /dev/ttyACM0 _baud:=38400
I couldn't find any help for this problem online. Without crossing this step, I cannot progress to
rostopic pub servo std_msgs/UInt16 <angle>
I'm following this tutorial for the steps: http://wiki.ros.org/rosserial_arduino/Tutorials/Servo%20Controller
Kindly help, Prasanth.S.S
Asked by Prasanth Suresh on 2018-06-13 16:14:07 UTC
Answers
- Please confirm your cable is working.
- Arduino usually makes
ttyACM0
only, as much as I know. Give permission to this interface using below command:
sudo chmod 777 /dev/ttyACM0
Were you able to flash the code?
You have to run roscore in a separate tab as well.
Saurabh
Asked by saurabh on 2018-06-14 01:22:06 UTC
Comments
Hi, thank you for the prompt reply, here are the answers to your questions:
1.) Yes, my cable is working, because I used the same cable to test arduino stand-alone to control a servo motor. 2.) Yes, you are right, I tried both ACM0 and USB0, but neither of them cause any change in the error.
Asked by Prasanth Suresh on 2018-06-14 10:13:34 UTC
I also tried adding baud rate to the end, like, rosrun rosserial_python serial_node.py /dev/ttyACM0 _baud:=9600. That doesn't help either. 3.) I did give it all the permissions now, that didn't change anything. 4.) Yes, I was able to flash the code successfully. 5.) Yes, roscore is in a seperate tab
Asked by Prasanth Suresh on 2018-06-14 10:14:43 UTC
Are you sure that arduino is connected to ACM0? You can see the serial port name as /dev/USBx or /dev/ACMx in the arduino IDE. Please check that once and use that.
Asked by PratNag on 2018-06-19 13:30:24 UTC
@PratNag, yes, I made sure of that.
Asked by Prasanth Suresh on 2018-06-19 13:33:20 UTC
So that happens due to differences in libraries for Python 2.7 and Python 3.0. The tutorials are based on Python 2.7 so be careful of that.
Asked by PratNag on 2018-06-21 04:32:56 UTC
Comments
ROS-Indigo on Ubuntu 14.04 is a pain. The reason it doesn't work is because in Indigo "write_timeout" was in camel case being "writeTimeout". If you change the code and replace "write_timeout" with "writeTimeout" that should do the trick. You could also just upgrade to kinetic or higher and that should solve your problem as well.
Have a nice day.
Asked by ojboba on 2019-06-11 01:33:19 UTC
Comments
Could you show the setup part of your code, I had a similar issue due to a bad configuration of the serial communication(s). Also you can try
rosrun rosserial_python serial_node.py _port:=/dev/ttyACM0
to launch the node.Asked by RicHykkie on 2018-06-15 02:26:00 UTC
Hi Ric,
Assuming you mean the setup of the arduino code, I'm using the exact sample code that is in the library:
void setup(){ pinMode(13, OUTPUT);
nh.initNode(); nh.subscribe(sub);
servo.attach(9); //attach it to pin 9 }
Asked by Prasanth Suresh on 2018-06-15 14:48:30 UTC
Try adding
This should open the serial connection with the Arduino.
Asked by RicHykkie on 2018-06-17 00:45:51 UTC
Hi Ric, I tried that, it didn't make any difference, so just for debugging sakes, I tried commenting out the line that was mentioned in the error above and now I get this: self.port.flushInput() AttributeError: 'SerialClient' object has no attribute 'port'; Does any of this mean anything to you?
Asked by Prasanth Suresh on 2018-06-17 15:48:41 UTC
Regarding what you've done, it's normal. Line 358 of SerialClient.py goes:
If you comment it, the class SerialClient has no more
port
data defined, hence the error.I'm still looking at your issue.
Asked by RicHykkie on 2018-06-19 00:05:05 UTC
This same line in my SerialClient.py is:
self.port = Serial(port, baud, timeout=self.timeout*0.5)
Be aware that I'm using lunar and 16.04, but that might be related to your issue.
Asked by RicHykkie on 2018-06-19 00:07:08 UTC
try changing write_timeout to writeTimeout. I am not sure about this but you can try. From what I read its different for py2.7 and py3.0. Try it and let me know.
Asked by PratNag on 2018-06-19 13:49:18 UTC
I shall try that asap and get back to you, thanks.
Asked by Prasanth Suresh on 2018-06-19 13:53:11 UTC
@RicHykkie That worked... Amazing, thank you so much!!!!
Asked by Prasanth Suresh on 2018-06-19 18:40:02 UTC
@Prasanth Suresh Glad to hear it. Let's make a recap as an answer so that other people having the same issue get a clear answer :) Did you delete the last argument of replaced it as @PratNag suggested?
Asked by RicHykkie on 2018-06-19 20:15:19 UTC
Hi @RicHykkie, yes, I accessed /home/prasanth/catkin_ws/src/rosserial/rosserial_python/src/rosserial_python/SerialClient.py file and on line 356 and 358, I changed it from write_timeout to writeTimeout. That did the trick. Thanks all :)
Asked by Prasanth Suresh on 2018-06-20 10:17:18 UTC
@PratNag Worked for me!! thank u (Change write_timeout to writeTimeout) i suppose that is for the version of pyserial installed in my pc..
Asked by Matzelwow on 2019-04-01 21:32:43 UTC
@prasanth Suresh , changing write_timeout to writeTimeout in the error code worked for me. Thanks @PratNag
Asked by sajal on 2019-06-12 06:44:05 UTC