I am new to Lua but have been watching a lot of videos on Lua and ardupilot and managed to get started but am not able to get it working.
I am trying to create a simple script to run after the drone is armed and capture the Yaw position to be used when the “LAND” command is called in an Auto Mission. So when the drone takes off it will land with the same orientation as when it took off.
Here is what I have managed to get running but it’s not changing the drone’s YAW when landing.
Any help would be greatly appreciated.
Here is the script that I was able to piece together:
local takeoffYaw = nil – variable to store the yaw angle at takeoff
local landingYaw = nil – variable to store the desired yaw angle at landing
local takeoffYawRad = nil – variable to store the yaw angle at takeoff in radians
local armed = false
local landed = false – flag to track if the vehicle has landed
if not arming:is_armed() then
armed = false – vehicle is disarmed
return update, 100
if not armed then -- vehicle just got armed armed = true -- Get the takeoffYaw every time the vehicle is armed takeoffYaw = math.deg(ahrs:get_yaw()) -- record the yaw angle at takeoff takeoffYawRad = ahrs:get_yaw() -- record the yaw angle at takeoff in radians gcs:send_text(0, "Takeoff yaw recorded: " .. takeoffYaw) end if takeoffYaw then -- if takeoffYaw is set if not landingYaw then -- if landingYaw is not set landingYaw = takeoffYaw -- set the desired yaw angle at landing to match takeoff yaw gcs:send_text(0, "Desired yaw at landing: " .. landingYaw) else local currentYaw = math.deg(ahrs:get_yaw()) -- get the current yaw angle local yawDifference = currentYaw - takeoffYaw -- calculate the difference between current yaw and takeoff yaw -- Check if the drone is approaching the landing stage if vehicle:get_mode() == "LAND" and not landed then gcs:send_text(0, "Yaw difference at landing: " .. yawDifference) landed = true -- mark the vehicle as landed end end end return update, 1000 -- reschedule the loop
return update() – run immediately before starting to reschedule