Levia
Levia

Reputation: 75

gomavlib Arming Command Not Working with QGroundControl

I'm trying to arm a drone using the gomavlib library and sending the MAV_CMD_COMPONENT_ARM_DISARM command with Param1 set to 1. However, the command doesn't seem to be working with QGroundControl. The code runs without errors, but the drone doesn't arm and there's no response received from QGroundControl.

Here's the relevant code snippet:

// DroneCommander handles MAVLink communication with the drone

type DroneCommander struct {

  node *gomavlib.Node

}


// NewDroneCommander creates a new instance of DroneCommander

func NewDroneCommander() (*DroneCommander, error) {

  node, err := gomavlib.NewNode(gomavlib.NodeConf{

    Endpoints: []gomavlib.EndpointConf{

      gomavlib.EndpointTCPClient{"localhost:port"},

    },

    Dialect:     ardupilotmega.Dialect,

    OutVersion:   gomavlib.V2,

    OutSystemID: 10,

  })

  if err != nil {

    return nil, err

  }


  return &DroneCommander{

    node: node,

  }, nil

}


func (dc *DroneCommander) Arm(targetSystem uint8, targetComponent uint8) error {

  msg := &ardupilotmega.MessageCommandLong{

    TargetSystem: targetSystem,

    TargetComponent: targetComponent,

    Command:        common.MAV_CMD_COMPONENT_ARM_DISARM,

    Param1:         1, // Arming flag (1 for arm)

  }


  return dc.node.WriteMessageAll(msg)

}


// Close closes the MAVLink connection

func (dc *DroneCommander) Close() {

  if dc.node != nil {

    dc.node.Close()

  }

}


func main() {

  // Create new drone commander

  dc, err := NewDroneCommander()

  if err != nil {

    log.Fatalf("Failed to create drone commander: %v", err)

  }

  defer dc.Close()


  targetSystem := uint8(1) // Usually 1 for a single vehicle

  targetComponent := uint8(1) // Usually 1 for the autopilot


  log.Println("Sending arm command...")


  // Send arm command in a separate goroutine with retries

  go func() {

    for {

      err := dc.Arm(targetSystem, targetComponent)

      if err == nil {

        return // Exit the loop on successful arm command

      }

      log.Printf("Failed to send arm command: %v, retrying...\n", err)

      time.Sleep(1 * time.Second)

    }

  }()


  // Monitor incoming messages in the main thread

  for evt := range dc.node.Events() {

    if frm, ok := evt.(*gomavlib.EventFrame); ok {

      if ack, ok := frm.Message().(*common.MessageCommandAck); ok {

        fmt.Printf("COMMAND_ACK: Command=%s, Result=%s\n", ack.Command.String(), ack.Result.String())

      }

    }

  }

}

I tried to use https://github.com/bluenviron/mavp2p to listen for the exact command (or sequence of commands) that QGroundControl is using to perform the arm (there's the --print option for that).

Upvotes: 1

Views: 33

Answers (0)

Related Questions