Companion Computer can't change flight mode

Hi,

I am currently working on a project where I try to control the flight controller from the companion computer (ESP32)

The companion computer is running this firmware : ESP32-MAVLink
I can retreive telemetries and arm the drone via MAVLink (connected with TX/RX with the flight controller)

When I try to change the flight mode to GUIDED_DISARMED, I get an error from the flight controller : Setting mode to 88. Command 176 result code 2
I get the same result code when trying to set other flight modes.

As I have seen while doing research, this result code means that the command is invalid ? see MAV_RESULT

I have disabled safety checks and EKF thinking that it was causing a problem, but the result is still the same

Does anyone know how to solve that ?
Thx !

There is no such mode…

        STABILIZE =     0,  // manual airframe angle with manual throttle
        ACRO =          1,  // manual body-frame angular rate with manual throttle
        ALT_HOLD =      2,  // manual airframe angle with automatic throttle
        AUTO =          3,  // fully automatic waypoint control using mission commands
        GUIDED =        4,  // fully automatic fly to coordinate or fly at velocity/direction using GCS immediate commands
        LOITER =        5,  // automatic horizontal acceleration with automatic throttle
        RTL =           6,  // automatic return to launching point
        CIRCLE =        7,  // automatic circular flight with automatic throttle
        LAND =          9,  // automatic landing with horizontal position control
        DRIFT =        11,  // semi-autonomous position, yaw and throttle control
        SPORT =        13,  // manual earth-frame angular rate control with manual throttle
        FLIP =         14,  // automatically flip the vehicle on the roll axis
        AUTOTUNE =     15,  // automatically tune the vehicle's roll and pitch gains
        POSHOLD =      16,  // automatic position hold with manual override, with automatic throttle
        BRAKE =        17,  // full-brake using inertial/GPS system, no pilot input
        THROW =        18,  // throw to launch mode using inertial/GPS system, no pilot input
        AVOID_ADSB =   19,  // automatic avoidance of obstacles in the macro scale - e.g. full-sized aircraft
        GUIDED_NOGPS = 20,  // guided mode but only accepts attitude and altitude
        SMART_RTL =    21,  // SMART_RTL returns to home by retracing its steps
        FLOWHOLD  =    22,  // FLOWHOLD holds position with optical flow without rangefinder
        FOLLOW    =    23,  // follow attempts to follow another vehicle or ground station
        ZIGZAG    =    24,  // ZIGZAG mode is able to fly in a zigzag manner with predefined point A and point B
        SYSTEMID  =    25,  // System ID mode produces automated system identification signals in the controllers
        AUTOROTATE =   26,  // Autonomous autorotation
        AUTO_RTL =     27,  // Auto RTL, this is not a true mode, AUTO will report as this mode if entered to perform a DO_LAND_START Landing sequence
        TURTLE =       28,  // Flip over after crash

that project is not for ardupilot its for a diy board, mavlink comes in a lot of different dialects and not all are compatible.

Eosbandi,

I was refering to MAV_MODE_GUIDED_DISARMED
see more here mavlink modes

Maybe I have to stick to the ArduPilot modes and not from MAVLink modes ?

Yes I understood that but the cigritous project does not fit my needs while ESP32 is perfect.
It provides a high level MAVLink abstraction

From what i have seen that libary is custom for that project, you would need to modify it for use with ardupilot, it probably wouldnt take that much to get it compatible, flight mode numbers would be the main issue i see.

The library I’m using is a fork of the mavlink C library.

The flight mode I’m trying to set can be found in the MAVLink source code on GitHub

you need the ardupilot specific section

I have tried the Copter Mode you sent me, but the result remains the same :

Command is invalid (is supported but has invalid parameters). Retrying same command and parameters will not work

What do you guys think of this piece of code ?

void MAVLink::set_mode(const uint16_t& mode){
  Serial.printf("Setting mode to %u\n", mode);
  mavlink_message_t msg;
  uint8_t buf[MAVLINK_MAX_PACKET_LEN];

  uint16_t command = 176; //do set mode
  uint8_t conf = 0;
  float param1 = float(mode); //auto disarmed

  mavlink_msg_command_long_pack(
    this->sys_id, 
    this->comp_id, 
    &msg, 
    this->tgt_sys, 
    this->tgt_comp, 
    command, 
    conf, 
    param1, 
    0, 0, 0, 0, 0, 0
  );
  uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
  
  Serial2.write(buf, len);
}

param1 custom_mode_enabled flag must be set (0x01)
param2 ardupilot mode

I have done some quick tests and it seems to have solved my issue !

Thank you so much !

I will do some further tests tomorrow

What would be the best library to use in my case ?

I have seen MAVSDK, what do you think about that ?