Pi Servo Hat is Slow

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
lbsa_drexel
 
Posts: 31
Joined: Thu Oct 05, 2017 10:54 am

Pi Servo Hat is Slow

Post by lbsa_drexel »

Hello,

I am using the hat to control 14 servos and it seems that the kit.servo function is slow. I am using a pi 4 8gb. I have different arrays for each servo and a for loop that reads through each one and sends the position to the appropriate servo (code below is an example of how the other code is written). I designed a motion that should take 2.5 seconds and I wanted to run it at 100hz, so there are 250 data points in each array. It takes the code 0.14s to run through when only sending to one servo and then 0.4s for 3 servos. When I do the same thing for 14 servos it takes 2.47s. 250/2.47=101hz. I am approaching the point where I am reading at the limit and my delays are actually causing the code to run long (see below other code). Is there a way to speed this process up? Otherwise I will have to lower my resolution.

Code: Select all

from adafruit_servokit import ServoKit
import time

kit = ServoKit(channels=16)
kit.servo[0].actuation_range = 140 
kit.servo[1].actuation_range = 140 
kit.servo[2].actuation_range = 140 

val1 = [40,43.4,46.7,50,53.3,56.5,59.7,62.8,65.9,68.9,71.9,74.8,77.6,80.4,83,85.7,88.2,90.7,93.1,95.4,97.7,99.8,101.9,103.8,105.7,107.5,109.2,110.8,112.2,113.6,114.9,116,117.1,118,118.8,119.5,120,120.5,120.8,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,120.9,120.1,118.5,116.2,113.5,110.3,106.8,103.2,99.6,96.1,92.9,90.1,87.7,86.1,85.1,85,85,85,85,85,84.7,82.2,77.9,72.2,65.7,59,52.5,46.8,42.6,40.3,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40]
val2 = [40,43.4,46.7,50,53.3,56.5,59.7,62.8,65.9,68.9,71.9,74.8,77.6,80.4,83,85.7,88.2,90.7,93.1,95.4,97.7,99.8,101.9,103.8,105.7,107.5,109.2,110.8,112.2,113.6,114.9,116,117.1,118,118.8,119.5,120,120.5,120.8,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,120.9,120.1,118.5,116.2,113.5,110.3,106.8,103.2,99.6,96.1,92.9,90.1,87.7,86.1,85.1,85,85,85,85,85,84.7,82.2,77.9,72.2,65.7,59,52.5,46.8,42.6,40.3,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40]
val3 = [40,43.4,46.7,50,53.3,56.5,59.7,62.8,65.9,68.9,71.9,74.8,77.6,80.4,83,85.7,88.2,90.7,93.1,95.4,97.7,99.8,101.9,103.8,105.7,107.5,109.2,110.8,112.2,113.6,114.9,116,117.1,118,118.8,119.5,120,120.5,120.8,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,120.9,120.1,118.5,116.2,113.5,110.3,106.8,103.2,99.6,96.1,92.9,90.1,87.7,86.1,85.1,85,85,85,85,85,84.7,82.2,77.9,72.2,65.7,59,52.5,46.8,42.6,40.3,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40]

starttime = time.time()
reps=1
for x in range(reps):
	for (num1, num2, num3) in zip(val1, val2, val3):
		tiktokS = time.time()
		kit.servo[0].angle = num1
		kit.servo[1].angle = num2
		kit.servo[2].angle = num3
		

		tiktokE = time.time()

endtime = time.time()
print(endtime - starttime) #measure time to make sure matches desired run time 
print("done")

Code: Select all

	
		if((tiktokE-tiktokS)<0.01): 
			time.sleep(0.01- (tiktokE-tiktokS)) 

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Pi Servo Hat is Slow

Post by mikeysklar »

Interesting scaling issue.

I've seen threads being used in a Pi forum post to speed up access:

https://forums.raspberrypi.com/viewtopic.php?t=308026

You could also setup cProfile to get an idea where the code is spending so much additional time with the additional servos.

https://stackoverflow.com/questions/582 ... hon-script

User avatar
lbsa_drexel
 
Posts: 31
Joined: Thu Oct 05, 2017 10:54 am

Re: Pi Servo Hat is Slow

Post by lbsa_drexel »

I'll take a look at this. Thanks!

I still want to know what an Adafruit team member thinks of the issue though, so if they see this please let me know.

User avatar
lbsa_drexel
 
Posts: 31
Joined: Thu Oct 05, 2017 10:54 am

Re: Pi Servo Hat is Slow

Post by lbsa_drexel »

Any Adafruit team members have any suggestions?

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”