iPhone Coding: Using the Accelerometer
You don't need to use a lot of fancy routines to subscribe to data generated by the iPhone's built in accelerometer. A single UIApplication delegate lets you know whenever the iPhone has updated its tilt data.
When you add a method for (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz to any UIApplication, the iPhone sends regular messages to your application giving you values for X, Y, and Z. These values are not exactly what I first expected:
X = Roll X corresponds to roll, or rotation around the axis that runs from your home button to your earpiece. Values vary from 0.5 (rolled all the way to the left) to -0.5 (rolled all the way to the right).
Y = Pitch. Place your iPhone on the table and mentally draw a horizontal line about half-way down the screen. That's the axis around which the Y value rotates. Values go from 0.5 (the headphone jack straight down) to -0.5 (the headphone jack straight up).
Z = Face up/face down. I expected the Z value to correspond to yaw. And it does not. It refers to whether your iPhone is face up (-0.5) or face down (0.5). When placed on it side, either the side with the volume controls and ringer switch, or the side directly opposite, the Z value equates to 0.0.
I went to expert Nate True for some advice about these values. He explained the reason that Z is not yaw. It's because yaw is a compass value, something the iPhone can't measure. X, Y, and Z are linear acceleration values. They point to whichever way gravity is operating. Together, X, Y and Z form a 3-D acceleration vector that indicates the direction of gravity. "Say you're tossing the phone up into the air, then all the values will fall to 0 you could make an app that makes the phone scream if you drop it."
You can use the X and Y values to determine the highest point of the iPhone by using an arctan function, i.e. float angle = atan2(yy, xx);. I've put together a sample application that demonstrates this. As the screen shot here shows, it provides the raw X, Y and Z values and keeps the arrow pointing to whatever point is "up" for your iPhone.
If you'd like to play some more with the accelerometer, I've also thrown together a simple game called Butterfly that allows you to slide a net around your screen to catch a virtual monarch butterfly.
Share
Categories
You don't need to use a lot of fancy routines to subscribe to data generated by the iPhone's built in accelerometer. A single UIApplication...
Add a Comment
I'm writing an iPhone compass.
Although it is of much more limited use. :)
http://mockdock.com/compass/
ross byers, umm.. The Wii remote has yaw, and it doesn't have a gyro in it.
September 17 2007 at 10:01 AM Report abuse Permalink rate up rate down Replyso then I was wrong it seems :(
Once I figured out how to code for the iPhone I was gonna make a version of Nate True's thing that was a 3D arrow instead of 2D, but I guess without a Z it's not possible :(
or is it just me? (it is pretty early in the morning and I am not thinking this through on purpose)
Can someone make an app that lets me hit my iPhone to snooze the alarm?
September 16 2007 at 6:02 PM Report abuse Permalink rate up rate down ReplyTheramin: Can someone pleeeeaaaaase make an app for the iphone to make Theramin sounds!?!
September 16 2007 at 5:39 PM Report abuse Permalink rate up rate down Reply#11: I took Erica's code and did that: http://robterrell.com/iSaber.app.zip
September 11 2007 at 8:04 AM Report abuse Permalink rate up rate down Reply+1 for a setting up the toolchain and compiling a simple Hello World app for iPhone!
Erica... make it so. :)
I've been working on a few neat things with the iPhone's accelerometer, but it's incredibly frustrating having an accelerometer but not a gyro (or Wiimote's "sensor bar").
September 11 2007 at 4:01 AM Report abuse Permalink rate up rate down ReplyHey, isn't it a right time to write "iPhone Saber" application, similar to MacSaber and WiiSaber? :)
September 11 2007 at 12:27 AM Report abuse Permalink rate up rate down ReplyWarning: Science content
There is a reason that you cannot get yaw from the acceleromter: It's an acceleromter, not a gyro. Assuming the iPhone is at rest, the only thing the accelerometer can measure is gravity. So imagine a line with a weight on it (a plumb-bob) tied to your iPhone. If you pitch or roll it, the angle of the string changes. This is measurable. Now rotate it in place, yawing it. The angle of the string does not change. Thus, the accelerometer cannot measure it.
This is why the Wiimote, which uses the same technology, requires the 'sensor bar' in order to get correct yaw.
PS: The numbers being reported at not angles in an arc, they are forces on an axis, where X=the narrow axis(left-right), Y=the long axis(forward-back), and Z=the vertical axis(up, down).
Hot Apps on TUAW
Deals of the Day
more deals- Used Apple iMac 17" Core 2 Duo 1.83GHz for $430 + $28 s&h
- Lounge Deluxe Stand for iPhone / iPod touch for $28 + $8 s&h
- Brookstone Surround-Sound Earbuds for $14 + $7 s&h
- Refurbished Skullcandy Tokidoki Smokin' Buds Mic'd Headset for $5 + $2 s&h
- Stitchway Backup Battery for iPod / iPhone for $5 + free shipping
- Used Apple MacBook Pro 2.4GHz 15" LED Laptop for $1,030 + $29 s&h
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



19 Comments