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

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

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

Post by MaxZ »

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: 596
Joined: 15 Feb 2018, 23:32
Contact:

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

Post by Phil_G »

The idea of the post is that once set up, its pure hardware, there is no software running whatever.
You could do the Paul Daniels tablecloth trick and whip out all the code from under setup() and loop() and it would continue generating servo pulses. However as a pure hardware thing it can only generate one output.

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: 330
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

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

Post by MaxZ »

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: 330
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

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

Post by MaxZ »

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: 330
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

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

Post by MaxZ »

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: 596
Joined: 15 Feb 2018, 23:32
Contact:

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

Post by Phil_G »

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: 330
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

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

Post by MaxZ »

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.
beenie
Posts: 4
Joined: 03 Nov 2020, 21:47

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

Post by beenie »

DaveL wrote: 24 Mar 2018, 11:41 I built something similar last year for an indoor model, might be of interest to you:
https://www.youtube.com/watch?v=Fz9ai16gz-k


Based on the Attiny85, it has a single button which when pressed for (say) 5 seconds will give a motor run (ramped up/down) of 20 seconds. The button also allows you to select 1-5 power levels and calibrate the esc. I was trying to do it with the minimum of components as it was destined for a small model which you can see flying at:
https://www.youtube.com/watch?v=wG9xbIG5zEU

I'd be happy to post the code if you wish, hth

Hi Dave,

Very interested in your free flight timer project, a friend has asked if I can help program something similar. Would you be willing to share your code?

many thanks

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

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

Post by Phil_G »

Carl did one using the hardware PWM generator listed here
viewtopic.php?p=1683#p1683
beenie
Posts: 4
Joined: 03 Nov 2020, 21:47

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

Post by beenie »

Thanks Phil & Carl,

I'll have a look very soon.

Cheers

Dave
Post Reply