-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfilter.go
52 lines (48 loc) · 1.41 KB
/
filter.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
package godrone
import (
"math"
"time"
)
type Filter struct {
// AccGain as fraction of 1
AccGain float64
// GyroGain as fraction of 1
GyroGain float64
// SonarGain as fraction of 1
SonarGain float64
// SonarMax in m
SonarMax float64
}
func (f Filter) Update(placement *Placement, sensors Sensors, dt time.Duration) {
if f.AccGain+f.GyroGain != 1 {
panic("Gains must add up to 1")
}
var (
dts = dt.Seconds()
accDeg = PRY{
Pitch: degAngle(sensors.Acc.Roll, sensors.Acc.Yaw),
Roll: degAngle(sensors.Acc.Pitch, sensors.Acc.Yaw),
}
gyroDeg = PRY{
Pitch: placement.Pitch + (sensors.Gyro.Pitch * dts),
Roll: placement.Roll + (sensors.Gyro.Roll * dts),
Yaw: placement.Yaw + (sensors.Gyro.Yaw * dts),
}
)
// Implements a simple complementation filter.
// see http://www.pieter-jan.com/node/11
placement.Pitch = gyroDeg.Pitch*f.GyroGain + accDeg.Pitch*f.AccGain
placement.Roll = gyroDeg.Roll*f.GyroGain + accDeg.Roll*f.AccGain
// @TODO Integrate gyro yaw with magotometer yaw
placement.Yaw = gyroDeg.Yaw
// The sonar sometimes reads very high values when on the ground. Ignoring
// the sonar above a certain altitude solves the problem.
// @TODO Use barometer above SonarMax
if sensors.Sonar < f.SonarMax {
placement.Altitude += (sensors.Sonar - placement.Altitude) * f.SonarGain
}
}
func degAngle(a, b float64) float64 {
const rad2Deg = 180 / math.Pi
return math.Atan2(a, b) * rad2Deg
}