Making a Camera PTZ Decoder
by Michael Shaw, July 2012
PTZ Decoder
Interfaced with the Maxwell MP-101 Power Panner
|
The serial communications format that’s used to talk to
the PTZ decoder has been standardized over the years, with the most popular
format being the Pelco “D” protocol. I got the specifics of the Pelco D
protocol from the website at CommFront, http://www.commfront.com/RS232_Examples/CCTV/Pelco_D_Pelco_P_Examples_Tutorial.HTM.
The picture in figure1 shows a DVR 4 camera system with the PTZ control in the
bottom right panel. The DVR system controls a particular PTZ positioning device
using a specific address for each decoder. Up to 255 different addresses can be
assigned.
Figure 1, example of a DVR Pan Tilt
control panel
|
The Pelco “D” serial protocol will work with baud rates
from 300 baud up to 9600 baud. The
protocol consists of a 7 byte control message that contains the information
needed for controlling a PTZ device. The information in this control message
contains the address of the device being controlled and the direction and speed
of motion. The format of the command is as follows: (note: all values in hex)
Byte 1
|
Byte 2
|
Byte 3
|
Byte 4
|
Byte 5
|
Byte 6
|
Byte 7
|
Sync
|
Camera
Address
|
Command
1
|
Command
2
|
Data
1
|
Data
2
|
Checksum
|
Byte 1 (sync), the synchronization byte, fixed at FF
Byte 2 (Camera Address), defined range is between 1-FFByte 3 (Command 1), first set of control parameters (explained below)
Byte 4 (Command 2), second set of control parameters (explained below)
Byte 5 (Data 1), pan speed parameters ranging from 00 (stop) to 3F (high speed) and FF for turbo (maximum speed of device)
Byte 6 (Data 2), tilt speed parameters ranging from 00 (stop) to 3F (maximum)
Byte 7 (Checksum), sum of bytes (excluding sync byte)
then modulo 100
Bytes 3 and 4 are used for the Command 1 and Command 2 control
information and are encoded in the following format:
Bit 7
|
Bit 6
|
Bit 5
|
Bit 4
|
Bit 3
|
Bit 2
|
Bit 1
|
Bit 0
|
|
Command
1
|
Sense
|
Reserved
|
Reserved
|
Auto/Manual
Scan
|
Camera
On/Off
|
Iris
Close
|
Iris
Open
|
Focus
Near
|
Command
2
|
Focus
Far
|
Zoom
Wide
|
Zoom
Tele
|
Tilt
Down
|
Tilt
Up
|
Pan
Left
|
Pan
Right
|
Fixed
to 0
|
The Maxwell MP-101 electronic power
panner has a connector that is used by a remote control to control the position
of the camera. This connector will be used for interfacing the decoder to the
Maxwell power panner. All the Maxwell power panner needs is a contact closure
to activate the desired pan or tilt motion. The decoder uses relays to provide
this contact closure, so all that’s needed is to detect the Tilt up/down and
Pan left/right commands in the Pelco D message.
I used a PICAXE 18M2 chip as the
controller in this decoder design. The PICAXE 18M2 chip is real easy to work
with and provides enough I/O pins to do what I want to do. I’ll need 4 outputs
for the relays that will be controlling the direction of motion; these are tilt
up, tilt down, pan left and pan right. I also want to be able to provide a way
for assigning an address to the decoder, so I’ll need 8 pins for that. To make it easy, I’ve fixed the baud rate at
4800, but I’ll need a pin for the serial input coming from the RS485
transceiver chip and an output pin for a status LED. The schematic for the
decoder is pictured in figure 2.
Figure 2, PTZ Decoder Schematic
|
The
software checks for a total of 8 conditions for the pan tilt
operations since pan and tilt control could be combined in a single message.
The following is a truth table and the resulting values for the various
pan/tilt conditions:
Command
2
|
Tilt
Down
Bit
4
|
Tilt
Up
Bit 3
|
Pan
Left
Bit
2
|
Pan
Right
Bit
1
|
Fixed
to 0
Bit
0
|
16
|
8
|
4
|
2
|
1
|
|
2
|
X
|
||||
4
|
X
|
||||
8
|
X
|
||||
16
|
X
|
||||
10
|
X
|
X
|
|||
18
|
X
|
X
|
|||
12
|
X
|
X
|
|||
20
|
X
|
X
|
The design prototype was constructed in about a day on a PC board
from Radio Shack. The components were purchased from several different
suppliers. The average cost to build the decoder was around $50.
Picture of the PTZ
Decoder Prototype
|
The Decoder Parts List
Ref
|
Quantity
|
Description
|
Part
Number
|
Vendor
|
RLY1-RLY4
|
4
|
SPDT 5V Relay
|
275-0240
|
RadioShack
|
PCB1
|
1
|
Prototype PC Board
|
276-168
|
RadioShack
|
U2
|
1
|
RS485 transceiver, 5V, 8pin DIP
|
LTC485CN8#PBF-ND
|
Digi-Key
|
U3
|
1
|
5V voltage regulator
|
COM-00107
|
SparkFun
|
U1
|
1
|
PICAXE 18M2 Microcontroller
|
COM-10187
|
SparkFun
|
SOCKET1
|
1
|
18 pin DIP Socket
|
PRT-07940
|
SparkFun
|
SOCKET2
|
1
|
8 pin DIP Socket
|
PRT-07937
|
SparkFun
|
J1
|
1
|
DC Power Jack
|
PRT-00119
|
SparkFun
|
J2
|
1
|
2-pin PCB mount Spring Terminal
|
PRT-08073
|
SparkFun
|
C2
|
1
|
Capacitor, 100uf, 50VDC
|
272-1044
|
RadioShack
|
C3
|
1
|
Capacitor, 10uf, 10VDC
|
55047371
|
RadioShack
|
C1,C4
|
2
|
Capacitor, .1uf, 50VDC
|
399-4266-ND
|
Digi-Key
|
R5
|
1
|
330 ohm 0.25W resistor
|
271-1315
|
RadioShack
|
D5
|
1
|
Green LED
|
COM-09592
|
SparkFun
|
RN1
|
1
|
10K ohm, 9 resistor network
|
CSC10KY-ND
|
Digi-Key
|
R1-R4
|
4
|
10K ohm, 0.25W resistors
|
271-1335
|
RadioShack
|
SW1-SW8
|
1
|
8 position DIP switch
|
COM-08034
|
SparkFun
|
Q1-Q4
|
4
|
2N3904 NPN Small Signal Transistors
|
COM-00521
|
SparkFun
|
D1-D4
|
4
|
1N4005 Diodes
|
276-1104
|
RadioShack
|
PWR1
|
1
|
Wall Adapter Power Supply, 9VDC
|
TOL-00298
|
SparkFun
|
AR
|
24 AWG hookup Wire
|
This decoder design works as expected
with a typical DVR setup and is relatively inexpensive to make. You no longer have
to be satisfied with a single camera view and can position the camera remotely
to view the area of interest. In the next design I will incorporate PWM
controlled motors into the circuit, and eliminate the relays. This will allow
me to create a complete PTZ unit.
Below is a copy of the PICAXE code
'PTZ Decoder FOR PICAXE 18M2Below is a copy of the PICAXE code
'decodes Pelco "D" type signals
'By Mike Shaw 7/2012
'Settings Baud 4800,8,N,1
Symbol LED = C.2
Symbol Relay1 = C.6 ' Tilt Up
Symbol Relay2 = C.7 ' Tilt Down
Symbol Relay3 = C.0 ' Pan Right
Symbol Relay4 = C.1 ' Pan Left
Symbol stx = b0
Symbol adr = b1
Symbol cmd1 = b2
Symbol cmd2 = b3
Symbol dat1 = b4
Symbol dat2 = b5
Symbol chks = b6
Symbol my_adr = b7
Symbol tmp = b8
Symbol RX_PIN = C.5 'Input from RS485 chip
Symbol RX_BAUD = T4800 'Baud fixed at true 4800
Gosub GetADD
Do
Gosub GetPacket
If adr = my_adr Then
Gosub HandlePacket
End If
Loop
GetPacket:
Do
SerIn RX_PIN,RX_BAUD,stx,adr,cmd1,cmd2,dat1,dat2,chks
tmp = adr + cmd1 + cmd2 + dat1 + dat2
Loop Until tmp = chks
Return
GetADD: ' Read then Display Unit Address
my_adr = pinsB ^ 001111 'invert address
for b0 = 1 to my_adr
high LED
pause 300
low LED
pause 300
next b0
high LED
Return
HandlePacket:
low LED
if cmd2 = 2 then high Relay3 End If 'Pan Right
if cmd2 = 4 then high Relay4 End If 'Pan Left
if cmd2 = 8 then high Relay1 End If 'Tilt Up
if cmd2 = 10 then high Relay3 End If 'Relay1 Pan Right & Tilt Up
if cmd2 = 12 then high Relay4 End If 'Relay1 Pan Left & Tilt Up
if cmd2 = 16 then high Relay2 End If' 'Tilt Down
if cmd2 = 18 then high Relay3 End If 'Relay2 Pan Right & Tilt Down
if cmd2 = 20 then high Relay4 End If 'Relay2 Pan Left & Tilt Down
pause 1000 ' 1 sec delay
low Relay1,Relay2,Relay3,Relay4
high LED
Return