MAV_CMD_DO_SET_MODE all possible modes

Hi everybody,
I am developing some pymavlink /dronekit code for PX4 flight controller.
I would like to send a command_long in order to change the flight mode. Unfortunately I cannot find anywhere the list of numbers corresponding to PX4 flight modes for MAV_CMD_DO_SET_MODE mavlink message.

Thank you very much for your help

P.S. Here is the dronekit commands I am using, if you have any suggestion I will be happy to hear them

vehicle._master.mav.command_long_send(vehicle._master.target_system, vehicle._master.target_component, mavutil.mavlink.MAV_CMD_DO_SET_MODE, 0,mavMode,0, 0, 0, 0, 0, 0)

3 Likes

I think that it is quite late reply, but I want to leave my answer for others who have the same problem.

You need to assign base_mode, main_mode, and sub_mode.
For instance, in order to trigger ‘position’ flight mode, you can use the following pymavlink code. I think that it will be same in dronekit code.

master.mav.command_long_send(
master.target_system, master.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_MODE, 0,
base_mode, main_mode, sub_mode, 0, 0, 0, 0)

sub_mode is only defined for the AUTO_MOD

master.mav.command_long_send(
master.target_system, master.target_component,
mavutil.mavlink.MAV_CMD_DO_SET_MODE, 0,
217, 3, 0, 0, 0, 0, 0)

It seems likely to me that the base_mode value will be changed according to current flight mode (If it is incorrect, please correct me).

FYI, you can use the following main_mode, and sub_mode.

  1. Manual: base_mode:217, main_mode:1, sub_mode:0
  2. Stabilized: base_mode:209, main_mode:7, sub_mode:0
  3. Acro: base_mode:209, main_mode:5, sub_mode:0
  4. Rattitude: base_mode:193, main_mode:8, sub_mode:0
  5. Altitude: base_mode:193, main_mode:2, sub_mode:0
  6. Offboard: base_mode:209, main_mode:6, sub_mode:0
  7. Position: base_mode:209, main_mode:3, sub_mode:0
  8. Hold: base_mode:217, main_mode:4, sub_mode:3
  9. Missition: base_mode:157, main_mode:4, sub_mode:4
  10. Return: base_mode:157, main_mode:4, sub_mode:5
  11. Follow me: base_mode:157, main_mode:4, sub_mode:8

As I mentioned, the base_mode will be changed, but there are only 4 cases: 157, 193, 209 and 217. Thanks!

5 Likes

Hey thanks for this! How did you figure out all the different base/main/sub modes?

Hi, sorry to bump this up a bit, I have the same question as @chungma. Is there official documentation for the the PX4 modes? In particular, I’m looking to activate offboard mode via MAVLINK (I’m aware of the risks - we are a university research lab and taking the necessary precautions).

@chungma @ftherien Sorry for my late reply. I am not following PX4 blog for a while. I figured out this from PX4’s source code. Unfortunately, there is no such a specific information on the official documentation.

hello ı am using this parameters my interface but hold and mission same parameters therefore hold is broken dont work what should ı do ?

had this very problem, had to build px4 to browse the source code to figure it all out. Here are my notes so far.

To get the current flight mode, listen to heartbeat. PX4’s mode is encoded in custom_mode field of heartbeat message as:

0x####0000
    ^^   main mode
  ^^     sub mode

to set the mode, you can either use a deprecated SET_MODE mavlink message, or COMMAND_LONG

in SET_MODE:

base_mode = ? (use 1?)
custom_mode = same as heartbeat.custom_mode

in COMMAND_LONG:

MAV_CMD_DO_SET_MODE:
param1 = ? (using 1.0 works)
param2 = main mode
param3 = sub mode

The mode numbers are:
src/modules/commander/px4_custom_mode.h:

enum PX4_CUSTOM_MAIN_MODE {
	PX4_CUSTOM_MAIN_MODE_MANUAL = 1,
	PX4_CUSTOM_MAIN_MODE_ALTCTL, // = 2
	PX4_CUSTOM_MAIN_MODE_POSCTL, // = 3
	PX4_CUSTOM_MAIN_MODE_AUTO, // = 4
	PX4_CUSTOM_MAIN_MODE_ACRO, // = 5
	PX4_CUSTOM_MAIN_MODE_OFFBOARD, // = 6
	PX4_CUSTOM_MAIN_MODE_STABILIZED, // = 7
	PX4_CUSTOM_MAIN_MODE_RATTITUDE_LEGACY, // = 8
	PX4_CUSTOM_MAIN_MODE_SIMPLE, /* unused, but reserved for future use */
	PX4_CUSTOM_MAIN_MODE_TERMINATION // = 10
};

enum PX4_CUSTOM_SUB_MODE_AUTO {
	PX4_CUSTOM_SUB_MODE_AUTO_READY = 1,
	PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF, // = 2
	PX4_CUSTOM_SUB_MODE_AUTO_LOITER, // = 3 //hold mode?
	PX4_CUSTOM_SUB_MODE_AUTO_MISSION, // = 4
	PX4_CUSTOM_SUB_MODE_AUTO_RTL, // = 5
	PX4_CUSTOM_SUB_MODE_AUTO_LAND, // = 6
	PX4_CUSTOM_SUB_MODE_AUTO_RESERVED_DO_NOT_USE, // was PX4_CUSTOM_SUB_MODE_AUTO_RTGS, deleted 2020-03-05
	PX4_CUSTOM_SUB_MODE_AUTO_FOLLOW_TARGET, // = 8
	PX4_CUSTOM_SUB_MODE_AUTO_PRECLAND, // = 9
	PX4_CUSTOM_SUB_MODE_AUTO_VTOL_TAKEOFF, // = 10
	PX4_CUSTOM_SUB_MODE_EXTERNAL1, // = 11
	PX4_CUSTOM_SUB_MODE_EXTERNAL2, 
	PX4_CUSTOM_SUB_MODE_EXTERNAL3,
	PX4_CUSTOM_SUB_MODE_EXTERNAL4,
	PX4_CUSTOM_SUB_MODE_EXTERNAL5,
	PX4_CUSTOM_SUB_MODE_EXTERNAL6,
	PX4_CUSTOM_SUB_MODE_EXTERNAL7,
	PX4_CUSTOM_SUB_MODE_EXTERNAL8,
};

enum PX4_CUSTOM_SUB_MODE_POSCTL {
	PX4_CUSTOM_SUB_MODE_POSCTL_POSCTL = 0,
	PX4_CUSTOM_SUB_MODE_POSCTL_ORBIT, // = 1
	PX4_CUSTOM_SUB_MODE_POSCTL_SLOW // = 2
};

see also Commander::custom_command function in /src/modules/commander/Commander.cpp

1 Like

I have made a connection between Mission planner sitl and mavlink then I printed HEARTBEAT messages in the loop everytime i change the mode on mission planner i can see the info of the mode on the terminal