Driving an RC Servo from the Joystick Port (Antic, 1984)
- Year
- 1984
- Type
- DIY project
- Category
- Robotics / motion control project
- Manufacturer
- Homebrew (Evan Rosen, Antic)
- Tags
- diy, robotics, servo, radio-control, joystick-port, pia, porta, vbi, interrupt, antic, basic, beginner
- Related
- Build-Your-Own EPROM Burners, Joystick-Port Interfacing: the PIA as a Parallel I/O Port, Real-World Interface (Antic Relay Controller), Build-Your-Own Speech Synthesizer (SP0256-AL2), Antic Stepper Motor Robot-Controller, Joystick Ports as I/O Lines: Programming the PIA from BASIC and Assembly
Overview
In Antic's January 1984 issue (Vol. 2 No. 10), Evan Rosen, co-author of Valpar's Val-FORTH, followed his "Anatomy of a Robot" article with a hands-on project: "Has Your Robot Hugged You Today?". With a model-shop radio-control servo, a joystick plug, a battery and a 42-line BASIC program, an Atari 400 or 800 can position a motor shaft anywhere in its range, the first step towards a gripper or robot arm.
Parts
| Part | Notes from the article |
|---|---|
| RC servo | a cheap, ready-assembled, medium-sized model with three wires (red +5 V, black ground, and a pulse wire). Rosen tested World, Ram, Kraft and Royal brands. Avoid "retract" servos, which only have two positions. About 20 oz-in of torque is plenty, and 180° rotation is typical |
| DE-9 female plug | or a salvaged Atari joystick cable: white = pin 1, blue = 2, green = 3, brown = 4, orange = 6, black = 8. Check with an ohmmeter |
| 5 V supply | a regulated 5 V supply of at least 300 mA, four NiCd cells, or a 6 V lantern battery with two 1N4001 diodes in series to drop about a volt. Don't go much above 5.2 V |
| Clips or solder | alligator clips are fine for a first try |
Wiring
Make every connection with the plug out of the computer:
- Power-supply negative to the servo's black wire and to pin 8 (ground) of the plug, so everything shares a common ground.
- The servo's pulse wire (neither red nor black) to pin 1 of the plug.
- Power-supply positive to the servo's red wire.
Then plug into joystick port 2. Rosen warns that a misconnected supply can damage the PIA, and suggests plugging powered homemade gadgets in and out only while the computer is on.
How the software works
A servo sets its position from the width of a pulse repeated about 60 times a second, roughly 1 to 2 ms wide. The program:
- reprograms PIA port A so that bit 4, which is port 2's pin 1, becomes an output. Lines 105–125 flip PACTL (
$D302) to reach the data-direction register and write$10to it. - POKEs a 30-odd-byte machine-language routine into page 6 (
$0600). It sets the pin high, counts a delay made of a minimum (OPULSE) plus the position value (SERVO), then sets the pin low. - hooks the routine into the deferred vertical-blank vector (
VVBLKD, locations 548–549) and chains to the OS exit, so it runs every frame in the background while BASIC carries on.
From then on, POKE SERVO,n moves the servo. The demo sweeps it back and forth every few seconds.
Calibrating
- Press BREAK and
POKE SERVO,0. If the servo vibrates at its end stop, it is straining. RepeatPOKE OPULSE,PEEK(OPULSE)+2until it stops vibrating, add two more steps for margin, read the value withPRINT PEEK(OPULSE), and put it in line 170. POKE SERVO,150, then step up by 5 until the case vibrates, back off by 2 until it stops, back off twice more, and use that value asTOPin line 1010.- Keep positions between 0 and
TOP.
If you move the driver, keep it on a page boundary. A branch that crosses a page takes an extra cycle and upsets the timing. Once installed, NEW clears the BASIC loader and the driver keeps running.
The listing (Antic disk, January 1984)
95 REM *****SERVO******
96 REM *BY EVEN ROSEN *
97 REM *ANTIC MAGAZINE*
98 REM *JANUARY, 1984 *
99 REM ****************
100 REM PGRM JOY 2 FOR OUTPUT
105 TEMP=PEEK(54018)
110 POKE 54018,TEMP-4
120 POKE 54016,16
125 POKE 54018,TEMP
130 REM STORE SERVO DRIVER
140 SERVO=1536:OPULSE=SERVO+1
145 ENTRY=SERVO+2:PTR=ENTRY
150 REM SERVO IS PAGE 6 BUT
155 REM MAY BE RELOCATED
160 POKE SERVO,128:REM MIDPOINT
165 REM MAY BE RELOCATED
170 POKE OPULSE,100:REM MIN PULSE
175 POKE PTR,174:PTR=PTR+1
180 POKE PTR,OPULSE-INT(OPULSE/256)*256
185 POKE PTR+1,INT(OPULSE/256):PTR=PTR+2
190 FOR I=1 TO 12
195 READ X:POKE PTR,X:PTR=PTR+1
200 NEXT I
205 DATA 232,169,16,141,0,211,234,234,202,208,251,174
210 POKE PTR,SERVO-INT(SERVO/256)*256
215 POKE PTR+1,INT(SERVO/256):PTR=PTR+2
220 FOR I=1 TO 14
225 READ X:POKE PTR,X:PTR=PTR+1
230 NEXT I
235 DATA 232,234,234,234,234,202,208,249,169,0,141,0,211,76
240 POKE PTR,PEEK(58467)
245 POKE PTR+1,PEEK(58468)
250 POKE 54286,0:REM DISAB VBLANK
255 POKE 548,ENTRY-INT(ENTRY/256)*256
260 POKE 549,INT(ENTRY/256)
265 POKE 54286,64:REM ENAB VBLANK
1000 REM OSCILLATING DEMO
1010 TOP=150
1020 FOR I=0 TO TOP:POKE SERVO,I:NEXT I
1030 FOR I=TOP TO 0 STEP -1:POKE SERVO,I:NEXT I
1040 GOTO 1020
Tips
- Servo motors contain permanent magnets. Rosen advises keeping them at least three feet from your disks.
- Don't turn the output shaft by hand: it puts slack into the gear train and causes jitter.
- For more joystick-port control projects, see joystick-port interfacing and the stepper motor robot-controller.
Credits
- Evan Rosen — Has Your Robot Hugged You Today?, Antic Vol. 2 No. 10 (January 1984) — Evan Rosen; AtariMagazines.com (Kevin Savetz), 1984
- SERVO.BAS from the Antic disk for January 1984 (detokenized listing) — AtariMagazines.com (Kevin Savetz), 1984
