-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #817 from cyoung/icm20948
ICM-20948 support.
- Loading branch information
Showing
4 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
Submodule goflying
updated
6 files
+212 −0 | icm20948/dmp_constants.go | |
+924 −0 | icm20948/icm20948.go | |
+88 −0 | icm20948/test/read_icm20948.go | |
+17 −19 | mpu9250/dmp_constants.go | |
+2 −2 | mpu9250/mpu9250.go | |
+4 −1 | mpu9250/test/read_mpu9250.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Package sensors provides a stratux interface to sensors used for AHRS calculations. | ||
package sensors | ||
|
||
import ( | ||
"../goflying/icm20948" | ||
"github.com/kidoman/embd" | ||
) | ||
|
||
const ( | ||
gyroRange = 250 // gyroRange is the default range to use for the Gyro. | ||
accelRange = 4 // accelRange is the default range to use for the Accel. | ||
updateFreq = 50 // updateFreq is the rate at which to update the sensor values. | ||
) | ||
|
||
// ICM20948 represents an InvenSense ICM-20948 attached to the I2C bus and satisfies | ||
// the IMUReader interface. | ||
type ICM20948 struct { | ||
mpu *icm20948.ICM20948 | ||
} | ||
|
||
// NewICM20948 returns an instance of the ICM-20948 IMUReader, connected to an | ||
// ICM-20948 attached on the I2C bus with either valid address. | ||
func NewICM20948(i2cbus *embd.I2CBus) (*ICM20948, error) { | ||
var ( | ||
m ICM20948 | ||
mpu *icm20948.ICM20948 | ||
err error | ||
) | ||
|
||
mpu, err = icm20948.NewICM20948(i2cbus, gyroRange, accelRange, updateFreq, true, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Set Gyro (Accel) LPFs to 25 Hz to filter out prop/glareshield vibrations above 1200 (1260) RPM | ||
mpu.SetGyroLPF(25) | ||
mpu.SetAccelLPF(25) | ||
|
||
m.mpu = mpu | ||
return &m, nil | ||
} | ||
|
||
// Read returns the average (since last reading) time, Gyro X-Y-Z, Accel X-Y-Z, Mag X-Y-Z, | ||
// error reading Gyro/Accel, and error reading Mag. | ||
func (m *ICM20948) Read() (T int64, G1, G2, G3, A1, A2, A3, M1, M2, M3 float64, GAError, MAGError error) { | ||
var ( | ||
data *icm20948.MPUData | ||
i int8 | ||
) | ||
data = new(icm20948.MPUData) | ||
|
||
for data.N == 0 && i < 5 { | ||
data = <-m.mpu.CAvg | ||
T = data.T.UnixNano() | ||
G1 = data.G1 | ||
G2 = data.G2 | ||
G3 = data.G3 | ||
A1 = data.A1 | ||
A2 = data.A2 | ||
A3 = data.A3 | ||
M1 = data.M1 | ||
M2 = data.M2 | ||
M3 = data.M3 | ||
GAError = data.GAError | ||
MAGError = data.MagError | ||
i++ | ||
} | ||
return | ||
} | ||
|
||
// ReadOne returns the most recent time, Gyro X-Y-Z, Accel X-Y-Z, Mag X-Y-Z, | ||
// error reading Gyro/Accel, and error reading Mag. | ||
func (m *ICM20948) ReadOne() (T int64, G1, G2, G3, A1, A2, A3, M1, M2, M3 float64, GAError, MAGError error) { | ||
var ( | ||
data *icm20948.MPUData | ||
) | ||
data = new(icm20948.MPUData) | ||
|
||
data = <-m.mpu.C | ||
T = data.T.UnixNano() | ||
G1 = data.G1 | ||
G2 = data.G2 | ||
G3 = data.G3 | ||
A1 = data.A1 | ||
A2 = data.A2 | ||
A3 = data.A3 | ||
M1 = data.M1 | ||
M2 = data.M2 | ||
M3 = data.M3 | ||
GAError = data.GAError | ||
MAGError = data.MagError | ||
return | ||
} | ||
|
||
// Close stops reading the MPU. | ||
func (m *ICM20948) Close() { | ||
m.mpu.CloseMPU() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters