Driving more than 4 servos

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: Driving more than 4 servos

Post by fma » Thu Jun 26, 2014 8:13 am

I would also love to use HW, but I don't have the skills to :(

I will make some tests to evaluate jitter of the above method...

Thanks.
Frédéric

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Driving more than 4 servos

Post by dhylands » Thu Jun 26, 2014 8:30 am

Look at http://forum.micropython.org/viewtopic.php?f=2&t=156 for an example of the amount of jitter caused by the 1 msec tick timer. That looks to be about 17 usec, which is about 1.7% when consider that the servo pulse width varies by about 1000 usec.

fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: Driving more than 4 servos

Post by fma » Sat Jun 28, 2014 3:34 pm

I made some tests to drive servos, using the way you described a few posts above, but it looks like it is too slow doing this in pure python, with timer interrupt callback.

So, before I dig into C-dev, and be able to to write something at low level (which won't happend anytime soon, as I will have to learn so much things!) I'm thinking to use some shift registers, driven by SPI. I have 2 questions:

- what is the requency range the SPI can use?
- any advice for shift register chips I could use?

Thanks,

PS : I'm wondering: why is the Servo object limited to 4 channels? As I can see, the STM32405RG has plenty of timers, so, the Servo object could be extended to at least 20 outputs (using other timers, of course). Is it something planed in the future?
Frédéric

User avatar
polygontwist
Posts: 36
Joined: Sat Jun 28, 2014 4:54 pm
Location: Germany, Rostock
Contact:

Re: Driving more than 4 servos

Post by polygontwist » Sat Jun 28, 2014 5:07 pm

Hello,
you can use the tlc5940nt. you can connect 16 Servos per chip. you need only 7 data-pins. You can connect multiple chips!
(search tlc5940nt+arduino)

salü

fma
Posts: 164
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: Driving more than 4 servos

Post by fma » Sun Jun 29, 2014 10:33 am

Ah, yes, I know this chip, as I buit/reprogrammed a JIXClock, a friend designed:

https://www.logre.eu/wiki/JIXClock
https://github.com/fma38/JIXoo

I also found other more servo-oriented devices:

http://www.adafruit.com/product/815
http://www.hobbytronics.co.uk/servo-controller-12ch-ht

Pololu also has a nice device, with more features:

http://www.pololu.com/product/1357

but much mor eexpansive...

One thing I will really need is the ability to drive a servo to a given position during a given delay; this is mandatory to have trajectories...
Frédéric

wagnerc4
Posts: 7
Joined: Wed Jul 01, 2015 3:48 pm

Re: Driving more than 4 servos

Post by wagnerc4 » Wed Jul 01, 2015 3:52 pm

I have microservos "sg90", and I want to build a tote cuadruped robot with the pyboard so a need 12 servos....
I can move then with this code:
from pyb import Pin, Timer
timer = Timer(2, freq=50)
ch = timer.channel(1, Timer.PWM, pin=Pin.board.X1)
ch.pulse_width_percent(8)
ch.pulse_width_percent(13)
ch.pulse_width_percent(3)

Good luck!

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Driving more than 4 servos

Post by dhylands » Wed Jul 01, 2015 4:34 pm

I find I prefer to control the timing if my timers when driving a servo.

So, if you initialize the timer like this (take from https://github.com/dhylands/upy-example ... .py#L6-L13):

Code: Select all

# For this example, we'll setup a timer in PWM mode to generate a servo pulse.
# Using a prescalar of 83 gives a timer-tick frequency of 1 MHz (84 MHz / 84).
# The period of 19999 gives a 20,000 usec or 20 msec period. The pulse width
# is then in microseconds.
servo_pin = pyb.Pin.board.X1
t5 = pyb.Timer(5, prescaler=83, period=19999);
servo = t5.channel(1, pyb.Timer.PWM, pin=servo_pin)
servo.pulse_width(1000)
Timers 2-7 and 12-14 have a source frequency of 84 MHz, so dividing by 84 gives 1 MHz tick. The other timers (1 and 8-11) have a source freq of 168 MHz, so replace 83 with 167 for those timers).

You can then specify the period of 20 msec, and when you call servo.pulse_width the number you pass in represents microseconds.

This will give you microsecond granularity on the servo. If you initialize the timer and let it choose the period and prescalar (which is what happens when you specify freq=) then you don't know the actual pulse_width granularity and if might differ depending on the timer's source frequency.

That's just my personal preference. If you prefer using percentages then by all means - continue to do so.

wagnerc4
Posts: 7
Joined: Wed Jul 01, 2015 3:48 pm

Re: Driving more than 4 servos

Post by wagnerc4 » Thu Jul 02, 2015 7:16 pm

Is better that way!
but I try on the pin x9 and I have an error

>>> from pyb import Pin, Timer
>>> t5 = Timer(5, prescaler=83, period=19999)
>>> servo = t5.channel(1, Timer.PWM, pin=Pin.board.X9)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: pin B6 doesn't have an af for TIM5

wagnerc4
Posts: 7
Joined: Wed Jul 01, 2015 3:48 pm

Re: Driving more than 4 servos

Post by wagnerc4 » Thu Jul 02, 2015 7:25 pm

Sorry, this is the code:

>>> from pyb import Pin, Timer
>>> t4 = Timer(4, prescaler=83, period=19999)
>>> servo = t4.channel(1, Timer.PWM, pin=Pin.board.X9)
>>> servo.pulse_width(1000)

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Driving more than 4 servos

Post by dhylands » Thu Jul 02, 2015 10:02 pm

Your second example works for me (Timer 4 on X9)

Post Reply