Using interrupts with rosserial_arduino to count wheel encoder pulses
Hi.
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?
Thanks!
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.
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?
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.
Steve_Daniluk, NEngelhard - please see edited question.
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.
Of course - how stupid of me!