hybridgroup.gobot/platforms/ardrone/ardrone_adaptor.go

62 lines
1.3 KiB
Go
Raw Normal View History

package ardrone
import (
2014-04-29 02:23:12 +08:00
client "github.com/hybridgroup/go-ardrone/client"
"github.com/hybridgroup/gobot"
)
var _ gobot.Adaptor = (*ArdroneAdaptor)(nil)
2014-10-16 05:43:38 +08:00
// drone defines expected drone behaviour
2014-06-15 04:55:12 +08:00
type drone interface {
Takeoff() bool
Land()
Up(n float64)
Down(n float64)
Left(n float64)
Right(n float64)
Forward(n float64)
Backward(n float64)
Clockwise(n float64)
Counterclockwise(n float64)
Hover()
}
type ArdroneAdaptor struct {
name string
2014-04-29 02:23:12 +08:00
drone drone
2014-11-17 05:46:23 +08:00
connect func(*ArdroneAdaptor) (err error)
}
2014-10-16 05:43:38 +08:00
// NewArdroneAdaptor creates a new ardrone and connects with default configuration
func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
2014-04-29 02:23:12 +08:00
return &ArdroneAdaptor{
name: name,
2014-11-17 05:46:23 +08:00
connect: func(a *ArdroneAdaptor) (err error) {
config := client.DefaultConfig()
if len(v) > 0 {
config.Ip = v[0]
}
d, err := client.Connect(config)
2014-04-29 02:23:12 +08:00
if err != nil {
2014-11-17 05:46:23 +08:00
return
2014-04-29 02:23:12 +08:00
}
a.drone = d
2014-11-17 05:46:23 +08:00
return
2014-04-29 02:23:12 +08:00
},
}
}
func (a *ArdroneAdaptor) Name() string { return a.name }
2014-10-16 05:43:38 +08:00
// Connect returns true when connection to ardrone is established correclty
func (a *ArdroneAdaptor) Connect() (errs []error) {
if err := a.connect(a); err != nil {
return []error{err}
}
return
}
2014-10-16 05:43:38 +08:00
// Finalize returns true when connection is finalized correctly
func (a *ArdroneAdaptor) Finalize() (errs []error) { return }