Shop OBEX P1 Docs P2 Docs Learn Events
Using Hitachi Compass with servo motors — Parallax Forums

Using Hitachi Compass with servo motors

JohnB1988JohnB1988 Posts: 1
edited 2009-04-15 23:35 in BASIC Stamp
So this is the code that I have right now. I got this code from the Parallax website and I have been toying with it but need some help getting the right code. Basically what I'm looking to do is for my Boebot to adjust itself in order to face North as its first function and then move in that direction 1 foot (servo motors #12 and 13), grip an object with the gripper (#14), turn around in a 180 degrees (using just the servo motors, not the compass), move back to origins (1 foot) and then play a song.

I have the code for the gripper, servo motors moving 1 foot and back, and the song. However I'm having trouble using this code to get my boe bot to adjust itself. I'm using the IF/THEN command but not sure what values I can give for the angle in order for it to move. I want to break it into 8 parts. Essentially every 45 degrees. I was thinking something along the life of:

IF angle = 0-44 THEN
PULSOUT 12 and so on
ENDIF

IF angle = 45-89 THEN
same sort of idea

And I assumed I can do it like that.

the following is sort of what I have right now but its not working so I assume that my IF/THEN commands are incorrect. Was wondering what the correct commands would be.

I only have 1 servo motor working for that one and only 2 seperate if commands but I was just trying to test it out.

Thanks for the help!


' ============================================================================
' TestHm55bCompass.bs2 - This Hitachi HM55B Compass Module test program
' displays x (N/S) and y (W/E) axis measurements along with the direction the
' Compass Module is pointing, measured in degrees clockwise from north.
'
' Author.... (C) 2005 Parallax, Inc -- All Rights Reserved
' Email..... support@parallax.com
'
' {$STAMP BS2}
' {$PBASIC 2.5}
' ============================================================================

'
[noparse][[/noparse] Pins/Constants/Variables ]
DinDout PIN 6 ' P6 transceives to/from Din/Dout
Clk PIN 5 ' P5 sends pulses to HM55B's Clk
En PIN 4 ' P4 controls HM55B's /EN(ABLE)

Reset CON %0000 ' Reset command for HM55B
Measure CON %1000 ' Start measurement command
Report CON %1100 ' Get status/axis values command
Ready CON %1100 ' 11 -> Done, 00 -> no errors
NegMask CON %1111100000000000 ' For 11-bit negative to 16-bits

x VAR Word ' x-axis data
y VAR Word ' y-axis data
status VAR Nib ' Status flags
angle VAR Word ' Store angle measurement

'
[noparse][[/noparse] Main Routine ]

DO ' Main loop

GOSUB Compass_Get_Axes ' Get x, and y values

angle = x ATN -y ' Convert x and y to brads
angle = angle */ 360 ' Convert brads to degrees

IF angle = 0-180 THEN
PULSOUT 14, 500
ENDIF

IF angle = 181-359 THEN
PULSOUT 14, 1000
ENDIF

PAUSE 10 ' Debug delay for slower PCs

LOOP ' Repeat main loop




'
[noparse][[/noparse] Subroutines ]

Compass_Get_Axes: ' Compass module subroutine

HIGH En: LOW En ' Send reset command to HM55B
SHIFTOUT DinDout,clk,MSBFIRST,[noparse][[/noparse]Reset\4]

HIGH En: LOW En ' HM55B start measurement command
SHIFTOUT DinDout,clk,MSBFIRST,[noparse][[/noparse]Measure\4]
status = 0 ' Clear previous status flags

DO ' Status flag checking loop
HIGH En: LOW En ' Measurement status command
SHIFTOUT DinDout,clk,MSBFIRST,[noparse][[/noparse]Report\4]
SHIFTIN DinDout,clk,MSBPOST,[noparse][[/noparse]Status\4] ' Get Status
LOOP UNTIL status = Ready ' Exit loop when status is ready

SHIFTIN DinDout,clk,MSBPOST,[noparse][[/noparse]x\11,y\11] ' Get x & y axis values
HIGH En ' Disable module

IF (y.BIT10 = 1) THEN y = y | NegMask ' Store 11-bits as signed word
IF (x.BIT10 = 1) THEN x = x | NegMask ' Repeat for other axis

RETURN

Comments

  • JDJD Posts: 570
    edited 2009-04-15 23:35
    JohnB,

    The IF... THEN statement would need to be altered a little so it will compile. You can replace the IF..THEN with the following format to get it to compile.

    :: CURRENT CODE ::
    IF angle = 0-180 THEN
    · PULSOUT 14, 500
    ENDIF

    :: CHANGE TO ::
    IF angle >= 0 AND angle <= 180 THEN
    · PULSOUT 14, 500
    ENDIF

    Here is a link to the free download for your reference of Syntax
    BASIC Stamp Syntax & Reference Manual

    I hope this helps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Respectfully,


    Joshua Donelson
    www.parallax.com
Sign In or Register to comment.