Shop OBEX P1 Docs P2 Docs Learn Events
servo position — Parallax Forums

servo position

BoneyardBoneyard Posts: 10
edited 2004-09-21 22:46 in BASIC Stamp
Im using a track ball to to control a servo, Im having one problem , every time I stop the ball movement the servo returns to the end position. How can I have the servo keep its position
until·I move the track ball again?
·

Comments

  • WhelzornWhelzorn Posts: 256
    edited 2004-08-20 21:50
    yeah, I can see how that would be a problem, and why. I believe what you would need to do is continuously send the pulse of whatever position you want the servo to stay in until you move the track ball. So, if the track ball was in a position where it was sending, say, PULSOUT 700 on whatever pin, you would need to keep sending that pulse to the servo until the trackball is moved.
    Hope this helps!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-08-20 23:11
    Without seeing you code, it's hard to tell why this is happening on your setup.· However, presumably you have a variable set to the PULSOUT for the servo.· When you move the trackball one way it should be adding to this value, and the other way should be subtracting from it, while keeping within the limits of the range of PULSOUT for you BS model, which I also don't know.· Ya know?

    turn.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    ·
  • BoneyardBoneyard Posts: 10
    edited 2004-08-21 02:20
    thanx for the input heres my code

    '{$STAMP BS2}

    'P0 to cs
    'P1 to Data
    'P2 to Clock
    'P7 to servo

    cs VAR OUT0········· 'chip select line
    clock VAR OUT1······ 'chip clock line
    Do VAR IN2·········· 'Chip data input line
    Servo VAR OUT15········· 'servo output data
    mousex VAR Bit······ 'mouse input value
    mousey VAR Byte······ 'mouse clock line
    pass VAR Bit········ 'loop counter variable
    servotime VAR Word· 'output time for servo




    programtop:
    DIRL=%1011··········· ' set low byte of IO direction
    DIRH=11111

    mousey=1
    cs=1················· 'set chip select nominal state
    clock=1·············· 'set clock nominal state to reset chip

    DEBUG "starting"
    '*****************************************
    looptop:
    '········· start a loop that reads the data from the mouse
    clock=0
    pass=0
    mousey=1
    cs=0


    '·········· clock the data in
    getmousey:
    mousex=mousex <<1······ 'shift the data bits in the value variable
    PULSOUT 1,2············ 'strobe the clock bit
    mousex=do·············· 'get the data and stuff it into a bit var
    mousex=mousex········· 'or the bit var with the value variable
    pass=pass+1············ 'increment the bit counter
    IF pass >7 THEN getmousey··· 'if not 8 bits done do again






    cs=1························· 'end read with chip select

    '······ the data is now read into the
    '······ mouse variable and can be used for
    '······ other things

    '*****************************************
    '······ start a servo output time calculation
    '······ based on the read data
    servotime = mousex * 5
    PULSOUT 15, 1500-1··· 'send the pulse to the servo on pin 15
    PULSOUT 15,servotime





    pulsout 6,500············ 'send a dummy pulse on pin 6 (unused)
    ······················ ' to slow the loop down for the servo
    goto looptop
    goto programtop
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-08-21 15:20
    Okay, I'm going to nit-pick your code a bit, just because there aren't too many things I can help you with until it's cleaned up a bit...These are a few things you need to look over...They may be problems, and they may not be...
    • In the comments at the beginning of your code you notate that P7 is going to the servo, but in your declarations you specify P15.
    • In your delcarations you specify a variable for "Do" which is a PBASIC keyword, so I'm not sure if this will cause a problem or not.· I haven't tried the code to see...
    • You specify a variable size of Bit for the mousex, while the mousey has a Byte size.· A Bit variable can only hold a 1 or a 0.
    • Same with your pass variable (loop counter variable)...As a Bit it won't be counting anything.
    • Where you set the DIRL and DIRH you specify a Binary value for DIRL, but a Decimal value for DIRH, making me wonder if this was intentional, since it's inconsistent (And since you used all ones, although five, not four).
    • In the getmousey: code, you're shifting bits within a single bit variable, which is probably related to my above comment.
    • Right after that you have:· mousex = mousex, which does nothing.
    • Next you're testing the pass variable for >7 but it can't be any greater than 1 do to above.

    That's just the few I got from giving it the once over...I would address these issues first.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris


    Post Edited (Chris Savage) : 8/21/2004 5:50:28 PM GMT
  • BoneyardBoneyard Posts: 10
    edited 2004-08-21 22:16
    you are correct· about picking at the code i have been making so may changes to it

    i have not cleaned it up

    yes p7 should be 15

    im chechking the rest ill clean it up and re submit.
  • BoneyardBoneyard Posts: 10
    edited 2004-08-22 16:07
    I have cleaned up the code .
    the mousex=mousx line i cant explain but if I remove it the the servo will not turn.
    i only use the mouse clock line to make this work.
    and it does work well, just cant hold the position when i stop moving the track ball


    '{$STAMP BS2}

    'P0 to cs
    'P1 to Data
    'P2 to Clock
    'P15 to servo

    cs VAR OUT0········· 'chip select line
    clock VAR OUT1······ 'chip clock line
    Do VAR IN2·········· 'Chip data input line
    Servo VAR OUT15········· 'servo output data
    mousex VAR Bit······ 'mouse input value
    mousey VAR Byte······ 'mouse clock line
    pass VAR Bit········ 'loop counter variable
    servotime VAR Word· 'output time for servo
    '******************************************************
    programtop:
    DIRL=%1011··········· ' set low byte of IO direction


    mousey=1
    cs=1················· 'set chip select nominal state
    clock=1·············· 'set clock nominal state to reset chip

    DEBUG "starting"
    '*****************************************
    looptop:
    '········· start a loop that reads the data from the mouse
    clock=0
    pass=0
    mousey=1
    cs=0
    '·········· clock the data in
    getmousey:
    mousex=mousex <<1······ 'shift the data bits in the value variable
    PULSOUT 1,2············ 'strobe the clock bit
    mousex=do·············· 'get the data and stuff it into a bit var
    mousex=mousex········· 'or the bit var with the value variable
    pass=pass+1············ 'increment the bit counter
    IF pass >7 THEN getmousey··· 'if not 8 bits done do again

    cs=1························· 'end read with chip select

    '······ the data is now read into the
    '······ mouse variable and can be used for
    '······ other things

    '*****************************************
    '······ start a servo output time calculation
    '······ based on the read data
    servotime = mousex * 5
    PULSOUT 15, 1500-1··· 'send the pulse to the servo on pin 15
    PULSOUT 15,servotime

    PULSOUT 6,500············ 'send a dummy pulse on pin 6 (unused)
    ······················ ' to slow the loop down for the servo
    GOTO looptop
    GOTO programtop





    ·
  • Don BuczynskiDon Buczynski Posts: 31
    edited 2004-08-22 16:29
    I had a problem in that my servos slowly moved due to noise without periodic updates. Periodic position update is necessary as previously stated. Try using a subroutine call in your main program loop to something like the following. To move the servo, just set the NewServoPos variable to the desired position value. Servo mid position is achieved with about a 1.5 ms pulse. Try an initial value of 500 for both variables and experiment from there. Once timing values are known, you may be able to change the WORD working variables to BYTE to save memory space. See second example.

    Example 1
    ==============================================
    NewServoPos VAR BYTE 'Desired servo position
    CurServoPos VAR BYTE 'Current servo position
    MoveRate CON 5 'Servo move step

    ' Servo move subroutine. CurServoPos holds the current servo position.
    ' NewServoPos holds the desired servo position. MoveRate specifies the
    ' size of the servo step. This value, the rate this routine is called, and the
    ' Basic Stamp being used determines how fast the servo moves.

    MoveServo:
    IF NewServoPos = CurServoPos THEN MoveServo2
    IF NewServoPos > CurServoPos THEN MoveServo1
    CurServoPos = CurservoPos - MoveRate
    GOTO MoveServo2
    MoveServo1:
    CurServoPos = CurservoPos + MoveRate
    MoveServo2:
    PULSOUT 15,CurServoPos
    RETURN


    Example 2
    ==============================================
    NewServoPos VAR BYTE 'Desired servo position
    CurServoPos VAR BYTE 'Current servo position
    MoveRate CON 5 'Servo move step
    MinPos CON 600 'Minimum servo position

    ' Servo move subroutine. CurServoPos holds the current servo position.
    ' NewServoPos holds the desired servo position. MoveRate specifies the
    ' size of the servo step. This value, the rate this routine is called, and the
    ' Basic Stamp being used determines how fast the servo moves.

    MoveServo:
    IF NewServoPos = CurServoPos THEN MoveServo2
    IF NewServoPos > CurServoPos THEN MoveServo1
    CurServoPos = CurservoPos - MoveRate
    GOTO MoveServo2
    MoveServo1:
    CurServoPos = CurservoPos + MoveRate
    MoveServo2:
    PULSOUT 15,MinPos+CurServoPos
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don Buczynski

    http://www.geocities.com/donbuczynski
  • BoneyardBoneyard Posts: 10
    edited 2004-08-22 19:06
    thank you· I will try this today and let you know they looks lik eit will work
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-08-22 20:42
    Boneyard,

    ·· Are you sure you want Bit variables for mousex and pass?· If you have a counter with a Bit variable, it can only count to 1.· And again, you are shifting bits in a single bit variable.

    ·· Can anybody give a second opinion?· I am kinda out of it today, but I don't think that can be done, not in this code.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    ·
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-08-22 20:45
    Oh, I almost forgot...You said you don't understand the mousex = mousex code, but if you remove it it won't work.· But right before it you have mousex = do, so first you are assigning do to mousex, then you are assigning mousex to mousex...That's why, you'd have to remove both statements if that's not your intention.· Did you write this code, or did someone else?· I ask because you don't seem to know why you have stuff in there, but say it won't work without it, and that makes me think you don't understand what that segment of code is doing?· Am I wrong?


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    ·
  • BoneyardBoneyard Posts: 10
    edited 2004-08-22 21:28
    yes to wrote moset of the code some if it i took form a a/d convertor code and modified it

    if i remove the mousex if you look further down the code

    its used in the servotime cal.

    with out that command the code will not work

    also the sequence only respondes in the bit· 0 or 1· on pass and mousex

    this code works fine its doing what i want it to do just will not hold position wiht i stop the track ball

    Ihave yet to Dons idea
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-08-22 21:50
    You've got me then...Especially considering I can't fathom how you can get pass to ever be greater than 7 when it's only a Bit variable.· These kind of things (Program Inconsitencies) always exclude me from seeing what you're trying to do, since I always try to fix the immediate problems before addressing the bigger issues...

    Same with that mousex variable...You're assigning it two different variables in a row, which I don't see what that's accomplishing...Perhaps someone else can figure this out.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris


    Post Edited (Chris Savage) : 8/22/2004 9:56:21 PM GMT
  • BoneyardBoneyard Posts: 10
    edited 2004-08-22 23:04
    i know excatly what your saying that some of this code does not make scence

    but its works (go figure)



    I really thank you for all your time and help
  • DaveGDaveG Posts: 84
    edited 2004-08-23 04:55
    Howdy,

    Even though your code seems to work, I think that when you previously combined the code from several different
    sources, some of the variables got mixed around and the program is probably not doing what you think it is.

    I would like to suggest a few changes that will make it easier to understand and it probably will work a lot better.

    To start off, I believe the desired program flow is as follows:
    1. Initialization
    2. Start the loop:
    3. Get 8 bits from the mouse, one at a time(mousex), and combine them into an 8-bit Byte(mousey)
    4. Multiply the byte(mousey) by 5, to get Servotime
    5. Send Servotime to the servo, using PULSOUT
    6. Slow down the loop for a while, then do the loop again.

    FYI: Servos typically want a 20 millisecond delay between PULSOUTs.

    Here is a suggested finished program, with my comments in ( ).

    'P0 to cs
    'P1 to Clock (it looks like you switched the words associated with P1 and P2)
    'P2 to Data
    'P15 to servo

    cs VAR OUT0
    clock VAR OUT1
    Dat VAR IN2 (I would change "Do" to "Dat" since the word Do is used in some versions of Basic to "DO" things)
    Servo VAR OUT15
    mousex VAR Bit 'mouse input bit
    mousey VAR Byte 'mouse byte value (I would suggest that you change your comment from "mouse clock line" to
    "mouse byte value", which is more descriptive)
    pass VAR Byte (you absolutely need to change "Bit" to "Byte" since the "pass" variable is used to count from
    0 to 8 in the loop below. A bit can only count up to 1, a byte can count up to 255).
    servotime VAR Word

    '*********************************************************************
    programtop:
    (you should add the following two lines to define the inputs and outputs)
    DIRL = %00000011 'set low byte of IO direction (bits 0 & 1 are outputs for cs & clock, bit 2 is an input for the mouse data)
    DIRH = %10000000 'set high byte of IO direction (bit 15 is a 1, for the servo output)

    mousey = 1
    cs = 1
    clock = 1

    DEBUG "starting"
    '**********************************************************************
    looptop:
    ' start a loop that reads 8 bits of data from the mouse (notice the wording here)
    clock = 0
    pass = 0
    mousey = 1
    cs = 0
    ' clock 8 bits of data in, one bit per getmousey loop
    getmousey:
    mousey = mousey << 1 (you need to change "mousex" to "mousey" in this line. Somehow the two got switched)
    PULSOUT 1,2
    mousex = Dat (please use "Dat" instead of "Do")
    mousey = mousey | mousex (this is the critical step, where the new bit is combined into the byte.
    Mousex and mousey must be in this exact order. Also notice the "|" between them.
    The "|" is the symbol for the "or" function and is located above the backslash on your keyboard)

    pass = pass + 1 (this variable counts up from 0 to 8)
    IF pass > 7 then getmousey
    cs = 1


    '***********************************************************************

    servotime = mousey * 5 (you need to change "mousex" to "mousey", they are switched again)

    PULSOUT 15, 1500-1 (you need to delete this line. The next line is the correct one to control the servo.
    PULSOUT 15, servotime

    PULSOUT 6, 500 (I would delete this line and replace it with a more accurate line below)
    PAUSE 20 '20 millisecond pause for the servo

    GOTO looptop

    GOTO programtop (this line could be deleted because the program always stays in the Looptop loop)

    Good luck
    Dave
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-08-23 14:46
    Thanks Dave,

    ·· I myself cannot focus on troubleshooting until the obvious stuff is licked, and we couldn't seem to get past that...I mentioned already several of these things, but anyway...Glad you were able to provide a more overall solution.· I didn't want to try and re-write his code without knowing exactly what data was coming in (i.e. from the trackball controller).



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    ·
  • steve_bsteve_b Posts: 1,563
    edited 2004-08-23 17:14
    try removing the mousex=do·· it should still work after that.

    Then you could probably get rid of the mousex=mousex.

    just put a ' in front of each of those lines.· It'll just remark them out of the code.

    ·

    Check Jon Williams programming template.· Trust me....when I first started I was doing things like you...hodgepodge messy and all.· I find that now, if I come back to the program a couple months later, it's WAY easier to understand what the heck I was doing!

    ·

    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·

    Steve
    http://members.rogers.com/steve.brady
    http://www.geocities.com/paulsopenstage

    "Inside each and every one of us is our one, true authentic swing. Something we was born with. Something that's ours and ours alone. Something that can't be learned... something that's got to be remembered."

  • BoneyardBoneyard Posts: 10
    edited 2004-09-21 22:46
    Thank you for all your replys on this project·

    I will try the changes this weekend and let you know how it went
Sign In or Register to comment.