2

Am developing an application to calculate the steps of the human while walking, I did this using accelerometer but I found that it's not accurate. So after googling this, I found that using GyroScope sensor is more accurate than the accelerometer. Any help with a control that might do this as I didn't ever develop using GyroScope, Or a document to read more about GyroScope Sensor. Thanks in advance for help.

-(void)addAcceleration:(UIAcceleration*)accel
{
    x = accel.x;
    y = accel.y;
    z = accel.z;
}

-(NSString*)name
{
    return @"You should not see this";
}

@end

#define kAccelerometerMinStep               5.02
#define kAccelerometerNoiseAttenuation      1.0

double Norm(double x, double y, double z)
{
    return sqrt(x * x + y * y + z * z);
}

double Clamp(double v, double min, double max)
{
    if(v > max)
        return max;
    else if(v < min)
        return min;
    else
        return v;
}

// See http://en.wikipedia.org/wiki/Low-pass_filter for details low pass filtering
@implementation LowpassFilter

-(id)initWithSampleRate:(double)rate cutoffFrequency:(double)freq
{
    self = [super init];
    if(self != nil)
    {
        double dt = 1.0 / rate;
        double RC = 1.0 / freq;
        filterConstant = dt / (dt + RC);
    }
    return self;
}

-(void)addAcceleration:(UIAcceleration*)accel
{
    double alpha = filterConstant;

    if(adaptive)
    {
        double d = Clamp(fabs(Norm(x, y, z) - Norm(accel.x, accel.y, accel.z)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
        alpha = (1.0 - d) * filterConstant / kAccelerometerNoiseAttenuation + d * filterConstant;
    }

    x = accel.x * alpha + x * (1.0 - alpha);
    y = accel.y * alpha + y * (1.0 - alpha);
    z = accel.z * alpha + z * (1.0 - alpha);
}

-(NSString*)name
{
    return adaptive ? @"Adaptive Lowpass Filter" : @"Lowpass Filter";
}

@end

// See http://en.wikipedia.org/wiki/High-pass_filter for details on high pass filtering
@implementation HighpassFilter

-(id)initWithSampleRate:(double)rate cutoffFrequency:(double)freq
{
    self = [super init];
    if(self != nil)
    {
        double dt = 1.0 / rate;
        double RC = 1.0 / freq;
        filterConstant = RC / (dt + RC);
    }
    return self;
}

-(void)addAcceleration:(UIAcceleration*)accel
{
    double alpha = filterConstant;

    if(adaptive)
    {
        double d = Clamp(fabs(Norm(x, y, z) - Norm(accel.x, accel.y, accel.z)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
        alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0 - d) * filterConstant;
    }

    x = alpha * (x + accel.x - lastX);
    y = alpha * (y + accel.y - lastY);
    z = alpha * (z + accel.z - lastZ);

    lastX = accel.x;
    lastY = accel.y;
    lastZ = accel.z;
}

-(NSString*)name
{
    return adaptive ? @"Adaptive Highpass Filter" : @"Highpass Filter";
}
Kareem Waheed
  • 429
  • 2
  • 6
  • 13

2 Answers2

3

Use CoreMotion FrameWork.

#import <CoreMotion/CoreMotion.h>

    self.motionManager = [[CMMotionManager alloc] init];


    //Gyroscope
    if([self.motionManager isGyroAvailable])
    {
        /* Start the gyroscope if it is not active already */ 
        if([self.motionManager isGyroActive] == NO)
        {
            /* Update us 2 times a second */
            [self.motionManager setGyroUpdateInterval:1.0f / 2.0f];

            /* Add on a handler block object */

            /* Receive the gyroscope data on this block */
            [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue]
             withHandler:^(CMGyroData *gyroData, NSError *error)
            {
                NSString *x = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.x];
                self.gyro_xaxis.text = x;

                NSString *y = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.y];
                self.gyro_yaxis.text = y;

                NSString *z = [[NSString alloc] initWithFormat:@"%.02f",gyroData.rotationRate.z];
                self.gyro_zaxis.text = z;
            }];
        }
    }
    else
    {
        NSLog(@"Gyroscope not Available!");
    }

Read this: http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/Introduction/Introduction.html

Need to use two classes: CMGyrodata and CMMotionManager

Related to your question: How to count steps using an Accelerometer?

Read that, will be a good starting point...

Hope this helps...

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276
1

On an Android device, I was able to use the accelerometer to count steps without much problem, see my answer here.

Community
  • 1
  • 1
Stochastically
  • 7,616
  • 5
  • 30
  • 58
  • Thanks for help, but I actually use the same method on my code as I edited my question, but it gave inaccurate results – Kareem Waheed May 27 '13 at 00:53
  • 1
    @KareemWaheed the devil is always in the detail! What I did is got a lot of friends to walk for me, and I counted how many steps they took. As they were walking, my Android device was logging all sensor output. I then developed a program in C# (because that's my favourite language) that analysed all the log files, and optimised a methodology for counting steps that I then ported to Android java. – Stochastically May 27 '13 at 08:11
  • Do you think that it's better to try to enhance the accelerometer or should I use the GyroScope sensor :) – Kareem Waheed May 27 '13 at 09:53
  • 1
    I think the best results will come from using all sensors available. However, whatever sensors you end up using, logging a whole load of data and then analyzing to work out how best to count the steps is what I would recommend. – Stochastically May 27 '13 at 17:22