-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiofusion.go
179 lines (151 loc) · 4.48 KB
/
xiofusion.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package ahrs
import (
"math"
"gonum.org/v1/gonum/num/quat"
"gonum.org/v1/gonum/spatial/r3"
)
const (
initialGain = 10.0
// Seconds
initializationPeriod = 3.
)
var quatIdentity = quat.Number{Real: 1}
// NewXioAHRS instances a AHRS system with a IMU+heading sensor.
// Subsequent calls to Update read from IMU.
func NewXioAHRS(gain float64, imuWithMagnetometer IMUHeading) *XioAHRS {
f := NewXioARS(gain, imuWithMagnetometer)
f.ahrs = imuWithMagnetometer
return f
}
// NewFusionAHRS instances a AHRS system with only IMU sensor readings.
// Calls to Update read from IMU.
func NewXioARS(gain float64, imu IMU) *XioAHRS {
if imu == nil {
panic("nil IMU in NewFusionAHRS")
}
f := &XioAHRS{
gain: gain,
maxMFS: 1e200,
attitude: quatIdentity,
rampedGain: initialGain,
ars: imu,
}
if !(gain > 0) {
f.gain = initialGain
}
return f
}
// Taken shamelessly from xioTechnologies/Fusion on github.
type XioAHRS struct {
gain float64
// Magnetic field limits (squared)
maxMFS, minMFS float64
attitude quat.Number
acceleration r3.Vec
rampedGain float64
ahrs IMUHeading
ars IMU
}
func (f *XioAHRS) getVectors() (accel, rot, magnet r3.Vec) {
ax, ay, az := f.ars.Acceleration()
gx, gy, gz := f.ars.AngularVelocity()
accel = scaledVecFromInt(1e-6, ax, ay, az)
rot = scaledVecFromInt(1e-6, gx, gy, gz)
if f.ahrs == nil {
magnet = r3.Vec{X: 1} // prevent singularities
return
}
mx, my, mz := f.ahrs.North()
magnet = scaledVecFromInt(1, mx, my, mz)
return accel, rot, magnet
}
func (f *XioAHRS) SetGain(gain float64) { f.gain = gain }
func (f *XioAHRS) SetMagneticField(min, max float64) {
f.minMFS = min * min
f.maxMFS = max * max
}
// Update updates the internal quaternion
func (f *XioAHRS) Update(samplePeriod float64) {
q := f.attitude
var accel, gyro, magnet r3.Vec = f.getVectors()
// Half feedback error calculation
var hfe, halfWest, aux, gd2 r3.Vec
// If measurement is invalid, end calculation
mfs := 0.0
if accel.X == 0 && accel.Y == 0 && accel.Z == 0 {
goto ENDCALC
}
// Calculate direction of gravity assumed by quaternion
gd2 = r3.Vec{ // half gravity
X: q.Imag*q.Kmag - q.Real*q.Jmag,
Y: q.Real*q.Imag + q.Jmag*q.Kmag,
Z: q.Real*q.Real - .5 + q.Kmag*q.Kmag,
} // equal to 3rd column of rotation matrix representation scaled by 0.5
hfe = r3.Cross(r3.Unit(accel), gd2)
// Abandon magnetometer feedback calculation if magnetometer measurement invalid
mfs = r3.Norm2(magnet)
if f.ahrs == nil || mfs < f.minMFS || mfs > f.maxMFS {
goto ENDCALC
}
// Compute direction of 'magnetic west' assumed by quaternion
halfWest = r3.Vec{
X: q.Imag*q.Jmag + q.Real*q.Kmag,
Y: q.Real*q.Real - 0.5 + q.Jmag*q.Jmag,
Z: q.Jmag*q.Kmag - q.Real*q.Imag,
} // equal to 2nd column of rotation matrix representation scaled by 0.5
// calculate magnetometer feedback error
aux = r3.Cross(accel, magnet)
hfe = r3.Add(hfe, r3.Cross(r3.Unit(aux), halfWest))
ENDCALC:
if f.gain == 0 {
f.rampedGain = 0
}
feedbackGain := f.gain
if f.rampedGain > f.gain {
f.rampedGain -= (initialGain - f.gain) * samplePeriod / initializationPeriod
}
halfGyro := r3.Scale(0.5, gyro)
// apply feedback to gyro
halfGyro = r3.Add(halfGyro, r3.Scale(feedbackGain, hfe))
f.attitude = quat.Add(f.attitude, mulQuatVec(f.attitude, r3.Scale(samplePeriod, halfGyro)))
// Normalize quaternion
f.attitude = NormalizeQuaternion(f.attitude)
// Calculate linear acceleration
gravity := r3.Vec{
X: 2.0 * (q.Imag*q.Kmag - q.Real*q.Jmag),
Y: 2.0 * (q.Real*q.Imag + q.Jmag*q.Kmag),
Z: 2.0 * (q.Real*q.Real - 0.5 + q.Kmag*q.Kmag),
}
f.acceleration = r3.Sub(accel, gravity)
// no magnetometer correction discards change in Yaw.
if f.ahrs == nil {
f.setYaw(0)
}
}
func (f *XioAHRS) setYaw(yaw float64) {
q := f.GetQuaternion()
// Calculate inverse yaw
iyaw := math.Atan2(q.Imag*q.Jmag+q.Real*q.Kmag, q.Real*q.Real-.5+q.Imag*q.Imag) // Euler angle of conjugate
//half inverse yaw minus offset?
hiymo := 0.5 * (iyaw - yaw)
iyawQuat := quat.Number{
Real: math.Cos(hiymo),
Kmag: -math.Sin(hiymo),
}
f.attitude = quat.Mul(iyawQuat, f.attitude)
}
func scaledVecFromInt(scale float64, x, y, z int32) (result r3.Vec) {
result.X = scale * float64(x)
result.Y = scale * float64(y)
result.Z = scale * float64(z)
return result
}
func (f *XioAHRS) GetQuaternion() quat.Number {
return f.attitude
}
// func (f *FusionAHRS) GetEulerAngles() r3.Vec {
// return quatToEuler(f.attitude)
// }
func (f *XioAHRS) GetLinearAcceleration() r3.Vec {
return f.acceleration
}