Welcome

Thanks for visiting the Mike's Make blog. It is a compilation of many of my interests. I hope you find the articles in this blog helpful and entertaining. Leave a comment and let me know what you think.

Wednesday, July 11, 2012

Making a Camera PTZ Decoder


Making a Camera PTZ Decoder

by Michael Shaw, July 2012

 This article explains the design of a Pan-Tilt-Zoom (PTZ) decoder that is used with a camera surveillance system. These systems are used for remotely positioning surveillance cameras and uses twisted pair RS485 for the physical connection. I made this decoder for use with a Maxwell MP-101 electronic power panner, and since this unit only has pan and tilt capability, this article is really about making a pan/ tilt decoder.

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-FF

Byte 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 for the decoder is pretty straight forward. When it first starts, it reads the unit address switches then waits for a serial message to arrive. When a serial message is received, it verifies that the checksum is correct and then checks the unit address for a match. If the addresses match then it checks for the pan and tilt commands in the command 2 message.
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 18M2
 '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











15 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks! Ok, I'll place the code at the end of the article

    ReplyDelete
    Replies
    1. Hi Mike
      Excelent article . I need your help to just get the address bit from the camera and decode to binary with only 4 bits, is it possible? I intend to do this to create a small video matrix to be commanded by a Pelco KBD-300 controller. Example: By typing on Pelco's controller table the address of camera 3 at the decoder output it shows me 0010.

      Delete
    2. Hi Mike ,
      Excelent article . I need your help to just get the address bit from the camera and decode to binary with only 4 bits, is it possible? I intend to do this to create a small video matrix to be commanded by a Pelco controller. Example: By typing on Pelco's controller table the address of camera 3 at the decoder output it shows me 0010.

      Delete
    3. yes it's possible to use only 4 bits. That would limit you to only 1-16 address range. I used 6 bits (1-64 address) because I didn't have enough io to do an 8 bit codes. Just change the line my_adr = pinsB ^ 001111 to my_adr = pinsB ^ 1111.

      Delete
  3. Hi Mike,
    Great article. I followed it from the Jameco Page. I see the picture of a populated PCB above, but do not see a link for purchase or a parts list which includes the board. Is this a "yet to come" prototype?
    Looks like it includes the Zoom function as well (6 relays as opposed to 4). Thanks again for the great post.
    Pat

    ReplyDelete
    Replies
    1. Hi PJHALL,

      wanted to let you know that Jameco now has this available as a kit on their website at:

      http://www.clubjameco.com/index.php/projects/project_details/24/20808

      Shawshack Mike

      Delete
    2. Hi Mike,

      I bought one of your kits from Jameco which was easy to build and appears to be very well thought out. Unfortunately, Jameco seem to have dropped the ball on two of the parts required to complete the kit. The power switch they supplied doesn't fit the board (at least the one I received didn't fit) and there is no 2-pin PCB mount Spring Terminal (P/N PRT-08073) supplied in the kit. They show it in the instruction on step 16, but the part isn't listed on the Bill of Materials.

      I don't believe they supplied enough instructions with the kit to get it to actually work once it's assembled. There is no schematic and the instructions on how to program the address are there, but are incomplete. All the instructions say to do is to, "Use the PROG button to program a known address into the unit." Nothing else is mentioned on how to do this and the procedure is different from the procedure used to program the address for the circuit on this page. I assume, from reading the new code for the kit, that you push the PROG button while sending a command from the controller (which contains the address in the command string) for the decoder you are programming. A little more information on how to get the kit running would be nice to have and a schematic of the new circuit would be greatly helpful in troubleshooting the circuit to determine what's wrong if it doesn't work.

      Dave

      Delete
    3. Dave, Jameco has updated the kit to make it easier to program the address and there a schematic on the web page as well at http://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_2169475_-1

      Delete
  4. good morning
    can you help me:
    how to Select pelco d or pelco p and get add 1-32 use sw1,sw2,sw3,sw4,sw5,sw6,sw7,sw8 ?????

    example:
    ------------------------------------------------
    Protocol setting
    (SW1 + SW2):
    01==PelcoD
    11==PelcoP
    -------------------------------------------------------------
    ID Adress Setting 1= ON 0 = OFF

    ID SW3 SW4 SW5 SW6 SW7 SW8

    0 0 0 0 0 0 0
    1 0 0 0 0 0 1
    2 0 0 0 0 1 0
    3 0 0 0 0 1 1
    4 0 0 0 1 0 0
    5 0 0 0 1 0 1
    6 0 0 0 1 1 0
    7 0 0 0 1 1 1
    8 0 0 1 0 0 0
    9 0 0 1 0 0 1
    10 0 0 1 0 1 0
    11 0 0 1 0 1 1
    12 0 0 1 1 0 0
    13 0 0 1 1 0 1
    14 0 0 1 1 1 0
    15 0 0 1 1 1 1
    16 0 1 0 0 0 0
    17 0 1 0 0 0 1
    ...
    62 1 1 1 1 1 0
    63 1 1 1 1 1 1

    thanks a lot
    have fun

    ReplyDelete
  5. I guess, but why 2 bits for Pelco-P/Pelco-D, only need 1, 0=Pelco-P, 1=Pelco-D. Then you'll have 64 address.

    ReplyDelete
  6. Nice post...... Information about Digital Camera are really important for picture clarity . To get more information related to this system contact here Ptz Camera and get high class information

    ReplyDelete
  7. Wow nice ptz camera having all new features in it and within budget.

    ReplyDelete
  8. This product has good specification and seems to offer great benefits to the users. I' also using same kind ofptz camera for security.

    ReplyDelete
  9. Wow,yo got a bigger list of Pelco products, I got my Pelco Camera from this site, https://www.123securityproducts.com/pelco.html

    ReplyDelete