2014-08-01 14:39:52 +08:00
|
|
|
package mavlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2020-05-04 04:40:15 +08:00
|
|
|
"go.bug.st/serial"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
common "gobot.io/x/gobot/v2/platforms/mavlink/common"
|
2014-08-01 14:39:52 +08:00
|
|
|
)
|
|
|
|
|
2017-04-17 02:22:45 +08:00
|
|
|
// Adaptor is a Mavlink transport adaptor.
|
|
|
|
type BaseAdaptor interface {
|
|
|
|
gobot.Connection
|
|
|
|
|
|
|
|
io.Writer
|
|
|
|
ReadMAVLinkPacket() (*common.MAVLinkPacket, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adaptor is a Mavlink-over-serial adaptor.
|
2016-09-26 03:44:40 +08:00
|
|
|
type Adaptor struct {
|
2014-11-29 09:56:34 +08:00
|
|
|
name string
|
|
|
|
port string
|
2014-08-01 14:39:52 +08:00
|
|
|
sp io.ReadWriteCloser
|
2014-12-19 07:27:23 +08:00
|
|
|
connect func(string) (io.ReadWriteCloser, error)
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
// NewAdaptor creates a new mavlink adaptor with specified port
|
|
|
|
func NewAdaptor(port string) *Adaptor {
|
|
|
|
return &Adaptor{
|
2016-10-04 01:06:37 +08:00
|
|
|
name: "Mavlink",
|
2014-11-29 09:56:34 +08:00
|
|
|
port: port,
|
2014-12-19 07:27:23 +08:00
|
|
|
connect: func(port string) (io.ReadWriteCloser, error) {
|
2017-05-13 17:19:54 +08:00
|
|
|
return serial.Open(port, &serial.Mode{BaudRate: 57600})
|
2014-08-01 14:39:52 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
func (m *Adaptor) Name() string { return m.name }
|
|
|
|
func (m *Adaptor) SetName(n string) { m.name = n }
|
|
|
|
func (m *Adaptor) Port() string { return m.port }
|
2014-11-29 09:56:34 +08:00
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// Connect returns true if connection to device is successful
|
2023-11-16 03:51:52 +08:00
|
|
|
func (m *Adaptor) Connect() error {
|
|
|
|
sp, err := m.connect(m.Port())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-11-20 15:21:19 +08:00
|
|
|
}
|
2023-11-16 03:51:52 +08:00
|
|
|
m.sp = sp
|
|
|
|
|
|
|
|
return nil
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// Finalize returns true if connection to devices is closed successfully
|
2023-11-16 03:51:52 +08:00
|
|
|
func (m *Adaptor) Finalize() error {
|
|
|
|
return m.sp.Close()
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
2017-04-17 02:22:45 +08:00
|
|
|
|
|
|
|
func (m *Adaptor) ReadMAVLinkPacket() (*common.MAVLinkPacket, error) {
|
|
|
|
return common.ReadMAVLinkPacket(m.sp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Adaptor) Write(b []byte) (int, error) {
|
|
|
|
return m.sp.Write(b)
|
|
|
|
}
|