How to get Acceleration Time & Acceleration Magnitude in iPhone - ios

Currently I'm trying to use CMAcceleration to get accelerometer values for x,y& z axis is it possible to get Acceleration Time & Acceleration Magnitude with this x,y & z values.When i checked the developer forum i can't find any delegates / methods to get this two.Guide me to find the values.
Using the following code:
int updatesensorFrequencyInterval = [sensorFrequencySliderTxt.text intValue];
self.motionManager.accelerometerUpdateInterval = updatesensorFrequencyInterval;
self.motionManager.gyroUpdateInterval = updatesensorFrequencyInterval;
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[self outputAccelertionData:accelerometerData.acceleration];
if(error){
NSLog(#"%#", error);
}
}];

The x,y,z values of CMAccelerometerData acceleration struct are magnitudes in m/s^2.
CMAccelerometerData is a subclass of CMLogItem, so it inherits the timestamp property which provides the NSTimeInterval since the phone was booted. This can be used to measure elapsed time between any pair of samples.

Related

Get accelerometer and gyroscope data from apple watch (and not the iphone)?

After seeing this question, I tried to code up a quick program that would save the watches accelerometer and gyroscope data to a file.
#implementation InterfaceController{
NSMutableArray *accData;
bool recording;
}
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
// Configure interface objects here.
self.motionManager = [[CMMotionManager alloc] init];
[self.motionManager setAccelerometerUpdateInterval:.01];
}
- (IBAction)startStopRecording {
if (!recording){//We are starting to record.
recording = YES;
accData = [[NSMutableArray alloc] init];
[self.startRecording setTitle:#"Stop Recording"];
[self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
[accData addObject:[NSString stringWithFormat:#"%f, %f, %f", accelerometerData.acceleration.x, accelerometerData.acceleration.y, accelerometerData.acceleration.z]];
}];
}else{
recording = NO;//we are stopping the recording
[self.motionManager stopAccelerometerUpdates];
[self.startRecording setTitle:#"Start Recording"];
[InterfaceController openParentApplication:#{ #"accData": accData } reply:^(NSDictionary *replyInfo, NSError *error) { //this method saves the array to a csv file.
NSLog(#"Data has been saved.");
}];
}
}
I had plotted this data and for the life of me, no matter how hard I shook the watch, all my plots looked like this:
Until 8 hours later, I started to suspect that I wasn't grabbing the acceleration data from the watch, but rather from the phone (sitting still on the table next to me). I ran some tests and confirmed that this is exactly what is happening.
Which leads me to the original question. How do I pull acceleration/gyro/data from the watch and not from the iPhone?
The problem was that I wasn't running watchOS2. I assumed I was but it's still in beta and I hadn't installed it. The data I was getting was accelerometer data from the phone. Also, currently, you can only get acc data from the watch using watchOS2 and not gyro data.
you can use CoreMotion framework to get activity data.
while I can only get accel data, the gyro often return false.

apple watch CMDeviceMotion is not giving me good readings

Currently, I am just recording a bunch of motion data and saving it to a file. However, when I plot the data, I am having a hard time believing I am getting the right readings. Here is my watch code:
- (IBAction)startStopRecording {
if (!recording){
NSLog(#"starting to record");
recording = YES;
data = [[NSMutableArray alloc] init];
[self.startRecording setTitle:#"Stop Recording"];
if (self.motionManager.deviceMotionAvailable) {
[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
[data addObject:[NSString stringWithFormat:#"%f, %f, %f, %f, %f, %f, %f, %f, %f", motion.attitude.pitch, motion.attitude.roll, motion.attitude.yaw, motion.userAcceleration.x, motion.userAcceleration.y, motion.userAcceleration.z, motion.rotationRate.x, motion.rotationRate.y, motion.rotationRate.z]];
NSLog(#".");
}];
}
}else{
recording = NO;
NSLog(#"stopping recording");
[self.motionManager stopDeviceMotionUpdates];
[self.startRecording setTitle:#"Start Recording"];
[InterfaceController openParentApplication:#{ #"data": data } reply:^(NSDictionary *replyInfo, NSError *error) {
NSLog(#"Data has been saved.");
NSLog(#"replyInfo %#", replyInfo);
}];
}
}
The parent application just writes all the data to a file. I recorded the watch rotating back and forth on all three axes (pitch, then roll, then yaw):
And then when I plotted the data, this is what I got:
The yaw is so noisy that you can't see a signal at all in there. I also have a similar problem when plotting the acceleration after jerking the watch in three different directions. I can see spikes of acceleration, but they don't seem to be direction dependent. Any ideas on how to improve this? Am I missing something? Could I just have a bad sensor in my watch?
The reason is because I wasn't actually pulling data from the watch. It was pulling data from the phone. In order to pull data (currently only acc data is available from the watch) you need to have watchOS2 (currently in beta). Otherwise the watch will just get handed data from the phone.

Detect user arrived to a location and depart from a location pattern using CMMotionManager

I am working on app which requires to detect user arrived to place and depart from a place and time spent in that location.
I am reading CoreMotion Readings but unable to get any clue how to form patterns.
if (_motionManager == nil) {
_motionManager = [[CMMotionManager alloc]init];
}
_motionManager.deviceMotionUpdateInterval = 1/60;
[_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
if (error) {
[_motionManager stopDeviceMotionUpdates];
NSLog(#"Error while reading accelerometer data: %#", error.description);
return;
}
[self getacceleration:motion];
}];
In filterMotionData I am trying to read device acceleration with motion.userAcceleration but not find any good solution.
- (void)getacceleration:(CMMotion*)motion {
CMAcceleration acceleration = motion.userAcceleration;
//Calculate Device acceleration
}
Here If I can calculate device acceleration then on slowing down device acceleration I can say user arrived to a Location and when as device acceleration increased for particular time period means user depart from a location.
I was exploring I came to know Moves app does the same for all version of devices.
I gone through CLVisit API provided by CLLocationManager but it won't give short stay of user in a location and also its notify very late.
Please guide if anyone work on same type of requirement.

Detect if the iPhone movement changes direction?

In my app the iPhone is moved in two cases:
Moved in lineare direction
Moved in a arc direction
How can I detect whether the direction of the movement changes the direction?
You'll have to pull in the CoreMotion framework and start the device accelerometer and/or gyroscope.
CoreMotion Reference
What you'r looking to get is CMAccelerometerData. This is done by instantiating a CMMotionManager object and calling startAccelerometerUpdatesToQueue:withHandler:
Something like this:
CMMotionManager *manager = [[CMMotionManager alloc] init];
manager.accelerometerUpdateInterval = 0.5; // half a second
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[manager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
double x = accelerometerData.acceleration.x;
double y = accelerometerData.acceleration.y;
double z = accelerometerData.acceleration.z;
// post back to main queue to update UI
dispatch_async(dispatch_get_main_queue(), ^{
});
}];
You'll need to use some good old-fashioned geometry to detect arcs vs. lines.

CMMotionManager updateinterval not set

I'm using the CMMotionManager to gather accelerometer data. I am trying to set the update interval to every half second with the following:
[_motionManager setDeviceMotionUpdateInterval:.5];
[_motionManager startAccelerometerUpdatesToQueue:[[NSOperationQueue alloc] init]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelectorOnMainThread:#selector(update:) withObject:accelerometerData waitUntilDone:NO];
});}];
yet I receive updates far more frequently than every half second. Any idea why?
Wasn't setting the update interval for accelerometer itself.
[_motionManager setAccelerometerUpdateInterval:.5];

Resources