Skip to content

Commit b376d05

Browse files
authored
Merge pull request #226 from pkendall64/head-tracker-fixes
Head tracker fixes
2 parents b309da3 + bbb3643 commit b376d05

8 files changed

Lines changed: 63 additions & 27 deletions

File tree

lib/HeadTracker/ICMSeries.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ bool ICMSeries::initialize() {
3333

3434
writeRegister(0x21, 9); // 16G, 100Hz
3535
writeRegister(0x20, 9); // 2000dps, 100Hz
36+
writeRegister(0x23, 0x05); // GYRO_CONFIG1, 34Hz UI LPF
37+
writeRegister(0x24, 0x45); // ACCEL_CONFIG1, 34Hz UI LPF
3638
writeRegister(0x1F, 0x0F); // Low noise mode for Accel/Gyro
3739

40+
deltaTime = 0.01f;
41+
3842
gyroRange = 2000.0;
3943
gRes = GRES;
4044

lib/HeadTracker/IMUBase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class IMUBase {
1919

2020
const float *getCalibration();
2121

22-
int getSampleRate() const { return sampleRate; }
22+
float getDeltaTime() const { return deltaTime; }
2323
float getGyroRange() const { return gyroRange; }
2424

2525
protected:
2626
int address;
27-
int sampleRate = 0;
27+
float deltaTime = 0.0f;
2828
float gyroRange;
2929
float gRes = 0;
3030

lib/HeadTracker/MPU6050.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#define WHO_AM_I 0x75
55
#define ACCEL_X_H 0x3B
66

7+
#define MPU6050_DELTA_TIME 0.01f
8+
79
#define ARES (16.0 / 32768)
810
#define GRES (2000.0 / 32768)
911

@@ -16,12 +18,14 @@ bool MPU6050::initialize() {
1618
writeRegister(0x6B, 0x01); // PWR_MGMT_1, use X axis gyro as clock reference
1719
writeRegister(0x1B, 0x18); // GYRO_CONFIG, 2000dps
1820
writeRegister(0x1C, 0x18); // ACCEL_CONFIG, +-16g
19-
writeRegister(0x1A, 0x01); // CONFIG, 184/188Hz DLPF
21+
writeRegister(0x1A, 0x03); // CONFIG, 42Hz gyro / 44Hz accel DLPF
2022
writeRegister(0x19, 0x09); // SMPRT_DIV, 1kHz / (1 + 9) = 100Hz
2123
writeRegister(0x37, 0x02); // INT_PIN_CFG, I2C_BYPASS_EN
2224
writeRegister(0x38, 0x01); // INT_ENABLE, DATA_RDY_EN
2325
writeRegister(0x23, 0x00); // FIFO_EN, disabled
2426

27+
deltaTime = MPU6050_DELTA_TIME;
28+
2529
gyroRange = 2000.0;
2630
gRes = GRES;
2731

@@ -31,13 +35,13 @@ bool MPU6050::initialize() {
3135
bool MPU6050::getDataFromRegisters(FusionVector &accel, FusionVector &gyro) {
3236
uint8_t values[14];
3337

34-
// Read Accel, temp & gyro data (ignore the temp)
38+
// Read accel, temp, and gyro data. Temperature is ignored.
3539
readBuffer(ACCEL_X_H, values, 14);
36-
accel.axis.x = (int16_t)((values[0] << 8) | values[1]) * ARES;
37-
accel.axis.y = (int16_t)((values[2] << 8) | values[3]) * ARES;
38-
accel.axis.z = (int16_t)((values[4] << 8) | values[5]) * ARES;
39-
gyro.axis.x = (int16_t)((values[8] << 8) | values[9]) * GRES;
40-
gyro.axis.y = (int16_t)((values[10] << 8) | values[11]) * GRES;
41-
gyro.axis.z = (int16_t)((values[12] << 8) | values[13]) * GRES;
40+
accel.axis.x = (int16_t) ((values[0] << 8) | values[1]) * ARES;
41+
accel.axis.y = (int16_t) ((values[2] << 8) | values[3]) * ARES;
42+
accel.axis.z = (int16_t) ((values[4] << 8) | values[5]) * ARES;
43+
gyro.axis.x = (int16_t) ((values[8] << 8) | values[9]) * GRES;
44+
gyro.axis.y = (int16_t) ((values[10] << 8) | values[11]) * GRES;
45+
gyro.axis.z = (int16_t) ((values[12] << 8) | values[13]) * GRES;
4246
return true;
4347
}

lib/HeadTracker/MPU6050.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ class MPU6050 : public IMUBase {
99

1010
protected:
1111
bool getDataFromRegisters(FusionVector &accel, FusionVector &gyro);
12-
};
12+
};

lib/HeadTracker/QMI8658C.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#define REVISION_ID 0x01
88
#define ACCEL_X_L 0x35
99

10+
#define QMI8658_DELTA_TIME (1.0f / 112.1f)
11+
1012
#define ARES (16.0 / 32768)
1113
#define GRES (1024.0 / 32768)
1214

@@ -54,12 +56,12 @@ bool QMI8658C::initialize() {
5456
return false;
5557
}
5658

57-
writeRegister(0x03, 0b00110011); // 16G, 896.8 ODR
58-
writeRegister(0x06, 0b00000111); // both LPF enabled @ 13.37% ODR
59-
writeRegister(0x03, 0b10110011); // 16G, 896.8 ODR + self test
60-
writeRegister(0x04, 0b01100011); // 1024dps, 896.8 ODR
61-
writeRegister(0x06, 0b01110111); // both LPF enabled @ 13.37% ODR
62-
writeRegister(0x04, 0b11100011); // 1024dps, 896.8 ODR + self test
59+
writeRegister(0x03, 0b00110110); // 16G, 112.1Hz ODR
60+
writeRegister(0x06, 0b00000110); // both LPF enabled @ 27.5% ODR
61+
writeRegister(0x03, 0b10110110); // 16G, 112.1Hz ODR + self test
62+
writeRegister(0x04, 0b01100110); // 1024dps, 112.1Hz ODR
63+
writeRegister(0x06, 0b01110110); // both LPF enabled @ 27.5% ODR
64+
writeRegister(0x04, 0b11100110); // 1024dps, 112.1Hz ODR + self test
6365
writeRegister(0x08, 0b10000011); // SyncSample, G+A enabled
6466

6567
// disable AHB clock gating
@@ -73,6 +75,7 @@ bool QMI8658C::initialize() {
7375
return false;
7476
}
7577

78+
deltaTime = QMI8658_DELTA_TIME;
7679
gyroRange = 1024.0;
7780
gRes = GRES;
7881

lib/HeadTracker/QMI8658C.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ class QMI8658C : public IMUBase {
1111

1212
private:
1313
int writeCommand(uint8_t cmd);
14-
};
14+
};

lib/HeadTracker/devHeadTracker.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,23 @@ static void initialize()
5858
}
5959
}
6060
imu->setInterruptHandler(PIN_INT);
61+
if (imu->getDeltaTime() <= 0.0f) {
62+
delete imu;
63+
imu = nullptr;
64+
ht_state = STATE_ERROR;
65+
return;
66+
}
6167

