forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardrone_ps3.go
118 lines (104 loc) · 2.73 KB
/
ardrone_ps3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/platforms/ardrone"
"github.com/hybridgroup/gobot/platforms/joystick"
"math"
"time"
)
type pair struct {
x float64
y float64
}
func main() {
gbot := gobot.NewGobot()
joystickAdaptor := joystick.NewJoystickAdaptor("ps3")
joystick := joystick.NewJoystickDriver(joystickAdaptor, "ps3", "./platforms/joystick/configs/dualshock3.json")
ardroneAdaptor := ardrone.NewArdroneAdaptor("Drone")
drone := ardrone.NewArdroneDriver(ardroneAdaptor, "Drone")
work := func() {
offset := 32767.0
rightStick := pair{x: 0, y: 0}
leftStick := pair{x: 0, y: 0}
gobot.On(joystick.Events["square_press"], func(data interface{}) {
drone.TakeOff()
})
gobot.On(joystick.Events["triangle_press"], func(data interface{}) {
drone.Hover()
})
gobot.On(joystick.Events["x_press"], func(data interface{}) {
drone.Land()
})
gobot.On(joystick.Events["left_x"], func(data interface{}) {
val := float64(data.(int16))
if leftStick.x != val {
leftStick.x = val
}
})
gobot.On(joystick.Events["left_y"], func(data interface{}) {
val := float64(data.(int16))
if leftStick.y != val {
leftStick.y = val
}
})
gobot.On(joystick.Events["right_x"], func(data interface{}) {
val := float64(data.(int16))
if rightStick.x != val {
rightStick.x = val
}
})
gobot.On(joystick.Events["right_y"], func(data interface{}) {
val := float64(data.(int16))
if rightStick.y != val {
rightStick.y = val
}
})
gobot.Every(10*time.Millisecond, func() {
pair := leftStick
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)
}
})
gobot.Every(10*time.Millisecond, func() {
pair := rightStick
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)
}
})
}
gbot.Robots = append(gbot.Robots,
gobot.NewRobot("ardrone", []gobot.Connection{joystickAdaptor, ardroneAdaptor}, []gobot.Device{joystick, drone}, work))
gbot.Start()
}
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
}
return 1.0
}
return 0.0
}