mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-24 13:48:49 +08:00
core: Starting refactor of new adaptor/new driver function signatures with ARDrone
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
574dce7b39
commit
56558cd9cb
@ -4,6 +4,8 @@ package gobot
|
||||
type Adaptor interface {
|
||||
// Name returns the label for the Adaptor
|
||||
Name() string
|
||||
// SetName sets the label for the Adaptor
|
||||
SetName(n string)
|
||||
// Connect initiates the Adaptor
|
||||
Connect() []error
|
||||
// Finalize terminates the Adaptor
|
||||
|
@ -31,6 +31,7 @@ type testDriver struct {
|
||||
func (t *testDriver) Start() (errs []error) { return }
|
||||
func (t *testDriver) Halt() (errs []error) { return }
|
||||
func (t *testDriver) Name() string { return t.name }
|
||||
func (t *testDriver) SetName(n string) { t.name = n }
|
||||
func (t *testDriver) Pin() string { return t.pin }
|
||||
func (t *testDriver) Connection() gobot.Connection { return t.connection }
|
||||
|
||||
@ -69,6 +70,7 @@ var testAdaptorFinalize = func() (errs []error) { return }
|
||||
func (t *testAdaptor) Finalize() (errs []error) { return testAdaptorFinalize() }
|
||||
func (t *testAdaptor) Connect() (errs []error) { return testAdaptorConnect() }
|
||||
func (t *testAdaptor) Name() string { return t.name }
|
||||
func (t *testAdaptor) SetName(n string) { t.name = n }
|
||||
func (t *testAdaptor) Port() string { return t.port }
|
||||
|
||||
func newTestAdaptor(name string, port string) *testAdaptor {
|
||||
|
@ -4,6 +4,8 @@ package gobot
|
||||
type Driver interface {
|
||||
// Name returns the label for the Driver
|
||||
Name() string
|
||||
// SetName sets the label for the Driver
|
||||
SetName(s string)
|
||||
// Start initiates the Driver
|
||||
Start() []error
|
||||
// Halt terminates the Driver
|
||||
|
@ -10,8 +10,8 @@ import (
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
|
||||
drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone")
|
||||
ardroneAdaptor := ardrone.NewAdaptor()
|
||||
drone := ardrone.NewDriver(ardroneAdaptor)
|
||||
|
||||
work := func() {
|
||||
drone.On(ardrone.Flying, func(data interface{}) {
|
||||
|
@ -20,10 +20,10 @@ func main() {
|
||||
|
||||
_, currentfile, _, _ := runtime.Caller(0)
|
||||
cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml")
|
||||
window := opencv.NewWindowDriver("window")
|
||||
camera := opencv.NewCameraDriver("camera", "tcp://192.168.1.1:5555")
|
||||
ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
|
||||
drone := ardrone.NewArdroneDriver(ardroneAdaptor, "drone")
|
||||
window := opencv.NewWindowDriver()
|
||||
camera := opencv.NewCameraDriver("tcp://192.168.1.1:5555")
|
||||
ardroneAdaptor := ardrone.NewAdaptor()
|
||||
drone := ardrone.NewDriver(ardroneAdaptor)
|
||||
|
||||
work := func() {
|
||||
detect := false
|
||||
|
@ -17,14 +17,13 @@ type pair struct {
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
joystickAdaptor := joystick.NewJoystickAdaptor("ps3")
|
||||
stick := joystick.NewJoystickDriver(joystickAdaptor,
|
||||
"ps3",
|
||||
joystickAdaptor := joystick.NewAdaptor()
|
||||
stick := joystick.NewDriver(joystickAdaptor,
|
||||
"./platforms/joystick/configs/dualshock3.json",
|
||||
)
|
||||
|
||||
ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
|
||||
drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone")
|
||||
ardroneAdaptor := ardrone.NewAdaptor()
|
||||
drone := ardrone.NewDriver(ardroneAdaptor)
|
||||
|
||||
work := func() {
|
||||
offset := 32767.0
|
||||
|
@ -27,6 +27,7 @@ var testDriverHalt = func() (errs []error) { return }
|
||||
func (t *testDriver) Start() (errs []error) { return testDriverStart() }
|
||||
func (t *testDriver) Halt() (errs []error) { return testDriverHalt() }
|
||||
func (t *testDriver) Name() string { return t.name }
|
||||
func (t *testDriver) SetName(n string) { t.name = n }
|
||||
func (t *testDriver) Pin() string { return t.pin }
|
||||
func (t *testDriver) Connection() Connection { return t.connection }
|
||||
|
||||
@ -54,6 +55,7 @@ var testAdaptorFinalize = func() (errs []error) { return }
|
||||
func (t *testAdaptor) Finalize() (errs []error) { return testAdaptorFinalize() }
|
||||
func (t *testAdaptor) Connect() (errs []error) { return testAdaptorConnect() }
|
||||
func (t *testAdaptor) Name() string { return t.name }
|
||||
func (t *testAdaptor) SetName(n string) { t.name = n }
|
||||
func (t *testAdaptor) Port() string { return t.port }
|
||||
|
||||
func newTestAdaptor(name string, port string) *testAdaptor {
|
||||
|
@ -19,22 +19,21 @@ type drone interface {
|
||||
Hover()
|
||||
}
|
||||
|
||||
// ArdroneAdaptor is gobot.Adaptor representation for the Ardrone
|
||||
type ArdroneAdaptor struct {
|
||||
// Adaptor is gobot.Adaptor representation for the Ardrone
|
||||
type Adaptor struct {
|
||||
name string
|
||||
drone drone
|
||||
config client.Config
|
||||
connect func(*ArdroneAdaptor) (drone, error)
|
||||
connect func(*Adaptor) (drone, error)
|
||||
}
|
||||
|
||||
// NewArdroneAdaptor returns a new ArdroneAdaptor and optionally accepts:
|
||||
// NewAdaptor returns a new ardrone.Adaptor and optionally accepts:
|
||||
//
|
||||
// string: The ardrones IP Address
|
||||
//
|
||||
func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
|
||||
a := &ArdroneAdaptor{
|
||||
name: name,
|
||||
connect: func(a *ArdroneAdaptor) (drone, error) {
|
||||
func NewAdaptor(v ...string) *Adaptor {
|
||||
a := &Adaptor{
|
||||
connect: func(a *Adaptor) (drone, error) {
|
||||
return client.Connect(a.config)
|
||||
},
|
||||
}
|
||||
@ -47,11 +46,14 @@ func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
|
||||
return a
|
||||
}
|
||||
|
||||
// Name returns the ArdroneAdaptors Name
|
||||
func (a *ArdroneAdaptor) Name() string { return a.name }
|
||||
// Name returns the Adaptor Name
|
||||
func (a *Adaptor) Name() string { return a.name }
|
||||
|
||||
// SetName sets the Adaptor Name
|
||||
func (a *Adaptor) SetName(n string) { a.name = n }
|
||||
|
||||
// Connect establishes a connection to the ardrone
|
||||
func (a *ArdroneAdaptor) Connect() (errs []error) {
|
||||
func (a *Adaptor) Connect() (errs []error) {
|
||||
d, err := a.connect(a)
|
||||
if err != nil {
|
||||
return []error{err}
|
||||
@ -61,4 +63,4 @@ func (a *ArdroneAdaptor) Connect() (errs []error) {
|
||||
}
|
||||
|
||||
// Finalize terminates the connection to the ardrone
|
||||
func (a *ArdroneAdaptor) Finalize() (errs []error) { return }
|
||||
func (a *Adaptor) Finalize() (errs []error) { return }
|
||||
|
@ -8,22 +8,21 @@ import (
|
||||
"github.com/hybridgroup/gobot/gobottest"
|
||||
)
|
||||
|
||||
var _ gobot.Adaptor = (*ArdroneAdaptor)(nil)
|
||||
var _ gobot.Adaptor = (*Adaptor)(nil)
|
||||
|
||||
func initTestArdroneAdaptor() *ArdroneAdaptor {
|
||||
a := NewArdroneAdaptor("drone")
|
||||
a.connect = func(a *ArdroneAdaptor) (drone, error) {
|
||||
func initTestArdroneAdaptor() *Adaptor {
|
||||
a := NewAdaptor()
|
||||
a.connect = func(a *Adaptor) (drone, error) {
|
||||
return &testDrone{}, nil
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func TestArdroneAdaptor(t *testing.T) {
|
||||
a := NewArdroneAdaptor("drone")
|
||||
gobottest.Assert(t, a.Name(), "drone")
|
||||
a := NewAdaptor()
|
||||
gobottest.Assert(t, a.config.Ip, "192.168.1.1")
|
||||
|
||||
a = NewArdroneAdaptor("drone", "192.168.100.100")
|
||||
a = NewAdaptor("192.168.100.100")
|
||||
gobottest.Assert(t, a.config.Ip, "192.168.100.100")
|
||||
}
|
||||
|
||||
@ -31,7 +30,7 @@ func TestArdroneAdaptorConnect(t *testing.T) {
|
||||
a := initTestArdroneAdaptor()
|
||||
gobottest.Assert(t, len(a.Connect()), 0)
|
||||
|
||||
a.connect = func(a *ArdroneAdaptor) (drone, error) {
|
||||
a.connect = func(a *Adaptor) (drone, error) {
|
||||
return nil, errors.New("connection error")
|
||||
}
|
||||
gobottest.Assert(t, a.Connect()[0], errors.New("connection error"))
|
||||
|
@ -9,20 +9,19 @@ const (
|
||||
Flying = "flying"
|
||||
)
|
||||
|
||||
// ArdroneDriver is gobot.Driver representation for the Ardrone
|
||||
type ArdroneDriver struct {
|
||||
// Driver is gobot.Driver representation for the Ardrone
|
||||
type Driver struct {
|
||||
name string
|
||||
connection gobot.Connection
|
||||
gobot.Eventer
|
||||
}
|
||||
|
||||
// NewArdroneDriver creates an ArdroneDriver with specified name.
|
||||
// NewDriver creates an Driver for the ARDrone.
|
||||
//
|
||||
// It add the following events:
|
||||
// 'flying' - Sent when the device has taken off.
|
||||
func NewArdroneDriver(connection *ArdroneAdaptor, name string) *ArdroneDriver {
|
||||
d := &ArdroneDriver{
|
||||
name: name,
|
||||
func NewDriver(connection *Adaptor) *Driver {
|
||||
d := &Driver{
|
||||
connection: connection,
|
||||
Eventer: gobot.NewEventer(),
|
||||
}
|
||||
@ -30,88 +29,91 @@ func NewArdroneDriver(connection *ArdroneAdaptor, name string) *ArdroneDriver {
|
||||
return d
|
||||
}
|
||||
|
||||
// Name returns the ArdroneDrivers Name
|
||||
func (a *ArdroneDriver) Name() string { return a.name }
|
||||
// Name returns the Driver Name
|
||||
func (a *Driver) Name() string { return a.name }
|
||||
|
||||
// Connection returns the ArdroneDrivers Connection
|
||||
func (a *ArdroneDriver) Connection() gobot.Connection { return a.connection }
|
||||
// SetName sets the Driver Name
|
||||
func (a *Driver) SetName(n string) { a.name = n }
|
||||
|
||||
// Connection returns the Driver Connection
|
||||
func (a *Driver) Connection() gobot.Connection { return a.connection }
|
||||
|
||||
// adaptor returns ardrone adaptor
|
||||
func (a *ArdroneDriver) adaptor() *ArdroneAdaptor {
|
||||
return a.Connection().(*ArdroneAdaptor)
|
||||
func (a *Driver) adaptor() *Adaptor {
|
||||
return a.Connection().(*Adaptor)
|
||||
}
|
||||
|
||||
// Start starts the ArdroneDriver
|
||||
func (a *ArdroneDriver) Start() (errs []error) {
|
||||
// Start starts the Driver
|
||||
func (a *Driver) Start() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Halt halts the ArdroneDriver
|
||||
func (a *ArdroneDriver) Halt() (errs []error) {
|
||||
// Halt halts the Driver
|
||||
func (a *Driver) Halt() (errs []error) {
|
||||
return
|
||||
}
|
||||
|
||||
// TakeOff makes the drone start flying, and publishes `flying` event
|
||||
func (a *ArdroneDriver) TakeOff() {
|
||||
func (a *Driver) TakeOff() {
|
||||
a.Publish(a.Event("flying"), a.adaptor().drone.Takeoff())
|
||||
}
|
||||
|
||||
// Land causes the drone to land
|
||||
func (a *ArdroneDriver) Land() {
|
||||
func (a *Driver) Land() {
|
||||
a.adaptor().drone.Land()
|
||||
}
|
||||
|
||||
// Up makes the drone gain altitude.
|
||||
// speed can be a value from `0.0` to `1.0`.
|
||||
func (a *ArdroneDriver) Up(speed float64) {
|
||||
func (a *Driver) Up(speed float64) {
|
||||
a.adaptor().drone.Up(speed)
|
||||
}
|
||||
|
||||
// Down makes the drone reduce altitude.
|
||||
// speed can be a value from `0.0` to `1.0`.
|
||||
func (a *ArdroneDriver) Down(speed float64) {
|
||||
func (a *Driver) Down(speed float64) {
|
||||
a.adaptor().drone.Down(speed)
|
||||
}
|
||||
|
||||
// Left causes the drone to bank to the left, controls the roll, which is
|
||||
// a horizontal movement using the camera as a reference point.
|
||||
// speed can be a value from `0.0` to `1.0`.
|
||||
func (a *ArdroneDriver) Left(speed float64) {
|
||||
func (a *Driver) Left(speed float64) {
|
||||
a.adaptor().drone.Left(speed)
|
||||
}
|
||||
|
||||
// Right causes the drone to bank to the right, controls the roll, which is
|
||||
// a horizontal movement using the camera as a reference point.
|
||||
// speed can be a value from `0.0` to `1.0`.
|
||||
func (a *ArdroneDriver) Right(speed float64) {
|
||||
func (a *Driver) Right(speed float64) {
|
||||
a.adaptor().drone.Right(speed)
|
||||
}
|
||||
|
||||
// Forward causes the drone go forward, controls the pitch.
|
||||
// speed can be a value from `0.0` to `1.0`.
|
||||
func (a *ArdroneDriver) Forward(speed float64) {
|
||||
func (a *Driver) Forward(speed float64) {
|
||||
a.adaptor().drone.Forward(speed)
|
||||
}
|
||||
|
||||
// Backward causes the drone go backward, controls the pitch.
|
||||
// speed can be a value from `0.0` to `1.0`.
|
||||
func (a *ArdroneDriver) Backward(speed float64) {
|
||||
func (a *Driver) Backward(speed float64) {
|
||||
a.adaptor().drone.Backward(speed)
|
||||
}
|
||||
|
||||
// Clockwise causes the drone to spin in clockwise direction
|
||||
// speed can be a value from `0.0` to `1.0`.
|
||||
func (a *ArdroneDriver) Clockwise(speed float64) {
|
||||
func (a *Driver) Clockwise(speed float64) {
|
||||
a.adaptor().drone.Clockwise(speed)
|
||||
}
|
||||
|
||||
// CounterClockwise the drone to spin in counter clockwise direction
|
||||
// speed can be a value from `0.0` to `1.0`.
|
||||
func (a *ArdroneDriver) CounterClockwise(speed float64) {
|
||||
func (a *Driver) CounterClockwise(speed float64) {
|
||||
a.adaptor().drone.Counterclockwise(speed)
|
||||
}
|
||||
|
||||
// Hover makes the drone to hover in place.
|
||||
func (a *ArdroneDriver) Hover() {
|
||||
func (a *Driver) Hover() {
|
||||
a.adaptor().drone.Hover()
|
||||
}
|
||||
|
@ -7,22 +7,22 @@ import (
|
||||
"github.com/hybridgroup/gobot/gobottest"
|
||||
)
|
||||
|
||||
var _ gobot.Driver = (*ArdroneDriver)(nil)
|
||||
var _ gobot.Driver = (*Driver)(nil)
|
||||
|
||||
func initTestArdroneDriver() *ArdroneDriver {
|
||||
a := NewArdroneAdaptor("drone")
|
||||
a.connect = func(a *ArdroneAdaptor) (drone, error) {
|
||||
func initTestArdroneDriver() *Driver {
|
||||
a := NewAdaptor()
|
||||
a.connect = func(a *Adaptor) (drone, error) {
|
||||
return &testDrone{}, nil
|
||||
}
|
||||
d := NewArdroneDriver(a, "drone")
|
||||
d := NewDriver(a)
|
||||
d.SetName("mydrone")
|
||||
a.Connect()
|
||||
return d
|
||||
}
|
||||
|
||||
func TestArdroneDriver(t *testing.T) {
|
||||
d := initTestArdroneDriver()
|
||||
gobottest.Assert(t, d.Name(), "drone")
|
||||
gobottest.Assert(t, d.Connection().Name(), "drone")
|
||||
gobottest.Assert(t, d.Name(), "mydrone")
|
||||
}
|
||||
|
||||
func TestArdroneDriverStart(t *testing.T) {
|
||||
|
@ -19,8 +19,8 @@ Example:
|
||||
func main() {
|
||||
gbot := gobot.NewGobot()
|
||||
|
||||
ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
|
||||
drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone")
|
||||
ardroneAdaptor := ardrone.NewAdaptor()
|
||||
drone := ardrone.NewDriver(ardroneAdaptor)
|
||||
|
||||
work := func() {
|
||||
drone.TakeOff()
|
||||
|
Loading…
x
Reference in New Issue
Block a user