-
Notifications
You must be signed in to change notification settings - Fork 17.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scripting: added copter deploy example #26651
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--[[ | ||
script to auto-deploy a vehicle on descent after reaching a specified altitude | ||
uses raw pressure to not depend on either GPS or on home alt | ||
--]] | ||
local PARAM_TABLE_KEY = 72 | ||
local PARAM_TABLE_PREFIX = "DEPL_" | ||
local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7} | ||
|
||
---@diagnostic disable: missing-parameter | ||
---@diagnostic disable: param-type-mismatch | ||
|
||
-- bind a parameter to a variable | ||
function bind_param(name) | ||
local p = Parameter() | ||
assert(p:init(name), string.format('could not find %s parameter', name)) | ||
return p | ||
end | ||
|
||
-- add a parameter and bind it to a variable | ||
function bind_add_param(name, idx, default_value) | ||
assert(param:add_param(PARAM_TABLE_KEY, idx, name, default_value), string.format('could not add param %s', name)) | ||
return bind_param(PARAM_TABLE_PREFIX .. name) | ||
end | ||
|
||
assert(param:add_table(PARAM_TABLE_KEY, PARAM_TABLE_PREFIX, 10), 'could not add param table') | ||
|
||
local end_pos = bind_add_param('OPEN_POS', 1, 1200) | ||
local offset = bind_add_param('OPEN_OFFSET', 2, 400) | ||
local deployment_alt = bind_add_param('ALT', 3, 1) | ||
local DEPL_CLIMB_ALT = bind_add_param('CLIMB_ALT', 4, 2) | ||
local base_pressure = nil | ||
local SERVO_FUNCTION = 94 | ||
local reached_climb_alt = false | ||
local deployed = false | ||
|
||
local MODE_LAND = 9 | ||
|
||
function update_state() | ||
local pressure = baro:get_pressure() | ||
if not pressure then | ||
return | ||
end | ||
if not base_pressure then | ||
base_pressure = pressure | ||
end | ||
local altitude = baro:get_altitude_difference(base_pressure, pressure) | ||
if not altitude then | ||
return | ||
end | ||
gcs:send_named_float('DALT',altitude) | ||
logger.write("DEPL",'BP,P,Alt','fff', | ||
base_pressure, pressure, altitude) | ||
if not reached_climb_alt then | ||
local start_pos = end_pos:get() + offset:get() | ||
SRV_Channels:set_output_pwm(SERVO_FUNCTION, start_pos) | ||
if altitude > DEPL_CLIMB_ALT:get() then | ||
reached_climb_alt = true | ||
gcs:send_text(MAV_SEVERITY.ERROR, "DEPL: Reached climb alt") | ||
end | ||
return | ||
end | ||
if deployed then | ||
return | ||
end | ||
if altitude > deployment_alt:get() then | ||
return | ||
end | ||
deployed = true | ||
gcs:send_text(MAV_SEVERITY.INFO, "DEPL: deploying") | ||
vehicle:set_mode(MODE_LAND) | ||
arming:arm_force() | ||
if not vehicle:get_mode() == MODE_LAND or not arming:is_armed() then | ||
gcs:send_text(MAV_SEVERITY.INFO, "DEPL: Arming failed") | ||
return | ||
end | ||
--SRV_Channels:set_output_pwm(SERVO_FUNCTION, end_pos:get()) | ||
gcs:send_text(MAV_SEVERITY.INFO, "DEPL: Deployed successfully") | ||
end | ||
function update() -- this is the loop which periodically runs | ||
update_state() | ||
return update, 20 -- Reschedules the loop at 50Hz | ||
end | ||
gcs:send_text(MAV_SEVERITY.INFO, "DEPL: loaded") | ||
return update() -- Run immediately before starting to reschedule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be preferable to check the number of arguments being passed to the C function (
lua_gettop
?) rather than creating methods like this, IMO.