Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For the sake of an answer to newcomers struggling with ROS, here is the answer I was given at: https://robotics.stackexchange.com/a/22442

found what I believe is a relevant answer here, and I think it's relevant because of the following lines in your serial_server.py file:

When it has been initialized, it'll start executing the following callback.

def executeCallback(self, goalHandle):

    # <other code>

    #Send a small Hello to Arduino.
    arduino = serial.Serial(port="/dev/ttyUSB0")

The way I read this makes it look like you're starting a serial connection repeatedly. The post I linked states,

Establishing a serial connection to an arduino causes it to reset, so it was never online at the moment the pi was sending data, and so never replied.

There's a longer explanation about the behavior over at Arduino.SE that says, in part,

The Arduino uses the RTS (Request To Send) (and I think DTR (Data Terminal Ready)) signals to auto-reset.

The answer there also links here for instructions on disabling the auto-reset-on-serial-connection feature. Links can rot, so in the interest of future visitors I'll quote a subset of the page here, describing how to use a pull-up resistor on the reset pin to disable the auto-reset:

The simple way that doesn't require any permanent modifying of your hardware or software configuration changes: Stick a 120 ohm resistor in the headers between 5v and reset (you can find these on the isp connector too). 120 is hard to find so just combine resistors. Don't go below 110 ohms or above 124 ohms, and don't do this with an isp programmer attached. You can just pull out the resistor when you want auto-reset back.

In summary, it looks like you're restarting your serial connection in every execute callback. Establishing the serial connection is resetting your arduino, and then it's resetting while you perform the serial write so it doesn't realize you're expecting a response when you get to the serial read.

You can avoid this by either starting the serial connection and storing the handle to the connection as a member variable or taking it as a callback parameter (preferred) or by disabling the auto-restart function.