Skip to content

Commit 1d810bf

Browse files
petrosiliuspeterbarker
authored andcommitted
AP_Scripting: added tracker Pelco-D control script
This script uses the scaled output from the antennatracker servos and map them to corresponding Pelco-D messages to be sent via a RS-485 interface to a motorized base (can be anything from motorized tracker to a PTZ camera). If your FCU doesnt offer a RS-485 interface by default, you can use or TTL-RS485- or USB-RS485-adapters. Pelco-D allows to control using either speed-/differential- or absolute-control control of the pan-/tilt-axis. Currently the script uses speed based control using by mapping the "ContinuousRotation" type servos outputs to the corresponding Pelco-D messages. The absolute control messages are implemented nevertheless for future use. The script assumes therefor at least the following parameters to be set: SCR_ENABLE = 1 SERVO_PITCH_TYPE = 2 # ContinuousRotation type servo SERVO_YAW_TYPE = 2 # ContinuousRotation type servo SERIALx_PROTOCOL = 28 # serial port used by luascript Additionally the PITCH2SRV and YAW2SRV tuning needs to be done as described by the antennatracker description. Also keep attention to the PITCH_MIN, PITCH_MAX and YAW_RANGE parameters to fit your Pelco-D hardware!
1 parent 5fe4fec commit 1d810bf

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
--[[
2+
Pelco-D control implementation for antennatracker.
3+
Implemented by using knowledge from the excellent python implementation in https://gist.github.com/jn0/cc5c78f4a0f447a6fb2e45a5d9efa13d.
4+
--]]
5+
6+
local SERVO_PAN = 71
7+
local SERVO_TILT = 72
8+
local SERIAL_BAUD = 9600
9+
10+
-- Antennattracker modes
11+
local MODE_STOP = 1
12+
local MODE_SCAN = 2
13+
local MODE_SERVOTEST = 3
14+
local MODE_GUIDED = 4
15+
16+
gcs:send_text(0, "Starting Pelco-D Control")
17+
18+
local port = assert(serial:find_serial(0), "Pelco-D: No Scripting Serial Port")
19+
port:begin(SERIAL_BAUD)
20+
port:set_flow_control(0)
21+
22+
function set_bit(value, n)
23+
return value | (0x01 << n)
24+
end
25+
26+
function PelcoD_msg_addchecksum(msg)
27+
local sum = 0
28+
for i = 2, #msg-1 do
29+
sum = sum + msg[i]
30+
end
31+
checksum = sum % 256
32+
msg[7] = checksum
33+
end
34+
35+
function PelcoD_move(panspeed, tiltspeed)
36+
local command = 0x00
37+
local scale = 63 -- max pelcod speed
38+
if panspeed < 0 then -- left
39+
command = set_bit(command, 2)
40+
elseif panspeed > 0 then -- right
41+
command = set_bit(command, 1)
42+
end
43+
if tiltspeed < 0 then -- down
44+
command = set_bit(command, 4)
45+
elseif tiltspeed > 0 then -- up
46+
command = set_bit(command, 3)
47+
end
48+
local msg = {0xFF, 0x01, 0x00, command, math.floor(math.abs(panspeed) * scale), math.floor(math.abs(tiltspeed) * scale), 0x00}
49+
PelcoD_msg_addchecksum(msg)
50+
return msg
51+
end
52+
53+
-- write msg to the serial port
54+
function send_message(msg)
55+
for _, v in ipairs(msg) do
56+
port:write(v)
57+
end
58+
end
59+
60+
function update()
61+
tilt_norm = SRV_Channels:get_output_scaled(SERVO_TILT)
62+
pan_norm = SRV_Channels:get_output_scaled(SERVO_PAN)
63+
if (vehicle:get_mode() == MODE_SCAN or vehicle:get_mode() == MODE_SERVOTEST or vehicle:get_mode() == MODE_GUIDED) then
64+
-- Limit pan and tilt to -1...+1
65+
pan_norm=math.max(pan_norm,-1.0)
66+
pan_norm=math.min(pan_norm,1.0)
67+
tilt_norm=math.max(tilt_norm,-1.0)
68+
tilt_norm=math.min(tilt_norm,1.0)
69+
local msg=PelcoD_move(-pan_norm,tilt_norm)
70+
send_message(msg)
71+
elseif (vehicle:get_mode() == MODE_STOP) then
72+
local msg=PelcoD_move(0,0)
73+
send_message(msg)
74+
end
75+
76+
return update, 20 -- 50 hz
77+
end
78+
79+
PelcoD_move(0,0)
80+
return update()
81+
82+
83+
--[[
84+
function PelcoD_pan_absolute_position(degrees)
85+
centidegrees = degrees*100
86+
local msg = {0xFF, 0x01, 0x00, 0x4b, (centidegrees >> 8) & 255 , centidegrees & 255, 0x00}
87+
PelcoD_msg_addchecksum(msg)
88+
return msg
89+
end
90+
91+
function PelcoD_tilt_absolute_position(degrees)
92+
centidegrees = degrees*100
93+
local msg = {0xFF, 0x01, 0x00, 0x4d, (centidegrees >> 8) & 255 , centidegrees & 255, 0x00}
94+
PelcoD_msg_addchecksum(msg)
95+
return msg
96+
end
97+
98+
function PelcoD_zoom_absolute_position(position)
99+
local msg = {0xFF, 0x01, 0x00, 0x4f, (position >> 8) & 255 , position & 255, 0x00}
100+
PelcoD_msg_addchecksum(msg)
101+
return msg
102+
end
103+
104+
function PelcoD_zero_absolute_position(degrees)
105+
centidegrees = degrees*100
106+
local msg = {0xFF, 0x01, 0x00, 0x49, 0x00, 0x00, 0x00}
107+
PelcoD_msg_addchecksum(msg)
108+
return msg
109+
end
110+
--]]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Pelco-D antennatracker lua script
2+
3+
This scripts uses the scaled output from the antennatracker servos and map them to corresponding Pelco-D messages to be sent via a RS-485 interface to a motorized base (can be anything from motorized tracker to a PTZ camera). If your FCU doesnt offer a RS-485 interface by default, you can use or TTL-RS485- or USB-RS485-adapters. I tested this script using a USB-RS485 adapter using Linux/Obal board and a Hikvision PTZ camera.
4+
5+
Pelco-D allows to control using either speed-/differential- or absolute-control control of the pan-/tilt-axis. Currently the script uses speed based control using by mapping the "ContinuousRotation" type servos outputs to the corresponding Pelco-D messages. The absolute control messages are implemented nevertheless for future use.
6+
7+
The script assumes the following parameters to be set:
8+
9+
SCR_ENABLE = 1
10+
SERVO_PITCH_TYPE = 2 # ContinuousRotation type servo
11+
SERVO_YAW_TYPE = 2 # ContinuousRotation type servo
12+
SERIALx_PROTOCOL = 28 # replace 'x' with the serial port used by luascript
13+
14+
Additionally the PITCH2SRV, YAW2SRV tuning needs to be done as described by the antennatracker description.
15+
Also keep attention to the PITCH_MIN, PITCH_MAX and YAW_RANGE parameters to fit your Pelco-D hardware!
16+

0 commit comments

Comments
 (0)