0
\$\begingroup\$

I am trying to implement a complementary filter in my system but I do not quite understand what "dt" means and how should I find out the "dt" in the complementary filter equation.

I found the complementary filter equation here.

This is the equation:

$$angle = 0.98 * (angle + gyrData * dt) + 0.02 * (accData)$$

This is the code I am using now:

while n!= 10:

data = bus.read_i2c_block_data(0x68, 0x3B, 6)

xAccl = data[0] * 256 + data[1]
if xAccl > 32767 :
        xAccl -= 65536

yAccl = data[2] * 256 + data[3]
if yAccl > 32767 :
        yAccl -= 65536

zAccl = data[4] * 256 + data[5]
if zAccl > 32767 :
        zAccl -= 65536

data = bus.read_i2c_block_data(0x68, 0x43, 6)

xGyro = data[0] * 256 + data[1]
if xGyro > 32767 :
        xGyro -= 65536

yGyro = data[2] * 256 + data[3]
if yGyro > 32767 :
        yGyro -= 65536

zGyro = data[4] * 256 + data[5]
if zGyro > 32767 :
        zGyro -= 65536

totZdata += zAccl
totXdata += xAccl
totYdata += yAccl
totGyroXdata += xGyro
totGyroYdata += yGyro
totGyroZdata += zGyro
zAcclG = (zAccl - AcclzOff) / Accl_scale
xAcclG = (xAccl - AcclxOff) / Accl_scale
yAcclG = (yAccl - AcclyOff) / Accl_scale
xGyroDPS = (xGyro - GyroxOff) / Gyro_scale
yGyroDPS = (yGyro - GyroyOff) / Gyro_scale
zGyroDPS = (zGyro - GyrozOff) / Gyro_scale    


print ("Acceleration in X-Axis : %.2f G" %xAcclG)
print ("Acceleration in Y-Axis : %.2f G" %yAcclG)
print ("Acceleration in Z-Axis : %.2f G" %zAcclG)
print (" X rotation : %f" % (get_x_rotation(xAcclG, yAcclG, zAcclG)))
print (" Y rotation : %f" % (get_y_rotation(xAcclG, yAcclG, zAcclG)))
print ("X-Axis of Rotation : %.2f" %xGyroDPS)
print ("Y-Axis of Rotation : %.2f" %yGyroDPS)
print ("Z-Axis of Rotation : %.2f" %zGyroDPS)
n += 1

How can I determine the "dt" from the cmplementary filter equation?

\$\endgroup\$
  • 2
    \$\begingroup\$ #define dt 0.01 // 10 ms sample rate! \$\endgroup\$ – Sunnyskyguy EE75 May 5 at 15:36
  • \$\begingroup\$ I do not understand why the author get to set the dt as 0.1 . \$\endgroup\$ – ckkkk May 5 at 15:40
  • \$\begingroup\$ where? ......... \$\endgroup\$ – Sunnyskyguy EE75 May 5 at 16:04
  • \$\begingroup\$ dt is just the time between two calls of the routine you posted. There should be a TIMER or alike in your code doing so at a rate of your choice. \$\endgroup\$ – carloc May 5 at 16:17
  • \$\begingroup\$ Wait, now i know what dt means. But this i do not quite understand. "There should be a TIMER or alike in your code doing so at a rate of your choice" \$\endgroup\$ – ckkkk May 6 at 11:44
1
\$\begingroup\$

\$dt\$ in that expression is the time step. So if you're sampling at 100Hz, \$dt = 10\mathrm{ms}\$, if you're sampling at 500Hz, \$dt = 2\mathrm{ms}\$, etc.

\$\endgroup\$
  • \$\begingroup\$ But, how can i know about the sampling rate? \$\endgroup\$ – ckkkk May 5 at 15:40
  • \$\begingroup\$ @ckkkk Read the datasheet? \$\endgroup\$ – Long Pham May 5 at 15:47
  • \$\begingroup\$ I meant how do i know if the sensor actually really did output that much of data? \$\endgroup\$ – ckkkk May 6 at 11:39
  • \$\begingroup\$ That's entirely a separate problem, and way too complicated to answer as a comment in something unrelated. If you're concerned, ask it separately. The bottom line, though, is that you check to make sure. \$\endgroup\$ – TimWescott May 6 at 15:14
  • \$\begingroup\$ Yea, by default its 100Hz so i am good, so i should just follow the author and set the dt to 0.01? \$\endgroup\$ – ckkkk May 7 at 16:39
0
\$\begingroup\$

Its the period (in ms) of your filter routine.

\$\endgroup\$
  • \$\begingroup\$ Means how many sensor data i can get? \$\endgroup\$ – ckkkk May 5 at 15:40
  • \$\begingroup\$ No, it means a time difference between two last new values. In your example, the time between two readings from sensor. \$\endgroup\$ – Michal Podmanický May 5 at 15:52
  • \$\begingroup\$ Oh its like how many ms required for 1 data reading \$\endgroup\$ – ckkkk May 6 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.