From 990126a58b9c39b96daffb95c788d614c6ab0129 Mon Sep 17 00:00:00 2001 From: Randy Mackay Date: Thu, 27 Jun 2024 15:58:18 +0900 Subject: [PATCH] AP_Scripting: example script to release gripper on thrust loss --- .../copter-release-gripper-thrust-loss.lua | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 libraries/AP_Scripting/examples/copter-release-gripper-thrust-loss.lua diff --git a/libraries/AP_Scripting/examples/copter-release-gripper-thrust-loss.lua b/libraries/AP_Scripting/examples/copter-release-gripper-thrust-loss.lua new file mode 100644 index 0000000000..131b215097 --- /dev/null +++ b/libraries/AP_Scripting/examples/copter-release-gripper-thrust-loss.lua @@ -0,0 +1,58 @@ +--[[ +This script should only be used on multicopters +The gripper is released when thrust loss is detected +Note that thrust loss false positives are common meaning the gripper may be released unnecessarily +--]] + +local RC_OPTION = {GRIPPER=19} +local AuxSwitchPos = {LOW=0, MIDDLE=1, HIGH=2} +local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7} + +local update_interval_ms = 100 +local thrust_loss_counter = 0 +local gripper_released = false + +function update() + + -- initialise gripper released state + if thrust_loss_counter == 0 then + gripper_released = false + end + + -- return immediately if not armed + if not arming:is_armed() then + thrust_loss_counter = 0 + return update, update_interval_ms + end + + -- return if no thrust loss + if not MotorsMatrix:get_thrust_boost() then + thrust_loss_counter = 0 + return update, update_interval_ms + end + + -- return if vehicle is not descending (or cannot retrieve climb rate) + local vel_NED = ahrs:get_velocity_NED() + if vel_NED == nil or vel_NED:z() <= 0 then + thrust_loss_counter = 0 + return update, update_interval_ms + end + + -- vehicle is descending and thrust loss is detected + thrust_loss_counter = thrust_loss_counter + 1 + if thrust_loss_counter < 10 then + return update, update_interval_ms + end + + -- release gripper and warn user + if not gripper_released then + if rc:run_aux_function(RC_OPTION.GRIPPER, AuxSwitchPos.LOW) then + gripper_released = true + gcs:send_text(MAV_SEVERITY.WARNING, "Thrust loss, gripper released!") + end + end + + return update, update_interval_ms +end + +return update, update_interval_ms