Tracking a magnet with a smartphone (Android) (ii)

Source: Internet
Author: User

In the previous blog, we mentioned the formula for calculating the position coordinates of the sensor using magnetic field strength, and the following describes how to use the smartphone to complete the magnet tracking (any device with a magnetic sensor can), here is mainly the use of Android phones.

?

1: Procedure steps:

    1. First put the magnet away from the phone position, and then hold the phone in the air to do "pour 8" character movement (this is the trajectory:). The purpose of this is to standardize the magnetic sensor of the mobile phone, because the magnetic induction device is susceptible to the interference of the field becomes inaccurate.
    2. Keep your phone stationary and record the current intensity of the magnetic field as the "Original magnetic field" (the magnetic field must be accurately averaged over multiple times). The Sensoreventlistener class is needed for Android development.
    3. The magnet is then placed about 20cm away from the phone, and the magnet is positioned above the horizontal line where the phone is located. The reason for this is because the magnetic field strength and distance of the power of K is inversely proportional, if too close to affect the cell phone magnetic sensor, too far away from the magnetic field too fast. It is above the horizontal line of the cell phone, because it was previously said that on the XY plane, the formula that was pushed on the previous page is not available.
    4. Recording the current magnetic field as the "Center magnetic Field", the "center magnetic field" minus "The original magnetic field" is the magnetic field strength generated by the magnet in the cell phone's magnetic sensor position. Then you can calculate a "center position" based on the formula in the previous article.
    5. Moving the magnet, according to the change of the magnetic field can be real-time minus the original magnetic field, and then calculate the magnetic sensor relative to the position of the magnet. The "center position" is then subtracted to get a "move" relative to the center position.
    6. The vector position of the 1:R1 and R2 can be calculated from the previous formula, and then the R3=R1-R2 is the position of the magnet relative to the center, knowing that the relative moving position of the magnet can be traced.

Figure 1

?

2: Smoothing Filter for magnetic field strength Value:

The above method uses the Android to encode after found the effect is very poor, mainly because uses the magnetic sensor to obtain the magnetic field value is not smooth, the magnetic field value is constantly fluctuation. Causes the position of the magnet to fluctuate during positioning. 2 is the original non-smoothed magnetic field value, you can see the magnetic field fluctuations are very large. The three lines in the figure are the magnetic field values generated by the magnetic field on XYZ three axes.

Figure 2

Therefore, the magnetic field values need to be smoothed filtering, I use the method is Kalman filter algorithm, interested can go to check the information, here do not do a detailed introduction. Mainly introduce me to this algorithm to the Kalman algorithm to do some changes. Figure 3 is the 5 equations of the Kalman (2 in the picture should be changed to-1), and Figure 4 is a description of the equation. Since the magnetic field value is a one-dimensional value on the xyz three axis, it is simpler to use than the matrix calculation, so the above formulation is simple (refer to: http://blog.chinaunix.net/uid-26694208-id-3184442.html).

Figure 3

Figure 4

Here we need to explain the Q and r two parameters, Q: That is the noise covariance matrix, I understand that the size of Q determines whether the optimal value is closer to the measured value or the estimated value. R: For the process noise covariance, I understand that the field value is measured without any processing, then the variance is r based on the measured value, and the R value can be obtained by experiment.

and the value of Q this needs to be judged, assuming that the K value determines the size of Q. When Q is larger (k larger) the optimal value in the Kalman is closer to the measured value, and the smaller the Q (k smaller) the better the optimal value in the Kalman is closer to the priori estimate. The popular saying is that q the greater the smoothness of the effect of the more bad, and the more "follow" the smoothed value (so-called "follow" is the original measurement of the rapid change, after the smoothed value is also very good change). The smaller the Q, the smoother the better, and the smoother values "don't follow".

For example, suppose that the measured value of three is [0,1.0,0], and if the Q value is large then the effect of smoothing is [0,0.8,0]. And if the Q value is small and smoothed, the value is [0,0.1,0].

So we have to find a good Q value to make a "smooth" and "follow" the balance between the test process I set the K value to 4 results smooth effect is good, can be very good positioning magnet position (the first blog in the demo video of the K value is-4). I said before that the demo video is not the final result, and then I think about Q is worth computing: since it is also "smooth" and to Do "follow", then you can dynamically determine the value of Q according to the change in the magnetic field value. That is, when the magnetic field changes the greater the value of Q to "follow", when the magnetic field changes in the hour to reduce the value of Q to make it more "smooth." Assuming that the current measured value is the optimal value of the previous state in the Kalman filter. And the difference between the two is the absolute value, then assume and K for the function.

The next step is to determine the function, according to the assumption that the greater k should be larger, and the smaller k the smaller. So the first thing to think about is the linear function (A, B is unknown). The results of the experiment 5 (the black line is the original magnetic field, green for the smooth magnetic field), you can see that this method can actually be achieved is "smooth" and "follow". But look closely at the pictures and you will find that when the magnetic field changes violently to a smooth process, the "green" line is always not quick to return to the stationary state (not quickly approaching the black line).

Figure 5

???? Because it is an average mapping, a "non-average mapping" is required to solve a problem that cannot be quickly smooth. That is, when the variable hour requires the K value to become smaller than the speed, so think of the non-average mapping function log. So suppose (the values of A and B can be calculated by looking at two points, for example, when 0.1 is k=-7, 3.0 is k=-1, and then A and B are computed.) These two parameters can be very good to achieve "smooth" and "follow". Then get the results of experiment 6 (black for the original magnetic field, blue who average map, red for non-average mapping): You can see the red line effect is significantly better than the Blue line. [Note: The above about Kalman filter is my understanding, and can not guarantee that the theory is correct. And the experimental results are good. If someone has a better way or opinion please be sure to contact me e-mail:[email protected]]

Figure 6

?

3: Precautions:

???? The above introduction is the knowledge of programming needs, I was on the Android experiment, on other devices should also be no problem. Here's a look at some of the areas that need attention.

    1. Before the experiment took the mobile phone "painting" inverted 8 words, in order to standardize the magnetic sensor. Otherwise, the non-standard eating sensor can not be accurately measured.
    2. Ensure that the magnets are on the top and sides of the phone, as previously introduced in the XY plane above the formula is not tenable.
    3. Magnets do not distance too far and too close, too far to detect the magnetic field, too close to affect the magnetic sensor.

?

Now about the use of smart phone (Android) tracking the contents of the magnets are all finished, if someone has questions, ideas, interests, etc. can contact me e-mail:[email protected]

Tracking a magnet with a smartphone (Android) (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.