ARM/DISARM command to PX4

Morning. I’m a beginner in this. I’m trying to send an arm/disarm command from an Arduino to PX4, I wanna know where’s the mistake in my code.

I have a command_long handler function and a send_command function.

void handle_command_long_packet(mavlink_message_t message){
  mavlink_command_long_t cmd;
  //mavlink_msg_command_long_decode(const mavlink_message_t *msg, mavlink_command_long_t *command_long)
  mavlink_msg_command_long_decode(&message, &cmd);


  switch(cmd.command){
    case MAV_CMD_COMPONENT_ARM_DISARM:{
      Serial1.print("ARM/DISARM COMMAND");
      send_long_command(MAV_CMD_COMPONENT_ARM_DISARM, 0, 1, 0, 0, 0, 0, 0, 0);
      send_ack_message(MAV_UAV_ID, MAV_UAV_COMP_ID, MAV_CMD_COMPONENT_ARM_DISARM, MAV_RESULT_ACCEPTED, 0, 0, MAV_GCS_ID, MAV_GCS_COMP_ID);
    }
  }
}


void send_long_command(uint16_t command, uint8_t confirmation, float param1, float param2, float param3, float param4, float param5, float param6, float param7){
  mavlink_message_t msg;
  uint16_t len;

  mavlink_msg_command_long_pack(MAV_GCS_ID, MAV_GCS_COMP_ID,&msg, MAV_UAV_ID, MAV_UAV_COMP_ID, command, confirmation, param1, param2,  param3,  param4, param5, param6,  param7);
  len = mavlink_msg_to_send_buffer(buffer, &msg);

  Serial1.write(buffer,len);
}

I’d like to know if you could provide me a source besides this article: Mavlink and Arduino step by step

If you’re a beginner, then you have set yourself a tricky challenge. MAVLink can be a bit challenging to get right.

I would do any of these:

  • use a higher level library (e.g. MAVSDK) on a Raspberry Pi instead
  • get inspiration from how things are done within other implementation (e.g. arming in MAVSDK or this MAVLink example.
  • Use an LLM to help you with this. It should do an ok job for that sort of thing as you’re not the first one trying that.

What library are You using?
First check that PX4 port is configured and baud rate matches with your Arduino.