Ask Your Question
2

How to change the serial port in the rosserial lib for the Arduino side?

asked 2014-11-25 08:53:36 -0600

WFH27 gravatar image

updated 2014-11-27 05:52:31 -0600

For a lab at my university I have developed a robot with an Arduino Mega ADK that can be controlled by ROS via the rosserial package. The connection between the Arduino and the laptop running ROS is made via bluetooth. During the prototyping phase the bluetooth module, HC-05, was connected to the Serial port that is also used for flashing the Arduino. Whenever I wanted to flash the Arduino I just disconnected the bluetooth module to prevent it from interfering.

However, we need to put all the electronics in a 3D printed casing, because we don't want students to touch the electronics during the lab. This revealed that we needed a solution for the interference of the bluetooth module. One option is to turn off the bluetooth with a switch, but this is far from ideal. The other option is to force the Arduino to use another Serial port (Serial1 in this case) for communication with ROS. In ros_lib (generated withrosrun rosserial_arduino make_libraries.py .) I found the file ArduinoHardware.h which contains some definitions including the iostream on lines 66-72. I changed line 71 to Serial1 which forced the Arduino to use Serial1 instead of Serial.

Although this did the job, it is not a very elegant solution. Is there some other, better way to solve this problem? For instance, could I use #define USBCON to change the Serial port or will this have other implications because this is for the Leonardo? (I only saw this just now, so I still have to test this)

P.S. Just to be sure, I'm looking for an answer for the client side, not the host side. I already found numerous answers for changing the port on the host side.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2014-11-26 13:05:51 -0600

updated 2014-11-27 07:29:11 -0600

You need to find the ArduinoHardware.h file in your machine (it's usually in your libraries folder somewhere in ros_lib) and change this line to use the Serial you want (e.g. iostream = &Serial1;)

Hope it helps!

Sorry, I don't know why I didn't see that on the OP.

Here's what I've found, you should create a new Hardware definition class in your sketch and specify the port you want there and instanciate it when you create the nodeHandle object.

class NewHardware : public ArduinoHardware
{
  public:
  NewHardware():ArduinoHardware(&Serial1, 57600){};
};

ros::NodeHandle_<NewHardware>  nh;

I don't have a Mega to test this so I used a regular UNO and re-specified &Serial as the serial port and it worked.

Hope it helps!

edit flag offensive delete link more

Comments

The solution that you describe is what I already came up with in my question. This will fix the Serial port for all sketches that I want to write, but I would like a solution where I can adjust the used Serial port per sketch.

WFH27 gravatar image WFH27  ( 2014-11-27 05:50:40 -0600 )edit

Thanks a lot. It works.

WFH27 gravatar image WFH27  ( 2014-11-27 08:25:52 -0600 )edit

can you mark it as solved if this worked for you? So others can find it useful. Thanks!

Gary Servin gravatar image Gary Servin  ( 2014-11-27 08:46:05 -0600 )edit

i had different problem. This solved. Thanks very much.

narendiran1996 gravatar image narendiran1996  ( 2017-01-15 19:32:49 -0600 )edit

Your Answer

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

Add Answer

Question Tools

4 followers

Stats

Asked: 2014-11-25 08:53:36 -0600

Seen: 2,605 times

Last updated: Nov 27 '14