gobot.io/x/gobot@v1.16.0/examples/tello_ps3.go (about) 1 // +build example 2 // 3 // Do not build by default. 4 5 /* 6 How to setup 7 You must be using a PS3 or compatible controller, along with a DJI Tello drone to run this example. 8 9 You run the Go program on your computer and communicate wirelessly via WiFi with the Tello. 10 11 How to run 12 13 go run examples/tello_ps3.go 14 */ 15 16 package main 17 18 import ( 19 "fmt" 20 "sync/atomic" 21 "time" 22 23 "gobot.io/x/gobot" 24 "gobot.io/x/gobot/platforms/dji/tello" 25 "gobot.io/x/gobot/platforms/joystick" 26 ) 27 28 type pair struct { 29 x float64 30 y float64 31 } 32 33 var leftX, leftY, rightX, rightY atomic.Value 34 35 const offset = 32767.0 36 37 func main() { 38 joystickAdaptor := joystick.NewAdaptor() 39 stick := joystick.NewDriver(joystickAdaptor, "dualshock3") 40 41 drone := tello.NewDriver("8888") 42 43 work := func() { 44 leftX.Store(float64(0.0)) 45 leftY.Store(float64(0.0)) 46 rightX.Store(float64(0.0)) 47 rightY.Store(float64(0.0)) 48 49 stick.On(joystick.TrianglePress, func(data interface{}) { 50 drone.TakeOff() 51 }) 52 53 stick.On(joystick.XPress, func(data interface{}) { 54 drone.Land() 55 }) 56 57 stick.On(joystick.UpPress, func(data interface{}) { 58 fmt.Println("FrontFlip") 59 drone.FrontFlip() 60 }) 61 62 stick.On(joystick.DownPress, func(data interface{}) { 63 fmt.Println("BackFlip") 64 drone.BackFlip() 65 }) 66 67 stick.On(joystick.RightPress, func(data interface{}) { 68 fmt.Println("RightFlip") 69 drone.RightFlip() 70 }) 71 72 stick.On(joystick.LeftPress, func(data interface{}) { 73 fmt.Println("LeftFlip") 74 drone.LeftFlip() 75 }) 76 77 stick.On(joystick.LeftX, func(data interface{}) { 78 val := float64(data.(int16)) 79 leftX.Store(val) 80 }) 81 82 stick.On(joystick.LeftY, func(data interface{}) { 83 val := float64(data.(int16)) 84 leftY.Store(val) 85 }) 86 87 stick.On(joystick.RightX, func(data interface{}) { 88 val := float64(data.(int16)) 89 rightX.Store(val) 90 }) 91 92 stick.On(joystick.RightY, func(data interface{}) { 93 val := float64(data.(int16)) 94 rightY.Store(val) 95 }) 96 97 gobot.Every(50*time.Millisecond, func() { 98 rightStick := getRightStick() 99 100 switch { 101 case rightStick.y < -10: 102 drone.Forward(tello.ValidatePitch(rightStick.y, offset)) 103 case rightStick.y > 10: 104 drone.Backward(tello.ValidatePitch(rightStick.y, offset)) 105 default: 106 drone.Forward(0) 107 } 108 109 switch { 110 case rightStick.x > 10: 111 drone.Right(tello.ValidatePitch(rightStick.x, offset)) 112 case rightStick.x < -10: 113 drone.Left(tello.ValidatePitch(rightStick.x, offset)) 114 default: 115 drone.Right(0) 116 } 117 }) 118 119 gobot.Every(50*time.Millisecond, func() { 120 leftStick := getLeftStick() 121 switch { 122 case leftStick.y < -10: 123 drone.Up(tello.ValidatePitch(leftStick.y, offset)) 124 case leftStick.y > 10: 125 drone.Down(tello.ValidatePitch(leftStick.y, offset)) 126 default: 127 drone.Up(0) 128 } 129 130 switch { 131 case leftStick.x > 20: 132 drone.Clockwise(tello.ValidatePitch(leftStick.x, offset)) 133 case leftStick.x < -20: 134 drone.CounterClockwise(tello.ValidatePitch(leftStick.x, offset)) 135 default: 136 drone.Clockwise(0) 137 } 138 }) 139 } 140 141 robot := gobot.NewRobot("tello", 142 []gobot.Connection{joystickAdaptor}, 143 []gobot.Device{stick, drone}, 144 work, 145 ) 146 147 robot.Start() 148 } 149 150 func getLeftStick() pair { 151 s := pair{x: 0, y: 0} 152 s.x = leftX.Load().(float64) 153 s.y = leftY.Load().(float64) 154 return s 155 } 156 157 func getRightStick() pair { 158 s := pair{x: 0, y: 0} 159 s.x = rightX.Load().(float64) 160 s.y = rightY.Load().(float64) 161 return s 162 }