0

I am experimenting with the accelerometer on my sunspot. I am trying to calculate the velocity by measuring the values of the accelerometer from the axis X and Y but they kind of seem random to me.

What's the proper way to get the values and the orientation/direction of the movement and to calculate its velocity?

This is my code:

while (true) {

        try {
             offset=Math.sqrt(accel.getAccelX()*accel.getAccelX()+accel.getAccelY()*accel.getAccelY());
               if(offset<0.1 && offset>-0.1)
                   offset=0;

            v=(offset+ ((offset-anterior))/2)*0.2; //0.2 is 200millisecs
            anterior=offset;
            Utils.sleep(200);//5 reads per secound
          }}...

UPDATE For example, I move the sunspot in a direction and the variable v(velocity) give me values from negative up to 7ms by random order (not sequencial). If the change the direction of the movement it doesn't give me negative values as I expected.

example:(if I move it to my right)

v =0.4771031167950723
v =0.4771031167950723
v =-0.15903437226502407
v =-0.15903437226502407
v =0.33841556285063856
v =0.33841556285063856
v =0.7397145777969039

Thanks in advance.

rgettman
  • 176,041
  • 30
  • 275
  • 357
Artur Peniche
  • 481
  • 6
  • 27
  • In what way do they seem random? You aren't really telling us what the problem is. – Jon Story Dec 09 '14 at 16:13
  • sorry, i updated the question – Artur Peniche Dec 09 '14 at 16:21
  • How often do you poll for data? You may need to take some sort of average over a set period in order to remove fluctuations within the hardware – Jon Story Dec 09 '14 at 16:22
  • iam making readings 5x per second. you mean i need to make several readings over a period of 0.2 seconds and then make the average of them to make a total of 5 averages per second? – Artur Peniche Dec 09 '14 at 16:25
  • Sample as often as you need, and combine over as long a period as necessary to produce a reasonable number: without your hardware in my hand it's hard to give you an idea of how often you should be polling, but the concept should help with some trial and error – Jon Story Dec 09 '14 at 16:27
  • Is it expected that your results are in exactly identical pairs? – Weather Vane Dec 09 '14 at 16:46
  • probably. Each set of 5 values were taken in 1s. – Artur Peniche Dec 09 '14 at 16:58
  • 3
    What is a "sunspot" (or "Sun Spot")? The tag relates to [this](http://sunspot.github.io/), which does not seem to relate to your quation. – Clifford Dec 09 '14 at 23:16
  • 1
    Velocity is the *integral* of acceleration. The sum of all acceleration samples will give velocity relative to an *unknown* initial velocity, or just velocity is you assume it is at rest initially. – Clifford Dec 09 '14 at 23:19
  • @ArturPeniche read this: http://stackoverflow.com/q/19727298/2521214 entire thread – Spektre Dec 12 '14 at 08:46

1 Answers1

0

Like @John Story stated the best way to handle this is by making an average. Sun spots accelarometers are very unstable and aren't suited for precise velocity prevision.

Anyway this is the best code i could make

double v=0;
    double anterior=0;

    while (true) {
        double time=System.currentTimeMillis()+200;
        double currentTime=System.currentTimeMillis();
        double offset=0;
        int tries=0;
        double maximo=0;
        double minimo=0;
        while(time>currentTime){ //lets run for 0.2seconds :)

            try {
                double temp=-accel.getAccelY(); //front shouln't be negative
                tries++;
                if(temp<0.1201 && temp>-0.1201){
                    tries--; //oops threadshould sample
                }else{
                    if(temp>maximo)
                        maximo=temp;
                    if(temp<minimo)
                        minimo=temp;
                    offset+=temp;
                }   
            }catch (Exception e) {
                System.err.println("Caught " + e + " while collecting/sending sensor samples.");
            }
            Utils.sleep(10); //sleep 10milisecs
            currentTime=System.currentTimeMillis();
        }
        if(tries>2)
            offset=(offset-minimo-maximo)/(tries-2); //remove max value and min value from sample and makes average
        else if(tries>0)
            offset/=2; //you wont take max or min from a two value sample
        try {
            dg.reset();         //clears
            v=anterior+offset*0.2; //vf=vi+at
            dg.writeDouble(Math.abs(v*100)); // sends in cm
            anterior=offset;    //vi=vf
            rCon.send(dg);     //sends radiogram
            Utils.sleep(200); //sleep 0.2s
        }catch (Exception e) {
            System.err.println("Caught " + e + " while sending velocity.");
        }
    }
}
Artur Peniche
  • 481
  • 6
  • 27