hybridgroup.gobot/platforms/mavlink/mavlink_driver.go

95 lines
2.1 KiB
Go
Raw Normal View History

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
"github.com/hybridgroup/gobot"
common "github.com/hybridgroup/gobot/platforms/mavlink/common"
)
const (
// PacketEvent event
PacketEvent = "packet"
// MessageEvent event
MessageEvent = "message"
// ErrorIOE event
ErrorIOEvent = "errorIO"
// ErrorMAVLinkEvent event
ErrorMAVLinkEvent = "errorMAVLink"
)
type Driver struct {
name string
connection gobot.Connection
interval time.Duration
gobot.Eventer
2014-08-01 14:39:52 +08:00
}
type MavlinkInterface interface {
}
// NewDriver creates a new mavlink driver.
//
// It add the following events:
// "packet" - triggered when a new packet is read
// "message" - triggered when a new valid message is processed
func NewDriver(a *Adaptor, v ...time.Duration) *Driver {
m := &Driver{
connection: a,
Eventer: gobot.NewEventer(),
interval: 10 * time.Millisecond,
2014-08-01 14:39:52 +08:00
}
if len(v) > 0 {
m.interval = v[0]
}
m.AddEvent(PacketEvent)
m.AddEvent(MessageEvent)
m.AddEvent(ErrorIOEvent)
m.AddEvent(ErrorMAVLinkEvent)
2014-08-01 14:39:52 +08:00
return m
}
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 }
// adaptor returns driver associated adaptor
func (m *Driver) adaptor() *Adaptor {
return m.Connection().(*Adaptor)
2014-08-01 14:39:52 +08:00
}
// Start begins process to read mavlink packets every m.Interval
// and process them
func (m *Driver) Start() (errs []error) {
2014-08-04 09:18:01 +08:00
go func() {
for {
packet, err := common.ReadMAVLinkPacket(m.adaptor().sp)
if err != nil {
m.Publish(ErrorIOEvent, err)
2014-08-04 09:18:01 +08:00
continue
}
m.Publish(PacketEvent, packet)
2014-08-04 09:18:01 +08:00
message, err := packet.MAVLinkMessage()
if err != nil {
m.Publish(ErrorMAVLinkEvent, err)
2014-08-04 09:18:01 +08:00
continue
}
m.Publish(MessageEvent, message)
<-time.After(m.interval)
2014-08-01 14:39:52 +08:00
}
2014-08-04 09:18:01 +08:00
}()
return
2014-08-01 14:39:52 +08:00
}
// Halt returns true if device is halted successfully
func (m *Driver) Halt() (errs []error) { return }
// SendPacket sends a packet to mavlink device
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
}