Summary: I have been trying to write a robot tank using a Raspberry Pi Zero W in Python and am running into an issue wrt to lag. The left motor moves 100-500ms before the right motor (or vice versa), and the gap is long enough to make the motor spin 10-45 degrees to the right before moving ahead. This problem is worst when using the "Robot" class and non-existent if I use the DigitalOutputDevice to manipulate the pins.
(Edit: I've updated the code below and added the piscope picture, but based on several hours of additional experimentation, I am thinking this might be related to just the single core of the Pi Zero that limits how quickly it can turn on the pins while in competition with any other process for processor scheduling. Would be very helpful if someone with a pi zero can run the below code and confirm if they see the same latency or not. If so then I might just move to a Pi 3A+ instead, since it has more cores.)
Bare Minimum Code that I could reproduce it with (The original code had a line follower, distance sensor, etc code, but that's too long to post here, happy to post a link if anyone is interested):
from time import sleep
from gpiozero import Device, Robot
from gpiozero.pins.pigpio import PiGPIOFactory
Device.pin_factory = PiGPIOFactory(host="pi-zero-01")
robot = Robot(left=(19, 26), right=(13, 6), pwm=False)
sleep(2)
print("up pressed")
robot.forward()
sleep(0.2)
robot.stop()
I am running this in 2 ways and both are causing the same lag:
- Directly on the RPi Zero using PiGPIOFactory
- From my laptop, and Using host=""
Things I have done to rule out obvious coding or wiring issues - Both run just fine:
- Run a javascript based program on the pi zero (zero bot: https://github.com/CoretechR/ZeroBot)
- Connected the robot hardware to a Pi 4B and then run the code via my laptop
In both cases everything runs fine with no lag issues. Only when I try to use a RPi Zero W do I get that lag. Initially I assumed that by VNCing to the pi I was stressing out it's single processor, so I tried to SSH instead and finally didnt connect at all and ran it from my laptop as described above. None of this has helped.
The only thing that has helped is that I wrote a simpler program that didnt use Robot and instead used "DigitalOutputDevice"
if keyboard.up:
print("up pressed")
left_forward_pin.on()
right_forward_pin.on()
right_backward_pin.off()
left_backward_pin.off()
And surprisingly this works just fine.
Edit: Here is some proof from the piscope that shows the lag on the pi-zero:
Thanks and Regards, Basil Dsouza