Join Steven Lipton for an in-depth discussion in this video Rotation rate with the gyro, part of Advanced iOS App Development: Core Motion.
- [Narrator] In fencing, all motion should be that linear Y motion in a thrust attack. You are not allowed to slash. However, in saber, slashing is allowed. Let's look at adding slash movements by reading the rotation rate of another sensor, the gyroscope. The slash motion we'll make is a high rotation rate, either positive or negative, along the Z-axis. At the same time, we have a thrust acceleration in Y. I'll measure rotation rates through the gyroscope. Unlike the accelerometer, the gyroscope needs time to start up, as much as three seconds.
Before using that sensor, check to make sure it's available. So let's go into the code again. Close the console and put under our thrust and parry, if self.motionManager.isGyroAvailable. Make a block. And afterwards, make it else. If it's not, we'll print a message, "Gyro not available yet." In the code for the gyro, I'm going to make another constant, let gyro = deviceMotion.rotationRate.
We'll have an X, Y, and Z in gyro that we can then use whichever axis we need. We'll be looking at the Z rotation rate for this, so I'm going to go ahead and do a print like I've done the others. And I'll do a string again, and I'll call it Rotation Rate for Z because we're going to be rotating along the Z-axis for this one. And it's going to be 7.4 again, like so. And then after that, I'm going to do gyro.z.
Okay, with that set up, we can once again get ready to run code, start this app up. You'll see our rotation rate which is a little tiny one here. And even right on the table, just twist it 90 degrees towards the left. Now twist it 90 degrees towards the right. If you notice, for a second or two there, if you move it back and forth while it's still on the table, you can see those rotations. Without a lot of effort, a swing is a high rotation rate.
The rotation rate is measured in radians per second. If you are swinging a phone in about a 60 to 90 degree arc in front of you in a second, pick it up and try it. You'll see five or six on your Z rates. There's values faster than that on your data. I can imagine a slash at above a plus or minus two radians per second. Notice, we get some parries and thrusts in that data. Swinging a phone does change acceleration. A Z-axis rotation will affect an X and a Y-axis acceleration through centripetal force, just like that bike turning a corner.
In this swing, we have a lot of X-axis acceleration, which is why there's so many parries. So this is an attack. I add the slash to the code for thrust so it won't confuse it with a parry. So we're going to do a little changing in code here. Go ahead and stop the code. And I'm going to go back into the code here. And I'm going to take all this and cut it. And I'm going to make it part of the thrust. And I'm going to go up here and do another if statement for this where it checks if the gyro is available and gets the gyro amount.
And right above here, I'm going to add a line where I'm going to change the gyro to let gyro = CMRotationRate(). And I made the var let problem, so I'm just going to change that to a var. And then, underneath this I'm going to put another if statement for the acceleration. if, whoops, that's a little too far over there.
if gyro.z > 2.0 || gyro.z < -2.0 we'll print to slash and do something like that to make it look pretty. If it's not a slash, it's a thrust.
There we go. Now, go ahead and run. Core Motion launches, gyro moves up pretty fast. And now, reach out your arm and slash at your opponent. And you can get parries in there. You can get thrusts in there. You notice your thrusts are not showing up if you do them. It's saying all slashes. So let's go look at that. You could change this, of course, by changing the rotation rate tolerance.
Depending on your goals for an application, you could go either way. So let's stop this here, and let me show you where we're talking about. This number here, let me change this to 4.0 and this to -4.0 and run it again. Stick your arm out and give it a good slash. Now, thrust forward. If you keep your arm pretty straight, you'll get a thrust and not a slash.
If you move it around at all in a rotation of any kind, you get the slash. Depending on my app, I would use different numbers for different things. In a fencing skills app, I would want to leave these numbers as tight as possible so I would train a fencer for an accurate linear thrust. In other apps, I'd have entirely different values so that I would be able to swing around a little bit more. So, it depends on your app, which one you're going to do. So, go ahead and stop this. And I want to look at another thing that you can get in device motion.
- Reading Core Motion data
- Understanding Core Motion methods
- Creating a pedometer app
- Using pace and distance data from Core Motion
- Pushing and pulling device motion data
- Accessing the altitude sensors
- Working with the motion activity manager (CMMotionActivityManager)