Need help with software to calculate orientation for ITG-3200 Gyro

I’ve spent two days and around 12+ hours writing and rewriting code for this infernal gyroscope and I’m still not happy with what I have. I want to use a gyroscope to calculate orientation based off the measured angular velocities.

Im building a multirotor. All the code is ready to be tested, and the hardware purchased. I just need to get this stupid gyroscope to give me good values, then i’ll be ready to begin flight testing.

Things I know, that you dont need to tell me:

  • “orientation is usually calculated with an accelerometer too (accel + gyro = IMU).”
    I know. But with rotation matrices, high precision gyroscopes and 1000 samples a second, you should be able to get a pretty dang good reading from just a gyro. Then I can combine that with some controller tuning to get it hovering at least.

  • “Gyroscopes are temperature sensitive”
    I’m aware of this too, and if youve noticed I added a function to read the temperature value. Unfortunately I havent been able to map the offset drift to the temperature, or find information online for the ITG-3200’s tendencies. Now if you tell me how to make the adjustments THEN youre really being helpful.

  • I have checked http://www.arduino-projects4u.com/itg3200/ and while their library/code is alot neater than mine, I actually feel like my program’s output is more filtered than theirs.

My code:

  • Contains a two moving average filters (recent values are stored into constantly updated arrays to be averaged, and the length of those arrays are easily adjustable. Mine are set to the last 5 values).

  • Calculates the variance of the data in the moving average filter arrays and sets it as a dynamic tolerance/sensitivity, but deactivates variance calculation when the gyro is in motion.

  • Contains (un-debugged) code for 3x3 rotation matrix conversions to actually calculate 3D rotations (Though it is currently commented out) and a simple linear integration function has been used in the mean time for debugging.

Problems:
Time delay. can be remedied by playing with the timing and length of the Moving Average Filters (MAF) at the cost of sensitivity.
Not measuring sudden rotations well.

I went pretty deep with the rotation matrices and MAFs and variance calculations but I’m not a very good programmer. Most of what I know is self taught and adapted from other people’s code. On top of that, I’m messy in real life too.

So I apologize in advance for the logical mess that is my thoughts expressed into a computer program.

Any helpful help would be greatly appreciated. But if you’re unsure about this yourself please dont screw me over by giving me guesswork/bad advice. I’m confused and aggravated enough already.

The code is attached.
Sources:
adapted from Sparkfun ITG-3200 sample code for setup and read commands
adapted from www.euclideanspace.com for rotation matices

Gyroscope_Right_Handed.zip (6.14 KB)

you should be able to get a pretty dang good reading from just a gyro. Then I can combine that with some controller tuning to get it hovering at least.

. No, you can't. Rate gyros drift rather rapidly and that drift must be corrected by some other source of directional information.

I dont mean to doubt your knowledge, but if you download and run the program I have right there, it does not drift.

I used the moving average filter to calculate the variance of the incoming data, which i set as a dynamic tolerance/sensitivity.

I wrote software to sacrifice sensitivity to eliminate drift. But I never needed ridiculously good sensitivity anyway. And the program is sensitive enough to update the orientation if you brush it with a finger.

Drift is not the issue.

Drift is the basic problem. The measures you have taken to eliminate it, by reducing the sensitivity and the response time, are introducing new difficulties.

Edit: While it is usually possible to correct for bias (leading to drift upon integration) while the gyro is sitting still on the table at room temperature, see this technical reference for the other problems that can't be so easily corrected: http://www.analog.com/static/imported-files/tech_articles/MS-2158.pdf To quote from that reference

gyros in the real world exhibit errors due to multiple sources that prevent users from exploiting the high bias stability being touted in the data sheet. Indeed, about the only place you’ll get that level of performance is on the lab bench.

To summarize, these difficulties explain why people don't try to do what you are doing, and use additional sensors to provide attitude information.

Ahh i understand. I spose i need to buy another sensor then. Thanks for your advice, you probably saved me alot of time.

You can't beat the sale price on this accelerometer/magnetometer combo from Pololu: http://www.pololu.com/product/2124

I use it and am quite happy, plus they have great product support.

Edit: I spent quite a bit of time trying to figure out how to beat the noise in accelerometers (to no avail), much like you have done with gyros, and now chalk it up to a learning experience. Consumer grade sensors just aren't good enough yet to do what most of us really want.

That looks pretty great indeed, but i live in Indonesia, so my choice in sensors are significantly limited hahaha. I'll see how long/how much it will cost to ship that over and compare to the ones i can get here.

Quite funny how that is, i guess when people first pick them up they dont realize they're sensor is not all popowerful cos they get blown away by the stats like 1000samples per second built in low pass filtering etc.

Thanks again