I would like to know if someone else experienced the same problem with their servo motor combined with ultrasonic sensors on a Raspberry Pi (I own a 3B).
Essentially, I have mounted two ultrasonic sensors (HC-SR04) with 180° apart from each other on top of a servo motor (HS-422) to act as a radar.
Here's a sample code. The Servo and Sonic classes were made by me. They are fully functional and they use Joan's pigpio.
import pigpio, time
from Files.servo import Servo
from Files.sonic import Sonic
pi = pigpio.pi()
servoMotor = Servo(name = 'Test', gpio_list = [14], pi)
sensor = Sonic(name = 'Test2', trig = 2, echo = 3, pi)
servoMotor.rotate(0)
time.sleep(1)
try:
while True:
for i in range(181):
servoMotor.rotate(angleR = i)
time.sleep(0.01)
print(i, sensor.distance())
for i in range(181):
servoMtor.rotate(angleR = 180 - i)
time.sleep(0.01)
print((180 - i), sensor.distance())
except KeyboardInterrupt:
pi.stop()
The problem: When I make the servo rotate angle by angle (180 times), everything works just fine. When I instance my ultrasonic sensors along with the servo and return their distance()
, everything starts to lag, and the servo moves really slowly.
I tried to print the distances to see if it was a servo jitter problem, but my suspicion was confirmed: it's all lagging.
As if the RPi was too slow.
Sometimes, I get a moment of "normal" speed for like 20° and then it slows down again. This is what confirmed to me it isn't a gpio
library problem nor a sensor problem.
Is there a way out of this? Is the RPi really lagging?