Ask Your Question

Simple... open a serial port and send a value to microcontroller

asked 2012-02-23 20:38:41 -0500

kleekru gravatar image

updated 2014-01-28 17:11:28 -0500

ngrennan gravatar image

I'm looking through the rosserial tutorials and the basics are escaping me. Does anyone have an example of python code that connects to a serial port (a linked USB in my case) and sends values to a Microcontroller (BS2 in my case). I'm using the BS2 to actuate a very basic remote controlled arm (OWI Arm Trainer) from an Ubuntu/ROS diamondback laptop. I've been through most of the tutorials but cannot determine a simple way to open the serial port and send values to the BS2. Can someone at least get me started in the right direction?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2012-04-18 05:17:58 -0500

updated 2012-04-18 05:20:42 -0500

If you are not using rosserial, I would work on getting basic serial communication working outside of ROS first. @kleekru mentioned he is working in Python. I would look at the documentation for pySerial, and figure out how to just send the characters you want to send. For example, the following snippet would send the ascii character for '2' as you requested

import serial
com = serial.Serial('/dev/ttyUSB0',baudrate=115200)

For @nachoarreola, you need to look up termios.h (assuming you are on linux). A bunch of good examples can be found here. Once you understand how to send commands programmatically, you can then write a ROS node that helps you out.

The system I'm currently working with has robots that cannot run ROS onboard. So I have a serial node that advertises a ServiceServer. This serial node is responsible for opening the connection and sending data. When a different node wants to send a serial packet to a robot, it calls the service advertised by the serial node, the serial node assembles the serial packet, sends the data out the serial port, and then replies to the client node with the robot's response.

edit flag offensive delete link more

answered 2012-02-23 23:43:38 -0500

dornhege gravatar image

rosrun rosserial_python /dev/ttyUSB0

edit flag offensive delete link more



Ok, that helps... I'll see if I can dissect these but they are somewhat over my head. To cut to the chase, ...Say I wanted to pass a value to the BS2 like "2". Would I simply add a parameter to the command line or do I need to update the code?

kleekru gravatar imagekleekru ( 2012-02-24 03:09:17 -0500 )edit

Maybe you are misunderstanding what rosserial is? It's not a serial access library to send characters. It requires a matching firmware on the MC and allows you to publish/subscribe ROS message directly on the MC. If you just want to send chars, you can grab that functionality from the

dornhege gravatar imagedornhege ( 2012-02-24 05:42:27 -0500 )edit

Thanks Christian... I did have a suspicion I was barking up the wrong tree. I'm not a Python developer but hope to learn more. Would you mind helping me through this? I'll try running against ttyusb0 at 9600 baud and see what happens... But I'm flying blind...

kleekru gravatar imagekleekru ( 2012-02-24 13:50:44 -0500 )edit

What are you actually trying to achieve? Just for sending bytes manually, you can use something like gtkterm. If you programmatically want to send them from python code, have a look at how the does it in the send() function. Or do you want to run rosserial on your device?

dornhege gravatar imagedornhege ( 2012-02-24 16:09:31 -0500 )edit

I would prefer to run rosserial on the BS2 but it looks as though that hasn't been done yet. I'm probably not the one to pave new ground without some help but I'm certainly willing to contribute. What I lack in programming experience, I might partially make up for in enthusiasm and attention. For now, I'm building the platform to be completely remote controlled. I have a Roomba with a laptop onboard running Ubuntu/ROS Diamondback with the iRobot SCI cable, and the Roomba_500_series package is being called by some php pages. The laptop is also a webserver so I'm able to run the robot remotely via the internet. I'm using it as a telepresence platform and I would like to add a very simple arm to it and control it via the same web page.

kleekru gravatar imagekleekru ( 2012-02-24 19:39:25 -0500 )edit

After I get the ARM running, I may want to begin experimenting with more automation, navigation, or add the Kinect for vision & navigation. For now, I just want some clarity on how to leverage ROS to programmatically actuate the ARM. The way I have the ARM and the BS2 is set up now... if I use the PBASIC debug option, I can enter any value 0-9 to actuate one of the ARM joints on the laptop keyboard... but as I mentioned above, I would like to send the commands programatically from a php page.

kleekru gravatar imagekleekru ( 2012-02-24 19:49:09 -0500 )edit

I was able to install rosserial and run I added the line send("1")

kleekru gravatar imagekleekru ( 2012-02-24 20:30:27 -0500 )edit

If you are only interested in sending values 0-9 from python, you won't need rosserial. You can just make up a very simple python script that uses the Serial class. If I interpret the correctly that should about two lines.

dornhege gravatar imagedornhege ( 2012-02-25 03:07:35 -0500 )edit

answered 2012-04-18 03:50:44 -0500

And a c++ example?

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


Asked: 2012-02-23 20:38:41 -0500

Seen: 2,819 times

Last updated: Apr 18 '12