@@ -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
0 commit comments