Skip to content

Commit

Permalink
AP_Scripting: copter-pos-offsets supports z-axis
Browse files Browse the repository at this point in the history
  • Loading branch information
rmackay9 committed Sep 30, 2024
1 parent 04e2574 commit e616280
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions libraries/AP_Scripting/examples/copter-posoffset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTI
-- setup script specific parameters
local PARAM_TABLE_KEY = 71
local PARAM_TABLE_PREFIX = "PSC_OFS_"
assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 4), 'could not add param table')
assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 6), 'could not add param table')

-- add a parameter and bind it to a variable
function bind_add_param(name, idx, default_value)
Expand Down Expand Up @@ -43,6 +43,16 @@ local PSC_OFS_POS_N = bind_add_param("POS_N", 1, 0)
--]]
local PSC_OFS_POS_E = bind_add_param("POS_E", 2, 0)

--[[
// @Param: PSC_OFS_POS_D
// @DisplayName: Position Controller Position Offset Down
// @Description: Position controller offset Down
// @Range: 0 1000
// @Units: m
// @User: Standard
--]]
local PSC_OFS_POS_D = bind_add_param("POS_D", 3, 0)

--[[
// @Param: PSC_OFS_VEL_N
// @DisplayName: Position Controller Velocity Offset North
Expand All @@ -51,7 +61,7 @@ local PSC_OFS_POS_E = bind_add_param("POS_E", 2, 0)
// @Units: m/s
// @User: Standard
--]]
local PSC_OFS_VEL_N = bind_add_param("VEL_N", 3, 0)
local PSC_OFS_VEL_N = bind_add_param("VEL_N", 4, 0)

--[[
// @Param: PSC_OFS_VEL_E
Expand All @@ -61,10 +71,17 @@ local PSC_OFS_VEL_N = bind_add_param("VEL_N", 3, 0)
// @Units: m/s
// @User: Standard
--]]
local PSC_OFS_VEL_E = bind_add_param("VEL_E", 4, 0)
local PSC_OFS_VEL_E = bind_add_param("VEL_E", 5, 0)

-- welcome message to user
gcs:send_text(MAV_SEVERITY.INFO, "copter-posoffset.lua loaded")
--[[
// @Param: PSC_OFS_VEL_D
// @DisplayName: Position Controller Velocity Offset Down
// @Description: Position controller velocity offset Down
// @Range: 0 10
// @Units: m/s
// @User: Standard
--]]
local PSC_OFS_VEL_D = bind_add_param("VEL_D", 6, 0)

function update()

Expand All @@ -73,30 +90,32 @@ function update()
return update, 1000
end

local pos_offsets_zero = PSC_OFS_POS_N:get() == 0 and PSC_OFS_POS_E:get() == 0
local vel_offsets_zero = PSC_OFS_VEL_N:get() == 0 and PSC_OFS_VEL_E:get() == 0
local pos_offsets_zero = PSC_OFS_POS_N:get() == 0 and PSC_OFS_POS_E:get() == 0 and PSC_OFS_POS_D:get() == 0
local vel_offsets_zero = PSC_OFS_VEL_N:get() == 0 and PSC_OFS_VEL_E:get() == 0 and PSC_OFS_VEL_D:get() == 0

-- if position offsets are non-zero or all offsets are zero then send position offsets
if not pos_offsets_zero or vel_offsets_zero then
-- set the position offset in meters in NED frame
local pos_offset_NED = Vector3f()
pos_offset_NED:x(PSC_OFS_POS_N:get())
pos_offset_NED:y(PSC_OFS_POS_E:get())
pos_offset_NED:z(PSC_OFS_POS_D:get())
if not poscontrol:set_posvelaccel_offset(pos_offset_NED, Vector3f(), Vector3f()) then
gcs:send_text(MAV_SEVERITY.ERROR, "copter-posoffset: failed to set pos offset")
end
test_position = not pos_offsets_zero
else
-- test velocity offsets in m/s in NED frame
local vel_offset_NED = Vector3f()
vel_offset_NED:x(PSC_OFS_VEL_N:get())
vel_offset_NED:y(PSC_OFS_VEL_E:get())
-- get position offset (cumulative effect of velocity offsets) and use to slowly move back to waypoint
-- first get position offset (cumulative effect of velocity offsets)
local pos_offset_NED, _, _ = poscontrol:get_posvelaccel_offset()
if pos_offset_NED == nil then
print_warning("unable to get dist to waypoint")
print_warning("unable to get position offset")
pos_offset_NED = Vector3f()
end
-- set velocity offsets in m/s in NED frame
local vel_offset_NED = Vector3f()
vel_offset_NED:x(PSC_OFS_VEL_N:get())
vel_offset_NED:y(PSC_OFS_VEL_E:get())
vel_offset_NED:z(PSC_OFS_VEL_D:get())
if not poscontrol:set_posvelaccel_offset(pos_offset_NED, vel_offset_NED, Vector3f()) then
gcs:send_text(MAV_SEVERITY.ERROR, "copter-posoffset: failed to set vel offset")
end
Expand Down

0 comments on commit e616280

Please sign in to comment.