github.com/bluenviron/gomavlib/v2@v2.2.1-0.20240308101627-2c07e3da629c/examples/stream-requests/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/bluenviron/gomavlib/v2"
     7  	"github.com/bluenviron/gomavlib/v2/pkg/dialects/ardupilotmega"
     8  )
     9  
    10  func main() {
    11  	// create a node which
    12  	// - communicates with a serial port
    13  	// - understands ardupilotmega dialect
    14  	// - writes messages with given system id
    15  	// - automatically requests streams to ardupilot devices
    16  	node, err := gomavlib.NewNode(gomavlib.NodeConf{
    17  		Endpoints: []gomavlib.EndpointConf{
    18  			gomavlib.EndpointSerial{
    19  				Device: "/dev/ttyUSB0",
    20  				Baud:   57600,
    21  			},
    22  		},
    23  		Dialect:             ardupilotmega.Dialect,
    24  		OutVersion:          gomavlib.V1, // Ardupilot uses V1
    25  		OutSystemID:         10,
    26  		StreamRequestEnable: true,
    27  	})
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	defer node.Close()
    32  
    33  	// print every message we receive
    34  	for evt := range node.Events() {
    35  		if frm, ok := evt.(*gomavlib.EventFrame); ok {
    36  			log.Printf("received: id=%d, %+v\n", frm.Message().GetID(), frm.Message())
    37  		}
    38  	}
    39  }