Game Development Community

Accelerometer/Gyro calibration

by Hitesh Patel · in iTorque 2D · 06/28/2012 (3:25 pm) · 11 replies

I want to calibrate accelerometer/gyro such that the user doesnt have to hold the device flat. I want to the game to take in to account the start angle and calibrate accelrometer accordingly.

Gyro is not that much of an issue. The only issue with the gyro is when the game is paused and if the device angle is changed I want the device to re-calibrate accordingly.

#1
06/28/2012 (10:56 pm)
Use a global offset on the Y axis. 45 degrees will be +0.5 Y, that's a good place to start. If you want to allow the user to customize the offset just detect the current angle when they unpause the game and use that as your new offset.
#2
06/29/2012 (6:08 am)
Thanks for the quick response. I think i tried something similar but as my game is in landscape mode if the starting angle is greater than 70 degress than the movement of the object is quite restricted.
#3
06/29/2012 (12:43 pm)
@Hitesh - If you are willing to jump into source, there is an easy way to reset the attitude reference. In engine/source/platformiphone/iOSMotionManager.mm, there is a reset function:

- (bool)resetDeviceMotionReference
{
    if(motionManager.deviceMotionAvailable)
    {
        referenceAttitude = [motionManager.deviceMotion.attitude retain];
    }
    else
    {
        Con::errorf("Device Motion not supported on this device (check OS)");
        return false;
    }
}

You can wrap that up into a ConsoleFunction:
ConsoleFunction(resetDeviceMotion, void, 1, 1, "() Reset Device Motion tracking reference")
{
    if(gMotionManager != NULL)
        [gMotionManager resetDeviceMotionReference];
    else
    {
        Con::warnf("Motion Manager was not initialized. Initializing now");
        gMotionManager = [[iOSMotionManager alloc] init];
        [gMotionManager resetDeviceMotionReference];
    }
}
#4
06/30/2012 (5:55 am)
Also, if you need your game to work at angles near 90 degrees in landscape mode (perhaps you're expecting players to play while lying down?) then you can just use the Z axis. Where Y maxes out to 1 at 90 degrees and therefore can't distinguish between tilts away or towards the user, Z is at 0 and is more useful.
#5
06/30/2012 (9:21 am)
@Micheal - thanks for that I will try that out
#6
07/01/2012 (4:23 am)
@Michael - I tried your solution and works perfect for the gyroscope. But I still need to solve the issue with accelrometer. At the moment I have to have the device flat for the accelrometer to work properly (my game is in landscape mode). I have seen other games for e.g. lane splitter which works with the accelerometer even if the device is tilted 90 degrees(this is a potrait game but I tried playing it with teh device turned to landscape and titlted almost 90 degrees and still picked up the motion perfectly).

Do you have any suggestions on this?
#7
07/01/2012 (6:37 am)
Hitesh - I made this game using only accelerometer inputs and it worked fine in landscape mode at all angles.

www.youtube.com/watch?v=CWzPlZVYMho

Why exactly do you need to have the device flat for the accelerometer to work?
#8
07/02/2012 (1:14 am)
@Conor - My object moves up/down on the y axis in landscape mode and therefore at the moment with accelerometer I can only play the game with device being flat. With the gyro its fine. All I want is for the device to take in to consideration the starting angle and then use the accelerometer movements accordingly.
#9
07/02/2012 (3:24 am)
It's just as I said above - add 0.5 (or subtract 0.5 depending on which way round you're holding the device) to your Y values and you've moved the 'untilted' position to 45 degrees. Now you don't have to hold it flat.
#10
07/14/2012 (6:11 am)
@Conor - I have tried this and it works for certain angles. But if the device is tiltted more than 45 degress the sensitivity is lost in downwards direction. If I tilt upwards then the object moves fine. The reason for this is that the max Y value is +1 or -1 therefore just offseting with the original Y value will cause this issue.

I read a few articles on the web where people were talking about taking the cotangent of Y and Z value to ensure the accelerometer works fine in any angle but I am not sure what to do exactly for this.
#11
08/07/2012 (7:27 am)
Has anyone got any suggestions for this?????