Skip to content

Commit

Permalink
Merge pull request hybridgroup#133 from hybridgroup/godoc-mavlink
Browse files Browse the repository at this point in the history
Godoc mavlink
  • Loading branch information
zankich committed Oct 27, 2014
2 parents 071b9bf + a9a2a4f commit 8e91202
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
72 changes: 72 additions & 0 deletions platforms/mavlink/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
This package contains the Gobot adaptor and driver for the MAVlink Communication Protocol (http://qgroundcontrol.org/mavlink/start).
Installing:
go get github.com/hybridgroup/gobot && go install github.com/hybridgroup/gobot/platforms/mavlink
Example:
package main
import (
"fmt"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/mavlink"
common "github.com/hybridgroup/gobot/platforms/mavlink/common"
)
func main() {
gbot := gobot.NewGobot()
adaptor := mavlink.NewMavlinkAdaptor("iris", "/dev/ttyACM0")
iris := mavlink.NewMavlinkDriver(adaptor, "iris")
work := func() {
gobot.Once(iris.Event("packet"), func(data interface{}) {
packet := data.(*common.MAVLinkPacket)
dataStream := common.NewRequestDataStream(100,
packet.SystemID,
packet.ComponentID,
4,
1,
)
iris.SendPacket(common.CraftMAVLinkPacket(packet.SystemID,
packet.ComponentID,
dataStream,
))
})
gobot.On(iris.Event("message"), func(data interface{}) {
if data.(common.MAVLinkMessage).Id() == 30 {
message := data.(*common.Attitude)
fmt.Println("Attitude")
fmt.Println("TIME_BOOT_MS", message.TIME_BOOT_MS)
fmt.Println("ROLL", message.ROLL)
fmt.Println("PITCH", message.PITCH)
fmt.Println("YAW", message.YAW)
fmt.Println("ROLLSPEED", message.ROLLSPEED)
fmt.Println("PITCHSPEED", message.PITCHSPEED)
fmt.Println("YAWSPEED", message.YAWSPEED)
fmt.Println("")
}
})
}
robot := gobot.NewRobot("mavBot",
[]gobot.Connection{adaptor},
[]gobot.Device{iris},
work,
)
gbot.AddRobot(robot)
gbot.Start()
}
For further information refer to mavlink README:
https://github.com/hybridgroup/gobot/blob/master/platforms/mavlink/README.md
*/
package mavlink
3 changes: 3 additions & 0 deletions platforms/mavlink/mavlink_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type MavlinkAdaptor struct {
connect func(*MavlinkAdaptor)
}

// NewMavLinkAdaptor creates a new mavlink adaptor with specified name and port
func NewMavlinkAdaptor(name string, port string) *MavlinkAdaptor {
return &MavlinkAdaptor{
Adaptor: *gobot.NewAdaptor(
Expand All @@ -30,11 +31,13 @@ func NewMavlinkAdaptor(name string, port string) *MavlinkAdaptor {
}
}

// Connect returns true if connection to device is successful
func (m *MavlinkAdaptor) Connect() bool {
m.connect(m)
return true
}

// Finalize returns true if connection to devices is closed successfully
func (m *MavlinkAdaptor) Finalize() bool {
m.sp.Close()
return true
Expand Down
10 changes: 10 additions & 0 deletions platforms/mavlink/mavlink_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type MavlinkDriver struct {
type MavlinkInterface interface {
}

// 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
func NewMavlinkDriver(a *MavlinkAdaptor, name string) *MavlinkDriver {
m := &MavlinkDriver{
Driver: *gobot.NewDriver(
Expand All @@ -30,10 +35,13 @@ func NewMavlinkDriver(a *MavlinkAdaptor, name string) *MavlinkDriver {
return m
}

// adaptor returns driver associated adaptor
func (m *MavlinkDriver) adaptor() *MavlinkAdaptor {
return m.Driver.Adaptor().(*MavlinkAdaptor)
}

// Start begins process to read mavlink packets every m.Interval
// and process them
func (m *MavlinkDriver) Start() bool {
go func() {
for {
Expand All @@ -55,8 +63,10 @@ func (m *MavlinkDriver) Start() bool {
return true
}

// SendPacket sends a packet to mavlink device
func (m *MavlinkDriver) SendPacket(packet *common.MAVLinkPacket) {
m.adaptor().sp.Write(packet.Pack())
}

// Halt returns true if device is halted successfully
func (m *MavlinkDriver) Halt() bool { return true }

0 comments on commit 8e91202

Please sign in to comment.