I have been working on a project in which I use a model aircraft transmitter and receiver to control servos. I am not plugging the servos directly into the receiver because I want to reverse/expo the servos without buying a new transmitter. The outputs from the receiver fluctuate a little bit and this causes the servo to be very jittery. Is there a way to round the microsecond output (1000-2000 microseconds) to the nearest 10? 1400 1410 1420 instead of 1401 1407 1417.
I tried using the round() function but I think that it is only for decimals. I also was thinking of taking the last 10 outputs from the receiver and averaging them but I am unsure how to do this.
Any help is much appreciated.
THANKS!
Rounding like that will give very jerky servos. The steps between each position will be noticeable. If it's close enough within +/-5 units, then you really won't notice that jitter in the output and it may even help you.
Averaging over time can be used. You can use an array of the last 10 values received, with an index pointing to the last one. When you get a new one, advance the index (wrap around to the beginning if required) and save the value in that position. To extract the averaged value, add up all the items in the array and divide by 10.
Another way of averaging without using so much storage is the Infinite Impulse Response (IIR) filter. The theory behind this can be quite complex but the simplest form is to store the previous (averaged) value. Each time you get a new reading, add 9/10 of the previous and 1/10 of the new value. You do need to be careful with rounding errors as the naieve way of doing this with integers will lose lots of precision.
The jitter may be a power supply problem. Always power servos with a separate supply (connecting the grounds) and budget one ampere per servo for as many servos as will move at one time.