2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-07 19:19:29 +08:00
|
|
|
"sync/atomic"
|
2014-07-11 08:21:21 +08:00
|
|
|
"time"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/platforms/joystick"
|
|
|
|
"gobot.io/x/gobot/platforms/parrot/ardrone"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type pair struct {
|
|
|
|
x float64
|
|
|
|
y float64
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
var leftX, leftY, rightX, rightY atomic.Value
|
|
|
|
|
|
|
|
const offset = 32767.0
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
func main() {
|
2016-09-25 17:46:55 +08:00
|
|
|
joystickAdaptor := joystick.NewAdaptor()
|
2018-04-14 19:34:28 +08:00
|
|
|
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2016-09-25 17:46:55 +08:00
|
|
|
ardroneAdaptor := ardrone.NewAdaptor()
|
|
|
|
drone := ardrone.NewDriver(ardroneAdaptor)
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
leftX.Store(float64(0.0))
|
|
|
|
leftY.Store(float64(0.0))
|
|
|
|
rightX.Store(float64(0.0))
|
|
|
|
rightY.Store(float64(0.0))
|
2014-04-26 18:11:51 +08:00
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
work := func() {
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.SquarePress, func(data interface{}) {
|
2014-04-26 18:11:51 +08:00
|
|
|
drone.TakeOff()
|
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.TrianglePress, func(data interface{}) {
|
2014-04-26 18:11:51 +08:00
|
|
|
drone.Hover()
|
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.XPress, func(data interface{}) {
|
2014-04-26 18:11:51 +08:00
|
|
|
drone.Land()
|
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.LeftX, func(data interface{}) {
|
2014-04-26 18:11:51 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
leftX.Store(val)
|
2014-04-26 18:11:51 +08:00
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.LeftY, func(data interface{}) {
|
2014-04-26 18:11:51 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
leftY.Store(val)
|
2014-04-26 18:11:51 +08:00
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.RightX, func(data interface{}) {
|
2014-04-26 18:11:51 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
rightX.Store(val)
|
2014-04-26 18:11:51 +08:00
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.RightY, func(data interface{}) {
|
2014-04-26 18:11:51 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
rightY.Store(val)
|
2014-04-26 18:11:51 +08:00
|
|
|
})
|
|
|
|
|
2014-06-09 10:50:15 +08:00
|
|
|
gobot.Every(10*time.Millisecond, func() {
|
2017-02-07 19:19:29 +08:00
|
|
|
leftStick := getLeftStick()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case leftStick.y < -10:
|
|
|
|
drone.Forward(ardrone.ValidatePitch(leftStick.y, offset))
|
|
|
|
case leftStick.y > 10:
|
|
|
|
drone.Backward(ardrone.ValidatePitch(leftStick.y, offset))
|
|
|
|
default:
|
2014-04-26 18:11:51 +08:00
|
|
|
drone.Forward(0)
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
switch {
|
|
|
|
case leftStick.x > 10:
|
|
|
|
drone.Right(ardrone.ValidatePitch(leftStick.x, offset))
|
|
|
|
case leftStick.x < -10:
|
|
|
|
drone.Left(ardrone.ValidatePitch(leftStick.x, offset))
|
|
|
|
default:
|
2014-04-26 18:11:51 +08:00
|
|
|
drone.Right(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-06-09 10:50:15 +08:00
|
|
|
gobot.Every(10*time.Millisecond, func() {
|
2017-02-07 19:19:29 +08:00
|
|
|
rightStick := getRightStick()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case rightStick.y < -10:
|
|
|
|
drone.Up(ardrone.ValidatePitch(rightStick.y, offset))
|
|
|
|
case rightStick.y > 10:
|
|
|
|
drone.Down(ardrone.ValidatePitch(rightStick.y, offset))
|
|
|
|
default:
|
2014-04-26 18:11:51 +08:00
|
|
|
drone.Up(0)
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
switch {
|
|
|
|
case rightStick.x > 20:
|
|
|
|
drone.Clockwise(ardrone.ValidatePitch(rightStick.x, offset))
|
|
|
|
case rightStick.x < -20:
|
|
|
|
drone.CounterClockwise(ardrone.ValidatePitch(rightStick.x, offset))
|
|
|
|
default:
|
2014-04-26 18:11:51 +08:00
|
|
|
drone.Clockwise(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-09 09:36:14 +08:00
|
|
|
robot := gobot.NewRobot("ardrone",
|
|
|
|
[]gobot.Connection{joystickAdaptor, ardroneAdaptor},
|
2016-09-01 18:17:43 +08:00
|
|
|
[]gobot.Device{stick, drone},
|
2014-07-09 09:36:14 +08:00
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
func getLeftStick() pair {
|
|
|
|
s := pair{x: 0, y: 0}
|
|
|
|
s.x = leftX.Load().(float64)
|
|
|
|
s.y = leftY.Load().(float64)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRightStick() pair {
|
|
|
|
s := pair{x: 0, y: 0}
|
|
|
|
s.x = rightX.Load().(float64)
|
|
|
|
s.y = rightY.Load().(float64)
|
|
|
|
return s
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|