The python code for BerryIMU now includes tilt compensation for the compass heading.
Git repository here
The code can be pulled down to your Raspberry Pi with;
Without tilt compensation the heading (magnetometer reading) will be skewed when the IMU is tilted.
The below graph below shows a magnetometer (or compass)being held at 200 degrees and being tilted in various directions. The blue line is the raw heading, the orange line is the heading after applying tilt compensation. As you can see, without tilt compensation the heading will change if the compass is tilted.
More information about tilt compensation can be found in this post