Exponential control

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

Exponential control

Post by MaxZ »

Hi all,

I am looking at building a simple proportional transmitter. I want to be able to add a degree of exponential control, so I have searched the net for a formula, and found a thread on RCG on the subject. Now, much of that discussion centered on the objective of creating a true exponential curve, which is not necessarily what i am looking for. However, I found a simple formula: y=k*POWER(x;3)+(1-k)*x , where k is the expo factor 0=<k=<1, and x runs from 0 to 1 .
I created a graph in Excel, but was not happy with the result. So I increased the power argument to 5, and that looked much better. A near linear curve up to about x=0.5, then swinging up to y=1 at x=1.
It will be easy enough to program this formula in Arduino code, but the POWER function only accepts floating point numbers as arguments, and delivers the result as a double precision floating point number. The Arduino site warns for longer processing times when using floats, but I have no idea how much longer. Hence my question, is it worthwhile to go this route or will it seriously disturb the typical channel value calculations in a proportional control sketch?

I seem to remember that Phil mentioned exponential somewhere in one of his topics, but I could not find it again.

Cheers,
Max.
Last edited by MaxZ on 10 Jun 2019, 08:12, edited 1 time in total.
Martin
Posts: 744
Joined: 16 Feb 2018, 14:11
Location: Warwickshire

Re: Exponential control

Post by Martin »

I would stick with integers for speed.

Approximate the shape of the expo curve you want as a series of straight lines - if you use, say, five points to define the 'curve' that should be okay, but you could use more - ten points or even a hundred if you want - the only drawback is that it will need more space (probably in RAM) to hold the table of points.

Then the Arduino can do linear interpolation between the defined points - you can do this with a single multiply instruction, a bit-shift (divide by power of two) and add - these are all integer operations that execute real quick.

You could still store a single 'expo' value and have the Arduino calculate the look-up table of points for you: it could even use floating point for the calculations as these calculations would only ever need to be performed at start-up or just once each time the user edits the expo amount.
MaxZ
Posts: 330
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Exponential control

Post by MaxZ »

Hi guys, thanks for the replies. I'd better wait for a professional solution from Mike/Phil then. But just to exercise and hopefully improve on my Arduino skills I would also like to give it a go.
Martin wrote: 09 Jun 2019, 19:33Then the Arduino can do linear interpolation between the defined points - you can do this with a single multiply instruction, a bit-shift (divide by power of two) and add -
Martin, I know how to do a linear interpolation, but I don't understand the need for a bit shift operation. My approach would be to divide the X-range into say 10 equal sections, calculate the Y-coordinates for each point, and divide the difference of each set of 2 by the ratio of the X-coordinate steps. No dividing by power of two involved?

Cheers,
Max.
User avatar
Mike_K
Posts: 669
Joined: 16 Feb 2018, 06:35
Location: Hertfordshire

Re: Exponential control

Post by Mike_K »

Hi Max

I'm 99.9% certain that the formula you found is that used by Futaba, JR, Spektrum et al (obviously allowing for the different expo signs, Futaba uses negative expo and JR/Spektrum uses positive expo to soften the response around neutral).

output = input - (expo * input) + (expo * input^3)
where input and output and expo are in the range +/-1

A few years back I rigged up my Futaba T9C (FF9) and JR DSX11 into a Meccano frame with a thumbscrew onto the joystick where I could move the joystick by small increments and hold it there. I then measured the output from my receiver with no expo, 10%, 20% and 50% (but the percentages are not really relevant) and moved the joystick between 0 and full in increments of approximately 10%. I then plotted the input (output with no rates or expo) against the outputs with expo. The theoretical curves and what I'd measured were as near a perfect match as you could get for both the Futaba and JR.

Prof Dave Burton reverse engineered it and published it under his pseudonym of BEB (Biggles Elder Brother) on the RCM&E forum. He later wrote an article about expo in RCM&E but didn't mention the expo formula. I then wrote to RCM&E to inform Prof Dave Burton about this formula, not knowing Dave was BEB. I had a charming response from him explaining that he did know of the formula and indeed that it was he who had reversed engineered it and written about it (as BEB) but considered it beyond what a normal reader would find interesting in RCM&E. I guess I'm not normal.

The formula is easy to implement using long integers, no need for floating point maths. I've helped Phil to replace the expo in his 7 channel encoder, his former expo used float variables and actual expo. I'll leave Phil to share his updated encoder when he is ready.

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

