2014-08-01 14:39:52 +08:00
|
|
|
package mavlink
|
|
|
|
|
|
|
|
import (
|
2014-08-04 09:18:01 +08:00
|
|
|
"time"
|
2014-08-01 14:39:52 +08:00
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
common "gobot.io/x/gobot/platforms/mavlink/common"
|
2014-08-01 14:39:52 +08:00
|
|
|
)
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
const (
|
2016-09-13 00:01:31 +08:00
|
|
|
// PacketEvent event
|
2016-09-01 18:17:43 +08:00
|
|
|
PacketEvent = "packet"
|
2016-09-13 00:01:31 +08:00
|
|
|
// MessageEvent event
|
2016-09-01 18:17:43 +08:00
|
|
|
MessageEvent = "message"
|
2016-11-08 02:44:57 +08:00
|
|
|
// ErrorIOEEvent event
|
2016-09-01 18:17:43 +08:00
|
|
|
ErrorIOEvent = "errorIO"
|
2016-09-13 00:01:31 +08:00
|
|
|
// ErrorMAVLinkEvent event
|
2016-09-01 18:17:43 +08:00
|
|
|
ErrorMAVLinkEvent = "errorMAVLink"
|
|
|
|
)
|
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
type Driver struct {
|
2014-11-29 09:56:34 +08:00
|
|
|
name string
|
|
|
|
connection gobot.Connection
|
|
|
|
interval time.Duration
|
|
|
|
gobot.Eventer
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type MavlinkInterface interface {
|
|
|
|
}
|
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
// NewDriver creates a new mavlink driver.
|
2014-10-22 02:48:08 +08:00
|
|
|
//
|
|
|
|
// It add the following events:
|
|
|
|
// "packet" - triggered when a new packet is read
|
|
|
|
// "message" - triggered when a new valid message is processed
|
2016-09-26 03:44:40 +08:00
|
|
|
func NewDriver(a *Adaptor, v ...time.Duration) *Driver {
|
|
|
|
m := &Driver{
|
2016-10-04 01:06:37 +08:00
|
|
|
name: "Mavlink",
|
2014-11-29 09:56:34 +08:00
|
|
|
connection: a,
|
|
|
|
Eventer: gobot.NewEventer(),
|
|
|
|
interval: 10 * time.Millisecond,
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2014-11-29 10:37:03 +08:00
|
|
|
if len(v) > 0 {
|
|
|
|
m.interval = v[0]
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
m.AddEvent(PacketEvent)
|
|
|
|
m.AddEvent(MessageEvent)
|
|
|
|
m.AddEvent(ErrorIOEvent)
|
|
|
|
m.AddEvent(ErrorMAVLinkEvent)
|
2014-08-01 14:39:52 +08:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2016-09-26 03:44:40 +08:00
|
|
|
func (m *Driver) Connection() gobot.Connection { return m.connection }
|
|
|
|
func (m *Driver) Name() string { return m.name }
|
|
|
|
func (m *Driver) SetName(n string) { m.name = n }
|
2014-11-29 09:56:34 +08:00
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// adaptor returns driver associated adaptor
|
2016-09-26 03:44:40 +08:00
|
|
|
func (m *Driver) adaptor() *Adaptor {
|
|
|
|
return m.Connection().(*Adaptor)
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// Start begins process to read mavlink packets every m.Interval
|
|
|
|
// and process them
|
2016-11-08 02:44:57 +08:00
|
|
|
func (m *Driver) Start() error {
|
2014-08-04 09:18:01 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
packet, err := common.ReadMAVLinkPacket(m.adaptor().sp)
|
|
|
|
if err != nil {
|
2016-09-01 18:17:43 +08:00
|
|
|
m.Publish(ErrorIOEvent, err)
|
2014-08-04 09:18:01 +08:00
|
|
|
continue
|
|
|
|
}
|
2016-09-01 18:17:43 +08:00
|
|
|
m.Publish(PacketEvent, packet)
|
2014-08-04 09:18:01 +08:00
|
|
|
message, err := packet.MAVLinkMessage()
|
|
|
|
if err != nil {
|
2016-09-01 18:17:43 +08:00
|
|
|
m.Publish(ErrorMAVLinkEvent, err)
|
2014-08-04 09:18:01 +08:00
|
|
|
continue
|
|
|
|
}
|
2016-09-01 18:17:43 +08:00
|
|
|
m.Publish(MessageEvent, message)
|
2016-11-05 20:05:49 +08:00
|
|
|
time.Sleep(m.interval)
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
2014-08-04 09:18:01 +08:00
|
|
|
}()
|
2016-11-08 02:44:57 +08:00
|
|
|
return nil
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2014-11-20 15:21:19 +08:00
|
|
|
// Halt returns true if device is halted successfully
|
2016-11-08 02:44:57 +08:00
|
|
|
func (m *Driver) Halt() (err error) { return }
|
2014-11-20 15:21:19 +08:00
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// SendPacket sends a packet to mavlink device
|
2016-09-26 03:44:40 +08:00
|
|
|
func (m *Driver) SendPacket(packet *common.MAVLinkPacket) (err error) {
|
2014-11-20 08:03:50 +08:00
|
|
|
_, err = m.adaptor().sp.Write(packet.Pack())
|
|
|
|
return err
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|