2014-04-26 18:11:51 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-07-11 08:21:21 +08:00
|
|
|
"math"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-09-25 17:46:55 +08:00
|
|
|
joystickAdaptor := joystick.NewAdaptor()
|
|
|
|
stick := joystick.NewDriver(joystickAdaptor,
|
2014-07-11 08:21:21 +08:00
|
|
|
"./platforms/joystick/configs/dualshock3.json",
|
|
|
|
)
|
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
|
|
|
|
|
|
|
work := func() {
|
|
|
|
offset := 32767.0
|
2014-06-11 06:16:11 +08:00
|
|
|
rightStick := pair{x: 0, y: 0}
|
|
|
|
leftStick := pair{x: 0, y: 0}
|
2014-04-26 18:11:51 +08:00
|
|
|
|
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()
|
|
|
|
})
|
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()
|
|
|
|
})
|
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()
|
|
|
|
})
|
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))
|
2014-06-11 06:16:11 +08:00
|
|
|
if leftStick.x != val {
|
|
|
|
leftStick.x = val
|
2014-04-26 18:11:51 +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))
|
2014-06-11 06:16:11 +08:00
|
|
|
if leftStick.y != val {
|
|
|
|
leftStick.y = val
|
2014-04-26 18:11:51 +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))
|
2014-06-11 06:16:11 +08:00
|
|
|
if rightStick.x != val {
|
|
|
|
rightStick.x = val
|
2014-04-26 18:11:51 +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))
|
2014-06-11 06:16:11 +08:00
|
|
|
if rightStick.y != val {
|
|
|
|
rightStick.y = val
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-06-09 10:50:15 +08:00
|
|
|
gobot.Every(10*time.Millisecond, func() {
|
2014-06-11 06:16:11 +08:00
|
|
|
pair := leftStick
|
2014-04-26 18:11:51 +08:00
|
|
|
if pair.y < -10 {
|
|
|
|
drone.Forward(validatePitch(pair.y, offset))
|
|
|
|
} else if pair.y > 10 {
|
|
|
|
drone.Backward(validatePitch(pair.y, offset))
|
|
|
|
} else {
|
|
|
|
drone.Forward(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pair.x > 10 {
|
|
|
|
drone.Right(validatePitch(pair.x, offset))
|
|
|
|
} else if pair.x < -10 {
|
|
|
|
drone.Left(validatePitch(pair.x, offset))
|
|
|
|
} else {
|
|
|
|
drone.Right(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2014-06-09 10:50:15 +08:00
|
|
|
gobot.Every(10*time.Millisecond, func() {
|
2014-06-11 06:16:11 +08:00
|
|
|
pair := rightStick
|
2014-04-26 18:11:51 +08:00
|
|
|
if pair.y < -10 {
|
|
|
|
drone.Up(validatePitch(pair.y, offset))
|
|
|
|
} else if pair.y > 10 {
|
|
|
|
drone.Down(validatePitch(pair.y, offset))
|
|
|
|
} else {
|
|
|
|
drone.Up(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pair.x > 20 {
|
|
|
|
drone.Clockwise(validatePitch(pair.x, offset))
|
|
|
|
} else if pair.x < -20 {
|
|
|
|
drone.CounterClockwise(validatePitch(pair.x, offset))
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func validatePitch(data float64, offset float64) float64 {
|
|
|
|
value := math.Abs(data) / offset
|
|
|
|
if value >= 0.1 {
|
|
|
|
if value <= 1.0 {
|
|
|
|
return float64(int(value*100)) / 100
|
|
|
|
}
|
2014-06-11 06:16:11 +08:00
|
|
|
return 1.0
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-06-11 06:16:11 +08:00
|
|
|
return 0.0
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|