github.com/bluenviron/gomavlib/v2@v2.2.1-0.20240308101627-2c07e3da629c/examples/router-edit/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 incoming frames 13 // 3) edit messages of a specific kind 14 // 4) recompute the frame checksum and signature 15 // 5) route frame to every other channel 16 17 func main() { 18 // create a node which communicates with a serial endpoint 19 node, err := gomavlib.NewNode(gomavlib.NodeConf{ 20 Endpoints: []gomavlib.EndpointConf{ 21 gomavlib.EndpointSerial{ 22 Device: "/dev/ttyUSB0", 23 Baud: 57600, 24 }, 25 gomavlib.EndpointUDPClient{"1.2.3.4:5900"}, 26 }, 27 Dialect: ardupilotmega.Dialect, 28 OutVersion: gomavlib.V2, // change to V1 if you're unable to communicate with the target 29 OutSystemID: 10, 30 }) 31 if err != nil { 32 panic(err) 33 } 34 defer node.Close() 35 36 // print incoming frames 37 for evt := range node.Events() { 38 if frm, ok := evt.(*gomavlib.EventFrame); ok { 39 log.Printf("received: id=%d, %+v\n", frm.Message().GetID(), frm.Message()) 40 41 // if incoming message is a heartbeat 42 if msg, ok := frm.Message().(*ardupilotmega.MessageHeartbeat); ok { 43 // edit a field 44 msg.Type = ardupilotmega.MAV_TYPE_SUBMARINE 45 46 // since we changed the frame content, recompute checksum and signature 47 err := node.FixFrame(frm.Frame) 48 if err != nil { 49 log.Printf("ERR: %v", err) 50 continue 51 } 52 } 53 54 // route frame to every other channel 55 node.WriteFrameExcept(frm.Channel, frm.Frame) 56 } 57 } 58 }