Background servo driver in hardware, no loop() code, no interrupts

Arduino projects on the go
MaxZ
Posts: 130
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Background servo driver in hardware, no loop() code, no interrupts

Post by MaxZ » 18 May 2019, 08:20

Phil_G wrote:
17 Feb 2018, 18:26
For anyone who wants to make an ultra simple servo tester project, here's a framework that continuously runs all the pulsing and timing in the background - the whole servo timing and pulse generation is completely isolated from any code you write in your loop() section.
Hi Phil,

I've been looking at this again. In one or two sketches I composed a while ago, I used the Servo.h library and writeMicroseconds().
I understood that this does the same thing you are describing, i.e. once you load a number generated by the main loop into the function, it will repeat the equivalent pulse output ad infinitum until you change the number again.
Can you tell me where the difference with your code example is or what the advantages are? One advantage I am able to read from your code is that you can set the length of the pulse repeat interval.

Cheers,
Max.

User avatar
Phil_G
Posts: 294
Joined: 15 Feb 2018, 23:32
Contact:

Re: Background servo driver in hardware, no loop() code, no interrupts

Post by Phil_G » 18 May 2019, 21:20

Hi Max, sorry I dont know, I've never used the servo library in any of my projects, to be honest I dont see the point in a library when its such a simple task to generate pulses in hardware Image
The idea of the post is that once set up, its pure hardware, there is no software running whatever, but
as such it can only generate one output. The servo library can do many outputs by sequentially shifting an array of values into the timer, maybe I'm missing something but to me that hardly necessitates the complexity of a library.

There are several ways to do it, one way is to set up a cyclic array to all the required servo positions, with a last element set to 20ms-(the sum of all the servo pulses). Set a timer-generated interrupt, load the timer from the 1st element of the array, set the timer to the first servo position, set the first bit and enable interrupts. When the timer expires the interrupt drops the previous output and sets the next, and loads the timer from the next 'servo' value from the array, incrementing the array pointer cyclically. Its convenient to have the servo pins in an array too. You can speed things up further by loading 'b00000001' into a register then using LSL (logical left shift) on every interrupt before outputting to the port, like a hardware shift-register.

Cheers
Phil

MaxZ
Posts: 130
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Background servo driver in hardware, no loop() code, no interrupts

Post by MaxZ » 18 May 2019, 21:47

Hi Phil,

writeMicroseconds() has, as the name suggests, a resolution of 1 us. And a range as wide as you want, until the servo hits the stops. I think the write() statement (0-180) is just a simplification.
As for the "simple task to generate pulses in hardware", not for me it is I'm afraid :?
Thanks for your reply, cheers,
Max.

MaxZ
Posts: 130
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Background servo driver in hardware, no loop() code, no interrupts

Post by MaxZ » 20 May 2019, 11:03

Hi Phil,

I may decide to use your code the next time I am getting involved in manipulating servos, but I have definitely decided not to try and understand it fully :? :D
I have done a bit of reading on the Arduino site on the subject of timers and bitwise operations. I do know the basics of bits, bytes and binary numbers, OR and AND operations (from my TTL days back in the 60's/70's) and so on, but it is just too much to get my head around. You have to make decisions what to spend your time on you know ;) .
You have described the implementation clearly enough, so I should not have any problems there :)

Cheers,
Max.

MaxZ
Posts: 130
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Background servo driver in hardware, no loop() code, no interrupts

Post by MaxZ » 07 Jul 2019, 11:13

Hi all,

Now that I have set up the Digispark programming, I started thinking of uses for it. I would like to experiment with onboard sequencers or mixers for multiple servos, so my question is will the subject code run on an Attiny85? I have a booklet about its uses (most of them useless ;) ), but ominously there is nothing in it about servo operation.

I could simply test it by loading the code on a Digispark, but then I would need to solder a servo connector on which then prevents it from being re-programmed with my current adapter. I can find ways around that, and if all fails I have a few bare Attiny85 chips that I can stick in a breadboard and rig up a programming cum test setup, but I thought I might ask first to see if it was a dead end route anyway.

Cheers,
Max.

User avatar
Phil_G
Posts: 294
Joined: 15 Feb 2018, 23:32
Contact:

Re: Background servo driver in hardware, no loop() code, no interrupts

Post by Phil_G » 07 Jul 2019, 11:59

Hi Max, unfortunately not, the ATTiny85 has no 16-bit timer so it cant do the 1uS pwm in hardware,
It does have an 8-bit timer which you could sequence (ie multiple counts) but that gets quite complex and its very difficult to get timings spot on with cascaded timers.
you could use 10uS resolution and the 8-bit timer, that would be easier, but 10uS is as large a step as you would want in an RC system. Typically 10uS equates to a 'trim pip'.
Or since all servo pulses are at least 900uS, you could use 5uS resolution and say a period made up of a fixed 850uS plus (1280uS in 256 steps), which would be quite usable. There is an ATTiny PPM library but I've not tried it, and I think theres a PWM servo library for the ATTiny85 which again I've never tried.
None of these are the equivalent of the 100% hardware idea posted though.
Cheers
Phil

MaxZ
Posts: 130
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Background servo driver in hardware, no loop() code, no interrupts

Post by MaxZ » 08 Jul 2019, 09:00

Thanks Phil. In the meantime I have located an 8-bit Servo library which claims serving up to 5 servos, with a resolution of 256 steps/8 microseconds per step: http://www.cunningturtle.com/attiny4585-servo-library/

I'll try that and report in a separate topic if it is any good.

Cheers,
Max.

Post Reply