mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
165 lines
3.3 KiB
Go
165 lines
3.3 KiB
Go
![]() |
// +build example
|
||
|
//
|
||
|
// Do not build by default.
|
||
|
|
||
|
/*
|
||
|
How to setup
|
||
|
You must be using a PS3 or compatible controller, along with
|
||
|
the DJI Tello drones to run this example.
|
||
|
|
||
|
You run the Go program on your computer and communicate
|
||
|
wirelessly with the Tello.
|
||
|
|
||
|
How to run
|
||
|
Pass the Bluetooth name or address as first param:
|
||
|
|
||
|
go run examples/tello_ps3.go "Travis_1234"
|
||
|
|
||
|
NOTE: sudo is required to use BLE in Linux
|
||
|
*/
|
||
|
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"sync/atomic"
|
||
|
"time"
|
||
|
|
||
|
"gobot.io/x/gobot"
|
||
|
"gobot.io/x/gobot/platforms/dji/tello"
|
||
|
"gobot.io/x/gobot/platforms/joystick"
|
||
|
)
|
||
|
|
||
|
type pair struct {
|
||
|
x float64
|
||
|
y float64
|
||
|
}
|
||
|
|
||
|
var leftX, leftY, rightX, rightY atomic.Value
|
||
|
|
||
|
const offset = 32767.0
|
||
|
|
||
|
func main() {
|
||
|
joystickAdaptor := joystick.NewAdaptor()
|
||
|
stick := joystick.NewDriver(joystickAdaptor,
|
||
|
"./platforms/joystick/configs/dualshock3.json",
|
||
|
)
|
||
|
|
||
|
drone := tello.NewDriver(os.Args[1])
|
||
|
|
||
|
work := func() {
|
||
|
leftX.Store(float64(0.0))
|
||
|
leftY.Store(float64(0.0))
|
||
|
rightX.Store(float64(0.0))
|
||
|
rightY.Store(float64(0.0))
|
||
|
|
||
|
recording := false
|
||
|
|
||
|
stick.On(joystick.CirclePress, func(data interface{}) {
|
||
|
// if recording {
|
||
|
// drone.StopRecording()
|
||
|
// } else {
|
||
|
// drone.StartRecording()
|
||
|
// }
|
||
|
recording = !recording
|
||
|
})
|
||
|
|
||
|
stick.On(joystick.SquarePress, func(data interface{}) {
|
||
|
//drone.Stop()
|
||
|
})
|
||
|
|
||
|
stick.On(joystick.TrianglePress, func(data interface{}) {
|
||
|
drone.TakeOff()
|
||
|
})
|
||
|
|
||
|
stick.On(joystick.XPress, func(data interface{}) {
|
||
|
drone.Land()
|
||
|
})
|
||
|
|
||
|
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)
|
||
|
})
|
||
|
|
||
|
gobot.Every(10*time.Millisecond, func() {
|
||
|
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)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
gobot.Every(10*time.Millisecond, func() {
|
||
|
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
|
||
|
}
|