gobot.io/x/gobot@v1.16.0/examples/minidrone_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 8 any of the Parrot Minidrone drones to run this example. 9 10 You run the Go program on your computer and communicate 11 wirelessly with the Parrot Minidrone. 12 13 How to run 14 Pass the Bluetooth name or address as first param: 15 16 go run examples/minidrone_ps3.go "Travis_1234" 17 18 NOTE: sudo is required to use BLE in Linux 19 */ 20 21 package main 22 23 import ( 24 "os" 25 "sync/atomic" 26 "time" 27 28 "gobot.io/x/gobot" 29 "gobot.io/x/gobot/platforms/ble" 30 "gobot.io/x/gobot/platforms/joystick" 31 "gobot.io/x/gobot/platforms/parrot/minidrone" 32 ) 33 34 type pair struct { 35 x float64 36 y float64 37 } 38 39 var leftX, leftY, rightX, rightY atomic.Value 40 41 const offset = 32767.0 42 43 func main() { 44 joystickAdaptor := joystick.NewAdaptor() 45 stick := joystick.NewDriver(joystickAdaptor, "dualshock3") 46 47 droneAdaptor := ble.NewClientAdaptor(os.Args[1]) 48 drone := minidrone.NewDriver(droneAdaptor) 49 50 work := func() { 51 leftX.Store(float64(0.0)) 52 leftY.Store(float64(0.0)) 53 rightX.Store(float64(0.0)) 54 rightY.Store(float64(0.0)) 55 56 recording := false 57 58 stick.On(joystick.CirclePress, func(data interface{}) { 59 if recording { 60 drone.StopRecording() 61 } else { 62 drone.StartRecording() 63 } 64 recording = !recording 65 }) 66 67 stick.On(joystick.SquarePress, func(data interface{}) { 68 drone.Stop() 69 }) 70 71 stick.On(joystick.TrianglePress, func(data interface{}) { 72 drone.HullProtection(true) 73 drone.TakeOff() 74 }) 75 76 stick.On(joystick.XPress, func(data interface{}) { 77 drone.Land() 78 }) 79 80 stick.On(joystick.LeftX, func(data interface{}) { 81 val := float64(data.(int16)) 82 leftX.Store(val) 83 }) 84 85 stick.On(joystick.LeftY, func(data interface{}) { 86 val := float64(data.(int16)) 87 leftY.Store(val) 88 }) 89 90 stick.On(joystick.RightX, func(data interface{}) { 91 val := float64(data.(int16)) 92 rightX.Store(val) 93 }) 94 95 stick.On(joystick.RightY, func(data interface{}) { 96 val := float64(data.(int16)) 97 rightY.Store(val) 98 }) 99 100 gobot.Every(10*time.Millisecond, func() { 101 rightStick := getRightStick() 102 103 switch { 104 case rightStick.y < -10: 105 drone.Forward(minidrone.ValidatePitch(rightStick.y, offset)) 106 case rightStick.y > 10: 107 drone.Backward(minidrone.ValidatePitch(rightStick.y, offset)) 108 default: 109 drone.Forward(0) 110 } 111 112 switch { 113 case rightStick.x > 10: 114 drone.Right(minidrone.ValidatePitch(rightStick.x, offset)) 115 case rightStick.x < -10: 116 drone.Left(minidrone.ValidatePitch(rightStick.x, offset)) 117 default: 118 drone.Right(0) 119 } 120 }) 121 122 gobot.Every(10*time.Millisecond, func() { 123 leftStick := getLeftStick() 124 switch { 125 case leftStick.y < -10: 126 drone.Up(minidrone.ValidatePitch(leftStick.y, offset)) 127 case leftStick.y > 10: 128 drone.Down(minidrone.ValidatePitch(leftStick.y, offset)) 129 default: 130 drone.Up(0) 131 } 132 133 switch { 134 case leftStick.x > 20: 135 drone.Clockwise(minidrone.ValidatePitch(leftStick.x, offset)) 136 case leftStick.x < -20: 137 drone.CounterClockwise(minidrone.ValidatePitch(leftStick.x, offset)) 138 default: 139 drone.Clockwise(0) 140 } 141 }) 142 } 143 144 robot := gobot.NewRobot("minidrone", 145 []gobot.Connection{joystickAdaptor, droneAdaptor}, 146 []gobot.Device{stick, drone}, 147 work, 148 ) 149 150 robot.Start() 151 } 152 153 func getLeftStick() pair { 154 s := pair{x: 0, y: 0} 155 s.x = leftX.Load().(float64) 156 s.y = leftY.Load().(float64) 157 return s 158 } 159 160 func getRightStick() pair { 161 s := pair{x: 0, y: 0} 162 s.x = rightX.Load().(float64) 163 s.y = rightY.Load().(float64) 164 return s 165 }