Rosanswers logo


I am trying to accurately measure my wheel velocities by counting the pulses coming from my encoders. My idea was to run an Arduino dedicated to only this task, which counts the pulses in the loop() function then uses a timer (from the Timer1 library) to trigger an interrupt service routine at a specified frequency (I was using 50Hz). Inside this ISR I work out the wheel velocity from the number of pulses counted over the 50Hz period, then publish this out over ROS.

Unfortunately when I try to connect to the node using rosserial_python, I get an error saying that it was expecting a certain number of bits but has received less than that, and the connection fails. Apologies, I don't have the Arduino with me right now so can't paste the full error, but will add it tomorrow if it will help diagnose the problem.

My guess is that whilst the Arduino is communicating back over serial, the ISR gets triggered, which interrupts the process, and ROS doesn't like it. Is this the case, or is something else going on? Can interrupts be used on an Arduino with ROS, or is this a no-go? If not, can anyone recommend a different way to count wheel encoder pulses over a well-defined time period?


Edit: the exact error I'm getting is

[WARN] [WallTime: 14839761.73.327399] Serial Port read returned short (expecting 81 bytes, received 24 instead).
[WARN] [WallTime: 14839761.73.328598] Serial Port read failure:
[INFO] [WallTime: 14839761.73.330119] Packet Failed : Failed to read msg data
[INFO] [WallTime: 14839761.73.331164] msg len is 8

My baud rate is left at the default 57600 and the data I'm sending is a custom msg containing two float32s.

Originally posted by epsilonjon on ROS Answers with karma: 123 on 2017-01-07

Post score: 0

Original comments

Comment by NEngelhard on 2017-01-07:
I implemented something like that and never had problems with a serial connection. Does it work if you just use the serial connection and a simple python script to read the data and convert it into a ROS-message?

Comment by Steven_Daniluk on 2017-01-08:
What are the contents of the messages you are publishing/subscribing too, and their frequencies? Also, do you have the baud rate set to something other than the default? It is possible to overload the arduino connection with messages.

Comment by epsilonjon on 2017-01-09:
Steve_Daniluk, NEngelhard - please see edited question.

Comment by billy on 2017-01-09:
You know this already, but you shouldn't send to serial in ISR. You likely were reentering the ISR while still in it. If you have a serial port buffer, there will be cases where you could write to a buffer from ISR, but message should be smaller than buffer and buffer known to be empty before.

Comment by epsilonjon on 2017-01-09:
Of course - how stupid of me!


1 Answer 1


Rosanswers logo

The error goes away if I don't do the publishing from inside the ISR, but instead from inside loop(). I then just create an "allow_publish" variable which gets set to true in the ISR, and use this to control when to publish in loop(). Not sure if this is the best way to approach it, but I'm getting a publish rate of 49.97 Hz using rostopic hz so it seems to be working okay.

Originally posted by epsilonjon with karma: 123 on 2017-01-09

This answer was ACCEPTED on the original site

Post score: 1


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.