万歩計を作ってみた。

#import <UIKit/UIKit.h>
#import <math.h>

#define kAccelerometerFrequency            25 //Hz
#define kFilteringFactor                0.1
#define kMinEraseInterval                0.5
#define kEraseAccelerationThreshold        0.5

@interface PassometerController : UIViewController <UIAccelerometerDelegate>
{
    int value;
    UIAccelerationValue    myAccelerometer[3];
    CFTimeInterval        lastTime;
}
@end

@implementation PassometerController
- (id)init
{
    if (!(self = [super init])) return self;
    self.title = @"万歩計";
    return self;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    UIAccelerationValue                length,
    x,
    y,
    z;
    //Use a basic high-pass filter to remove the influence of the gravity
    myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor);
    myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor);
    myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor);
    // Compute values for the three axes of the acceleromater
    x = acceleration.x - myAccelerometer[0];
    y = acceleration.y - myAccelerometer[1];
    z = acceleration.z - myAccelerometer[2];
    //Compute the intensity of the current acceleration
    length = sqrt(x * x + y * y + z * z);
    // If above a given threshold, play the erase sounds and erase the drawing view
    if((length >= kEraseAccelerationThreshold) && (CFAbsoluteTimeGetCurrent() > lastTime + kMinEraseInterval)) {
        [(UITextView *)self.view setText:[NSString stringWithFormat:@"\n%d", ++value]];
        lastTime = CFAbsoluteTimeGetCurrent();
    }
}

- (void)loadView
{
    // Set up the text view to show the current value
    UITextView *contentView = [[UITextView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    contentView.editable = NO;
    contentView.textAlignment = UITextAlignmentCenter;
    contentView.font = [UIFont fontWithName:@"American Typewriter" size:120];
    self.view = contentView;
    [contentView release];
    // Initialize at 50
    [(UITextView *)self.view setText:@"\n0"];
    value = 0;   

    // Activate the accelerometer
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
}

-(void) dealloc
{
    [super dealloc];
}
@end

@interface AppDelegate : NSObject <UIApplicationDelegate>
@end

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {   
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[PassometerController alloc] init]];
    [window addSubview:nav.view];
    [window makeKeyAndVisible];
}
@end

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
    [pool release];
    return retVal;
}

 

ピクチャ 10

■ポイント

1.プロトコルに UIAccelerometerDelegate を指定。

 

2.メソッド

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

 

3.[[UIAccelerometer sharedAccelerometer] setDelegate:self];

でセンサーをONにする。

 

さて、

一応、万歩計のつもり。

定期的に動いた距離をの差分を取得してカウントへ反映。

カウントの判定が微妙だ。。。

歩いたときの一歩、走ったときの一歩を確実に得るにはまだまだ、試行錯誤が必要。

『ピタゴラスの定理』を久しぶりに使った。

 

加速度センサーの利用方法がわかったところで今回はおしまい。

0 コメント

Recommended Money Makers

  • Chitika eMiniMalls
  • WidgetBucks
  • Text Link Ads
  • AuctionAds
  • Amazon Associates