2017-03-13 23:01:39 +08:00
|
|
|
// +build example
|
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
2015-10-23 07:56:03 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-07 19:19:29 +08:00
|
|
|
"sync/atomic"
|
2015-10-23 07:56:03 +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/bebop"
|
2015-10-23 07:56:03 +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
|
|
|
|
|
2015-10-23 07:56:03 +08:00
|
|
|
func main() {
|
2016-10-03 22:58:43 +08:00
|
|
|
joystickAdaptor := joystick.NewAdaptor()
|
2018-04-14 19:34:28 +08:00
|
|
|
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")
|
2015-10-23 07:56:03 +08:00
|
|
|
|
2016-10-03 22:58:43 +08:00
|
|
|
bebopAdaptor := bebop.NewAdaptor()
|
|
|
|
drone := bebop.NewDriver(bebopAdaptor)
|
2015-10-23 07:56:03 +08:00
|
|
|
|
|
|
|
work := func() {
|
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))
|
2015-10-23 07:56:03 +08:00
|
|
|
|
|
|
|
recording := false
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.CirclePress, func(data interface{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
if recording {
|
|
|
|
drone.StopRecording()
|
|
|
|
} else {
|
|
|
|
drone.StartRecording()
|
|
|
|
}
|
|
|
|
recording = !recording
|
|
|
|
})
|
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.SquarePress, func(data interface{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
drone.HullProtection(true)
|
|
|
|
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{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
drone.Stop()
|
|
|
|
})
|
2017-02-07 19:19:29 +08:00
|
|
|
|
2016-09-01 18:17:43 +08:00
|
|
|
stick.On(joystick.XPress, func(data interface{}) {
|
2015-10-23 07:56:03 +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{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
leftX.Store(val)
|
2015-10-23 07:56:03 +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{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
leftY.Store(val)
|
2015-10-23 07:56:03 +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{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
rightX.Store(val)
|
2015-10-23 07:56:03 +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{}) {
|
2015-10-23 07:56:03 +08:00
|
|
|
val := float64(data.(int16))
|
2017-02-07 19:19:29 +08:00
|
|
|
rightY.Store(val)
|
2015-10-23 07:56:03 +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(bebop.ValidatePitch(leftStick.y, offset))
|
|
|
|
case leftStick.y > 10:
|
|
|
|
drone.Backward(bebop.ValidatePitch(leftStick.y, offset))
|
|
|
|
default:
|
2015-10-23 07:56:03 +08:00
|
|
|
drone.Forward(0)
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
switch {
|
|
|
|
case leftStick.x > 10:
|
|
|
|
drone.Right(bebop.ValidatePitch(leftStick.x, offset))
|
|
|
|
case leftStick.x < -10:
|
|
|
|
drone.Left(bebop.ValidatePitch(leftStick.x, offset))
|
|
|
|
default:
|
2015-10-23 07:56:03 +08:00
|
|
|
drone.Right(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
gobot.Every(10*time.Millisecond, func() {
|
2017-02-07 19:19:29 +08:00
|
|
|
rightStick := getRightStick()
|
|
|
|
switch {
|
|
|
|
case rightStick.y < -10:
|
|
|
|
drone.Up(bebop.ValidatePitch(rightStick.y, offset))
|
|
|
|
case rightStick.y > 10:
|
|
|
|
drone.Down(bebop.ValidatePitch(rightStick.y, offset))
|
|
|
|
default:
|
2015-10-23 07:56:03 +08:00
|
|
|
drone.Up(0)
|
|
|
|
}
|
|
|
|
|
2017-02-07 19:19:29 +08:00
|
|
|
switch {
|
|
|
|
case rightStick.x > 20:
|
|
|
|
drone.Clockwise(bebop.ValidatePitch(rightStick.x, offset))
|
|
|
|
case rightStick.x < -20:
|
|
|
|
drone.CounterClockwise(bebop.ValidatePitch(rightStick.x, offset))
|
|
|
|
default:
|
2015-10-23 07:56:03 +08:00
|
|
|
drone.Clockwise(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("bebop",
|
|
|
|
[]gobot.Connection{joystickAdaptor, bebopAdaptor},
|
2016-09-01 18:17:43 +08:00
|
|
|
[]gobot.Device{stick, drone},
|
2015-10-23 07:56:03 +08:00
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
2016-10-19 03:08:25 +08:00
|
|
|
robot.Start()
|
2015-10-23 07:56:03 +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
|
2015-10-23 07:56:03 +08:00
|
|
|
}
|