github.com/bluenviron/gomavlib/v2@v2.2.1-0.20240308101627-2c07e3da629c/examples/message-read/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 // this example shows how to: 11 // 1) create a node which communicates with a serial endpoint 12 // 2) print selected incoming messages 13 14 func main() { 15 // create a node which communicates with a serial endpoint 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.V2, // change to V1 if you're unable to communicate with the target 25 OutSystemID: 10, 26 }) 27 if err != nil { 28 panic(err) 29 } 30 defer node.Close() 31 32 // print selected incoming messages 33 for evt := range node.Events() { 34 if frm, ok := evt.(*gomavlib.EventFrame); ok { 35 switch msg := frm.Message().(type) { 36 // if frm.Message() is a *ardupilotmega.MessageHeartbeat, access its fields 37 case *ardupilotmega.MessageHeartbeat: 38 log.Printf("received heartbeat (type %d)\n", msg.Type) 39 40 // if frm.Message() is a *ardupilotmega.MessageServoOutputRaw, access its fields 41 case *ardupilotmega.MessageServoOutputRaw: 42 log.Printf("received servo output with values: %d %d %d %d %d %d %d %d\n", 43 msg.Servo1Raw, msg.Servo2Raw, msg.Servo3Raw, msg.Servo4Raw, 44 msg.Servo5Raw, msg.Servo6Raw, msg.Servo7Raw, msg.Servo8Raw) 45 } 46 } 47 } 48 }