IOS calculates g-force

Simply, I need to make a shortcut in my project that says the current force g from the accelerometer. I found the code, but I don’t know where to put it in my code. I guess I need to put it in an expression of emptiness, but I'm not sure. Please point me in the right direction.

sx = acceleration.x * kFilteringFactor + sx * (1.0 - kFilteringFactor);
sy = acceleration.y * kFilteringFactor + sy * (1.0 - kFilteringFactor);
sz = acceleration.z * kFilteringFactor + sz * (1.0 - kFilteringFactor);

float aValue = sqrt(sx*sx+sy*sy+sz*sz);

[gforcelabel setText:[NSString stringWithFormat:@"%.2f",aValue]];
+4
source share
3 answers

I knew exactly how to do this ...

Viewcontroller.h

 IBOutlet UILabel *currentg;
IBOutlet UILabel *avgAccelLabel;



float avgAccel;
float currentAccel;
int accelCount;

float accelX;
float accelY;
float accelZ;

UIAccelerometer *accelerometer;

Viewcontroller.m

//Use a basic low-pass filter to only keep the gravity in the accelerometer values
accelX = acceleration.x; //* kFilteringFactor + accelX * (1.0 - kFilteringFactor);
accelY = acceleration.y; //* kFilteringFactor + accelY * (1.0 - kFilteringFactor);
accelZ = acceleration.z; //* kFilteringFactor + accelZ * (1.0 - kFilteringFactor);

//calculate acceleration resultant
float accel = sqrt((ABS(accelX)*ABS(accelX))+(ABS(accelY)*ABS(accelY))+           (ABS(accelZ)*ABS(accelZ)));

//keep track of average accel
avgAccel = ((avgAccel * accelCount) + accel)/(accelCount + 1);
currentAccel = (accel); 

accelCount++;


//display avg accel
NSString *avgAccelString = [NSString stringWithFormat:@"%.2f%@", avgAccel, @""];
avgAccelLabel.text = avgAccelString;

NSString *currentAccellString = [NSString stringWithFormat:@"%.2f%@",currentAccel, @"" ];
currentg.text = currentAccellString;

viewdidload

 self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = 0.4;
self.accelerometer.delegate = self;
-1
source

, UIAccelerometerDelegate , - iOS 5. , Core Motion, CMMotionManager, iOS 5 . ( ), () .

if (!self.manager) {
    self.manager = [CMMotionManager new];
}

if (self.manager.isAccelerometerActive) {
    [self.manager stopAccelerometerUpdates];
}

NSOperationQueue *queue = [NSOperationQueue new];

[self.manager setAccelerometerUpdateInterval:1.0 / 30.0]; // 30 Updates Per Second
[self.manager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
    NSLog(@"X: %f         Y: %f         Z: %f",accelerometerData.acceleration.x,accelerometerData.acceleration.y,accelerometerData.acceleration.z);
}];

, , , , , . - , ( , ), [NSOperationQueue mainQueue] .

, , , , .

+1

UIAccelerometer iOS 5.0. Core Motion Library.

If we assume that you are targeting iOS 5.0, the code you specified above will be in the view controller that implements the UIAccelerometerDelegate Protocol.To be specific, you must define the following protocol method and move your code inside the body of this method:

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

Source: https://habr.com/ru/post/1537167/


All Articles