0
$\begingroup$

I have 2 motors (JGA25-370) with hall-sensor encodes. They emit about 90-100 interrupts every 100ms, so 1000 a second. Which means I have an interrupt happening every 1ms.

The motors are driven by PCA9685 PWM board and L298N H-Bridge driver.

It's all being operated from Raspberry Pi using a program written in Rust. It starts 2 threads for sysfs interrupts and emits events when rising edge is detected.

Now, the problem is that the readings are not as consistent as I expected. I have another thread where I take measurements every 100ms. This is an example of the results:

90
81
90
90
85
90
89
79
89
89
79
87
80
88
90
90
79
89

The motor is not under any load, just holding it in my hand.

The problem for me are those random jumps from 89 -> 79 -> 89 event though the motor is not under load (except for a built-in gearbox). It makes PID control very hard when I'm adjusting for a 10% drop, but the motor did not slow down.

So I guess my questions are:
1. Is it just a normal thing and hall effect sensors are just not that precise?
2. Is it caused by Raspberry Pi not being able to handle interrupts in an accurate manner?

$\endgroup$
  • $\begingroup$ Might be worth looking into a counter chip: youtube.com/watch?v=41ogu0UlwCc&t=35s $\endgroup$ – Rob Reuss Feb 15 at 17:43
  • $\begingroup$ @RobReuss thank you so much! That was it! The guy in the video even has the exact same motor (slightly different gear ratio, but otherwise identical). The issue I was experiencing was the exact same one :) $\endgroup$ – Leonti Feb 16 at 13:29
  • $\begingroup$ Cool! Have you implemented the chip yet? I've ordered equipment for my new robot and included one of those chips in case I run into the same issue. The other advantage of the chip versus just using the encoders is being able to use SPI - much better than taking a huge number of GPIO pins on my Raspberry Pi. $\endgroup$ – Rob Reuss Feb 17 at 19:11
  • $\begingroup$ I didn't have to use the chip because I already have Arduino Nano attached with serial port communication. So it counts interrupts and sends them to RPi every 100ms 🙂 $\endgroup$ – Leonti Feb 17 at 23:55
1
$\begingroup$

Thanks to @RobReus, who pointed me to a Youtube video describing my issue I was able to fix it.
The issue was in fact my Raspberry Pi not being able to keep up with the amount of interrupts a second and losing some of the events.

The solution I came up with is to attach hall sensor to my Arduino Nano which is connected to Rpi via serial port.
Interrupts are incrementing counters for left and right wheels and then send the data via serial port every 100ms to RPi.
I get consistent readings now and PID control is finally working as expected!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.