How to properly exit Offboard mode and switch to Loiter (NavState = 5)?

I am facing an issue when trying to exit Offboard mode and switch the drone to Loiter (NavState = 5). I am following the example and guidelines from the px4_ros_com repository.

My Configuration

  • Language: Python
  • Platform: PX4
  • Firmware version: v1.13

What I Am Doing

  1. I execute a takeoff command:

    self.publish_vehicle_command(VehicleCommand.VEHICLE_CMD_NAV_TAKEOFF, param1=1.0, param7=5.0)
    

    After this, the drone switches to a hover mode (I assume this is Loiter).

  2. I switch the drone to Offboard mode:

    self.publish_vehicle_command(VehicleCommand.VEHICLE_CMD_DO_SET_MODE, param1=1.0, param2=6.0)
    

    After this, the drone successfully enters Offboard (NavState = 14), and I can control it.

  3. I attempt to switch back from Offboard to Loiter:

    self.publish_vehicle_command(VehicleCommand.VEHICLE_CMD_DO_SET_MODE, param1=1.0, param2=5.0)
    

    However, the NavState remains 14 (Offboard), and the drone continues in this mode.

Expected Behavior

After sending the command to switch to Loiter, the drone should transition to NavState = 5 and begin hovering at its current position.

Actual Behavior

The NavState remains 14 (Offboard), and the switch command has no effect on the mode.


Question

How can I reliably exit Offboard mode and switch to Loiter? Are there additional conditions or settings in PX4 that need to be considered?

I would greatly appreciate any recommendations or explanations!