Re: Exponential control

Post by MaxZ »

I must confess that I do not really use exponential when flying my models, my interest stems from the "requirements" of a mate who is also interested in a diy tranny.
I had a look at my Multiplex transmitter's graphic display of the expo setting, and it looks more in line with the ^3 formula, not like the ^5 formula I favoured in my earlier post. I considered the use of expo as a "pseudo dual rate", where the first 50% of stick would generate a near linear response (and the dual rate factor is the complement of the expo factor), but it appears that is not what my Tx does. And Multiplex matches Futaba in using a negative expo sign for softened response around zero input.

I made a start with arduino to calculate 10 reference points as setup, using floats. It does take a while to process those few lines... :shock:

Cheers,
Max.

^3 vs. ^5
Schermafbeelding 2019-06-10 om 13.22.18.png
Schermafbeelding 2019-06-10 om 13.21.45.png
Last edited by MaxZ on 10 Jun 2019, 12:41, edited 2 times in total.
MaxZ
Posts: 330
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Exponential control

Post by MaxZ »

In fact, ^2 is not so bad either, this one at expo = 0.8 :
Schermafbeelding 2019-06-10 om 13.46.44.png
MaxZ
Posts: 330
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Exponential control

Post by MaxZ »

Phil_G wrote: 10 Jun 2019, 12:28 Another point to remember is that expo reduces the effect of mechanical trims :D
But I am sure you can compensate for that with digital trims, Phil :D
MaxZ
Posts: 330
Joined: 31 Jan 2019, 11:48
Location: Boskoop, Netherlands

Re: Exponential control

Post by MaxZ »

I have tried to calculate the output of an exponential algorithm with arduino. In order to avoid floats, I used long integers with an input x between -512 and 512, aiming at an output y equally from -512 to 512.
I used an exponential factor between 0 and 15. I tried to use bit shifting for divisions, but I must be doing something wrong, since the outcome is not what one would expect.
The expo factor is 16 times the 0-1 factor in the formula, so both terms have to be divided by 16, i.e. >>4.
The first term becomes (expo/16)*(x/512)^3, the second term is ((16-expo)/16)*(x/512). Both terms have to be multiplied by 512 to get the required range for y. Together, the divisions of the first term by 16 and three times by 512, and the multiplication by 512 again result in a bit shift of 22 to the right. In the second term, the division by 512 and the multiplication by the same number negate each other, so there is only the >>4.
This is the code I used, where did I go wrong? I think I am overrunning a limit somewhere, but how to avoid that?
Schermafbeelding 2019-06-11 om 20.27.58.png
Cheers,
Max.
User avatar
Mike_K
Posts: 669
Joined: 16 Feb 2018, 06:35
Location: Hertfordshire

Re: Exponential control

Post by Mike_K »

Max

If you want to build a proportional transmitter why re-invent the wheel? A few years back Phil_G kindly shared his 7 channel encoder that has since been built in the hundreds. It is flight proven, so if you build it and it doesn't work, you know it is a problem with how you've wired it up not the code.

viewtopic.php?f=7&t=4

It implements expo in a different way using float variables and actual mathematical exponential, but the results are very similar (albeit taking more memory). And it doesn't have to have 7 channels, if built with less channels then just ground the unused analogue inputs.

Finally if you use Phil's code and modify it, then you still need to credit Phil for the code he's written. I've seen others on this forum use Phil's code as the basis for their "own" idea, and then shared it as their own without crediting Phil for the 90% of the code that he wrote. Which in my view isn't very polite.

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

Re: Exponential control

Post by MaxZ »

Mike,

Yeah, I figured it was a nice exercise in arduino programming, but I did already conclude that it is too ambitious for me. It's giving me headaches and is keeping me awake at night, and that cannot be a good thing. :shock:
Looks like I was just shifting bits into oblivion....... ;)
From Phil's remarks on you providing him with an exponential algorithm I concluded (wrongly) that he did not had that included in his designs yet. I'll have a look at the 7 channel encoder.

As for the credits: I fully agree with you, and I am very careful to keep those intact when I modify code developed by others. As I did with your S/C code, documenting every change I made to it.

Cheers,
Max.
Phil_G wrote: 11 Jun 2019, 22:21 Maybe you need some long casts in there?

P.S. Phil, what did you mean by "long casts", too much calculation in a single statement?
Post Reply