Skip to content

Commit

Permalink
core: Starting refactor of new adaptor/new driver function signatures…
Browse files Browse the repository at this point in the history
… with ARDrone

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Sep 25, 2016
1 parent 574dce7 commit 56558cd
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 67 deletions.
2 changes: 2 additions & 0 deletions adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions api/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/ardrone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down
8 changes: 4 additions & 4 deletions examples/ardrone_face_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions examples/ardrone_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 14 additions & 12 deletions platforms/ardrone/ardrone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}
Expand All @@ -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}
Expand All @@ -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 }
15 changes: 7 additions & 8 deletions platforms/ardrone/ardrone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,29 @@ 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")
}

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"))
Expand Down
56 changes: 29 additions & 27 deletions platforms/ardrone/ardrone_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,109 +9,111 @@ 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(),
}
d.AddEvent(Flying)
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()
}
14 changes: 7 additions & 7 deletions platforms/ardrone/ardrone_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions platforms/ardrone/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 56558cd

Please sign in to comment.