Shop OBEX P1 Docs P2 Docs Learn Events
How to calculate gravity and degree of angle from MEMSIC2125 with C Language? — Parallax Forums

How to calculate gravity and degree of angle from MEMSIC2125 with C Language?

mmc01mmc01 Posts: 1
edited 2012-06-23 03:37 in Accessories
I can measure ment pulse width with C Languge. But After that I don't know how to calculate it like Basic stamp http://www.parallax.com/dl/docs/prod/acc/memsickit.pdf .
This is pulse width which I can read from MEMSIC2125.

5250 uSec
5060 uSec
5080 uSec
5080 uSec
5070 uSec

I calculate like this formula.

xmG = ((xRaw / 10) - 500) * 8

such as. xmG = ((5250 / 10) - 500) * 8
xmG = 200 ' calc 1/1000 g
= 0.2 'g


If I calculate Tilt = ARCSIN(g) .
= arcsin(0.2)
= 11.536

I don't know it correct or not correct Tilt and gravity value.

How to calculate gravity and degree of angle ?

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2012-06-23 03:37
    The C code would look something like this.
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        int xRaw, xmG;
        double gforce;
        double tilt;
    
        xRaw = 5250;
        xmG = ((xRaw/10) - 500) * 8;
        gforce = ((double)xmG) / 1000.0;
        tilt = asin(gforce) * 180.0 / 3.141592;
    
        printf("tilt = %lf\n", tilt);
    
        return 0;
    }
    
    I ran this on my PC and it produced the same result of 11.536. The C routines compute angles in radians instead of degrees, so you need to do the conversion if you want degrees. I tried compiling this with PropGCC, and the default LMM code does not fit in the 32K hub RAM. I was able to build it using the XMMC model, but I didn't try to trun it. There may be some ways to get this to fit using the LMM model, but I don't know if that's possible with the floating point math routines linked in.
Sign In or Register to comment.