iPad 1 Gyroscope: roll,pitch,yaw stay zero - iphone

I'm trying to make a simple app utilizing gyroscope, where a character moves according to the rotation of the iPad 1.
My code is not working, so I tested to see the values of raw,pitch,yaw,
and they actually stay as zero however I move the device.
I'm sure iPad 1 supports CMMotionManager, so I'm not sure what's causing it...
My code is as follows
- (id) init{
if((self=[super init])){
self.isTouchEnabled = YES;
winSize = [[CCDirector sharedDirector] winSize];
[self createRabbitSprite];
self.motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 1.0/60.0;
if(motionManager.isDeviceMotionAvailable){
[motionManager startDeviceMotionUpdates];
}
[self scheduleUpdate];
//[self registerWithTouchDispatcher];
}
return self;
}
-(void)update:(ccTime)delta{
CMDeviceMotion *currentDeviceMotion = motionManager.deviceMotion;
CMAttitude *currentAttitude = currentDeviceMotion.attitude;
if(referenceFrame){
[currentAttitude multiplyByInverseOfAttitude:referenceFrame];
}
float roll = currentAttitude.roll;
float pitch = currentAttitude.pitch;
float yaw = currentAttitude.yaw;
NSLog(#"%.2f and %.2f and %.2f",roll,pitch,yaw);
rabbit.rotation = CC_RADIANS_TO_DEGREES(yaw);
}
Please help me out..
and thanx in advance.
(edit)
Apparently, motionManager.isDeviceMotionAvailable is returning FALSE...
which must mean that iPad 1 doesn't support CoreMotion???
Could it be something with the setting?

The iPad First generation does support CMMotionManager (as it has an accelerometer), but won't return any gyroscopic data - it doesn't have a gyroscope! You'll need to check the gyroAvailable property of a CMMotionManager instance.

Related

Acceleration in a for..... loop

I have a for....loop and the UI then is blocked when in the loop.
Is the accelerometer also blocked?
I have following code in viewDidLoad
- (void)viewDidLoad{
[super viewDidLoad];
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f/60.0f;
}
and further down
- (void)accelerometer:(UIAccelerometer *)acel didAccelerate:(UIAcceleration *)acceleration {
acc=acceleration.x;
}
then an IBAction connected to a button which starts a loop
- (IBAction)doSomething {
for (int n = 1; n<=100;n++) {
// do someting in the loop
NSLog(#"x: %g", acc);
}
}
when logging the accelerometer x value it shows only the first value when the button is pressed and does not update when the loop is running. The same first value is repeating continuously.
Is there a way to log the acceleration when in the loop?
It would seem that UIAccelerometer needs the main thread so is getting blocked by the for...loop. Core Motion also has an accelerometer property in the CMMotionManager object that can report acceleration, and does better backgrounding since it's what Nike+ uses. There are two ways to access it - a pull method where you get the data directly, or a call-back. To be honest, I just tried it and couldn't get the call-back to work (mostly because it requires NSOperationQueue and I don't have much experience with that so I'm probably putting it on the wrong queue), but if you're ok with pulling data, which your approach seems to need, then this works:
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
[motionManager startAccelerometerUpdates];
for (int i = 0; i < 10000; i++)
{
NSLog(#"Acceleration: %f", motionManager.accelerometerData.acceleration.x);
}
I just tried it on a device and the values reported in the for...loop update with the current acceleration.
Source: UIBackgroundModes and UIAccelerometer

Gyro CMAttitude pitch, roll and yaw angle problems

I have a problem getting pitch, roll and yaw angles from CMAttitude class.
First, I did a normal Gyro using 'CMMotionManager' class and atributes x,y,z and worked fine.
Then, I tried to use CMAttitude for "absolute angles", but I doesn't work because It seems that is not updating data. Angles are always 0 (but the isn't errors or warnings)
I have searched a lot in stackoverflow, and used some solutions I find, but I have the same problem.
Here's my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
motionManager = [[CMMotionManager alloc] init];
CMDeviceMotion *deviceMotion = motionManager.deviceMotion;
CMAttitude *attitude = deviceMotion.attitude;
referenceAttitude = attitude;
[motionManager startGyroUpdates];
timer = [NSTimer scheduledTimerWithTimeInterval:1/30.0
target:self
selector:#selector(doGyroUpdate)
userInfo:nil
repeats:YES];
}
-(void)doGyroUpdate {
//cambia el frame de referencia
[motionManager.deviceMotion.attitude multiplyByInverseOfAttitude: referenceAttitude];
double rRotation = motionManager.deviceMotion.attitude.roll*180/M_PI;
double pRotation = motionManager.deviceMotion.attitude.pitch*180/M_PI;
double yRotation = motionManager.deviceMotion.attitude.yaw*180/M_PI;
NSString *myString = [NSString stringWithFormat:#"%f",rRotation];
self.angYaw.text = myString;
myString = [NSString stringWithFormat:#"%f",pRotation];
self.angPitch.text = myString;
myString = [NSString stringWithFormat:#"%f",yRotation];
self.angRoll.text = myString;
}
Thanks a lot! :D
motionManager has 4 modes: Accelerometer, Gyroscope, Magnetometer and Device motion.
Depending on which one you need, you need to start appropriate mode: startAccelerometerUpdates, startGyroUpdates, startMagnetometerUpdates or startDeviceMotionUpdates.
You are starting startGyroUpdates but reading deviceMotion property. In your case only gyroData will be available.
do this instead and you will be getting the deviceMotion data:
[motionManager startDeviceMotionUpdates];

How to move a object by titling the screen in cocos2d

i have his code for making movemnt of the object through horizontal postion.
#define kAccelerationSpeed 10
#define kAccelerometerFrequency 200
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
CGPoint pt = thePlayer.position ;
CGFloat accel = acceleration.x * kAccelerationSpeed;
pt = [[CCDirector sharedDirector] convertToGL:pt];
pt = ccp (pt.x+accel>0 , yLocationOfPlayer+accel<768 );
}
in init statment
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
whats wrong with the above code,it is not working,when i tilt the screen,it is not making any movemnt for the "theplayer" object.please help me to do this.
thanks in advance.
Are you sure your delegates are setup to report accelerometer information? Are you also assigning thePlayer.position in the accelerometer callback? I can't recall why but I do recall trying assignment within the callback and the results were rather stuttered and problematic. The solution for me was to have a variable the accelerometer delegate call will modify and assign the position of thePlayer inside of a tick script based on the variable update in the delegate callback.
you're not assigning the position of the player sprite. Try this:
NSLog(#"position before = %#", [NSValue valueWithCGPoint:thePlayer.position]);
thePlayer.position = ccp (pt.x+accel>0 , yLocationOfPlayer+accel<768 );
NSLog(#"position after = %#", [NSValue valueWithCGPoint:thePlayer.position]);
The logging will help you to determine if 1) the delegate method is getting fired and 2) the positions are as you are expecting.

iPhone accelerometer changing senstivity - Cocoa Touch

Hello
I want the sensitivity changing, for when the user moves the device. At the moment its not very sensitive, I believe its on default. I want it so its more sensitive, so when the user shakes the phone a little bit the sound plays.
Here is the code
Thanks
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(motion == UIEventSubtypeMotionShake)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"whip" ofType:#"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
}
First, make sure your interface adopts the UIAccelerobeterDelegate protocol.
#interface MainViewController : UIViewController <UIAccelerometerDelegate>
Now in your implementation:
//get the accelerometer
self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = .1;
self.accelerometer.delegate = self;
Implement the delegate method:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float x = acceleration.x;
float y = acceleration.y;
float b = acceleration.z;
// here you can write simple change threshold logic
// so you can call trigger your method if you detect the movement you're after
}
The values that the accelerometer returns for x,y, and z will always be a float between -1.0 and positive 1.0. You should call NSLog and output to the console x,y, and z values so you can get a feel for what they mean. Then you can develop a simple way to detect movement.
You cannot change how the shake events work. If you need something different you will have to write your own shake-detection code based on the x/y/z forces given to you by [UIAccelerometer sharedAccelerometer]; If you set the sharedAccelerometer's delegate to your class, you can get the accelerometer's accelerometer:didAccelerate: delegate callback.
If you're trying to recreate the shake gesture, here are a few things to bear in mind:
A shake is a motion in a direction, followed by a motion in the generally opposite direction. That means you'll have to keep track of your previous acceleration vectors, so that you know when they change. So that you don't miss it, you'll have to sample from the accelerometer pretty frequently.
CGFloat samplesPerSecond = 30;
[[UIAccelerometer sharedAccelerometer] setDelegate: self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval: 1.0 / samplesPerSecond];
Then in your delegate callback:
- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration {
// TODO add this acceleration to a list of accelerations and keep the most recent (perhaps 30)
// TODO analyze accelerations and determine if a direction change occurred
// TODO if it did, then a shake occurred! Clear the list of accelerations and perform your action
}

Using CMDeviceMotion to track iPhone tilt over time

I'm trying to use CMDeviceMotion to track when the iPhone is tilted backwards, and then do something. I've successfully created the CMMotionManager, I'm getting motion data from the system, and I'm filtering for results above a certain acceleration.
What I want to do though, is detect when the device is being tilted backwards or forwards above a certain speed. How do I do that?
Here's the code I have so far:
UPDATE: I think I solved it. I was looking for the rotationRate property, CMRotationRate. I've paired them together, I really only need the x value, so I'll keep working on it. If anyone has some tips at the below code, it's much appreciated.
- (void)startMotionManager{
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}
motionManager.deviceMotionUpdateInterval = 1/15.0;
if (motionManager.deviceMotionAvailable) {
NSLog(#"Device Motion Available");
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMDeviceMotion *motion, NSError *error){
//CMAttitude *attitude = motion.attitude;
//NSLog(#"rotation rate = [%f, %f, %f]", attitude.pitch, attitude.roll, attitude.yaw);
[self performSelectorOnMainThread:#selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
}];
//[motionManager startDeviceMotionUpdates];
} else {
NSLog(#"No device motion on device.");
[self setMotionManager:nil];
}
}
- (void)handleDeviceMotion:(CMDeviceMotion*)motion{
CMAttitude *attitude = motion.attitude;
float accelerationThreshold = 0.2; // or whatever is appropriate - play around with different values
CMAcceleration userAcceleration = motion.userAcceleration;
float rotationRateThreshold = 7.0;
CMRotationRate rotationRate = motion.rotationRate;
if ((rotationRate.x) > rotationRateThreshold) {
if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {
NSLog(#"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
NSLog(#"motion.rotationRate = %f", rotationRate.x);
[self showMenuAnimated:YES];
}
}
else if ((-rotationRate.x) > rotationRateThreshold) {
if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {
NSLog(#"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
NSLog(#"motion.rotationRate = %f", rotationRate.x);
[self dismissMenuAnimated:YES];
}
}
}
Have you looked at CMAttitude? Sounds like what you need, plus some mathematics I guess.
EDIT: Ok you did.
Quoting Apple documentation, at chapter "Getting the Current Device Orientation" :
If you need to know only the general
orientation of the device, and not the
exact vector of orientation, you
should use the methods of the UIDevice
class to retrieve that information.
Using the UIDevice interface is simple
and does not require that you
calculate the orientation vector
yourself.
Great, they do the maths for us. Then I guess that tracking acceleration like you do + tracking orientation, as above documentation explains, should do the job, using UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown ?
If none of the UIDeviceOrientation fit your needs, what you will need is to calculate some spatial angles from two CMAttitude references, which provides a rotationMatrix and a quaternion... these school maths are far away for me... I would then suggest to maybe search/ask a math only question with these.