1

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:

  1. Directly on the RPi Zero using PiGPIOFactory
  2. From my laptop, and Using host=""

Things I have done to rule out obvious coding or wiring issues - Both run just fine:

  1. Run a javascript based program on the pi zero (zero bot: https://github.com/CoretechR/ZeroBot)
  2. 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: Lag between enabling of the 2 pins

Thanks and Regards, Basil Dsouza

2
  • The bare minimum code does not run, syntax errors, undefined symbols. Do you have a working example?
    – joan
    Oct 22, 2021 at 15:30
  • Ah, since the code was pretty standard i didnt include the imports. Will keep that in mind for the future, but the code you pasted below does match what i have. Oct 22, 2021 at 18:22

1 Answer 1

1

I suggest you have a mechanical problem outside the scope of this site.

I have run the following code.

import time

from gpiozero import Robot
from gpiozero.pins.pigpio import PiGPIOFactory

# Creating a robot:
robot = Robot(left=(19, 26), right=(13, 6), pwm=True, pin_factory=PiGPIOFactory())

# Moving it forward
robot.forward(speed=0.9)

time.sleep(1)

robot.stop()

If you look at a scope it is clear that each engine is started at the same time.

piscope capture

7
  • Which kind of Raspberry Pi did you run this on (Zero, 4, 3, etc?) ? Also, do you need some additional hardware to do this osiliscope like output? Or can it be done in software? Oct 22, 2021 at 18:24
  • Ah actually on closer inspection it seems like you ran this on a Pi-4B - but as I mentioned, it works for me as well on Pi-4B, it only fails on a pi zero W Oct 22, 2021 at 18:25
  • Then your wiring or other configuration is wrong. The GPIO are identical.
    – joan
    Oct 22, 2021 at 19:37
  • piscope
    – joan
    Oct 22, 2021 at 19:41
  • as i described, i connected the exact same wiring to a pi-4b and everything worked. I also wrote a javascript program on pi-zero and that worked as well. Also direct pin manipulation using python works. Only when i used Robot on a pi zero it has issues as described. Oct 22, 2021 at 20:33

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.