Fun with PTZ cameras over RS485 using PropForth
Peter Jakacki
Posts: 10,193
I had to interface to a PTZ dome camera last night and since I had an incomplete spec I thought I would interact with it in Forth so as to debug it and discover what I could do interactively. If you have access to a PTZ but were to afraid to try anything then this code will make it easy. You just need an RS485 chip such as a MAX485 or some other suitable device and 2 space Prop I/Os. Load up PropForth 4.6, connect the serial port at 57600 baud and paste in this code. To get started type in 40 LEFT . With Forth you can experiment and make new definitions. Most cameras support presets as well so once you get a particular PTZ setting you can just type in 1 SET for instance to save this to preset 1 and do this for other presets. Later on you could type 1 GOTO and it will jump back to that position and zoom setting. You could even name these presets in Forth with words like HOME instead of 1 GOTO etc.
fl [ifdef PTZ.fth forget PTZ.fth ] : PTZ.fth ; \ maintain a header here - also enables easy forget and reload { These words are used for testing a PELCO-D PTZ camera and they can be used interactively from the terminal or built into an application. Only RS485 transmit is supported in this version with a simple bit-basher. Baud rate default for Pelco-D is 2400 This version works with the standard PropForth 4.6 } \ Non-critical redefinitions to suit my personal preferences : ms delms ; decimal \ pin definitions 20 constant TR485 \ Transmit/Receive RS485 21 constant TE485 \ Transmit Enable RS485 \ constants I use all the time 0 constant OFF -1 constant ON hex : BitDelay 86 0 do loop ; \ bit delay for 2400 baud transmission : CharDelay 12 0 do BitDelay loop ; : TE \ ( on/off -- ) TR485 pinhi TR485 pinout \ transmit line is idle TE485 pinout if TE485 pinhi CharDelay \ turn on TE and wait at least a character time else CharDelay TE485 pinlo \ wait at least a character time then turn off then ; variable chksum \ Simple serial bit-basher with checksum : SEND \ ( char -- ) dup chksum W+! TR485 pinlo BitDelay 8 0 do dup 1 and TR485 px \ output lsb BitDelay 2/ \ for bit time then shift next bit loop drop \ discard shifted data TR485 pinhi BitDelay \ 1 stop bit ; variable camera : CAMERA \ ( address -- ) \ in case we want to address another camera other than the default #1 camera W! ; : PTZ \ ( data cmd -- ) ON TE \ turn on RS485 to transmit FF SEND \ synch byte 0 chksum W! \ maintain checksum from this point camera C@ 1 max SEND \ camera address or default of 1 dup SEND 8 rshift SEND \ 2 command bytes dup SEND 8 rshift SEND \ 2 data bytes chksum C@ SEND \ finally the simple checksum OFF TE ; \ ************* PTZ Commands ************* : STOP 0 0 PTZ ; \ Zoom : IN 1 2000 PTZ ; : OUT 1 4000 PTZ ; \ Focus : NEAR 0 1 PTZ ; : FAR 0 8000 PTZ ; \ Pan(speed) : LEFT 0400 PTZ ; : RIGHT 0200 PTZ ; \ Tilt(speed) : UP 8 lshift 0800 PTZ ; : DOWN 8 lshift 1000 PTZ ; : FLIP 2100 0700 PTZ ; \ Presets(n) : GOTO 8 lshift 0700 PTZ ; : SET 8 lshift 0300 PTZ ; : CLR 8 lshift 0500 PTZ ; : RESET 0 2900 PTZ ; \ On/off functions etc : FNC swap if GOTO else SET then ; : COLOR 43 FNC ; : MIRROR 3F FNC ; : FOCUS 3B FNC ; : IRIS 3C FNC ; : TITLE 40 FNC ; \ Examples decimal : WIGGLES STOP 0 do 200 LEFT 750 ms 200 RIGHT 750 ms loop STOP ; : DEMO OFF COLOR OUT 2000 ms STOP \ should be zoomed out (wide) ON COLOR 40 LEFT 1000 ms 40 RIGHT 1000 ms STOP 1 SET \ remember this position and zoom 100 UP 500 ms 100 DOWN 2000 ms \ should be fully down by now STOP IN 1000 ms STOP \ zoom in a little 50 UP 40 LEFT 5000 ms \ lets do a helical scan for a few seconds STOP 1 GOTO \ go back to our preset 3 WIGGLES ;
Comments
I definitely have to start learning forth and propforth..
Nice, px was tailor made for you.
cheers
Ron
When it comes to testing new hardware then there is nothing better than having a Forth to run interactively on the target. There are many times when I have developed new hardware and I need to test it's limits or implement some unique features and without Forth this can turn into a long and boring exercise yet with Forth it's so quick.
I even had a simple thing the other day where I had a client over in my workshop (rare) while I worked on his machine and I was just testing the pneumatic solenoids and I asked him how does the operator know if there has been a jam. He said that they just look over occasionally which I thought was rather silly but an audible alert would have to be very loud to be heard on a production line. So I quickly typed a one-liner to toggle these solenoids with variable frequency and period. Guess what? It works really well and those solenoids are really loud and can be made to sing different songs for different alerts. Another quick one-liner and I had HEE-HAW and HELP and OKAY sounds etc. However one drawback is that they are really annoying while testing
Thanks to you Prof and to Sal for all your efforts in regards to PropForth.