6268
FusionAhrsInitialise(&ahrs);
6369
// Set AHRS algorithm settings
70+
const unsigned int recoveryTriggerPeriod = (unsigned int) ((5.0f / imu->getDeltaTime()) + 0.5f);
6471
const FusionAhrsSettings settings = {
6572
.convention = FusionConventionNwu,
6673
.gain = 0.5f,
6774
.gyroscopeRange = imu->getGyroRange(), /* replace this with actual gyroscope range in degrees/s */
6875
.accelerationRejection = 10.0f,
6976
.magneticRejection = 10.0f,
70-
.recoveryTriggerPeriod = 5U * imu->getSampleRate(), /* 5 seconds */
77+
.recoveryTriggerPeriod = recoveryTriggerPeriod,
7178
};
7279
FusionAhrsSetSettings(&ahrs, &settings);
7380
DBGLN("starting head tracker");
@@ -138,21 +145,17 @@ static int timeout()
138145
{
139146
FusionVector a;
140147
FusionVector g;
148+
const float deltaTime = imu->getDeltaTime();
141149

142-
if (!imu->readIMUData(a, g))
150+
if (!imu->readIMUData(a, g)) {
143151
return DURATION_IMMEDIATELY;
152+
}
144153

145154
switch (ht_state) {
146155
case STATE_RUNNING: {
147156
rotate(a.array, orientation);
148157
rotate(g.array, orientation);
149158

150-
// Calculate delta time (in seconds) to account for gyroscope sample clock error
151-
const clock_t timestamp = micros();
152-
static clock_t previousTimestamp;
153-
const float deltaTime = (float) (timestamp - previousTimestamp) / (float) 1000000;
154-
previousTimestamp = timestamp;
155-
156159
FusionAhrsUpdate(&ahrs, g, a, FUSION_VECTOR_ZERO, deltaTime);
157160

158161
euler = FusionQuaternionToEuler(FusionAhrsGetQuaternion(&ahrs));
@@ -171,6 +174,7 @@ static int timeout()
171174
break;
172175
}
173176
}
177+
174178
return DURATION_IMMEDIATELY;
175179
}
176180

@@ -265,4 +269,4 @@ void getEuler(float *yaw, float *pitch, float *roll)
265269
*pitch = fmap(ptrChannelData[2], CRSF_CHANNEL_VALUE_1000, CRSF_CHANNEL_VALUE_2000, -180.0, 180.0);
266270
*roll = fmap(ptrChannelData[1], CRSF_CHANNEL_VALUE_1000, CRSF_CHANNEL_VALUE_2000, -180.0, 180.0);
267271
}
268-
#endif
272+
#endif

targets/debug.ini

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ extends = env:DEBUG_TIMER_ESP32_Backpack_via_UART
100100
# Head Tracker VRX backpack targets
101101
# ********************************
102102

103+
[env:DEBUG_ESP_HT_Backpack_via_UART]
104+
extends = env_common_esp8285, rapidfire_vrx_backpack_common
105+
upload_resetmethod = nodemcu
106+
build_flags =
107+
${env_common_esp8285.build_flags}
108+
${rapidfire_vrx_backpack_common.build_flags}
109+
-D DEBUG_LOG
110+
-D HAS_HEADTRACKING
111+
-D PIN_BUTTON=0
112+
-D PIN_LED=16
113+
-D PIN_MOSI=13
114+
-D PIN_CLK=14
115+
-D PIN_CS=15
116+
-D PIN_SDA=2
117+
-D PIN_SCL=4
118+
-D PIN_INT=9
119+
reset_method=nodemcu
120+
121+
[env:DEBUG_ESP_HT_Backpack_via_WIFI]
122+
extends = env:DEBUG_ESP_HT_Backpack_via_UART
123+
103124
[env:DEBUG_C3_HT_Backpack_via_UART]
104125
extends = env_common_esp32c3, rapidfire_vrx_backpack_common
105126
build_flags =

0 commit comments

Comments
 (0)