-
Notifications
You must be signed in to change notification settings - Fork 22
/
controller.go
40 lines (33 loc) · 1.06 KB
/
controller.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
package godrone
import (
"math"
"time"
)
type Controller struct {
RotationBand float64
ThrottleHover float64
ThrottleMin float64
Pitch PID
Roll PID
Yaw PID
Altitude PID
}
func (c *Controller) Control(actual, desired Placement, dt time.Duration) [4]float64 {
pitchOut := c.Roll.Update(actual.Pitch, desired.Pitch, dt)
rollOut := c.Roll.Update(actual.Roll, desired.Roll, dt)
yawOut := c.Yaw.Update(actual.Yaw, desired.Yaw, dt)
altOut := c.Altitude.Update(actual.Altitude, desired.Altitude, dt)
throttle := math.Max(c.ThrottleMin, math.Min(1-c.RotationBand, c.ThrottleMin+altOut))
return [4]float64{
throttle + clipBand(+rollOut+pitchOut+yawOut, c.RotationBand),
throttle + clipBand(-rollOut+pitchOut-yawOut, c.RotationBand),
throttle + clipBand(-rollOut-pitchOut+yawOut, c.RotationBand),
throttle + clipBand(+rollOut-pitchOut-yawOut, c.RotationBand),
}
}
func clipBand(val, band float64) float64 {
return band/2 + clip(val, band/2)
}
func clip(val, max float64) float64 {
return math.Max(math.Min(val, max), -max)
}