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"
|
|
|
|
)
|
|
|
|
|
2014-11-17 04:25:48 +08:00
|
|
|
var _ gobot.DriverInterface = (*MavlinkDriver)(nil)
|
|
|
|
|
2014-08-01 14:39:52 +08:00
|
|
|
type MavlinkDriver struct {
|
|
|
|
gobot.Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
type MavlinkInterface interface {
|
|
|
|
}
|
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// NewMavlinkDriver creates a new mavlink driver with specified name.
|
|
|
|
//
|
|
|
|
// It add the following events:
|
|
|
|
// "packet" - triggered when a new packet is read
|
|
|
|
// "message" - triggered when a new valid message is processed
|
2014-08-01 14:39:52 +08:00
|
|
|
func NewMavlinkDriver(a *MavlinkAdaptor, name string) *MavlinkDriver {
|
|
|
|
m := &MavlinkDriver{
|
|
|
|
Driver: *gobot.NewDriver(
|
|
|
|
name,
|
|
|
|
"mavlink.MavlinkDriver",
|
|
|
|
a,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
m.AddEvent("packet")
|
|
|
|
m.AddEvent("message")
|
2014-11-20 08:03:50 +08:00
|
|
|
m.AddEvent("error")
|
2014-08-01 14:39:52 +08:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// adaptor returns driver associated adaptor
|
2014-08-01 14:39:52 +08:00
|
|
|
func (m *MavlinkDriver) adaptor() *MavlinkAdaptor {
|
|
|
|
return m.Driver.Adaptor().(*MavlinkAdaptor)
|
|
|
|
}
|
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// Start begins process to read mavlink packets every m.Interval
|
|
|
|
// and process them
|
2014-11-20 15:21:19 +08:00
|
|
|
func (m *MavlinkDriver) Start() (errs []error) {
|
2014-08-04 09:18:01 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
packet, err := common.ReadMAVLinkPacket(m.adaptor().sp)
|
|
|
|
if err != nil {
|
2014-11-20 08:03:50 +08:00
|
|
|
gobot.Publish(m.Event("error"), err)
|
2014-08-04 09:18:01 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
gobot.Publish(m.Event("packet"), packet)
|
|
|
|
message, err := packet.MAVLinkMessage()
|
|
|
|
if err != nil {
|
2014-11-20 08:03:50 +08:00
|
|
|
gobot.Publish(m.Event("error"), err)
|
2014-08-04 09:18:01 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
gobot.Publish(m.Event("message"), message)
|
|
|
|
<-time.After(m.Interval())
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
2014-08-04 09:18:01 +08:00
|
|
|
}()
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|
|
|
|
|
2014-11-20 15:21:19 +08:00
|
|
|
// Halt returns true if device is halted successfully
|
|
|
|
func (m *MavlinkDriver) Halt() (errs []error) { return }
|
|
|
|
|
2014-10-22 02:48:08 +08:00
|
|
|
// SendPacket sends a packet to mavlink device
|
2014-11-20 08:03:50 +08:00
|
|
|
func (m *MavlinkDriver) SendPacket(packet *common.MAVLinkPacket) (err error) {
|
|
|
|
_, err = m.adaptor().sp.Write(packet.Pack())
|
|
|
|
return err
|
2014-08-01 14:39:52 +08:00
|
|
|
}
|