Turtle Mode Script

Hi there,

I am trying my hand out at writing Lua scripts. This one is for allowing the pilot to enable Turtle mode. Since most pilots don’t need it often enough to permanently tie up an RC switch, I thought maybe sharing it would be helpful to someone. Any feedback on the script is welcome too.

I have Stab, AltH, and Loit set on a three position switch on my TBS Tango 2. What the script does is monitor for a certain pitch or roll angle in the event the vehicle has landed up against an object like a wall or possibly upside down after a crash, and then swaps my Loit mode for Turtle until the pilot has been able to correct the vehicle. This has proven to be useful for me already on several occasions during my missions.

function update()
  local roll_angle = math.abs(math.deg(ahrs:get_roll()) )
  local pitch_angle = math.abs(math.deg(ahrs:get_pitch()))
  local turtle_angle = 50
  local turtle_available = -1

  if (not arming:is_armed()) and ((roll_angle > turtle_angle) or (pitch_angle > turtle_angle)) then
	turtle_available = 1
  else
	turtle_available = 0
  end

  -- If pilot switches to LOIT flight mode (5) while turtle mode is available, then the script will switch flight mode to Turtle (28)
  -- If pilot is in Turtle flight mode while turtle is no longer available, then the script will switch the flight mode back to LOIT
  if turtle_available == 1 and vehicle:get_mode() == 5 then
	vehicle:set_mode(28)
  elseif turtle_available == 0 and vehicle:get_mode() == 28 then
	vehicle:set_mode(5)
  end

  return update, 100
end

return update()

Thanks for looking!

2 Likes