November 22, 2024, 12:23:12 PM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Writing code for servos.

Started by skipmcnoob@yahoo.com, March 06, 2012, 10:37:32 PM

Previous topic - Next topic

skipmcnoob@yahoo.com

Would anyone be able to point me in the direction of a good step by step turorial on how to write code for servos using BS1?
Im not trying to power more than one servo, Im simply just trying to learn :)

JonnyMac

Servos can be tricky with a small controller like the Prop-1 because they need to be refreshed every 20ms and that's not easy to do with single-threaded code.  It can be done, it's just not as easy as anyone wants it.  For those wanting to develop custom applications with our HC-8+ I have developed a full-blown servo module (software) that gives you position and speed control of eight servos -- and this isn't affected by the main code.  This is possible because the HC-8+ uses a multi-core processor and I can devote one of the cores to maintaining the servos.

Back to the Prop-1 and basics.  Servos want to see a pulse every 20ms that commands the position; for 90-degree servos that pulse will be from 1000 microseconds to 2000 microseconds.  For 180-degree servos that pulse will be from 600 microseconds to 2400 microseconds.  Luckily, the Prop-1 has a PULSOUT command that does the hard work; its output is expressed in 10 microsecond units so the servo position value fits in one byte (100 to 200, or 60 to 240).

Here's a very simple demo that wags a servo back and forth.  Note that must setup the servo pin with the LOW command so that PULSOUT works correctly.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


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


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


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

SYMBOL  Servo           = 0                     ' Servo on P0


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

SYMBOL  CW              = 100
SYMBOL  Center          = 150
SYMBOL  CCW             = 200


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

SYMBOL  pos             = B2                    ' position


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

Power_Up:
  ' put code here that only happens at power-up/hard reset

Reset:
  LOW Servo                                     ' setup pin for servo


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

Main:
  FOR pos = CW TO CCW STEP 1
    PULSOUT Servo, pos
    PAUSE 20
  NEXT

  FOR pos = CCW TO CW STEP -1
    PULSOUT Servo, pos
    PAUSE 20
  NEXT

  GOTO Main


Jon McPhalen
EFX-TEK Hollywood Office

skipmcnoob@yahoo.com

Thanks John!
Ill tinker with it :)

any advice on the prop2 for servos? or do I need to post something in the prop2 section?

JonnyMac

The process is the same, you just have slightly different units (if you look through the Prop-2 section you'll find code for servos that will make sense now that you've got the basics).
Jon McPhalen
EFX-TEK Hollywood Office

skipmcnoob@yahoo.com

Sounds great!
thanks again for the help! ;)