-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurftrak_buttons.lua
68 lines (56 loc) · 1.83 KB
/
surftrak_buttons.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--[[
Map 3 joystick buttons to ArduSub SURFTRAK (range hold) functions.
See the README.md file for parameter settings.
]]--
function update()
local action_target_cm = param:get('SCR_USER1')
if action_target_cm == nil then
gcs:send_text(6, "surftrak_buttons.lua: set SCR_USER1 to rangefinder target, in cm")
return update, 10000
end
if action_target_cm < 100 then
gcs:send_text(6, "surftrak_buttons.lua: SCR_USER1 must be 100 (1m) or more")
return update, 10000
end
local action_inc_cm = param:get('SCR_USER2')
if action_inc_cm == nil then
gcs:send_text(6, "surftrak_buttons.lua: set SCR_USER2 to rangefinder target increment value, in cm")
return update, 10000
end
if action_inc_cm < 1 then
gcs:send_text(6, "surftrak_buttons.lua: SCR_USER2 must be 1 (1cm) or more")
return update, 10000
end
-- Get and clear button counts
local count = {}
for i = 1, 3 do
count[i] = sub:get_and_clear_button_count(i)
end
-- Ignore buttons if the rangefinder is unhealthy
for i = 1, 3 do
if count[i] > 0 and not sub:rangefinder_alt_ok() then
gcs:send_text(6, "surftrak_buttons.lua: rangefinder not ok, ignoring buttons")
return update, 200
end
end
local idx_set_target = 1
local idx_inc = 2
local idx_dec = 3
-- Set target
if count[idx_set_target] > 0 then
sub:set_rangefinder_target_cm(action_target_cm)
end
-- Increment or decrement
local net_inc = count[idx_inc] - count[idx_dec]
if net_inc ~= 0 then
local curr_target_cm = sub:get_rangefinder_target_cm()
local next_target_cm = curr_target_cm + net_inc * action_inc_cm
if next_target_cm < 80 then
gcs:send_text(6, "surftrak_buttons.lua: lower limit is 80cm")
next_target_cm = 80
end
sub:set_rangefinder_target_cm(next_target_cm)
end
return update, 200
end
return update(), 200