Skip to content

Commit

Permalink
Update ardrone adaptor, driver and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed May 21, 2014
1 parent efbc75e commit d32a527
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 68 deletions.
26 changes: 11 additions & 15 deletions examples/ardrone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,32 @@ package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/ardrone"
"github.com/hybridgroup/gobot/platforms/ardrone"
"time"
)

func main() {
ardroneAdaptor := ardrone.NewArdroneAdaptor()
ardroneAdaptor.Name = "Drone"

drone := ardrone.NewArdroneDriver(ardroneAdaptor)
drone.Name = "Drone"
gbot := gobot.NewGobot()
ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone")

work := func() {
drone.TakeOff()
gobot.On(drone.Events["Flying"], func(data interface{}) {
gobot.After("1s", func() {
gobot.After(1*time.Second, func() {
drone.Right(0.1)
})
gobot.After("2s", func() {
gobot.After(2*time.Second, func() {
drone.Left(0.1)
})
gobot.After("3s", func() {
gobot.After(3*time.Second, func() {
drone.Land()
})
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{ardroneAdaptor},
Devices: []gobot.Device{drone},
Work: work,
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("drone", []gobot.Connection{ardroneAdaptor}, []gobot.Device{drone}, work))

robot.Start()
gbot.Start()
}
41 changes: 16 additions & 25 deletions examples/ardrone_face_tracking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@ import (
"fmt"
cv "github.com/hybridgroup/go-opencv/opencv"
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/ardrone"
"github.com/hybridgroup/gobot/opencv"
"github.com/hybridgroup/gobot/platforms/ardrone"
"github.com/hybridgroup/gobot/platforms/opencv"
"math"
"path"
"runtime"
"time"
)

func main() {
gbot := gobot.NewGobot()

_, currentfile, _, _ := runtime.Caller(0)
cascade := path.Join(path.Dir(currentfile), "haarcascade_frontalface_alt.xml")

window := opencv.NewWindowDriver()
window.Name = "window"

camera := opencv.NewCamera()
camera.Name = "camera"
camera.Source = "tcp://192.168.1.1:5555"

ardroneAdaptor := ardrone.NewArdroneAdaptor()
ardroneAdaptor.Name = "Drone"

drone := ardrone.NewArdroneDriver(ardroneAdaptor)
drone.Name = "Drone"
window := opencv.NewWindowDriver("window")
camera := opencv.NewCamera("camera", "tcp://192.168.1.1:5555")
ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
drone := ardrone.NewArdroneDriver(ardroneAdaptor, "drone")

work := func() {
detect := false
Expand All @@ -39,11 +33,11 @@ func main() {
}
})
gobot.On(drone.Events["Flying"], func(data interface{}) {
gobot.After("1s", func() { drone.Up(0.2) })
gobot.After("2s", func() { drone.Hover() })
gobot.After("5s", func() {
gobot.After(1*time.Second, func() { drone.Up(0.2) })
gobot.After(2*time.Second, func() { drone.Hover() })
gobot.After(5*time.Second, func() {
detect = true
gobot.Every("0.3s", func() {
gobot.Every(0.3*time.Second, func() {
drone.Hover()
i := image
faces := opencv.DetectFaces(cascade, i)
Expand All @@ -68,16 +62,13 @@ func main() {
}
window.ShowImage(i)
})
gobot.After("20s", func() { drone.Land() })
gobot.After(20*time.Second, func() { drone.Land() })
})
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{ardroneAdaptor},
Devices: []gobot.Device{window, camera, drone},
Work: work,
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("face", []gobot.Connection{ardroneAdaptor}, []gobot.Device{window, camera, drone}, work))

robot.Start()
}
36 changes: 13 additions & 23 deletions examples/ardrone_ps3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/ardrone"
"github.com/hybridgroup/gobot/joystick"
"github.com/hybridgroup/gobot/platforms/ardrone"
"github.com/hybridgroup/gobot/platforms/joystick"
"math"
"time"
)

type pair struct {
Expand All @@ -13,23 +14,15 @@ type pair struct {
}

func main() {
joystickAdaptor := joystick.NewJoystickAdaptor()
joystickAdaptor.Name = "ps3"
joystickAdaptor.Params = map[string]interface{}{
"config": "../joytsitkc/configs/dualshock3.json",
}

joystick := joystick.NewJoystickDriver(joystickAdaptor)
joystick.Name = "ps3"
gbot := gobot.NewGobot()

ardroneAdaptor := ardrone.NewArdroneAdaptor()
ardroneAdaptor.Name = "Drone"
joystickAdaptor := joystick.NewJoystickAdaptor("ps3", "../joystick/configs/dualshock3.json")
joystick := joystick.NewJoystickDriver(joystickAdaptor, "ps3")

drone := ardrone.NewArdroneDriver(ardroneAdaptor)
drone.Name = "Drone"
ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone")

work := func() {

offset := 32767.0
right_stick := pair{x: 0, y: 0}
left_stick := pair{x: 0, y: 0}
Expand Down Expand Up @@ -68,7 +61,7 @@ func main() {
}
})

gobot.Every("0.01s", func() {
gobot.Every(0.01*time.Second, func() {
pair := left_stick
if pair.y < -10 {
drone.Forward(validatePitch(pair.y, offset))
Expand All @@ -87,7 +80,7 @@ func main() {
}
})

gobot.Every("0.01s", func() {
gobot.Every(0.01*time.Second, func() {
pair := right_stick
if pair.y < -10 {
drone.Up(validatePitch(pair.y, offset))
Expand All @@ -107,13 +100,10 @@ func main() {
})
}

robot := gobot.Robot{
Connections: []gobot.Connection{joystickAdaptor, ardroneAdaptor},
Devices: []gobot.Device{joystick, drone},
Work: work,
}
gbot.Robot = append(gbot.Robots,
gobot.NewRobot("ardrone", []gobot.Connection{joystickAdaptor, ardroneAdaptor}, []gobot.Device{joystick, drone}, work))

robot.Start()
gbot.Start()
}

func validatePitch(data float64, offset float64) float64 {
Expand Down
5 changes: 4 additions & 1 deletion platforms/ardrone/ardrone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ type ArdroneAdaptor struct {
connect func(*ArdroneAdaptor)
}

func NewArdroneAdaptor() *ArdroneAdaptor {
func NewArdroneAdaptor(name string) *ArdroneAdaptor {
return &ArdroneAdaptor{
Adaptor: gobot.Adaptor{
Name: name,
},
connect: func(a *ArdroneAdaptor) {
d, err := client.Connect(client.DefaultConfig())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion platforms/ardrone/ardrone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ = Describe("ArdroneAdaptor", func() {

BeforeEach(func() {
drone = &testDrone{}
adaptor = NewArdroneAdaptor()
adaptor = NewArdroneAdaptor("drone")
adaptor.connect = func(a *ArdroneAdaptor) {
a.drone = drone
}
Expand Down
3 changes: 2 additions & 1 deletion platforms/ardrone/ardrone_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ type DroneInterface interface {
Drone() drone
}

func NewArdroneDriver(adaptor DroneInterface) *ArdroneDriver {
func NewArdroneDriver(adaptor DroneInterface, name string) *ArdroneDriver {
return &ArdroneDriver{
Driver: gobot.Driver{
Name: name,
Events: map[string]chan interface{}{
"Flying": make(chan interface{}, 1),
},
Expand Down
4 changes: 2 additions & 2 deletions platforms/ardrone/ardrone_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ var _ = Describe("ArdroneDriver", func() {
)

BeforeEach(func() {
adaptor := NewArdroneAdaptor()
adaptor := NewArdroneAdaptor("drone")
adaptor.connect = func(a *ArdroneAdaptor) {
a.drone = &testDrone{}
}
driver = NewArdroneDriver(adaptor)
driver = NewArdroneDriver(adaptor, "drone")
adaptor.Connect()
})

Expand Down

0 comments on commit d32a527

Please sign in to comment.