Shop OBEX P1 Docs P2 Docs Learn Events
how do you keep a servo powered so it doesnt relax during a routine — Parallax Forums

how do you keep a servo powered so it doesnt relax during a routine

ClineCline Posts: 36
edited 2006-10-28 21:35 in BASIC Stamp
High everyone,

im working on a project that requires a servo to hold up an object. the problem is that everytime the servo moves it "relaxes" under the weight.
Is there some tupe of command or routine that will keep itsteady or "powered" while the other programs run?

Thanks for any and all input,

Charles Cline

Comments

  • LSBLSB Posts: 175
    edited 2006-10-25 16:14
    Charles--
    · Servos require that the position signal be repeated about every 20 milliseconds... put your "pulsout" routine in a loop
  • ClineCline Posts: 36
    edited 2006-10-25 16:38
    Could i have an example of that, also if its in a loop how do i get other parts of the program to run while the servo is holding its position

    sorry new to this
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-10-25 16:47
    ServoPos1 VAR BYTE




    Main:
    ' Do lots of stuff
    GOSUB SetServo
    PAUSE 20
    GOTO MAIN

    SetServo:
    PULSOUT Serv1Pin, 500 + 2 * ServoPos1
    ' Note: A servo goes from 1 mSec to 2 mSec.
    ' PULSOUT on the BS2 uses 2 uSec 'ticks'.
    ' So, the PULSOUT value should go from 500 to 750 to 1000.
    ' So, you can do this with one byte with 500 + 2 * <ByteValue>
    ' Where <ByteValue> should range from 0 to 125 to 250
    ' Add addional 'servo settings' here
    RETURN
  • Tom WalkerTom Walker Posts: 509
    edited 2006-10-25 17:02
    Cline,
    The way that most deal with this problem is to offload the "refreshing" to a servo controller board. I can highly recommend the Parallax Servo Controller (PSC). You just tell it, using SEROUT, what position you want your servo to go to, and it will hold it there while your Stamp does other things. And on top of that, you can control up to 8 servos with one board, addressing each·servo individually...all for a very good price and with the greatest support out there (or in here).

    Ther is more to be said, but it is covered in the product description and elsewhere on these fora.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...

    Post Edited (Tom Walker) : 10/25/2006 5:06:07 PM GMT
  • LSBLSB Posts: 175
    edited 2006-10-25 17:09
    Just kind of an aside-- and surely part B of this conversation.

    I'm doing a similar servo control project (self leveling platform using mez-whatever 2125) and get some pretty jittery results. I notice it's better (less jitter) when shortening the "PAUSE 20" to "PAUSE 5". I'm assuming that the rest of the code takes the other 15... Any other tips or tricks for minimizing servo jitter and optimizing timing?

    Thanks,

    Mark
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-10-25 17:21
    Cline,
    ·
    ·· It is entirely possible to write your other routines to handle refreshing the servo while still handling other functions.· Almost everything runs in some kind of loop, so it would make sense to handle it there.· How would be specific to your code.
    ·
    Mark,
    ·
    ·· Servos require refreshing every 20 – 50 mS.· If you’re doing it too often it will usually result in jitter as you’re over-driving the servo’s internal pulse circuitry.· Assuming the code takes 15 mS to run may be incorrect.· In fact you would need to be doing a lot of stuff to take that long.· If you have a scope you can measure the outgoing pulse to see how often it’s happening.· My guess is too often.· On the other hand you could just set the PAUSE back to 20 and see how it works, then reduce it from there.· Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ClineCline Posts: 36
    edited 2006-10-28 21:26
    Chris,

    heres the camera code. this is the original not the changed one, but the servos run like this one, what i need to do is keep the servos refreshed inbetween movements. Could you show me how i can implement this, thanks so much,,

    '{$STAMP BS2}
    '{$PBASIC 2.5}

    ' CMUcam1 BoeBot AppMod 1.1

    ' Basic Stamp 2
    ' CMUcam1 jumpered for 9600 Baud

    RcvData VAR Byte(10) ' Array
    SaveColorData VAR Byte(6) ' Array
    Result VAR Word
    RCcalibrate VAR Word
    N VAR Byte ' General purpose counter variable
    LostTrack VAR Nib
    Switch VAR Nib
    Debounce VAR Nib
    Function VAR Nib

    Main:
    ' Pause for CMUcam1 startup
    FOR N = 2 TO 5 ' Flash all LED's during pause
    SHIFTOUT 4,8,MSBFIRST,[noparse][[/noparse]255]
    PAUSE 400
    SHIFTOUT 4,8,MSBFIRST,[noparse][[/noparse]0]
    PAUSE 400
    NEXT
    ' Calibrate the button RCtime value
    HIGH 5
    PAUSE 1 ' for 1 ms.
    RCTIME 5, 1, RCcalibrate
    FREQOUT 13, 400, 2500 ' Beep to indicate ready

    ProgTop:
    ' Select function from push buttons
    GOSUB GetButton
    Debounce=Switch
    GOSUB GetButton
    ' Get button press twice and compare if not the same repeat
    IF Debounce <> Switch THEN ProgTop
    IF Switch = 2 THEN ' If Go/Stop button pushed then beep
    FREQOUT 13, 400, 2500 ' beep
    ENDIF
    BRANCH Switch, [noparse][[/noparse]ProgTop,Buttonselect,Execute]

    Buttonselect:
    Function = Function + 1
    IF Function > 9 THEN
    Function = 1
    SHIFTOUT 4,8,LSBFIRST,
    ENDIF
    PAUSE 300
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]DCD(Function-1)]
    GOTO ProgTop


    Execute:
    BRANCH Function, [noparse][[/noparse]ProgTop,Calibrate,SaveColor,TrackColor,Avoid,Adapt,FollowLine,Point,Color]
    GOTO ProgTop


    GetButton:
    Switch=0
    ' Activate RCtime for two buttons
    HIGH 5
    PAUSE 50 ' for 1 ms.
    RCTIME 5, 1, Result

    IF Result>=RCcalibrate-200 THEN
    Switch = 0 ' No button pressed
    RETURN
    ENDIF

    IF Result>((RCcalibrate/4)*3)-500 THEN
    Switch = 1 ' Select button pressed
    RETURN
    ENDIF

    IF Result>(RCcalibrate/2)-500 THEN
    Switch = 2 ' GO/Stop button pressed
    RETURN
    ENDIF

    Switch = 3 ' Both buttons pressed at the same time

    RETURN


    ' Track the color previously saved in EEPROM
    TrackColor:
    GOSUB ConfigureCMUcam
    ' Read EEPROM into SaveColorData array
    READ 0, SaveColorData(0)
    READ 1, SaveColorData(1)
    READ 2, SaveColorData(2)
    READ 3, SaveColorData(3)
    READ 4, SaveColorData(4)
    READ 5, SaveColorData(5)
    ' Set Track Color values from EEPROM
    SEROUT 7,84,[noparse][[/noparse]"TC"]
    SEROUT 7,84,[noparse][[/noparse]DEC SaveColorData(0)]
    SEROUT 7,84,[noparse][[/noparse]" "]
    SEROUT 7,84,[noparse][[/noparse]DEC SaveColorData(1)]
    SEROUT 7,84,[noparse][[/noparse]" "]
    SEROUT 7,84,[noparse][[/noparse]DEC SaveColorData(2)]
    SEROUT 7,84,[noparse][[/noparse]" "]
    SEROUT 7,84,[noparse][[/noparse]DEC SaveColorData(3)]
    SEROUT 7,84,[noparse][[/noparse]" "]
    SEROUT 7,84,[noparse][[/noparse]DEC SaveColorData(4)]
    SEROUT 7,84,[noparse][[/noparse]" "]
    SEROUT 7,84,[noparse][[/noparse]DEC SaveColorData(5),CR]
    PAUSE 200

    TopTrack:
    SHIFTOUT 4,8,MSBFIRST,[noparse][[/noparse]0] ' All LED's off
    GOSUB GetButton ' Check for stop button press
    IF Switch = 2 THEN' Switch 2 is stop button
    ' Two beeps for Stop
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]DCD (Function-1)]
    PAUSE 200
    FREQOUT 13, 400, 2500
    PAUSE 200
    FREQOUT 13, 400, 2500
    ' Reset the green tracking LED
    SEROUT 7, 84, [noparse][[/noparse]"L1 0",CR] ' Green LED off
    PAUSE 100
    SEROUT 7, 84, [noparse][[/noparse]"L1 2",CR] ' Green LED auto mode
    PAUSE 100
    GOTO ProgTop
    ENDIF

    ' Send command - Track color (with no arguments) -
    ' will track last color grabbed by TW command
    SEROUT 7, 84, [noparse][[/noparse]"TC",CR]
    SERIN 9, 84, [noparse][[/noparse]STR RcvData\10]
    PAUSE 100

    ' 45 is aprox H center, 70 is aprox V center

    ' Track and follow logic
    IF Function = 3 THEN
    IF RCVData(2) > 65 AND RcvData(9) > 25 THEN RotateLeft
    IF RCVData(2) < 25 AND RcvData(9) > 25 THEN RotateRight
    IF RCVData(7) > 120 AND RcvData(9) > 25 THEN Bwd
    IF RCVData(7) < 60 AND RcvData(9) > 25 THEN Fwd
    ENDIF

    ' Move and avoid logic
    IF Function = 4 THEN
    IF RCVData(3) > 50 AND RcvData(9) > 20 THEN Fwd
    IF RCVData(3) < 50 THEN BackUp
    IF RcvData(9) <= 20 THEN BackUp
    ENDIF

    ' Adaptive tracking logic
    IF Function = 5 THEN
    IF RCVData(2) > 65 AND RcvData(9) > 25 THEN RotateLeft
    IF RCVData(2) < 25 AND RcvData(9) > 25 THEN RotateRight
    IF RCVData(7) < 60 AND RcvData(9) > 25 THEN Fwd
    IF LostTrack > 6 THEN TW
    LostTrack = LostTrack + 1
    IF RcvData(9) > 25 THEN LostTrack = 0
    ENDIF

    ' Line Following logic
    IF Function = 6 THEN
    IF RCVData(2) > 50 AND RcvData(9) > 5 THEN TurnLeft ' 45
    IF RCVData(2) < 40 AND RcvData(9) > 5 THEN TurnRight ' 45
    IF RCVData(2) >= 40 AND RCVData(2) =< 50 AND RcvData(9) > 5 THEN Fwd
    ENDIF

    ' Finger point logic
    IF Function = 7 THEN
    IF RCVData(2) > 60 AND RcvData(9) > 25 THEN RotateRight ' Centroid X
    IF RCVData(2) < 30 AND RcvData(9) > 25 THEN RotateLeft ' Centroid X
    IF RCVData(3) > 30 AND RcvData(9) > 15 THEN Bwd ' Centroid Y
    ENDIF

    ' Color logic
    IF Function = 8 THEN

    SEROUT 7, 84, [noparse][[/noparse]"TC 30 240 16 30 16 30",CR] ' Red
    SERIN 9, 84, [noparse][[/noparse]STR RcvData\10]
    IF RcvData(9) > 20 THEN
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]195] ' All Red LED's on
    PAUSE 500
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]0] ' All LED's off
    ENDIF

    SEROUT 7, 84, [noparse][[/noparse]"TC 16 40 30 240 90 170",CR] ' Green
    SERIN 9, 84, [noparse][[/noparse]STR RcvData\10]
    IF RcvData(9) > 20 THEN
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]24] ' All Green LED's on
    PAUSE 500
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]0] ' All LED's off
    ENDIF

    SEROUT 7, 84, [noparse][[/noparse]"TC 200 240 200 240 16 50",CR] ' Yellow
    SERIN 9, 84, [noparse][[/noparse]STR RcvData\10]
    IF RcvData(9) > 20 THEN
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]36] ' All Yellow LED's on
    PAUSE 500
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]0] ' All LED's off
    ENDIF

    ENDIF

    ' Flash LED's to show bottom of tracking loop
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]129] ' 100000001
    GOTO TopTrack


    ' Calibrate CMUcam1 for lighting conditions
    Calibrate:
    ' Reset CMUcam1
    SEROUT 7, 84, [noparse][[/noparse]"RS",CR]
    PAUSE 200
    ' Turn on auto adjust
    SEROUT 7, 84, [noparse][[/noparse]"CR 18 44",CR]
    PAUSE 100
    ' Delay loop for CMUcam1 to auto adjust to lighting conditions
    FOR N = 2 TO 255
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]DCD (N)]
    GOSUB GetButton ' Check for stop button pressed
    IF Switch = 2 THEN EXIT ' Switch 2 is stop button
    NEXT
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]0] ' 000000000
    ' Turn off auto adjust
    SEROUT 7, 84, [noparse][[/noparse]"CR 18 44 19 32",CR]
    PAUSE 100
    GOSUB ConfigureCMUcam
    SHIFTOUT 4,8,LSBFIRST, ' 100000000
    FREQOUT 13, 400, 2500
    PAUSE 300
    FREQOUT 13, 400, 2500
    GOTO ProgTop


    SaveColor:
    ' sample and save color
    SHIFTOUT 4,8,MSBFIRST,[noparse][[/noparse]0]
    SEROUT 7,84,[noparse][[/noparse]"PM 1",CR]
    PAUSE 100
    ' Set a small window in center
    SEROUT 7,84,[noparse][[/noparse]"SW 30 54 50 90",CR]
    PAUSE 100
    ' Set raw mode 3
    SEROUT 7,84,[noparse][[/noparse]"RM 3",CR]
    PAUSE 100
    'set minimun and maximun values
    SaveColorData(0)=16
    SaveColorData(1)=240
    SaveColorData(2)=16
    SaveColorData(3)=240
    SaveColorData(4)=16
    SaveColorData(5)=240
    ' Get Mean
    SEROUT 7,84,[noparse][[/noparse]"GM",CR]
    SERIN 9,84, [noparse][[/noparse]STR rcvdata\8]
    PAUSE 200
    IF rcvdata(2)<=46 THEN gm1
    SaveColorData(0)=(rcvdata(2)-30)
    gm1:
    IF rcvdata(3)<=46 THEN gm2
    SaveColorData(2)=(rcvdata(3)-30)
    gm2:
    IF rcvdata(4)<=46 THEN gm3
    saveColorData(4)=(rcvdata(4)-30)
    gm3:
    IF rcvdata(2)>210 THEN gm4
    saveColorData(1)=(rcvdata(2)+30)
    gm4:
    IF rcvdata(3)>240 THEN gm5
    saveColorData(3)=(rcvdata(3)+30)
    gm5:
    IF rcvdata(4)>240 THEN gm6
    saveColorData(5)=(rcvdata(4)+30)
    gm6:
    ' Set raw mode to 0
    SEROUT 7,84,[noparse][[/noparse]"RM 0",CR]
    PAUSE 100
    ' Return window to full size
    SEROUT 7,84,[noparse][[/noparse]"SW",CR]
    ' Write color tracking range values to end of EEPROM
    WRITE 0,saveColorData(0)
    WRITE 1,saveColorData(1)
    WRITE 2,saveColorData(2)
    WRITE 3,saveColorData(3)
    WRITE 4,saveColorData(4)
    WRITE 5,saveColorData(5)
    GOSUB ConfigureCMUcam
    SHIFTOUT 4,8,LSBFIRST,
    ' Two beeps
    FREQOUT 13, 400, 2500
    PAUSE 400
    FREQOUT 13, 400, 2500
    PAUSE 400
    GOTO ProgTop


    Avoid:
    GOSUB ConfigureCMUcam
    SEROUT 7,84,[noparse][[/noparse]"TW",CR]
    PAUSE 1000 ' Wait 1 second for TW
    GOTO TopTrack


    Adapt:
    GOSUB ConfigureCMUcam
    TW:
    SHIFTOUT 4,8,MSBFIRST,[noparse][[/noparse]255] ' All LED's on
    FREQOUT 13, 150, 2500 ' Beep
    PAUSE 100
    FREQOUT 13, 150, 2500 ' Beep
    SHIFTOUT 4,8,MSBFIRST,[noparse][[/noparse]0] ' All LED's off
    SEROUT 7,84,[noparse][[/noparse]"TW",CR] ' Send Track Window command
    PAUSE 1000 ' Wait 1 second for TW to complete
    LostTrack = 0 ' Reset LostTrack counter
    GOTO TopTrack


    FollowLine:
    GOSUB ConfigureCMUcam
    SEROUT 7,84,[noparse][[/noparse]"TC 16 50 16 50 16 50",CR]' Black line color
    PAUSE 100
    GOTO Toptrack


    Point:
    GOSUB ConfigureCMUcam
    SEROUT 7,84,[noparse][[/noparse]"TC 200 240 180 240 50 100",CR] ' Skin color
    PAUSE 100
    GOTO Toptrack


    Color:
    GOSUB ConfigureCMUcam
    GOTO Toptrack


    ConfigureCMUcam:
    SEROUT 7,84,[noparse][[/noparse]"PM 1",CR]' Poll Mode
    PAUSE 100
    SEROUT 7,84,[noparse][[/noparse]"RM 3",CR]' Raw Mode
    PAUSE 100
    SEROUT 7,84,[noparse][[/noparse]"CR 32 8",CR]' White Balance smart mode
    PAUSE 100
    RETURN


    BackUp:
    ' backup
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]195]
    FOR n = 1 TO 15
    PULSOUT 14,1000 ' **

    PAUSE 15
    NEXT
    PAUSE 200
    ' rotate about 90 degrees
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]32]
    FOR n = 1 TO 100
    PULSOUT 14,1000 ' **

    NEXT
    GOTO TopTrack

    RotateLeft:
    SHIFTOUT 4,8,LSBFIRST,
    FOR n = 1 TO 10

    PULSOUT 15,500 ' **
    NEXT
    GOTO TopTrack
    RotateRight:
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]32]
    FOR n = 1 TO 10

    PULSOUT 15,1000 ' **
    NEXT
    GOTO TopTrack
    TurnLeft:
    SHIFTOUT 4,8,LSBFIRST,
    FOR n = 1 TO 40

    PULSOUT 15,750 ' **
    NEXT
    GOTO TopTrack
    TurnRight:
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]32]
    FOR n = 1 TO 40

    PULSOUT 15,1000 ' **
    NEXT
    GOTO TopTrack
    Bwd:
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]195]
    FOR n = 1 TO 10
    PULSOUT 14,1000 ' **

    PAUSE 15
    NEXT
    GOTO TopTrack
    Fwd:
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]24]
    FOR n = 1 TO 10
    PULSOUT 14,500 ' **

    PAUSE 15
    NEXT
    GOTO TopTrack
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-10-28 21:28
    Cline,
    ·
    ·· Attaching the code would help, since it is very difficult to read the way it is shown…All the formatting is messed up as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ClineCline Posts: 36
    edited 2006-10-28 21:28
    hm the enBackUp:
    ' backup
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]195]
    FOR n = 1 TO 15
    PULSOUT 14,1000 ' **

    PAUSE 15
    NEXT
    PAUSE 200
    ' rotate about 90 degrees
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]32]
    FOR n = 1 TO 100
    PULSOUT 14,1000 ' **

    NEXT
    GOTO TopTrack

    RotateLeft:
    SHIFTOUT 4,8,LSBFIRST,
    FOR n = 1 TO 10

    PULSOUT 15,500 ' **
    NEXT
    GOTO TopTrack
    RotateRight:
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]32]
    FOR n = 1 TO 10

    PULSOUT 15,1000 ' **
    NEXT
    GOTO TopTrack
    TurnLeft:
    SHIFTOUT 4,8,LSBFIRST,
    FOR n = 1 TO 40

    PULSOUT 15,750 ' **
    NEXT
    GOTO TopTrack
    TurnRight:
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]32]
    FOR n = 1 TO 40

    PULSOUT 15,1000 ' **
    NEXT
    GOTO TopTrack
    Bwd:
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]195]
    FOR n = 1 TO 10
    PULSOUT 14,1000 ' **

    PAUSE 15
    NEXT
    GOTO TopTrack
    Fwd:
    SHIFTOUT 4,8,LSBFIRST,[noparse][[/noparse]24]
    FOR n = 1 TO 10
    PULSOUT 14,500 ' **

    PAUSE 15
    NEXT
    GOTO TopTrackd got messed up let me try that again
  • ClineCline Posts: 36
    edited 2006-10-28 21:33
    sorry something isnt getting transfered, i cant see all of it,


    it is the 8 function demo from the camera i bought from parallax,
    the backword and forword commands are now elevation,
    its only pin 14 elevation
    pin 15 rotation,,

    i placed the board and camera on a rotating tripod and am using a servo to elevate the board and camera
  • ClineCline Posts: 36
    edited 2006-10-28 21:35
    ok i figured it out sorry,, attached is the code
Sign In or Register to comment.