November 22, 2024, 01:13:29 PM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


Servo loop

Started by imagineerdan, January 20, 2008, 05:29:17 PM

Previous topic - Next topic

imagineerdan

I am trying to get a servo to go from 0 to 255 and back again as fast as it can move, and in a loop. What would this look like?

JonnyMac

"Standard" servo values -- when using the Prop-1 -- are going to be from 100 (1 ms pulse) to 200 (2 ms pulse).  This program will wag a servo back and forth:

' =========================================================================
'
'   File...... Servo_Wag.BS1
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'              Copyright (c) 2007 EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Servo           = 0                     ' connect to P0


' -----[ Constants ]-------------------------------------------------------


' -----[ Variables ]-------------------------------------------------------

SYMBOL  pos             = B2                    ' servo position


' -----[ Initialization ]--------------------------------------------------

Reset:
  LOW Servo                                     ' initialize servo pin


' -----[ Program Code ]----------------------------------------------------

Main:
  FOR pos = 100 TO 200 STEP 1
    PULSOUT Servo, pos
    PAUSE 19
  NEXT
  FOR pos = 200 TO 100 STEP -1
    PULSOUT Servo, pos
    PAUSE 19
  NEXT
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------



You can play with the STEP size to adjust the speed.

Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

I was wondering what the code would look like to have 2 servos randomly moving (slowly) and I would be able to specify thier start and end place parameters. As in the servos would move randomly within those two destinations. So I would able to adjust speed as well? Thanks!

livinlowe

Take a look at this-

http://www.spiderspreyground.com/howto/prop-1/

Vern has a folder you can access (programs) that has several that move servos. Perhaps one will do what you need.

Shawn
Scaring someone with a prop you built -- priceless!

imagineerdan

I need the servo to stay within very specific perameters like only 50 to 100 and back. How would I get these codes to allow me to do that

livinlowe

In Jon's code, just change the FOR 100 to 200 to the values you need. Like this:


FOR pos = 50 TO 100 STEP 1
    PULSOUT Servo, pos
    PAUSE 19



You can make these values anythng, WITHIN the 100 to 200 pulse parameters!   :D
Shawn
Scaring someone with a prop you built -- priceless!

JonnyMac

Here's a program for randomizing the movements of two servos.  You'll need to adjust for the limits you desire.

' =========================================================================
'
'   File....... Random_2Servos.BS1
'   Purpose....
'   Author..... Jon Williams -- EFX-TEK
'               (based on work by Vern Graner)
'   E-mail..... jwilliams@efx-tek.com
'   Started....
'   Updated.... 16 AUG 2006
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Servo1          = 0
SYMBOL  Servo2          = 1


' -----[ Constants ]-------------------------------------------------------


' -----[ Variables ]-------------------------------------------------------

SYMBOL  lottery         = W0                    ' random seed/value
SYMBOL  pos1            = B2                    ' current1 position
SYMBOL  dest1           = B3                    ' target1 position
SYMBOL  hold1           = B4                    ' hold1 timer
SYMBOL  pos2            = B5                    ' current2 position
SYMBOL  dest2           = B6                    ' target2 position
SYMBOL  hold2           = B7                    ' hold2 timer


' -----[ EEPROM Data ]-----------------------------------------------------


' -----[ Initialization ]--------------------------------------------------

Reset:
  DIRS = %00000011                              ' initialize servo pins

  lottery = 1031                                ' seed RANDOM
  pos1 = 150 : dest1 = 150                      ' start 1 at center
  pos2 = 150 : dest2 = 150                      ' start 2 at center


' -----[ Program Code ]----------------------------------------------------

Main:
  PULSOUT Servo1, pos1                          ' update servos
  PULSOUT Servo2, pos2
  PAUSE 16

Check1:
  RANDOM lottery                                ' stir random value
  IF pos1 <> dest1 THEN Move1                   ' at destination?
    IF hold1 = 0 THEN New_Dest1                 ' yes, still holding?
      hold1 = hold1 - 1                         ' yes, dec hold timer
      GOTO Check2

New_Dest1:
  dest1 = lottery // 101 + 100                  ' create new destination
  hold1 = lottery // 201 + 50                   ' create new hold time
  GOTO Check2

Move1:
  IF pos1 > dest1 THEN Move1_CCW
    pos1 = pos1 + 1                             ' move position CW
    GOTO Check2

Move1_CCW:
  pos1 = pos1 - 1                               ' move position CCW


Check2:
  RANDOM lottery
  IF pos2 <> dest2 THEN Move2
    IF hold2 = 0 THEN New_Dest2
      hold2 = hold2 - 1
      GOTO Main

New_Dest2:
  dest2 = lottery // 101 + 100
  hold2 = lottery // 201 + 50
  GOTO Check2

Move2:
  IF pos2 > dest2 THEN Move2_CCW
    pos2 = pos2 + 1
    GOTO Main

Move2_CCW:
  pos2 = pos2 - 1
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

With a the code I am using at the bottem, I am getting the speed I would like, but the servo is a bot jerky, how can I smooth that with the same speed?

DIRS = %1111111                              ' initialize servo pins
SYMBOL  Servo           = 0                   ' connect to P0




' -----[ Constants ]-------------------------------------------------------


' -----[ Variables ]-------------------------------------------------------

SYMBOL  pos             = B2                    ' servo position


' -----[ Initialization ]--------------------------------------------------

Reset:
  LOW Servo                                     ' initialize servo pin


' -----[ Program Code ]----------------------------------------------------

Main:

  FOR pos = 100 TO 115 STEP 1
    PULSOUT Servo, pos
    PAUSE 40
  NEXT


  PAUSE 4500
  PIN1=1
  FOR pos = 115 TO 100 STEP -1
    PULSOUT Servo, pos
    PAUSE 40
  NEXT
  PAUSE 6000
  PIN1=0
  GOTO Main

JonnyMac

You have a loop that does this:

  FOR pos = 115 TO 100 STEP -1
    PULSOUT Servo, pos
    PAUSE 40
  NEXT


The 40 ms delay is on the outer edge of the refresh rate for the servo, and if there's any load on it that could cause it to look jerky.  Create another variable called "hold" and then update your loop (with another -- inner -- loop) like this:

  FOR pos = 115 TO 100 STEP -1
    FOR hold = 1 TO 2
      PULSOUT Servo, pos
      PAUSE 20
    NEXT
  NEXT


Now you're refreshing the servo every 20 milliseconds (which is standard) and this should smooth it out.
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

Hopefully that last post made sense.  Instead of updating every 40 milliseconds, you're updating the same position value two times in a row, and doing it every 20 milliseconds.
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

What would that look like? Thanks soo much for the help!!

JonnyMac

It's in the post above -- the second code section that has nested FOR-NEXT loops.
Jon McPhalen
EFX-TEK Hollywood Office