first servomotor control program

Upload: twchan69

Post on 13-Oct-2015

34 views

Category:

Documents


6 download

DESCRIPTION

Servomotor Programming

TRANSCRIPT

  • First Servomotor Control Program

    Online Catalog Shopping Cart Previous Page Next Page

    Home > Articles > Robotics > Servomotor Controlling Page 2

    First Servomotor Control Program

    In our first program , we will simply sweep the servomotor from CCW to CW and then sweep back. The program will be kept simple as to demonstrate the priniciples of controlling a servo with a the PIC Basic language. The schematic can be seen in figure 2 (below).

    The variable pw controls the pulsewidth, and is started at 100 (extreme left, -45 degrees). The program sends the pulse out to the servo, and then is increased by a value of 1 until it reaches 200 (extreme right, 45 degrees), at which point it will reverese the rotation.

    ----Listing 1----

    ' First servomotor program ' Sweeps left to right, then reverses Symbol B1 = pw ' create a variable pw pw = 100 ' start at extreme left sweep: pulsout 0,pw ' send pulse to motor pause 18 ' set frequency to about 50 Hz pw = pw + 1 ' increase pw by 1

    http://www.imagesco.com/articles/picservo/02.html (1 of 2)02-03-2006 2:49:44 pm

  • First Servomotor Control Program

    if pw > 200 then back ' at extreme right, turn CCW goto sweep ' otherwise, continue back: pulsout 0,pw ' send pulse to motor pause 18 ' set frequency to about 50 Hz pw = pw - 1 ' decrease pw by 1 if pw < 100 then sweep ' at extreme left, turn CW goto back ' otherwise, continue

    ----End of Listing 1----

    If desired, we could extend the rotation of the servomotor to a full 180 degrees (-90 to 90 degrees) rotation by decreasing the minimum pulsewidth to below 1 ms and increasing the maximum pulsewidth to over 2 ms. This can be accomplished with our previous program by modifying the occurances of 100 and 200 to your desired minimum and maximum pulsewidths, respectivly.

    However, a note of caution: the pulsewidth required for servos varies from brand to brand. One motor may require a 2.8 ms pulsewidth for maximum rotation, while another may only need 2.4 ms.

    Furthermore, servomotors have end stops that limit its rotation. If you send the motor a pulsewidth that is beyond the end stops, the motor will keep trying to turn. A motor in this stalled condition not only draws more current, but also puts wear on the internal gears, shortening the lifespan of the motor.

    Next Page

    Online Catalog Shopping Cart Previous Page Next Page

    http://www.imagesco.com/articles/picservo/02.html (2 of 2)02-03-2006 2:49:44 pm

  • Manual Servomotor Control

    Online Catalog Shopping Cart Previous Page Next Page

    Home > Articles > Robotics > Servomotor Controlling Page 3

    Manual Servo Control

    Our next program will allow you to control the direction of the servo manually via a SPDT switch (with a center-off position) connected to ports B1 and B2. Without a center-off position, you will have to use two switches. A schematic is shown in figure 3 (below).

    With the switch in the center position, the servo will not turn. When it is moved into forward, it will turn one way. Moving the switch down will make it turn the opposite direction. This program as-is will limit rotation to 45 degrees off-center, but can be modified to extend rotation through the aforementioned methods.

    ----Listing 2----

    ' Manual control of servo direction via ' an SPDT switch. Symbol B1=pw ' create a variable pw pw = 150 ' begin at center position check: if pin1 = 0 then left ' is pin 1 active? if pin2 = 0 then right ' is pin 2 active?

    http://www.imagesco.com/articles/picservo/03.html (1 of 2)02-03-2006 2:50:01 pm

  • Manual Servomotor Control

    Pulsout 0,pw ' send current pw pause 18 ' set frequency to about 50 Hz goto check ' check again left: pw = pw + 1 ' increase pulsewidth pulsout 0,pw ' send current pw pause 18 ' set frequency to about 50 Hz if pw > 200 then max ' dont go over 2 ms goto check ' go back and check again right: pw = pw - 1 ' decrease pulsewidth pulsout 0,pw ' send current pw pause 18 ' set frequency to about 50 Hz if pw < 100 then min ' dont go under 1 ms goto check ' check again max: pw = 200 ' cap pw at 2 ms goto check ' check again min: pw = 100 ' cap at 1 ms goto check ' check again

    ----End of Listing 2----

    Next Page

    Online Catalog Shopping Cart Previous Page Next Page

    http://www.imagesco.com/articles/picservo/03.html (2 of 2)02-03-2006 2:50:01 pm

  • Multiple Servomotors

    Online Catalog Shopping Cart Previous Page Next Page

    Home > Articles > Robotics > Servomotor Controlling Page 4

    Multiple Servomotors

    Using a modified version of the last program, we can control as many servomotors as we have I/O lines on port B. In the next listing, we will control two servos in the same manner as we controlled a single servo in the previous program. The circuit is shown in figure 4 (below).

    The program uses two pulsewidth variables, pw1 and pw2; and two sets of routines, left1 and left2, right1 and right2; one for each motor. As you can see in the schematic, the first servo is wired as per the previous circuit. The second servo is now using B3 as it's pulse out, and B4 and B5 for the SPDT switch.

    ----Listing 3----

    http://www.imagesco.com/articles/picservo/04.html (1 of 3)02-03-2006 2:50:11 pm

  • Multiple Servomotors

    'Manual control of two servomotors using 2 SPDT switches

    'Use B1 to hold pulsewidth variable for servo 1 'Use B2 to hold pulsewidth variable for servo 2

    'Initialize Variables

    B1 = 150 'start servo 1 at center position B2 = 150 'start servo 2 at center position

    start: 'check for switch closures IF pin1 = 0 Then left1 'is sw1 left active? IF pin2 = 0 Then right1 'is sw1 right active? IF pin4 = 0 Then left2 'is sw2 left active? IF pin5 = 0 Then right2 'is sw2 right active? PulsOut 0, B1 'send current servo 1 position out PulsOut 3, B2 'send current servo 2 position out Pause 18 GoTo start

    'Routines for Servomotor 1 left1: B1 = B1 + 1 'increase the pulse width PulsOut 0, B1 'send current B1 PulsOut 3, B2 'send current B2 Pause 18 'set frequency update about 50 hz IF B1 > 225 Then max1 'maximum 2.25 millisecond GoTo start right1: B1 = B1 - 1 'decrease the pulse width PulsOut 0, B1 'send current B1 PulsOut 3, B2 'send current B2 Pause 18 'set frequency update about 50 hz IF B1 < 75 Then min1 'minimum .75 millisecond GoTo start max1: B1 = 225 'cap max B1 at 2.25 milliseconds GoTo start min1: B1 = 75 'cap min B1 at .75 millisecond GoTo start

    'Routines for Servomotor 2 left2: B2 = B2 + 1 'increase the pulse width PulsOut 0, B1 'send current B1 PulsOut 3, B2 'send current B2 Pause 18 'set frequency update about 50 hz IF B2 > 225 Then max2 'maximum 2.25 millisecond GoTo start right2: B2 = B2 - 1 'decrease the pulse width PulsOut 0, B1 'send current B1 PulsOut 3, B2 'send current B2 Pause 18 'set frequency update about 50 hz

    http://www.imagesco.com/articles/picservo/04.html (2 of 3)02-03-2006 2:50:11 pm

  • Multiple Servomotors

    IF B2 < 75 Then min2 'minimum .75 millisecond GoTo start max2: B2 = 225 'cap max B2 at 2.25 milliseconds GoTo start min2: B2 = 75 'cap min B2 at .75 millisecond GoTo start

    ----End of Listing 3----

    Catalog Page for PIC Microcontrollers (a kit containing all necessary parts is available)

    Catalog Page for Servo Motors

    Back to Robotics Articles

    Online Catalog Shopping Cart Previous Page Next Page

    http://www.imagesco.com/articles/picservo/04.html (3 of 3)02-03-2006 2:50:11 pm

    imagesco.comFirst Servomotor Control ProgramManual Servomotor ControlMultiple Servomotors