a Go package for reading/writing mavlink messages.
still under development, not necessarily ready for production use
- grab the files:
go get github.com/liamstask/go-mavlink
- generate classes from mavlink xml definitions:
go generate
xml definitions can be updated from https://github.com/mavlink/mavlink
package go-mavlink/mavgen
is used only for generating classes from mavlink xml definitions, it is not generally intended to be used by anything other than the go generate
command.
package go-mavlink/mavlink
is the main package used for encoding/decoding mavlink messages.
mavlink.Packet
is the struct that gets sent over the wire, and mavlink.Message
is an interface implemented by all mavlink classes generated by mavgen
.
Several mavlink dialects define messages for overlapping msg ids, so it is required to specify which dialects you'd like to use. DialectCommon
is included by most of the available dialects, so is included by default.
dec := mavlink.NewDecoder(rdr)
dec.Dialects.Add(DialectArdupilotmega)
Existing dialects are:
- DialectArdupilotmega
- DialectAsluav
- DialectCommon (added to Encoders/Decoders by default, can be removed if desired)
- DialectMatrixpilot
- DialectPixhawk
- DialectUalberta
a typical decode loop might look like:
rdr := SomeIoReader() // any io.Reader: UDP, serial port, bytes.Buffer, etc
dec := mavlink.NewDecoder(rdr)
for {
pkt, err := dec.Decode()
if err != nil {
log.Fatal("Decode fail:", err)
}
// handle packet types you're interested in...
switch pkt.MsgID {
case mavlink.MSG_ID_PARAM_VALUE:
var pv mavlink.ParamValue
if err := pv.Unpack(pkt); err == nil {
// handle param value
}
}
}
the most convenient way to encode is to use Encoder.Encode()
:
w := SomeIoWriter() // any io.Writer: UDP, serial port, bytes.Buffer, etc
enc := mavlink.NewEncoder(w)
p := mavlink.Ping{
Seq: 12345,
}
if err := enc.Encode(0x1, 0x1, &p); err != nil {
log.Fatal("Encode fail:", err)
}
if for some reason you need to pass a Packet
directly, use Encoder.EncodePacket()
:
p := mavlink.Ping{
Seq: c.seq,
}
var pkt Packet
if err := p.Pack(&pkt); err != nil {
log.Fatal("Pack fail:", err)
}
if err := enc.EncodePacket(&pkt); err != nil {
log.Fatal("Encode fail:", err)
}