Power_Broker:
Oh I see now. I was confused with your last post. Either way, gyro drift is probably part of the problem. Might not need a HPF for the gyro, but would be cool to test if you get the chance. Not sure what cutoff frequency you want for the HPF, maybe 1Hz?
The sensors actually do implement filters on their own so you can just change the filtering by changing a registry value through I2C and therefore, saving that 'processing time' for the processor to work on other things. So the actual value that you are reading from the gyros is actually pre-processed but if filtering too much, you will lose the 'instantaneous better accuracy' that the gyro offers over the accelerometer, so I'd be careful with that. Now I'm speaking from memory so don't take me too seriously but I think the default value for the LPF is 10Hz.
Power_Broker:
As for the time integration drift, I think it has to do with too large of a sampling time period. 40-50ms is quite a long time IMHO. If you could somehow manage a sample period of no more than 10ms, I think you'd get less drift. The smaller the period the better.
You are likely right there with some limits... if you sample too fast you may start getting bigger noise errors but other than that yeah, the shorter the period, the more accurate the integration. To do that, one should make a software interrupt service routine that periodically sampled the gyro at a constant rate. It can be done but it is more work... Probably worth it and maybe I will implement it later on but right now I just don't have time to spare hahaha!
Power_Broker:
Also, you should try different values of "a" in your complimentary filter to see what gives the best results. Try decreasing the gyro authority from 98% and increase the accelerometer authority above 2%. That'd be my guess.
I already did. There's where I find out that the filter just scaled the drift error but didn't eliminate it. Actually, if you set 'a' to 0.5 (50% weight to each sensor) you are just calculating the arithmetic average of both. A not really elegant to solve the drift issue would be to set some 'if' condition that reset the 'accumulated angle' to 0 when the accelerometer is reading only 1G and the x and y components of acceleration are approximately zero. That should work but I'd prefer to find a more elegant and robust solution. Yeah, I've also heard about the Kalman filter but I don't like implementing 'magic' procedures into my project... like to understand exactly what I'm doing and Kalman filters would take many hours to learn properly.
Power_Broker:
Another point; this piece of code looks rather odd. Can you explain why you are zeroing out your gyro data here:
if (abs(gX)<2)
{
gX=0;
}
if (abs(gY)<2)
{
gY=0;
}
if (abs(gZ)<2)
{
gZ=0;
}
Yup, sure.
This is a deadband. I was just playing with possibilities. This limits the drift greatly.
When the board is almost still, the drift still accumulates because of gyro noise. With these lines I'm forcing readings to be zero if not greater than 2 bits, so the error accumulates when moving but not when still. It's not the best way to deal with it but for the time being, makes the problem smaller.
Power_Broker:
Yeah, the complimentary filter is cool, but I don't have a lot of experience with it. Based on descriptions I've seen, its supposed to combine both a LPF on the accelerometer and HPF on the gyro along with some gains relating to "a" or whatever. I can't find very vigorous or in-depth explanations on the nuts and bolts of it, so not sure what to tell you here. I would venture to guess that you are correct in assuming the error doesn't reset and only scales down.
Well, that's what it does on my implementation but it isn't what it is meant to be. I saw some experimental data from people supposedly implementing complementary filters and they were getting rid of the drift but the offered equations and code just did not translate into the same results, which leads me to think that I'm either missing something or they didn't post the final version of the code.
Power_Broker:
What I would do if all else fails is to get rid of the complimentary filter, keep the LPF for the accelerometer, implement a HPF for the gyro, calculate the angle from both accelerometer and gyroscope and then take a weighted average of the two. Does that make sense?
It makes... but it doesn't... I mean (no offense) that's exactly what the complementary filter is doing now.
Power_Broker:
I doubt it this will save you much processing, but I could be wrong. The reason discrete time filters require a fixed sample period is based on the how you convert a transfer function in the Laplace domain to the discrete time "Z" domain. If you work it out correctly, you will find you will have a constant, delta t, that will be a fixed sampling period. Change the sampling period and it will change the filter's frequency response.
Now that you mention it, it makes sense to me. Since 'a' and time constant are directly related, modifying the time constant (variable cycle time) would require to modify 'a' (which I don't). Still, I assume that the error from doing this is several orders of magnitude smaller than that of the drift so for now I'd just ignore it. But yes, I think you are right on your assertions.
Power_Broker:
Now, since the complimentary filter, I would dare to say, isn't a discrete time filter and more of a weighted average, I don't think you do not need to sample at a fixed rate (wouldn't hurt, though). If you implement a discrete time filter, like the one I posted earlier, you most definitely would need a fixed sample rate or you will get erroneous outputs most of the time.
Can try to test and see
Power_Broker:
Disclaimer: I'm only an undergrad in college myself and am no expert by any stretch. There are others in the forum that might be able to help you out better than I.
Also, if you use any of my techniques (if they work lol), I'd ask that you credit it. Thanks!
Well, you are actually helping which is a lot to me, thank you.
If the filter thing ends up on my code I'll definitely mention it on the report. I will have to do an exhaustive list of all my sources... it's going to be a long one, lol.