# What is the “dt” in the complementary filter equation and how do I find out the “dt”?

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?

• #define dt 0.01 // 10 ms sample rate! – Sunnyskyguy EE75 May 5 at 15:36
• I do not understand why the author get to set the dt as 0.1 . – ckkkk May 5 at 15:40
• where? ......... – Sunnyskyguy EE75 May 5 at 16:04
• 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. – carloc May 5 at 16:17
• 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" – ckkkk May 6 at 11:44

## 2 Answers

$$\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.

• But, how can i know about the sampling rate? – ckkkk May 5 at 15:40
• @ckkkk Read the datasheet? – Long Pham May 5 at 15:47
• I meant how do i know if the sensor actually really did output that much of data? – ckkkk May 6 at 11:39
• 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. – TimWescott May 6 at 15:14
• Yea, by default its 100Hz so i am good, so i should just follow the author and set the dt to 0.01? – ckkkk May 7 at 16:39

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

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