Go Down

Topic: calculating error, quickest way (Read 1 time) previous topic - next topic

winner10920

Well the problem is its not typed in but in/decremented with a rotary encoder, so for lower frequencies it just goes one at a time, at higher it would need to in/decre,ent by larger values, problem is that value changes, that's why I'm looking for a formula basically that mimics the curve of the error, I'm sure its simple I'm just not too well versed in math lol

robtillaart

#6
Dec 22, 2012, 08:02 pm Last Edit: Dec 23, 2012, 04:32 pm by robtillaart Reason: 1
OK make stepsize depending on the count

some partial code to get the idea
Code: [Select]

#include <math.h>   // for log10

long value = 0;
if (turnleft) value = value + stepsize(value);
if (turnright) value = value - stepsize(value);
....

long stepsize(long val)
{
 long st = 1;
 if (val > 0)
 {
   int x = max(0, log10(val)-2); // base 10 log ~ length of number
   for (int i=0; i< x; i++;) st *= 10;
 }
 return st;
}


This function step takes the value (e.g. 4567)
calculates log10(4567) = 3.6596   -2 = 1.6596  
floors this to an integer => 1.
Then the for loop multiplies the stepsize by that amount of 10, so the final stepsize = 10

step(123) => x = 0; ==>  step = 1
step(10) => x =0 (due to max) ; ==> step = 1
step(100100) => x= 3 ==> step = 1000

easier variation
Code: [Select]

long stepsize(long val)
{
 if (val >= 1000000000) return 10000000;
 if (val >= 100000000) return 1000000;
 if (val >= 10000000) return 100000;
 if (val >= 10000000) return 10000;
 if (val >= 1000000) return 1000;
 if (val >= 100000) return 100;
 if (val >= 10000) return 10;
 return 1;
}


hope this helps
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

winner10920

That's an interesting idea however it will not match the actual output because the error appears to change logarithmically(as in a graph of error vs frequency) tho I'm sure there is a linear equation because I believe when I graphed the difference between errors its linear, I just can't figure it out

dhenry

The best approach really is to use a dds.

Short of that, google "dithering".

winner10920

Yeah I wish I thought of tnat before I finished the pcb, also my budget is kinda small but anyway, how could I apply dithering ? I can't find a decent source of the actual process and how to apply it to my thing

Go Up