Hard iron distortion compensation for 3 axis magnetometer
Abstract
This paper presents the way how the hard iron effect could be compensated and a way to implement it on a small power device such as a microcontroller. Because of the magnetized materials that can stay near a magnetometer sensor and because of the very small magnetic field of the Earth, before the use of the measured values from a magnetometer to determine the heading (angle with the N direction) of the sensor a compensation is needed.
Full text
Recent Innovations in Mechatronics (RIiM) Vol. 3. (2016). No. 1-2. DOI: 10.17667/riim.2016.1-2/5. Hard iron distortion compensation for 3 axis magnetometer Cornea George Mihai Department of Mechatronics University of Oradea, Faculty of Engineering and Management Oradea, Romania [email protected] Nilgesz Arnold Department of Mechatronics University of Oradea, Faculty of Engineering and Management Oradea, Romania Birouas Ionut Flaviu Department of Mechatronics University of Oradea, Faculty of Engineering and Management Oradea, Romania Tarca Radu Department of Mechatronics University of Oradea, Faculty of Engineering and Management Oradea, Romania Abstract— This paper presents the way how the hard iron effect could be compensated and a way to implement it on a small power device such as a microcontroller. Because of the magnetized materials that can stay near a magnetometer sensor and because of the very small magnetic field of the Earth, before the use of the measured values from a magnetometer to determine the heading (angle with the N direction) of the sensor a compensation is needed. Keywords—3 axis magnetometer; compensation; hard iron; I. INTRODUCTION There are 2 main types of magnetic distortions that have to be taken into account when designing an eCompass: hard iron distortions and soft iron distortions. Hard iron distortions are made by materials that produce a constant magnetic field which is in superposition with the earth magnetic field. A speaker or a motor magnet, for example, will produce a hard iron distortion. This distortion can be visualized as an constant offset from the origin of the circle to the (0,0,0) coordinate point. In small robots hard iron distortion is the component that tends to be of the greatest importance. This is due to the fact that the total mass of ferrous materials in the robot is small, but strong magnets are used for actuators. In order to get a usable heading from the magnetometer sensors it is a must to deal with hard iron interferences. It is to be noted that in order to be able to apply the compensation, the magnetic fields have to be constant and in the same position relative to the sensor. Soft iron distortions are made by materials that influence the earth magnetic field but don't have a magnetic field of their own. They are more computational expensive to eliminate since they are not constant with respect to orientation. Soft iron distortions can be visualized as a tilted ellipsoid rather than a perfectly centered circle. Soft iron distortions are significant only in vehicles that contain a lot of ferrous materials. It has a certain influence on the compass of a car or airplane (where for analogic compasses there are correcting tables) but it is certainly an important component in the distortions of a ships compass as can be seen in Fig. 1. Fig. 1. Distortions in the magnetic field made by a large mass of ferrous material such as a ship It has been measured that a large container ship can make a significant distortion for a few km from its position. Other types of interferences are the erratic interferences that can be somewhat predicted and because of this corrected. An example of this type of interference is the interference done by the current that passes a pair of conductors in order to feed a motor. Based on the current loop surface and current intensity, there will be a certain magnetic field generated. Since the cables could be considered to be fixed in respect to the sensor, only the current is variable so that if the current can be measured, a compensation table can be determined to be used with different current intensities to compensate the heading. This type of interference can be greatly minimized by using good practice techniques when designing the high current caring cables inside the robot, such as minimizing the loop between current feed and current return, twisting the high current cables etc.
Recent Innovations in Mechatronics (RIiM) Vol. 3. (2016). No. 1-2. DOI: 10.17667/riim.2016.1-2/5. II. IDEAL CONDITIONS COMPENSATION In the case of an ideal dataset of measurements the hard iron compensation could be extremely easy to be computed. A lot of examples using this easy method are presented on the internet mostly because of its ease of use and implementation. One plot representing this kind of data can be observed in Fig. 2. Fig. 2. Ideal conditions for compass calibration data acquisition In an ideal case the calibration data for the magnetometer would be looking like the above images. This can be done through carefully guiding the sensor carrier board on a flat surface (for the left data set) or by rotating the sensor in every possible direction, homogeneously spacing as many measurements as possible. Outside a controlled environment this kind of calibration data acquisition can be really tricky to be done if not impossible. Fig. 3. Pololu 3 axis magnetometer, 3 axis accelerometer and 3 axis gyroscope used for test The type of data from the left side of Fig. 2 can be taken by guiding the sensor on a perfectly flat surface and slowly turning it around. This will guaranty us that all the measurements from a certain axis elevation will be registered and a local minimum and a local corresponding maximum is actually measured. The calibration for each axis is made separately, one after another, by taking the maximum and minimum values on the measured axis and computing the average of the 2. The result is the actual offset of the axis for the compensated sensor setup. It is to be noted that this method employs the usage of the local minimum and maximum defined as a slice true the sphere that would represent the full measurement space. Any misalignment of one of the measurements could place the resulting offset way of chart by greatly influencing the average of the 2 values. Even if this method is harder to be made in uncontrolled environments it still can be done if a flat surface is available to guide the sensor on it. On the right side of Fig. 2 another measurement datasheet can be seen. This time the entire measurement space was homogenously filled with measurement data. Between those 2 pictures it can be seen that the sphere containing the first measurement set (left side) also contains the second measurement set (right side). The difference between the two is that the second set of measurements tends to contain the absolute maximum and absolute maximum on each axis. In this case the offset can be simultaneously computed as the average of the max and min on each axis of the magnetometer. The second dataset of measurement is practically impossible to be made without special equipment that will bring the sensor in every possible position. For the presentation purpose it was generated by using the sphere from the first dataset. The offsets that where determined can go from here on to be used by shifting all the measurements done on their corresponding axis by the amount determined with the average of the extremes. When dealing with this type of data the determination of the bias vector is relatively simple. The chance of reaching a maximum on a certain axis without actually measuring it as a sample is less likely. This way it is safe to assume that the bias on every axis is determined by the average of the minimum and maximum of the measured values. This method can be easily optimized to fit a limited capability device as a microcontroller by only saving the minimum and maximum of the measured values on each axis and then compute the average, giving the bias. In the first case we also have to impose that the compensation procedure to be done separately on each axis. As simple this implementation would be, both in terms of required processing power and implementation, its major setback is the complicated and hardly repeatable compensation technique that has to be employed by the user. Many times
Recent Innovations in Mechatronics (RIiM) Vol. 3. (2016). No. 1-2. DOI: 10.17667/riim.2016.1-2/5. this procedure has to be done on the field, with no flat surfaces to be used for the calibration of the robot. III. FIELD/REAL CONDITION COMPENSATION When working with field data, in the case of an „on the spot” calibration we most likely will end up with an acquisition that will look like the one in Fig. 4. Fig. 4. Magnetometer compensation data generated by the free, random movement of the sensor by hand It can be seen that on the high scale of the mag_x axis there is a higher density, making it is more likely that the sensor hit its maximum in a measurement but on the low side there is a lack of measurements that will result in an offset-ed sphere after correction application. This types of „mistakes”, uneven coverage of the minimum and maximum of each axis is impossible to control in such a compensation, done by an untrained operator, that doesn't understand the mechanics behind the compensation process or even by someone who understands them but is unable to perfectly cover the sphere. Another observation is that, if we consider the soft iron interferences to be small enough to be negligible, we are dealing with a perfect sphere. That perfect sphere can be sufficiently determined by 4 points that are on its surface. Because of real data containing, even if negligible, soft iron interferences and noise, a best fit approach is the best solution for the dataset. A sphere can be defined by specifying its center point (Xc,Yc,Zc) and its radius, R. So the goal of this example is to develop a NLREG program that will compute the values of Xc, Yc, Zc and R that cause a sphere to best fit a set of data points whose coordinates (Xp,Yp,Zp) are provided as a random selection of measurements. The matrix form of the solver can be seen in equation (1). (1) Where X is the measurement matrices and Y is the vector of known dependent variables according to Freescale AN4399. (2) (3) In practice it is more convenient to add each new measurement as it comes rather than making a huge matrices of measurements and compute the result from them. The matrices represented at (2) and (3) can be computed for each measurement in part and only the intermediate result to be saved into memory. By doing so a lot of memory space can be spared which is very important in a low spec system as a microcontroller. The functions that are doing the calculations are the following: void magCalAddxtx( double x, double y, double z) { xtx[0][0] += x*x; xtx[0][1] += x*y; xtx[0][2] += x*z; xtx[0][3] += x; xtx[1][0] += x*y; xtx[1][1] += y*y; xtx[1][2] += y*z; xtx[1][3] += y; xtx[2][0] += x*z; xtx[2][1] += y*z; xtx[2][2] += z*z; xtx[2][3] += z; xtx[3][0] += x;
Recent Innovations in Mechatronics (RIiM) Vol. 3. (2016). No. 1-2. DOI: 10.17667/riim.2016.1-2/5. xtx[3][1] += y; xtx[3][2] += z; xtx[3][3] += 1; } void magCalAddxty(double x,double y,double z) { double dd = x*x+y*y+z*z; xty[0][0] += x*dd; xty[1][0] += y*dd; xty[2][0] += z*dd; xty[3][0] += 1*dd; } Usage: while(measuring) { magCalAddxtx(rawx,rawy,rawz); magCalAddxty(rawx,rawy,rawz); ... } MagCalCompute(); //will also print the resulted 4 lines matrices. The first 3 parameters represent the offsets on x, y and z. In order to get the result of the compensation the MagCalCompute function computes the product of the 2 matrices (xtx and xty) and the first 3 elements in the resulting matrices represents the offsets and the 4th element represent the field strength. In practice it has been seen that the result of this algorithm, when supplied with measurements taken too close from each other can be really offset-ed. This is happening because of the sensor noise that is very big is respect to the possible distances between 2 measurements. In order to solve this problem the usage of an additional gyroscope can be used to scatter the taken measurements in a convenient way while the calibration movement is taking place. Another benefit of using a second sensor as a gyroscope is the possibility to give the operator a feedback consisting on actions that has to be taken. This way, by integrating the rotation speeds on the axis that is been calibrated, magnetometer measurements can be taken on the right time after a certain angle of rotation has been achieved and the operator can be informed to switch the axis once the calibration is done. By doing this a certain spread of the measurements and be achieved so that the dilution of precision generated by the resolution of the sensor and the noise to be minimum. Another important thing to mention is that the gyroscope in the system is not an added cost only for the calibration of the magnetometer; instead it can be used later on, in a sensor fusion system, both to smooth the computed heading and to determine the tilt and roll of the sensor in conjunction with an accelerometer. CONCLUSIONS Two methods for hard iron magnetometer calibration have been presented in this paper together with the author additions to the calibration process. The first one, largely used because of its simplicity is not capable to offer good results in all cases. The second method, together with the gyroscope calibration assistance, can be easily integrated into a low cost MCU with less than 2 kB of ram and still leave room for other applications, since the RAM usage can be overlapped, using the resources for the specific application. It also has to be pointed that the second method isn't adding any supplementary costs in sensors as an gyroscope would more than likely be used in the final system in conjunction with other sensors. REFERENCES 1. http://cache.freescale.com/files/sensors/doc/app_note a. /AN4399 .pdf 2. http://wwwpersonal.umich.edu/~johannb/Papers/paper64.pdf 3. https://users.soe.ucsc.edu/~elkaim/Documents/magca l.pdf