We know that iOS app is really too many, many applications let us marvel!!! A lot of unexpected applications!
Like what:
1. Electronic Compass Compass and other applications-let us know the direction.
2. Sport type Software-let us know how many kilometres we run.
3. Shake and shake function in social software.
4. Play in the role class in the game according to the device's shaking and so on operation.
Wait a minute.. And there are many applications that are unfolding before us.
In fact, they're mostly using a framework in iOS-the core motion framework-Coremotion.framework
coremotion.framework What the framework does
We can use iOS to provide us with the coremotion framework to access data about accelerometers and gyroscopes !
Not only does it provide you with real-time acceleration values and rotational speed values, but more importantly, Apple integrates a number of algorithms that can directly give you the acceleration to peel off the gravitational acceleration component, eliminating your high-pass filtering operations, and providing you with a specialized device for three-dimensional attitude information!
When I first came into contact, it was also confused. Then popularize the relevant knowledge.
We know that smartphones have become commonplace in our daily lives, especially iOS devices such as iphones, which are already quite common (commonly known as: Arcade). In its hardware, a lot of sensors are actually installed. One of the accelerometers and gyroscopes is two of these sensors!
Common sensors in a smart machine
1. Proximity Sensor
Proximity sensors, as the name implies, are used to detect very close target objects. These sensors often use electromagnetic fields or electrostatic fields to change, or to measure the emission of electromagnetic waves reflected by the change of the wave to judge. Because the means are different, so the target objects of proximity sensors are different, some for metal objects, some for plastic objects. specifically to the mobile phone, today's mobile phones are using a new generation of reflective optical proximity sensors, can be used for more than a variety of surface types to detect. The process is very simple, they will emit a person can not see the infrared light, once the phone comes to the phone, you will certainly answer, the hearing caused the face skin away from the sensor is very close, so only need to use an optical detector to detect the amount of light reflected from the skin changes, proximity sensor detected this close. What is the use of this sensing result? An obvious example is the ability to improve the power saving function of the phone, such as determining when to automatically turn on or off the display, keyboard backlight or touch function, and even when the automatic shutdown/standby, depending on the device owner is talking, is typing, or directly put the device into the pocket. These tasks are picked up by the proximity sensor on one shoulder.
2. Acceleration Sensor
I personally estimate that we are most familiar with the accelerometer. As a result of more intuitive gaming experience, basic gesture recognition and environmental awareness, this kind of sensor from 08 began to blowout, because the iphone, ipod drive and booming, due to the integration of the Wii and large-scale deployment, and thus led to the price decline. Now if there is any phone or music player not equipped with this sensor, the factory to the shop are embarrassed and put together with other equipment greeting. The accelerometer principle is very simple, now the phone is basically equipped with 3-dimensional line sensor, that is, to measure the acceleration of x, Y, z three axes. Acceleration is the force that acts on an object as it accelerates, like the gravity of the Earth.
Why do I say that everyone should be familiar with the accelerometer, if the IBM notebook friends know that the former IBM, now Lenovo, the ThinkPad series notebook, has always been a hard disk protection function, this function is to use the acceleration sensor dynamically monitor the vibration of the Notebook, And according to this vibration choose to shut down the hard drive or continue to run. This maximizes protection from vibrations, such as bumps in the working environment, or the hard disk damage caused by accidentally falling on the computer, to protect the data in the maximum extent possible. A similar popular use is the current digital camera and camera, using an accelerometer to detect the vibrations of the hands at the time of shooting, and automatically adjust the focus of the camera according to these vibrations. Now, we have the Iphone,android Corps, Nokia N900 and other mobile phones, we know at least, accelerometer can help you play the game. A typical example is Labyrinth2, a Chinese name for a maze. But more broadly, the accelerometer is more useful in detecting people's immediate background information. For example, through the analysis of the change in the speed of the three axes, the phone can tell if you are walking or biking or riding, uphill or downhill, and so on.
Theoretically, with three axes of acceleration three-dimensional information, we can infer the acceleration direction of information, for example, you speed up the direction of the bike, or you take the elevator is facing up or down. According to the knowledge of junior maths, these directions and angles can be calculated by the addition and subtraction of vectors. Unfortunately, we can't just rely on the accelerometer to detect the angle of acceleration, or the direction of the phone itself. What is this for? To explain this, it involves a big problem that bothers the accelerometer, which is the interference of the gravitational acceleration component.
When your mobile device is stationary, the accelerometer can tell you that it is facing toward the ground plane, which is no problem; However, when the device is moving, the analysis direction is quite complicated. In this case, there are four types of discussions
- When the device is completely static or uniform motion horizontally, it will output a value in the vertical axis, which is the gravitational acceleration, which is nearly 9.8.
- When the device is free-falling, it will output 0 of the three axes.
- When the device is stationary or uniform motion, it is not at absolute level. At this point, any axis may have output values, but they are all components of the gravitational acceleration. Once in this state, we can find the angle of the cell phone rotation by the angle of the three axes.
- The device accelerates in any direction. At this point, the problem becomes complicated, because you can not know the current direction of the device information, so you do not know the weight of the gravitational acceleration is how to decompose, so ... You don't know what the value of an axis is, which is the gravitational acceleration, and which is the true linear acceleration part.
In addition, another acceleration sensor is powerless, is the pure horizontal rotation, or uniform horizontal change.
This type of motion, the accelerometer will only output a value: G. Because neither X, y, nor z-axis, there is a real acceleration of motion. What is the problem? It's like you're walking down the street with a cell phone and walking along a street, and the acceleration of the road is naturally possible. Then you turn and walk on the other side of the road, and the acceleration of this section can be picked up. You come home, take out the cell phone to look at the data, yes, both sides of the acceleration, mobile phone know I walked two sections of the road, very smart, what is missing something? The movement and angle of your turn! This accelerometer is not available, and this information is very useful in many augmented reality smart applications. To solve this problem, people began to introduce other kinds of sensors, such as the next one to introduce the gyroscope.
About the coordinates of the accelerometer:
3.Gyroscope
The gyroscope represents another important, but probably still need to wait a two years to take off the sensor field, mainly because the navigation-level MEMS gyroscope is still too expensive for mobile phones, ordinary gyroscope manufacturers rarely look at their eyes. Of course, in the iPhone4 led to the foreseeable mobile phone arms race, sooner or later will make the gyroscope gradually spread. The main function of a gyroscope is to measure the rotational rate along a particular axis, based on the theory of conservation of angular momentum. In use, the gyroscope's rotor rotates at high speed, always pointing to a fixed direction, and the gyroscope can feel it when the moving object is moving in a direction that deviates from the predetermined direction. In modern aviation equipment, when the aircraft is driving, it is to use up to more than 10 gyroscope to measure whether the body tumbling, and how to roll.
4. Electronic Compass
An electronic compass, also known as a magnetometer, or an electronic compass, is a sensor that, after the accelerometer, has been blowout since 09. If I remember correctly, the first mainstream smartphone to integrate this sensor is Google's G1, in this sense, the G1 for the magnetic sensor market, may be comparable to the iphone for the meaning of accelerometer. What's the use of this thing? As the name implies, is to tell you the north-south polar direction. The old compass, or the plane compass, can tell you where the Antarctic Arctic is after you have flattened the compass, so that you know the angle of your current front on the big plane axis of the Earth. Of course, because of the technical constraints, the plane compass requires you to maintain the level, if the tilt is not accurate. Many of the electronic compasses that appeared later were two axes, that is, the same as the electronic plane compass. In my memory, a few years ago the Openmoko phone was integrated, so there was the same limitation. Now the high-end smartphone is integrated with three-dimensional electronic compass, due to the addition of a tilt sensor, the compass can be tilted compensation, thus overcoming this flaw, can output three-dimensional direction of the angle information.
The principle of electronic compass is also very simple, is the Hall effect that we have learned in physics. When the current passes through a conductor in a magnetic field, the magnetic field creates a transverse Lorentz force on the electrons in the conductor, so the charge is deflected, the direction of deflection is perpendicular to the direction of the current and the direction of the magnetic field, and the positive and negative charges are deflected in the opposite direction, resulting in a voltage difference at both ends of the conductor. This voltage difference is also called the Hall voltage, and is proportional to two factors: the current size and the magnetic field strength. Of course there is a problem to be aware of, if the direction of the magnetic field is not perpendicular to the conductor, the actual magnetic field is actually a vector component of the original magnetic field. How do you know the angle of this magnetic field component? We can use two or more hall-effect sensors, perpendicular to each other, so that the direction of the magnetic field can be obtained by the ratio of the hall voltage on different sensors, the calculation process only need to know the basic linear algebra operation. Of course, there must be some tricky in the specific calculation, but it is not the key point, so this is not introduced here for the time being. So unlike gyroscopes, the electronic compass outputs the angular direction, and the gyroscope outputs the angular acceleration.
So it should be understood that in the application of the electronic compass, the magnetic field is the Earth's magnetic field, and the electronic compass measures the vector value of the Earth's magnetic field, which is then converted and expressed in system coordinates. In the ideal case, this measured direction value is also more accurate, once I know the mobile device is the beginning of the direction value, it can pass the output of the electronic compass after the device to find the next series of directions to change information.
Unfortunately, the ideal situation in this world does not exist. In an environment where we often use mobile devices, there are sometimes magnetic fields that are not effectively shielded except for the Earth, which is likely to interfere with the electronic Compass's reading and make the results untrustworthy. Furthermore, if there is metal around the mobile device, the electronic compass will be greatly affected once the metal is magnetized.
Self.motionmanager = [[Cmmotionmanager alloc] init];
Detection of accelerometers
if ([Self.motionmanager isaccelerometeravailable]) {
NSLog (@ "Accelerometer is available.");
} else{
NSLog (@ "Accelerometer is not available.");
}
if ([Self.motionmanager isaccelerometeractive]) {
NSLog (@ "Accelerometer is active.");
} else {
NSLog (@ "Accelerometer is not active.");
}
Gyroscope detection
if ([Self.motionmanager isgyroavailable]) {
NSLog (@ "Gryro is available.");
} else {
NSLog (@ "Gyro is not available.");
}
if ([Self.motionmanager isgyroactive]) {
NSLog (@ "Gryo is active.");
} else {
NSLog (@ "Gryo is not active.");
}
Description
1. Access to the accelerometer and gyroscope is encapsulated in the Cmmotionmanager class under the coremotion.framework framework. We use the method of class to get the acceleration data and gyroscope data we want.
2. The Isaccelerometeravailable method is used to see if the accelerometer is available.
3. The Isaccelerometeravailable method is used to view the status of the accelerometer: active (boot).
4. The Isgyroavailable method and the Isgyroactive method are used to detect the gyroscope.
used to Cmmotionmanager get the data for acceleration
Talking about getting the data for the accelerometer. There are two main ways of doing this.
1.push mode
In this way, the accelerometer data is obtained in real time and is displayed in the corresponding queue. is active.
Detection of accelerometers
if ([Self.motionmanager isaccelerometeravailable]) {
NSLog (@ "Accelerometer is available.");
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];
[Self.motionmanager Startaccelerometerupdatestoqueue:queue
withhandler:^ (Cmaccelerometerdata*accelerometerdata, Nserror *error) {
NSLog (@ "X =%.04f, Y =%.04f, Z =%.04f", accelerometerdata.acceleration.x, ACCELEROMETERDATA.ACCELERATION.Y, ACCELEROMETERDATA.ACCELERATION.Z);
}];
}
2.pull mode
is to get the data and, if it is to be displayed, ask accelerometer for the data. That is, the passive way.
Self.motionManager.accelerometerUpdateInterval = 0.01; Tell the manager that the update frequency is 100Hz
[Self.motionmanager startaccelerometerupdates]; Start the update and the background thread starts running.
Used to Cmmotionmanager the data for the gyroscope.
Similar to getting data from an accelerometer, let's get data on the gyroscope
Gyroscope detection
if ([Self.motionmanager isgyroavailable]) {
NSLog (@ "Gryro is available.");
if ([self.motionmanager isgyroactive] = = NO) {
[Self.motionmanager setgyroupdateinterval:1.0f/1.0f];
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];
[Self.motionmanager Startgyroupdatestoqueue:queue
withhandler:^ (Cmgyrodata *gyrodata,nserror *error) {
NSLog (@ "Gyro Rotation x =%.04f", gyrodata.rotationrate.x);
NSLog (@ "Gyro Rotation y =%.04f", gyrodata.rotationrate.y);
NSLog (@ "Gyro Rotation z =%.04f", gyrodata.rotationrate.z);
}];
}
}else {
NSLog (@ "Gyro is not available.");
}
Class 1th data: Cmaccelerometerdata should be the total acceleration data of the device
Class 2nd data: Tri-axis angular velocity data for cmgyrodata devices
Class 3rd data: triaxial magnetic data for cmmagnetometerdata devices
Class 4th Data: Cmdevicemotion is Special: 1, the angular velocity data (rotational rate, that is, the output of the gyroscope) is repeated, 2, the total acceleration data of the device is separated (that is, the original acceleration is two superimposed), Separate the Pure gravitational acceleration data (that is, the gravitational acceleration vector in the current device's reference coordinate system expression, the development of no longer need to filter to extract this information) and user acceleration data (no longer need to filter, but according to the program needs to add the filter algorithm can be retained); 3, provides the device posture data, Used to determine the position and posture of the phone in the current space.