Shop OBEX P1 Docs P2 Docs Learn Events
MDX2125 Accelerometer — Parallax Forums

MDX2125 Accelerometer

SELSEL Posts: 80
edited 2010-05-08 19:37 in Accessories
I need to scale x and y in degrees.

I am using MXD125 Simple Object in my code.

The nick name is: accel.

some of the code:

us := clkfreq/1_000_000 (microseconds)

ax := accel.x / us
ay := accel.y /us

I need to scan ax and ay to degrees.

I have done all the experiments in "Smart Senors & Applications". The scaling method used there I can not convert to spin. I am just too new at using spin.

Can anyone help?

Thanks.
Stan

Comments

  • FranklinFranklin Posts: 4,747
    edited 2010-05-08 03:57
    What would the math look like to do that? Not code, just the math.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • Beau SchwabeBeau Schwabe Posts: 6,557
    edited 2010-05-08 06:36
    SEL,

    If you are only looking at one axis (X or Y) then you will only be able to calculate the tilt angle which is a little different than degrees. On the other hand if you combine the X and Y and convert from Cartesian coordinates to Polar coordinates you can derive the degree as a vector with a radius value.

    I am away from my desk until Monday, so I won't be able to offer any code, but the basic principle can be found below...

    [b]convert Cartesian coordinates  to Polar coordinates:[/b]
    r = (x^2 + y^2)^1/2
    θ = atan(y/x)
    
    [b]convert Polar coordinates  to Cartesian coordinates:[/b]
    x = r cos(θ)
    y = r sin(θ)
    
    



    where:
    r = distance from origin to the point
    x = Cartesian x-coordinate
    y = Cartesian y-coordinate
    θ = angle relative to the zero axis (degrees)
    
    




    For measuring tilt, the same formula above would be used as in the Cartesian coordinates to Polar coordinates, but the opposite axis would be fixed to a value equal to the neutral or resting value of the active axis.

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

    IC Layout Engineer
    Parallax, Inc.
  • SELSEL Posts: 80
    edited 2010-05-08 17:25
    Beau Schwabe,

    I miss stated what I wanted. I should have said I need the tilt angle of X and Y. In "Smart Sensor and Applications. They scale tilt angle: -127 0 127.
    if X and Y are level then 0 tilt angle should be displayed.

    I am sorry I was just to thick to express what I needed.

    I hope you will still help me?

    With respect,
    SEL

    Here is the code I am using:

    CON

    _clkmode = xtal1 + pll16x ' Crystal and PLL settings.
    _xinfreq = 5_000_000 ' 5 MHz crystal x 16 = 80 MHz

    LCD_Pin = 7 ' I/O Pin For LCD
    LCD_Baud = 19_200 ' LCD Baud Rate
    LCD_Lines = 2 ' Parallax 2X16 Serial LCD (#27976)

    OBJ

    accel : "MXD2125 Simple" ' Memsic 2125 object
    LCD : "Debug_Lcd" ' LCD object

    PUB go | xTilt, yTilt, us

    accel.Start(0, 1) ' x-axis to P26, y-axis to P27

    LCD.init(LCD_Pin, LCD_Baud, LCD_Lines) ' Initialize LCD Object
    LCD.cursor(0) ' Turn Off Cursor
    LCD.backlight(true) ' Turn On Backlight
    LCD.cls ' Clear Display

    us := clkfreq/1_000_000 ' Clock tics in a microsecond

    repeat

    ' Get microsecond measurements for the x and y axes.
    xTilt := accel.x / us
    yTilt := accel.y / us

    ' Display measurement at 10 Hz
    LCD.home
    LCD.Str(String("xTilt= "))
    LCD.Dec(xTilt)
    LCD.gotoxy(0,1)
    LCD.Str(String("yTilt = "))
    LCD.dec(yTilt)
    waitcnt(clkfreq/10 + cnt)
  • Beau SchwabeBeau Schwabe Posts: 6,557
    edited 2010-05-08 18:42
    SEL,

    "I hope you will still help me?" - I will, but it won't be until Monday before I can really test anything, so my answers will be conservative.


    In the OBEX I have posted a DEMO that uses the MX2125 and outputs to a TV via the Propeller DEMO board.
    http://obex.parallax.com/objects/140/
    Here is a You-Tube video of that object in action...
    http://www.youtube.com/watch?v=ef7ZAb5fOv8


    Within the Memsic2125_v1.2.Spin file, there is a little bit of scaling that takes place that depends on the clock frequency you are using. Typically at 80MHz the Memsic2125 will return a RAW value of about 400 thousand when it is at rest. This translates to 1/2 of the period of the Memsic 2125 running at 100Hz (Page #2 ---> Memsic2125 v2.0.pdf) ... or a 5ms pulse. At 80MHz, you can count 400 thousand clock pulses within 5ms.

    ...Anyway back to the scaling....

    These values are calculated once and applied later to derive the X-axis tilt and the Y-axis tilt values

    offset := 90 * (clkfreq / 200) 'offset value for Tilt conversion
    scale := clkfreq / 800 'scale value for Tilt conversion

    '

    For X-Axis tilt:
    xTilt := 180 - ( ( (_xraw * 90) - offset) / scale )

    For Y-Axis tilt:
    yTilt := 180 - ( ( (_yraw * 90) - offset) / scale )


    The results of xTilt and yTilt are in degrees, but for many of the Propeller based sine/cosine functions you will need to convert the Deg angle into a 13-bit angle. To do that, multiply the Deg (Tilt) value by 1024 and then divide by 45.

    For example if the Deg is 33 ... 33 x 1024 = 33792 ... 33792 / 45 = 750 ... so the corresponding position in the sine lookup table for 33 Deg would be at an offset location of 750.

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

    IC Layout Engineer
    Parallax, Inc.

    Post Edited (Beau Schwabe (Parallax)) : 5/8/2010 6:47:43 PM GMT
  • SELSEL Posts: 80
    edited 2010-05-08 19:37
    Beau Schwabe,

    using the code below the xTilt at rest = 40048
    yTilt at rest = 38228

    I need to scale the above so that x and y are 0 and increase as slop increase and decrease as the slob decrease.
    I am trying to develop this code for my Stingray robot. When it is going up a hill it will increase torque accordingly and decrease torque when going down hill accordingly.

    Code:

    {{
    ******************************************************
    * File: Test Memisic2125_v1.2.spin *
    * Author: Stan Lindo *
    * Start: 05/08/2010 *
    * Modified: *
    * Description: Get X and Y axis and display on LCD. *
    ******************************************************
    }}

    CON

    _clkmode = xtal1 + pll16x ' Crystal and PLL settings.
    _xinfreq = 5_000_000 ' 5 MHz crystal x 16 = 80 MHz

    LCD_Pin = 7 ' I/O Pin For LCD
    LCD_Baud = 19_200 ' LCD Baud Rate
    LCD_Lines = 2 ' Parallax 2X16 Serial LCD (#27976)


    MMx = 0 'Memsic2125 X channel
    MMy = 1 'Memsic2125 Y channel


    OBJ

    MM2125 : "Memsic2125_v1.2" ' Memsic 2125 object
    LCD : "Debug_Lcd" ' LCD object

    PUB go | xTilt, yTilt

    LCD.init(LCD_Pin, LCD_Baud, LCD_Lines) ' Initialize LCD Object
    LCD.cursor(0) ' Turn Off Cursor
    LCD.backlight(true) ' Turn On Backlight
    LCD.cls ' Clear Display

    MM2125.start(MMx, MMy) ' Initialize Mx2125

    repeat
    xTilt := 180-MM2125.MxTilt ' Get xTilt Deg
    xTilt := (xTilt *1024)/45 ' Convert Deg to 13-Bit Angle
    yTilt := 180-MM2125.MyTilt ' Get yTilt Deg
    yTilt := (yTilt *1024)/45 ' Convert Deg to 13-Bit Angle

    ' Display measurement at 10 Hz
    LCD.home
    LCD.str(String("xTilt = "))
    LCD.dec(xTilt)
    LCD.gotoxy(0,1)
    LCD.Str(String("yTilt = "))
    LCD.dec(yTilt)
    waitcnt(clkfreq/10 + cnt)
Sign In or Register to comment.