From 07e875aba2893ba2f24288d66efc961971c0293b Mon Sep 17 00:00:00 2001 From: Randy Mackay Date: Wed, 25 Aug 2021 10:47:16 +0900 Subject: [PATCH] AP_Scripting: add copter-circle-speed.lua example Co-authored-by: Iampete1 --- .../examples/copter-circle-speed.lua | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 libraries/AP_Scripting/examples/copter-circle-speed.lua diff --git a/libraries/AP_Scripting/examples/copter-circle-speed.lua b/libraries/AP_Scripting/examples/copter-circle-speed.lua new file mode 100644 index 0000000000..a20d797f7d --- /dev/null +++ b/libraries/AP_Scripting/examples/copter-circle-speed.lua @@ -0,0 +1,36 @@ +-- Maintain a constant ground speed while flying in Circle mode even if the pilot adjusts the radius +-- +-- CAUTION: This script only works for Copter +-- this script waits for the vehicle to be armed and in circle mode and then +-- updates the target rate around the circle (in degrees / sec) to maintain the desired ground speed + +local circle_ground_speed = 6 -- the ground speed in circle mode (m/s) + +function update() -- this is the loop which periodically runs + + -- must be armed, flying and in circle mode + if (not arming:is_armed()) or (not vehicle:get_likely_flying()) or (vehicle:get_mode() ~= 7) then + return update, 1000 -- reschedules the loop, 1hz + end + + -- get circle radius + local radius = vehicle:get_circle_radius() + if not radius then + gcs:send_text(0, "speed-circle.lua: failed to get circle radius") + return update, 1000 + end + + -- set the rate to give the desired ground speed at the current radius + local new_rate = 360 * (circle_ground_speed / (radius*math.pi*2)) + new_rate = math.max(new_rate, -90) + new_rate = math.min(new_rate, 90) + if not vehicle:set_circle_rate(new_rate) then + gcs:send_text(0, "speed-circle.lua: failed to set rate") + else + gcs:send_text(0, string.format("speed-circle.lua: radius:%f new_rate=%f", radius, new_rate)) + end + + return update, 100 -- reschedules the loop, 10hz +end + +return update()