Shop OBEX P1 Docs P2 Docs Learn Events
Propeller Object: Memsic2125 Accelerometer — Parallax Forums

Propeller Object: Memsic2125 Accelerometer

Beau SchwabeBeau Schwabe Posts: 6,566
edited 2008-11-26 19:41 in Propeller 1
An object to read a Memsic 2125 Dual axis accelerometer has been uploaded
to the Propeller object exchange library.

The Memsic 2125 Dual axis accelerometer object is written in Propeller Assembly
and uses a 'cordic' routine to convert x and y tilt information into polar coordinates.
Output is RAW data, but can easily be resolved into acceleration (ro) and degrees (theta).

Upon startup, the demo expects the accelerometer to be in a level position
for an initial calibration leveling routine.

[b]Edit[/b]: Updated the way the digits were displayed in the 'SimpleNum' routine and
      also changed the order in which the graphics text vs. other graphics were
      rendered so that now the graphics text is "on top" of any of the other graphics.


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe

IC Layout Engineer
Parallax, Inc.

Post Edited (Beau Schwabe (Parallax)) : 8/11/2006 10:54:23 PM GMT

Comments

  • cgraceycgracey Posts: 14,141
    edited 2006-08-11 06:26
    ·Beau,

    Can you make an .avi to show it graphing in real time?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-08-11 06:33
    Chip,

    At the moment, my video cables are packed in a moving box (misplaced somewhere??).... and I don't have my camera
    with me? ...Maybe we can figure out something tomorrow at work, I have a fire-wire port on my laptop. Perhaps we can
    find a fire-wire cable and a video camera.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • cgraceycgracey Posts: 14,141
    edited 2006-08-11 06:50
    No problem. We'll just use our digital still camera in .mpg mode. A ten-second video will get the point across well. Rich can make this happen quickly.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • parskoparsko Posts: 501
    edited 2006-08-11 06:54
    Wow, Beau, you must be reading my mind! I was thinking about this exact thing this morning on my ride to work (about 45minutes ago)!!! I'm going on a spirited car driving excursion in September with some collegues. I have 2 of these things in my box-o-parts I was going to wire up to log some real data! You just saved me a few weeks of frustration, thanks! I'll try this out this weekend and let you know if someone at <MY> level is able to get it going. It'll be a nice change from trying to get the HS-SPI working...

    -Parsko

    PS- It's nice to see you guys are back from vacation.· How was it?· My vacation yielded a very very special surpriseyeah.gifjumpin.gif !

    Post Edited (parsko) : 8/11/2006 6:57:57 AM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-08-11 07:07
    Parsko,

    You can thank Chip for most of the code... I had an earlier version that used the existing sine table within the Propeller to do a successive approximation reverse lookup
    to function as an ArcSine routine. When I showed Chip, he said "Ohh!!... you can do that much better with this cordic routine I have been working on". I think I will have
    to agree with his comment.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • simonlsimonl Posts: 866
    edited 2006-08-11 09:12
    Beau that's just what I need - thanks [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon
  • JavalinJavalin Posts: 892
    edited 2006-08-11 10:54
    Ditto - just about to be using the Memsic. Beau you are a star!

    James
  • David BDavid B Posts: 592
    edited 2006-08-11 16:33
    Thanks, Beau,

    What timing! I just bought a Memsic this week and started learning how to use it. It arrived this past Tuesday, and by yesterday I had a primitive spin program that would respond to the basic tilting of the board, but this code goes waaaay beyond what I had even thought of.

    Earlier this year I bought my wife a big Dobsonian telescope with a very simple mount. I'm planning to see if this Memsic can sense the vertical position of the scope well enough to help locate celestial things. Earlier I'd explored the idea of using an SX to do this same thing but got bogged down in sine calculations, and the propeller seems much more suited for this sort of project.

    I'm not sure I can level the Memsic for calibration very easily once mounted on the scope; that might need some work in my application.

    By any chance, does anyone know whether the Memsic X and Y output pulses have a fixed phase relationship - do both go high together, then each drops according to its respective measurement, or something like that, or are they completely independent?

    David
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2006-08-11 18:12
    David B said...
    I'm not sure I can level the Memsic for calibration very easily once mounted on the scope; that might need some work in my application.
    Maybe if you could do a North Star sighting, you could use your latitude as a reference angle, instead of zero.

    -Phil
  • David BDavid B Posts: 592
    edited 2006-08-11 20:57
    I was hoping that the Memsic wouldn't need any calibration; once I had it mechanically aligned with the scope tube, I hoped it would just spit out a number that was close enough to use as is.

    Last night I built a test frame that will let my propeller board with the Memsic rotate about an axis like the telescope, so maybe this weekend I'll get a chance to see how good its positioning results are using this Memsic spin object.

    The Dobsonian mount has a large flat base, and I was thinking of using a computer mouse on it to generate pulses related to azimuth changes. So that would definitely need an initial calibration. I have an analog hall effect sensor that might work to align the azimuth axis, but if it isn't reliable enough, I may have to use something like a North star shot, and then could use it to calibrate both axes.

    David
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-08-11 21:17
    Memsic Calibration:

    I don't want to confuse the issue... this "calibration" is simply to null the device into thinking that it is level.
    In software it simply takes an initial reading and uses that value in subsequent readings to subtract from.
    The so called calibration is relative to the orientation of the device upon start up... perhaps I should have
    called it "Zero Leveling scheme" or something else to make it more intuitive.



    David B.
    You said...

    By any chance, does anyone know whether the Memsic X and Y output pulses have a fixed phase relationship
    - do both go high together, then each drops according to its respective measurement

    Yes, this is exactly what happens.... The Propeller Assembly takes advantage of this and looks a both inputs
    at the same time with CTRA and CTRB rather than looking at each input one at a time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • JavalinJavalin Posts: 892
    edited 2006-08-18 18:09
    Beau
    I've been trying to modify (well re-write) your memsic object to get Acceleration X and Y instead of tilt.

    Im not sure i've got the math right.· I am basically using your ASM to get the X and Y us - so with the memsic still I get about 4900us (4.9ms)·ish - which corresponds to the 50% duty cycle for t1/t2 mentioned in Nuts and Volts no.92

    The code I am using to convert this into is as follows - memsicPinXTime is generated by the ASM code and is the clock cycles for T1:
      s:=  clkfreq                              ' Clock cycles for 1 s
      ms:= clkfreq / 1_000                      ' Clock cycles for 1 ms
      us:= clkfreq / 1_000_000                  ' Clock cycles for 1 us 
     
      repeat
          debug.str(string("X = "))
          value1 := memsicPinXTime/us
          value1 := ((value1 / 10) - 500) * 8 
          debug.decx(value1,5)
    

    The values out I get are:
    X = -00040  Y = -00088
    

    Which I take to be 0.0040g and 0.0088g (as the memic is still)

    Does this sound right?

    Thanks for your help,

    James

    Post Edited (Javalin) : 8/18/2006 8:17:31 PM GMT
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-08-18 19:07
    Javalin,
    40 and 88 seem correct, there's also going to be a nice amount of noise if you are holding the accelerometer.

    It might help to look at the two relationships when calculating X,Y,ro, or theta:
    
    coord to polar:
     
       ro = SQR(x^2 + y^2)
    theta = ATN(y / x)
     
     
     
    polar to coord:
     
    x = sin(theta) * ro
    y = cos(theta) * ro
    

    ·Here are a few Spin routines to derive Sin and Cosine...

    pub cos(angle) : x
    ''Get cosine of angle (0-8191)
        x := sin(angle + $800)
     
    pub sin(angle) : y
    ''Get sine of angle (0-8191)
        y := angle << 1 & $FFE
        if angle & $800
           y := word[noparse][[/noparse]$F000 - y]
        else
           y := word[noparse][[/noparse]$E000 + y]
        if angle & $1000
           -y
    




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • JavalinJavalin Posts: 892
    edited 2006-08-18 19:13
    Beau,

    Thanks for the help. The memsic is on a table - so showing minor noise I guess.

    For the acceleration I dont need to sin/cosine do I?

    James
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-08-18 19:23
    Javalin,

    The acceleration in any direction is given in 'ro'

    You need 'theta' to determine which direction the vector is.



    If you don't like the 'polar coordinates' (ro and theta) you can easily convert it into 'Cartesian coordinates' (x and y).
    To do this I simply provided a formula that uses Sine and Cosine to help you get started.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • JavalinJavalin Posts: 892
    edited 2006-08-18 19:28
    Beau,

    Looking at Nuts and Volts 92 - Jon Williams has the formula for the acceleration from the Memsic 2125 as:

    A (g) = (T1 / T2 -0.5) / 12.5%

    Which I was taking to mean:
    A (g) = acceleration in G
    T1 / T2 = the high and low pulse time

    So I am confused as to the trig?

    James
  • JavalinJavalin Posts: 892
    edited 2006-08-18 19:48
    Beau

    Sorry just realized what you mean. Your ASM code generates the "ro" variable.

    Dont worry - I think based on your first comment that its ok.

    Cheers for the help,

    James
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2006-08-18 21:42
    Javalin,


    In reference to my original code, the output is only provided in polar coordinates.


    When I wrote the Memsic object, I looked very closely at Jon's BS2 code, and where I
    saw that he was using a lookup table for ARCSIN calculations, I decided not to use a
    lookup table simply because the Propeller has the overhead capability and bit-resolution
    to do this properly.· Not that Jon's code is wrong, it has more to do with the
    limitations of the BS2.


    That said, Your correct ....

    g = (T1 / T2 -0.5) / 12.5%


    T2 is calibrated to 10mS @ at room temperature.


    Converting this to BS2 code...

    g = ( ( T1 / 10 ) - 500 ) * 8


    So Basically 'g' becomes a scaling function of 'T1'.· Knowing that and looking at
    the 'polar coordinate' function for angle, it does not matter what units we use for
    X and Y as long as they are the same ... We still get the correct angle... therefore
    there is really no reason to do any scaling within the assembly program.

    polar coordinate:

    angle = ATN( y / x )


    ...We do however want to scale 'ro' against the clkfrequency but we can do this at
    the Spin level.

    From Spin we want to calculate the number of clock ticks that are in 1uS

    ticks = clkfreq / 1_000_000

    ...At 80MHz 'ticks' this value is equal to '80'


    The Sensitivity of the Memsic2125 is 12.5%/g

    Since our measurements are in mg· (1000mg = 1g) ; 12.5% of 1000 = 125

    Sensitivity = 125


    At 0g our accelerometer produces a 50% duty cycle and a pulsewidth equal to 5000uS

    duty = .5··· or··· duty = Pulsewidth / ( ticks * Sensitivity )

    PulseWidth = 5000


    So now we have enough information to calculate the 'clk_scale' I used in the demo...


    clk_scale = ticks * duty



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 8/19/2006 4:35:27 PM GMT
  • JavalinJavalin Posts: 892
    edited 2006-08-19 09:27
    Beau,

    Thats excellent - makes it much more clear. Thanks,

    James
  • Paul_HPaul_H Posts: 85
    edited 2008-11-26 18:02
    Happy Thanksgiving everyone,

    I am running into an application wall so any help is appreciated - I am trying to use the memsic 2125 to point a tracking antenna, much like David B's telescope example above. I am having a bit of difficulty understanding how the accelerometer can give what appears to be an absolute angle value...

    I only need to measure tilt as I am using a digital compass to determine the rotation position. Theta is the polar coordinate angle. If I am limiting myself to one axis, will theta be off if I rotate (compass based direction) then do a tilt measurement sequentially? I think the object reads both X and Y to calculate theta...

    Thanks for any insights or examples,

    Paul
  • Beau SchwabeBeau Schwabe Posts: 6,566
    edited 2008-11-26 19:41
    Paul_H,

    Yes, the object does calculate theta from X and Y by way of Cartesian coordinate to Polar coordinate conversion. I'm sorry, I'm not clear on your other question.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
Sign In or Register to comment.