Memsic 2125 code?


Closed Thread
Results 1 to 25 of 25

Hybrid View

  1. #1
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Memsic 2125 code?

    Hi Ed,

    If I were you, I'd use the Parallax Memsic 2125 example, as it is probably the easiest of what I have seen. It uses a lookup table and simple math to approximate the angle, with pretty good accuracy within the 0 to 45 degree range (but error will increase as roll increases, since a lookup table can only take one axis into account at a time).

    Parallax shows a table for angle/g from Memsic:
    Name:  memsic-table.png
Views: 5358
Size:  52.3 KB

    Straight from the nv92.pdf example:

    Now, we could take the easy route and use 62.35 as our conversion factor, but this would give us
    an error at low tilt angles that we really don't need to tolerate. What we'll do, then, is work
    backward from this data and determine the output from the 2125 at the angles specified in the
    chart. Then we can use LOOKDOWN to compare the 2125 output to the chart and a LOOKUP
    table to determine the proper tilt conversion factor.
    Calculating the maximum g-force output for a given range is done by dividing the arc for a range
    by its conversion factor. This will give us the maximum g-force output for that range.

    10 ÷ 57.50 = 0.17391
    20 ÷ 58.16 = 0.34387
    30 ÷ 59.05 = 0.50804
    40 ÷ 60.47 = 0.66148
    50 ÷ 62.35 = 0.80192

    Remember that the g-force output from the BASIC Stamp code is in 0.001g units, so we'll
    multiply the results above by 1000 for use in a LOOKDOWN table. Let's look at the code that
    converts g-force to tilt.

    Here is some of the code, modified only slightly for Picbasic:

    Code:
    xRaw    VAR Word ' pulse width from Memsic 2125
    xGForce VAR Word ' x axis g force (1000ths)
    xTilt   VAR Word ' x axis tilt (100ths)
    idx     VAR byte ' table index
    mult    VAR Word ' multiplier - whole part
    frac    VAR Word ' multiplier - fractional part
    HiPulse con 1    ' tells pulsin to look for highpulse, not lowpulse
    
    
    
    
    Read_X_Force:
        PULSIN PORTB.1, HiPulse, xRaw
        xRaw = xRaw * 2
        ' g = ((t1 / 0.01) - 0.5) / 12.5% ' formula from data sheet
        xGForce = ((xRaw / 10) - 500) * 8 ' Modified for our PIC - Converts to 1/1000 g's
    RETURN
    
    Read_X_Tilt:
    GOSUB Read_X_Force
        ' tilt = g x k
        '
        ' Select tilt conversion factor based on static
        ' G force. Table data derived from Memsic specs.
        LOOKDOWN2 ABS xGForce, <=[174, 344, 508, 661, 2000], idx
        ' above table are thousandths g results for 10,20,30,40 and max value degrees  
        LOOKUP idx, [57, 58, 59, 60, 62], mult          'integer lookup
        LOOKUP2 idx, [32768, 10486, 2621, 30802, 22938], frac 'gets fractional result
        ' G Force is divided by 10 to prevent roll-over errors at end
        ' of range. Tilt is returned in 100ths degrees.
        ' Parallax explains fractional lookup like this:
        ' 65,536 * .16 (this is from the fractional part of 20 degree, 58.16 number) = 10486
        xTilt = mult * (ABS xGForce / 10) + (frac ** (ABS xGForce / 10))
        Check_SignX:
        IF (xGForce.Bit15 = 0) THEN XT_Exit ' if positive, skip
        xTilt = -xTilt ' correct for g force sign
        XT_Exit:
    RETURN
    Attached Images Attached Images
    http://www.scalerobotics.com

  2. #2
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Memsic 2125 code?

    Thanks Walter! You are the best! Ed

  3. #3
    Join Date
    Mar 2011
    Location
    Los Angeles, California
    Posts
    322


    Did you find this post helpful? Yes | No

    Default Re: Memsic 2125 code?

    Hi Walter and everyone!
    Okay so I corrected the code in the Read_X_Force as the "return" caused the program to stop and the formula said to divide by 8 not multiply. With the digital scope I was able to know the exact timing of the pulses. The catch is that the devide puts out a pulse reading from 1810 to 3132 so the formula does not work. I attached word files to show the code, what the pulse measurement is at level, what the code does with level and what the code does with the device at +90 degrees (up) and -90 degrees (down). The original code uses 10 ms and the scope says it is 9.9 ms so rather than x2 I made it x20 to divide by 99. The problem with the code seems to be with the 50% duty cycle value? Please let me know what you think. Thanks, Ed


    Pulse Widths.doc
    Tilt Code.doc
    UP.doc
    Level.doc
    Down.doc

  4. #4
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default Re: Memsic 2125 code?

    Quote Originally Posted by Ramius View Post
    Okay so I corrected the code in the Read_X_Force as the "return" caused the program to stop and the formula said to divide by 8 not multiply.
    Ed, are you refering to the place in the formula where there was a /12.5% ? If so, this is simplified by *8.

    See this part of the previously attached document:
    "Since our T1 time is in microseconds and there are 1000 microseconds in a millisecond, we need
    to multiply the T2 value and 0.5 by 1000 to adjust the equation. This makes the math easy for the
    Stamp and gives us an output with a resolution of 0.001g. Finally, the divide by 12.5% part of the
    equation is converted to multiplying by eight ( 1 / 0.125 = 8 ) – we couldn't ask for a value much
    more convenient than that."
    http://www.scalerobotics.com

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts