Automatically change flight mode to auto when EKF/GPS signal becomes GOOD


I am building an autonomous boat and have everything pretty much working how I want it. The only issue is EKF needs to be locked and good before I am able to change the mode to auto. Naturally, if the boat is in the ocean and the flight controller needs to be rebooted, the flight mode needs to be set to auto automatically. Is there a way for me to do this (automatically change flight mode to auto after EKF and GPS lock)? Currently, I have INITIAL_MODE set to 10, but it will fail the change on startup because the GPS/EKF are not good. It takes probably 5 minutes at times to get a good lock on both before I can then change the flight mode.


This doesn’t answer your question but have you got INITIAL_MODE when set to 10 (Auto) to ever work? I was playing around with this a few weeks ago and it would not start in Auto. “Unknown” is shown in the HUD and Flight mode change failed in the Messages screen.

Actually I think we are talking about the same issue. The initial mode is selected before the GPS driver even loads when looking at the messages screen. So I don’t know how this can work w/o a delay.

initial mode


Hi Dave,

Never got it to work. It’ll really only work for modes which don’t require a valid GPS signal, otherwise you’ll get the flight change mode failed message. There’s got to be a way to automatically set the flight mode to auto after a certain amount of time or on an event change such as ekf status flag…

I tried setting flight mode in a flight plan but again, unless the flight controller is set to auto, the flight plan won’t even start…

Right, I don’t see how it can work. A Lua script maybe. What Flight Controller do you have? This is what I’m thinking using these commands.

set mode

Hi Dave,

Unsure if the GPS messages alone will work as I currently see that even with a 3dgps lock, the ekf may still not stabilize (shows red in mission planner status window). It’s only until the velocity_horiz and pos_horiz_abs flags go from red to white will the flight change mode to auto be available.


A lua script should definitely be able to do this, so now its about finding a library file (common.h?) which can read the ekf flags i think.

Hi Adrian-Yes, I see what you mean. Those status messages look to be on the right track.

Hi Dave,

So I have the pixhawk 1 and found out when I went to go enable scripts that I do not have the two values to toggle to enable it. Back to the drawing board…

Yea,that’s why I asked about your FC. I have one of those also.

I think there are potentially a couple of issues:

  • The initial mode feature may simply not work (issue here)
  • Rover-4.0 had additional checks added that stopped the vehicle from switching to Auto mode even while disarmed. I think this is fixed in “latest” but hasn’t been released yet. There’s a related issue here.

The Lua script method probably will work and an easy way to do it would be to use the AHRS’s home-is-set binding (here is a link to Lua scripting bindings available in Rover-4.0)

Thanks guys,

For the time being I skipped any Ardupilot/Pixhawk configuration of this desired automatically started auto mode over to the arduino companion computer.

Below is my implementation using the Arduino mavlink protocol:

I call the function below while checking whether or not I have a GPS lock that is greater than 3 and if the mode changes successfully, start the motor. In case others want to go this route, I also must mention I have a heartbeat monitor function which checks whether or not the mode has successfully changed to 10 (auto), otherwise keep running the function below every few seconds. This ensures the auto mode is set before commencing its journey.

void setmode_Auto() {
  //Set message variables
  uint8_t _system_id = 255; // id of computer which is sending the command (ground control software has id of 255)
  uint8_t _component_id = 2; // seems like it can be any # except the number of what Pixhawk sys_id is
  uint8_t _target_system = 1; // Id # of Pixhawk (should be 1)
  uint8_t _base_mode = 1;
  uint32_t _custom_mode = 10; //10 = auto mode

  // Initialize the required buffers
  mavlink_message_t msg;
  uint8_t buf[MAVLINK_MAX_PACKET_LEN];

  // Pack the message
  mavlink_msg_set_mode_pack(_system_id, _component_id, &msg, _target_system, _base_mode, _custom_mode);
  uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);  // Send the message (.write sends as bytes)
  Serial.print("\nsending set mode command...");
  Serial1.write(buf, len); //Write data to serial port

Hi all
I would like to implement a similar feature.

  • as soon as the rover is disarmed and later armed again the lua script should put the rover in loiter mode
  • as soon as I am ready with my GCS I would like to have the lua script deactivated and control the rover over telemetry.

Would something like that work?

If ~is_armed()
if is_armed() & prearm_healthy()
gps: status(instance)
ahrs: get_position()
check if position is reasonable (maybe gps: status(instance)
AP_AHRS method home_is_set boolean`
set_mode( 5 )