2023-05-20 20:25:21 +08:00
|
|
|
//go:build example
|
2018-04-13 19:49:12 +08:00
|
|
|
// +build example
|
2023-05-20 20:25:21 +08:00
|
|
|
|
2018-04-13 19:49:12 +08:00
|
|
|
//
|
|
|
|
// Do not build by default.
|
|
|
|
|
|
|
|
/*
|
2018-04-13 20:01:28 +08:00
|
|
|
How to setup
|
|
|
|
You must be using a PS3 or compatible controller, along with a DJI Tello drone to run this example.
|
2018-04-13 19:49:12 +08:00
|
|
|
|
2018-04-13 20:01:28 +08:00
|
|
|
You run the Go program on your computer and communicate wirelessly via WiFi with the Tello.
|
2018-04-13 19:49:12 +08:00
|
|
|
|
2018-04-13 20:01:28 +08:00
|
|
|
How to run
|
2018-04-13 19:49:12 +08:00
|
|
|
|
2018-04-13 20:01:28 +08:00
|
|
|
go run examples/tello_ps3.go
|
2018-04-13 19:49:12 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-18 18:15:18 +08:00
|
|
|
"fmt"
|
2018-04-13 19:49:12 +08:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/dji/tello"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/joystick"
|
2018-04-13 19:49:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type pair struct {
|
|
|
|
x float64
|
|
|
|
y float64
|
|
|
|
}
|
|
|
|
|
|
|
|
var leftX, leftY, rightX, rightY atomic.Value
|
|
|
|
|
|
|
|
const offset = 32767.0
|
|
|
|
|
|
|
|
func main() {
|
2023-09-23 18:32:31 +08:00
|
|
|
joystickAdaptor := joystick.NewAdaptor("0")
|
2018-04-14 19:34:28 +08:00
|
|
|
stick := joystick.NewDriver(joystickAdaptor, "dualshock3")
|
2018-04-13 19:49:12 +08:00
|
|
|
|
2018-04-13 20:01:28 +08:00
|
|
|
drone := tello.NewDriver("8888")
|
2018-04-13 19:49:12 +08:00
|
|
|
|
|
|
|
work := func() {
|
|
|
|
leftX.Store(float64(0.0))
|
|
|
|
leftY.Store(float64(0.0))
|
|
|
|
rightX.Store(float64(0.0))
|
|
|
|
rightY.Store(float64(0.0))
|
|
|
|
|
|
|
|
stick.On(joystick.TrianglePress, func(data interface{}) {
|
|
|
|
drone.TakeOff()
|
|
|
|
})
|
|
|
|
|
|
|
|
stick.On(joystick.XPress, func(data interface{}) {
|
|
|
|
drone.Land()
|
|
|
|
})
|
|
|
|
|
2018-04-18 18:15:18 +08:00
|
|
|
stick.On(joystick.UpPress, func(data interface{}) {
|
|
|
|
fmt.Println("FrontFlip")
|
|
|
|
drone.FrontFlip()
|
|
|
|
})
|
|
|
|
|
|
|
|
stick.On(joystick.DownPress, func(data interface{}) {
|
|
|
|
fmt.Println("BackFlip")
|
|
|
|
drone.BackFlip()
|
|
|
|
})
|
|
|
|
|
|
|
|
stick.On(joystick.RightPress, func(data interface{}) {
|
|
|
|
fmt.Println("RightFlip")
|
|
|
|
drone.RightFlip()
|
|
|
|
})
|
|
|
|
|
|
|
|
stick.On(joystick.LeftPress, func(data interface{}) {
|
|
|
|
fmt.Println("LeftFlip")
|
|
|
|
drone.LeftFlip()
|
|
|
|
})
|
|
|
|
|
2018-04-13 19:49:12 +08:00
|
|
|
stick.On(joystick.LeftX, func(data interface{}) {
|
|
|
|
val := float64(data.(int16))
|
|
|
|
leftX.Store(val)
|
|
|
|
})
|
|
|
|
|
|
|
|
stick.On(joystick.LeftY, func(data interface{}) {
|
|
|
|
val := float64(data.(int16))
|
|
|
|
leftY.Store(val)
|
|
|
|
})
|
|
|
|
|
|
|
|
stick.On(joystick.RightX, func(data interface{}) {
|
|
|
|
val := float64(data.(int16))
|
|
|
|
rightX.Store(val)
|
|
|
|
})
|
|
|
|
|
|
|
|
stick.On(joystick.RightY, func(data interface{}) {
|
|
|
|
val := float64(data.(int16))
|
|
|
|
rightY.Store(val)
|
|
|
|
})
|
|
|
|
|
2018-08-24 00:07:38 +08:00
|
|
|
gobot.Every(50*time.Millisecond, func() {
|
2018-04-13 19:49:12 +08:00
|
|
|
rightStick := getRightStick()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case rightStick.y < -10:
|
|
|
|
drone.Forward(tello.ValidatePitch(rightStick.y, offset))
|
|
|
|
case rightStick.y > 10:
|
|
|
|
drone.Backward(tello.ValidatePitch(rightStick.y, offset))
|
|
|
|
default:
|
|
|
|
drone.Forward(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case rightStick.x > 10:
|
|
|
|
drone.Right(tello.ValidatePitch(rightStick.x, offset))
|
|
|
|
case rightStick.x < -10:
|
|
|
|
drone.Left(tello.ValidatePitch(rightStick.x, offset))
|
|
|
|
default:
|
|
|
|
drone.Right(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-08-24 00:07:38 +08:00
|
|
|
gobot.Every(50*time.Millisecond, func() {
|
2018-04-13 19:49:12 +08:00
|
|
|
leftStick := getLeftStick()
|
|
|
|
switch {
|
|
|
|
case leftStick.y < -10:
|
|
|
|
drone.Up(tello.ValidatePitch(leftStick.y, offset))
|
|
|
|
case leftStick.y > 10:
|
|
|
|
drone.Down(tello.ValidatePitch(leftStick.y, offset))
|
|
|
|
default:
|
|
|
|
drone.Up(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case leftStick.x > 20:
|
|
|
|
drone.Clockwise(tello.ValidatePitch(leftStick.x, offset))
|
|
|
|
case leftStick.x < -20:
|
|
|
|
drone.CounterClockwise(tello.ValidatePitch(leftStick.x, offset))
|
|
|
|
default:
|
|
|
|
drone.Clockwise(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
robot := gobot.NewRobot("tello",
|
|
|
|
[]gobot.Connection{joystickAdaptor},
|
|
|
|
[]gobot.Device{stick, drone},
|
|
|
|
work,
|
|
|
|
)
|
|
|
|
|
|
|
|
robot.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|