Lagging-Domain Model for Compensation of Hysteresis of xMR Sensors in Positioning Applications

The hysteresis of magnetoresistive sensors remains a considerable cause of inaccuracy of positioning applications. The phenomena itself has been well studied and described by different physical and phenomenological models. Various biasing techniques have been proposed. However, the increased fabrication and computational price they require is undesirable. In this paper, a computational algorithm for the compensation of hysteresis of linear and rotary encoders is proposed. A lagging-domain model based on play operators is presented for prediction of hysteresis. The outlined procedure for the calibration of parameters allows the use of the algorithm for various types of encoders without knowing their exact material properties. The method was tested on different anisotropic magnetoresistive and tunneling magnetoresistive sensors. Results show that the impact of hysteresis was reduced by up to 90% without a significant increase of computational time or production costs.


Introduction
Magnetic field sensors have been constantly gaining attention [1,2] in the position and angular sensing industry due to their accuracy, reasonable price, and immunity to dirt. Aside from Hall and inductive sensors, three types of magnetoresistive sensors (xMR) are currently widely used commercially [3]. These are anisotropic magnetoresistive sensors (AMR), tunneling magnetoresistive sensors (TMR) and giant magnetoresistive sensors (GMR).
The AMR effect was discovered in 1856 by William Thompson, known also as Lord Kelvin [4]. He discovered that the resistance of iron and nickel changes if they are exposed to magnetic field. Nowadays, nickel-iron alloys, called permalloys, are used to form thin film resistors that can achieve up to 4% change in resistance in the presence of magnetic field. The effect happens due to spin-orbit interaction and s-d scattering as described in [5][6][7][8][9][10][11]. When the material is magnetized up to the saturation, the resistivity only depends on the angle between the magnetization and current density. AMR sensor has much simpler structure than TMR and GMR and was therefore the first of the three to become widely used.
The TMR [12] and GMR [13,14] effect can be observed in a similar multilayer thin film structure with two separated ferromagnetic layers. The first has its magnetization pinned by another antiferromagnetic layer. The second ferromagnetic layer can change the direction of the magnetization freely and tends to follow the external magnetic field. The resistance of the structure depends on the angle between the magnetization directions of the two layers.

The Lagging-Domain Model
The encoder is modeled in terms of an open-loop SISO (single input, single output) system. The measured position is proportional to the direction of the external magnetic field. As the field only rotates in the x-z plane of the sensor (the setup is explained in more detail in chapter Calibration), its direction can be uniquely described by angle θ. In this paper, angular degrees will be used as the units for the angle of rotation for rotary or for position within a magnetic pole for linear encoders. Otherwise, they may be replaced by micrometers or other units, respectively. The model accepts the actual direction of the external magnetic field as an input and calculates the measured direction as an output. The signal diagram is shown in Figure 1a. For explanation of symbols see Table 1. The model is time-discrete as is the duty cycle of the encoder. The Lagging-domain model is used for compensation of the hysteresis error of the encoder. Since the reference angle is not known in this case, the output of the encoder is used as an input of the model. The idea for the Lagging-domain model comes from the magnetic behavior of the material. The external magnetic field affects magnetic domains by torque; therefore, they reform, causing energy losses. The energy provided by external field increases with the magnetic torque, the torque depends on the angle between external magnetic field and the magnetization of the domain. Some domains need more energy to turn than others. The orientation of magnetization of such parts of the material will be "dragged" behind the external field with a lag, bigger than of those which turn more easily. The Lagging-domain model is used for compensation of the hysteresis error of the encoder. Since the reference angle is not known in this case, the output of the encoder is used as an input of the model. The idea for the Lagging-domain model comes from the magnetic behavior of the material. The external magnetic field affects magnetic domains by torque; therefore, they reform, causing energy losses. The energy provided by external field increases with the magnetic torque, the torque depends on the angle between external magnetic field and the magnetization of the domain. Some domains Sensors 2018, 18, 2281 4 of 14 need more energy to turn than others. The orientation of magnetization of such parts of the material will be "dragged" behind the external field with a lag, bigger than of those which turn more easily.
Similarly, the response of an xMR sensor can be mathematically subdivided into components called domains. The domain function must be defined with respect to three requirements for the model: continuity, symmetry, and space invariance. From the operators, discussed in the first chapter, an ordinary play operator is chosen for the domain function, while the relay operator fails to provide continuity, the stop operator and the generalized play operator are not space invariant and the K-P operator is generally not symmetric.
The value of domain D i in a time step t j depends on the direction of the external magnetic field and on the history of the domain according to Equation (1), where parameter φ i is the lag between directions of external magnetic field and domain magnetization in saturation. The response is represented graphically in Figure 2. Each of N domains has its own value φ i . Similarly, the response of an xMR sensor can be mathematically subdivided into components called domains. The domain function must be defined with respect to three requirements for the model: continuity, symmetry, and space invariance. From the operators, discussed in the first chapter, an ordinary play operator is chosen for the domain function, while the relay operator fails to provide continuity, the stop operator and the generalized play operator are not space invariant and the K-P operator is generally not symmetric.
The value of domain Di in a time step tj depends on the direction of the external magnetic field and on the history of the domain according to Equation (1), where parameter is the lag between directions of external magnetic field and domain magnetization in saturation. The response is represented graphically in Figure 2. Each of N domains has its own value .
The hysteresis loop of the described operator-based models, including the common Prandtl-Ishlinskii model is constructed as a weighted sum of mathematical domains. The Lagging-domain model's output is the estimated error of measurement due to hysteresis and is calculated in a slightly different way by Equation (2). This way, the output of the model can be directly used for the compensation of measurement error due to hysteresis. Also, the weights can be easily determined with the calibration routine described in the chapter Calibration. (1) The pseudo code of the algorithm is presented in Figure 3. We can see that the computational complexity is proportional to N. Computational space is taken mostly by arrays D, and which are of length N. The hysteresis loop of the described operator-based models, including the common Prandtl-Ishlinskii model is constructed as a weighted sum of mathematical domains. The Lagging-domain model's output is the estimated error of measurement due to hysteresis and is calculated in a slightly different way by Equation (2). This way, the output of the model can be directly used for the compensation of measurement error due to hysteresis. Also, the weights can be easily determined with the calibration routine described in the chapter Calibration.
The pseudo code of the algorithm is presented in Figure 3. We can see that the computational complexity is proportional to N. Computational space is taken mostly by arrays D, φ and w which are of length N.  Please note that the lags φ do not depend on the absolute position of the encoder. The shape of the hysteresis loop only depends on the change of the position and not on the absolute point of turn. The model is therefore suitable only for modeling sensors without a significant anisotropy of the material which would result in a spatially dependent hysteresis effect. The significance of anisotropy depends on the type of the sensor and its process parameters. A quick suitability test to check the appropriateness of the sensor for the Lagging-domain algorithm can be carried out by recording the error during straight-line upward and downward movement. The difference between the two should be constant over the whole operating range.

Calibration
The shape of the hysteresis is determined by arrays Φ and w. The calibration of the two is performed with the data, collected from the encoder and the reference encoder during the calibration procedure. The advantage of an experimental approach to calibration is that no material parameters must be known. The procedure is identical for all types of xMR sensors and their applications and can be remade anytime.
During the calibration procedure, the sensor must move monotonically in one direction and then monotonically in the reverse direction. The movement must be slow enough to exclude all transients and it must cover the range wide enough that the sensor's delay due to hysteresis settles down to a constant value. The output of the encoder and the reference signal are collected. The measurement must be free of other sources of errors such as irregularities of the magnetic scale or higher harmonics, which often occur in position measurements. Some errors can be artificially removed after the measurement is taken as they are common to both upward and downward movement. The noise can be reduced with a non-causal filter such as a Hamming window. The algorithm will be described for the case where the output value decreases in the first part of the measurement and then increases after the change of direction. The calibration for the opposite case can be done similarly with some minor changes. Please note that the lags ϕ do not depend on the absolute position of the encoder. The shape of the hysteresis loop only depends on the change of the position and not on the absolute point of turn. The model is therefore suitable only for modeling sensors without a significant anisotropy of the material which would result in a spatially dependent hysteresis effect. The significance of anisotropy depends on the type of the sensor and its process parameters. A quick suitability test to check the appropriateness of the sensor for the Lagging-domain algorithm can be carried out by recording the error during straight-line upward and downward movement. The difference between the two should be constant over the whole operating range.

Calibration
The shape of the hysteresis is determined by arrays Φ and w. The calibration of the two is performed with the data, collected from the encoder and the reference encoder during the calibration procedure ( Figure 4). The advantage of an experimental approach to calibration is that no material parameters must be known. The procedure is identical for all types of xMR sensors and their applications and can be remade anytime.
During the calibration procedure, the sensor must move monotonically in one direction and then monotonically in the reverse direction. The movement must be slow enough to exclude all transients and it must cover the range wide enough that the sensor's delay due to hysteresis settles down to a constant value. The output of the encoder and the reference signal are collected. The measurement must be free of other sources of errors such as irregularities of the magnetic scale or higher harmonics, which often occur in position measurements. Some errors can be artificially removed after the measurement is taken as they are common to both upward and downward movement. The noise can be reduced with a non-causal filter such as a Hamming window. The algorithm will be described for the case where the output value decreases in the first part of the measurement and then increases after the change of direction. The calibration for the opposite case can be done similarly with some minor changes.
First, four typical points are identified as shown in Figure 5. The measurement error e is calculated by subtracting the reference signal from the sensor's output as shown in Figure 5b. According to Equation (2), the error can be represented as a weighted sum of contributions of domains ( Figure 5c). The behavior of domains can be deduced from their definition. Graph e(x) between the critical points (t1) and (t2) (shown in Figure 5b) can be split into N linear sections with a gradient = Δ /Δ (Figure 5c above). By rewriting the gradients from right to left we obtain Equation (3), or, rewritten in a vector form and generalized for various number of domains, (4). The system of equations is solved by (5) to obtain the weights.
In general, vector Φ does not have to depend on the calibration procedure but can be chosen in advance. In this case, L must be bigger than the expected range of a hysteresis effect. However, the accuracy of the model can be optimized by varying combinations of N and L.
In general, vector Φ does not have to depend on the calibration procedure but can be chosen in advance. In this case, L must be bigger than the expected range of a hysteresis effect. However, the accuracy of the model can be optimized by varying combinations of N and L.

Compensation
The Lagging-domain model is used to estimate the current error of the xMR encoder. Once the error is known, it can be subtracted from the output angle and thus eliminated. Since the reference signal x, which was the input of the model, is not known, the output of the sensor is used instead (see Figure 1). There is a slight mismatch between the two, but its impact can be reduced by using the sensor's output instead of the reference signal already in the calibration routine. The compensation algorithm may be built in the encoder's microprocessor or added as a post-processing module.

Experiment and Results
The developed algorithm was tested on AMR and TMR linear sensor samples. The magnetic field of a sinusoidal shape was provided by a magnetic scale with alternating, 2 mm long magnetic poles. Sensing elements were connected in a double Wheatstone bridge and were thus providing the information about the sine and cosine component of magnetic field. The setup for testing is shown in Figure 6. A linear stage with an optical reference encoder with the resolution of 0.1 µ m, which exhibits no significant hysteresis, was used for accurate measurements of the linear position. The magnetic scale was placed right next to the scale for the optical encoder so that there were no moving mechanical parts between them. Therefore, there were no additional hysteresis effects caused by the reference encoder or the stage which could be unwittingly mistaken for the error of

Compensation
The Lagging-domain model is used to estimate the current error of the xMR encoder. Once the error is known, it can be subtracted from the output angle and thus eliminated. Since the reference signal x, which was the input of the model, is not known, the output of the sensor is used instead (see Figure 1). There is a slight mismatch between the two, but its impact can be reduced by using the sensor's output instead of the reference signal already in the calibration routine. The compensation algorithm may be built in the encoder's microprocessor or added as a post-processing module.

Experiment and Results
The developed algorithm was tested on AMR and TMR linear sensor samples. The magnetic field of a sinusoidal shape was provided by a magnetic scale with alternating, 2 mm long magnetic poles. Sensing elements were connected in a double Wheatstone bridge and were thus providing the information about the sine and cosine component of magnetic field. The setup for testing is shown in Figure 6. A linear stage with an optical reference encoder with the resolution of 0.1 µm, which exhibits no significant hysteresis, was used for accurate measurements of the linear position. The magnetic scale was placed right next to the scale for the optical encoder so that there were no moving mechanical parts between them. Therefore, there were no additional hysteresis effects caused by the reference encoder or the stage which could be unwittingly mistaken for the error of the MR encoder. The signal acquisition and processing was performed partly by the microcontroller and partly by PC with MATLAB. The data from sensing elements were collected along with the reference position. After some basic calibration of the signals, the angle of magnetic field above the magnetic scale was calculated from the sine and cosine signal. The position along the horizontal axis is proportional to this angle. The distance of 360° corresponds to one electrical period of the signal, which is one magnetic pole for AMR and one magnetic period (two poles) for TMR sensor. In multi-turn applications with single-turn encoders, the overflow between 360° and 0° must be considered. The proposed solution is that whenever an overflow of the input signal is detected, all domains are accordingly transferred for 360°. This may sometimes result in an output which is not within the [0°, 360°) interval, but this can be corrected by division by module 360.
Before the first use, the calibration sequence was carried out. The sensor was moved as proposed in the chapter Calibration. The collected data are shown in Figure 7. The number of domains N was set to 5. Vector Φ was determined. The data were split into linear regions and vector w was calculated according to Equation (5). Then, vector D was initialized. Since no information about the state of the material before the beginning of simulation is available, a demagnetized state is assumed, and D was filled with random numbers from range [0°, 360°). The data from sensing elements were collected along with the reference position. After some basic calibration of the signals, the angle of magnetic field above the magnetic scale was calculated from the sine and cosine signal. The position along the horizontal axis is proportional to this angle. The distance of 360 • corresponds to one electrical period of the signal, which is one magnetic pole for AMR and one magnetic period (two poles) for TMR sensor.
In multi-turn applications with single-turn encoders, the overflow between 360 • and 0 • must be considered. The proposed solution is that whenever an overflow of the input signal is detected, all domains are accordingly transferred for 360 • . This may sometimes result in an output which is not within the [0 • , 360 • ) interval, but this can be corrected by division by module 360.
Before the first use, the calibration sequence was carried out. The sensor was moved as proposed in the chapter Calibration. The collected data are shown in Figure 7. The number of domains N was set to 5. Vector Φ was determined. The data were split into linear regions and vector w was calculated according to Equation (5). Then, vector D was initialized. Since no information about the state of the material before the beginning of simulation is available, a demagnetized state is assumed, and D was filled with random numbers from range [0 • , 360 • ).  After initialization and calibration of the Lagging-domain model of a sensor, the measurements were taken. The sensor was moved in an arbitrary sequence. The reference signal and the output of the sensor were collected. The actual error was calculated by subtracting the reference signal x from the output y (Figure 8, blue). Then, the Lagging-domain model was used to calculate the estimated error, using the reference signal as the input (Figure 8, dotted). The estimated error was used to compensate the effect of the hysteresis at the output of the sensor by subtracting it. The difference between the measured value after correction and the reference is shown red in Figure 8. For the validation of the compensating algorithm, the test for quantitatively evaluating the error due to hysteresis had to be defined. The sensor was moved over the whole operating range with occasional changes of direction. The severity of the hysteresis impact on the measurement error (HME) was evaluated by finding the maximal difference of a measurement at the same position during upward and during downward movement. HME is expressed in the same unit as the error, in our case angular degrees. Please note that the noise may also false contribute to the HME.
The efficiency of the compensation was evaluated by comparing the HME before and after the compensation as shown in Figure 8. The result of tests performed on AMR sensor samples is shown in Figure 9. The result for tests on TMR sensors is in Figure 10. The detailed experimental data and After initialization and calibration of the Lagging-domain model of a sensor, the measurements were taken. The sensor was moved in an arbitrary sequence. The reference signal and the output of the sensor were collected. The actual error was calculated by subtracting the reference signal x from the output y (Figure 8, blue). Then, the Lagging-domain model was used to calculate the estimated error, using the reference signal as the input (Figure 8, dotted). The estimated error was used to compensate the effect of the hysteresis at the output of the sensor by subtracting it. The difference between the measured value after correction and the reference is shown red in Figure 8. After initialization and calibration of the Lagging-domain model of a sensor, the measurements were taken. The sensor was moved in an arbitrary sequence. The reference signal and the output of the sensor were collected. The actual error was calculated by subtracting the reference signal x from the output y (Figure 8, blue). Then, the Lagging-domain model was used to calculate the estimated error, using the reference signal as the input (Figure 8, dotted). The estimated error was used to compensate the effect of the hysteresis at the output of the sensor by subtracting it. The difference between the measured value after correction and the reference is shown red in Figure 8. Figure 8. Error of the sensor with and without hysteresis compensation. Before the compensation, a noticeable gap between the error during upward and downward movement can be seen. The gap is decreased after the compensation is applied. The remaining error is mostly due to the imperfections of the magnetic scale.
For the validation of the compensating algorithm, the test for quantitatively evaluating the error due to hysteresis had to be defined. The sensor was moved over the whole operating range with occasional changes of direction. The severity of the hysteresis impact on the measurement error (HME) was evaluated by finding the maximal difference of a measurement at the same position during upward and during downward movement. HME is expressed in the same unit as the error, in our case angular degrees. Please note that the noise may also false contribute to the HME.
The efficiency of the compensation was evaluated by comparing the HME before and after the compensation as shown in Figure 8. The result of tests performed on AMR sensor samples is shown in Figure 9. The result for tests on TMR sensors is in Figure 10. The detailed experimental data and Figure 8. Error of the sensor with and without hysteresis compensation. Before the compensation, a noticeable gap between the error during upward and downward movement can be seen. The gap is decreased after the compensation is applied. The remaining error is mostly due to the imperfections of the magnetic scale.
For the validation of the compensating algorithm, the test for quantitatively evaluating the error due to hysteresis had to be defined. The sensor was moved over the whole operating range with occasional changes of direction. The severity of the hysteresis impact on the measurement error (HME) was evaluated by finding the maximal difference of a measurement at the same position during upward and during downward movement. HME is expressed in the same unit as the error, in our case angular degrees. Please note that the noise may also false contribute to the HME.
The efficiency of the compensation was evaluated by comparing the HME before and after the compensation as shown in Figure 8. The result of tests performed on AMR sensor samples is shown in Figure 9. The result for tests on TMR sensors is in Figure 10. The detailed experimental data and samples descriptions are provided in the Appendix A. The tests have shown that the impact of hysteresis was reduced by up to 90% for AMR and by 70% for TMR samples. The tests have shown that the impact of hysteresis was reduced by up to 90% for AMR and by 70% for TMR samples. Figure 9. The efficiency of the compensation algorithm was tested on 10 different types of AMR sensors by comparing the indicator HME before and after compensation. The hysteresis before the compensation depends mostly on the sensing distance and on the method of construction. The Lagging-domain algorithm with 5 domains was implemented into an encoder with the STM32F303 microcontroller. The duty cycle was extended for approximately 5 µ s. Refresh rates of similar encoders are typically 20 to 50 µ s. The latency, caused by described algorithm, is therefore just within the acceptable range. The power consumption was not noticeably affected.

Discussion
The algorithm for compensating the error of xMR motion sensors due to the hysteresis effect using the Lagging-domain model was developed and tested on different AMR and TMR sensor samples. The resemblance of the concept of xMR sensors suggests that the model may be suitable for all three types. However, there may be exceptions, so the properties of material must be checked by The tests have shown that the impact of hysteresis was reduced by up to 90% for AMR and by 70% for TMR samples. Figure 9. The efficiency of the compensation algorithm was tested on 10 different types of AMR sensors by comparing the indicator HME before and after compensation. The hysteresis before the compensation depends mostly on the sensing distance and on the method of construction. The Lagging-domain algorithm with 5 domains was implemented into an encoder with the STM32F303 microcontroller. The duty cycle was extended for approximately 5 µ s. Refresh rates of similar encoders are typically 20 to 50 µ s. The latency, caused by described algorithm, is therefore just within the acceptable range. The power consumption was not noticeably affected.

Discussion
The algorithm for compensating the error of xMR motion sensors due to the hysteresis effect using the Lagging-domain model was developed and tested on different AMR and TMR sensor samples. The resemblance of the concept of xMR sensors suggests that the model may be suitable for all three types. However, there may be exceptions, so the properties of material must be checked by  The Lagging-domain algorithm with 5 domains was implemented into an encoder with the STM32F303 microcontroller. The duty cycle was extended for approximately 5 µs. Refresh rates of similar encoders are typically 20 to 50 µs. The latency, caused by described algorithm, is therefore just within the acceptable range. The power consumption was not noticeably affected.

Discussion
The algorithm for compensating the error of xMR motion sensors due to the hysteresis effect using the Lagging-domain model was developed and tested on different AMR and TMR sensor samples. The resemblance of the concept of xMR sensors suggests that the model may be suitable for all three types. However, there may be exceptions, so the properties of material must be checked by performing the suitability test before applying the algorithm.
The algorithm cannot calculate the error prediction correctly directly after the start-up. The state of the material before the turn-on of the encoder is unknown and the domains are therefore initialized to an arbitrary state. The encoder must move far enough to align the domains in the right position before the model converges to the right value. We suggest moving a sensor for at least one magnetic period after the start-up before the first use. However, a similar movement is required for all quasi-absolute positioning systems with an incremental scale and a reference mark, which must be reached to set the zero position. Because xMR sensors are often used in such systems, the compensating algorithm may be set up during the scan for the reference.
The algorithm does not include any temperature compensation. The hysteresis effect in general depends on the temperature because it affects the movement of the magnetic domains in the material. However, in applications that need the accuracy in the range of noticeable hysteresis, the temperature must be kept stable due to thermal expansions. Therefore, there is no need for additional temperature monitoring.
The proposed Lagging-domain model is designed for the encoders where the sensing distance does not change after the installation and calibration. In the future work, the extension for variable magnitude of magnetic field may be considered. The hysteresis increases significantly with the sensing distance. The solution may lie with the concurrent adaptation of parameters ϕ.
The computation time of the described algorithm is within the acceptable range. However, the speed of the algorithm could be further improved. The number of operations is proportional to the number of domains. Shorter computation time could be achieved by optimizing the code or by using fewer domains.

Conclusions
An algorithm for the compensation of hysteresis error for the linear and rotary encoders using the Lagging-domain model is proposed. The play operators were used for modeling the magnetoresistive sensors in a positioning application for the first time. The tests have shown that the Lagging-domain algorithm reduces the hysteresis of AMR encoders by up to 90%. The implementation into an embedded system with a microcontroller requires no additional resources. The duty cycle of the encoder was extended for approximately 5 µs. The calibration of the system can be performed within a microcontroller or externally, as long as the required moving sequence can be performed.

Conflicts of Interest:
The authors declare no conflict of interest.

Appendix A
Full experimental data is provided. All experiments on AMR sensor samples are described in Table A1 and all on TMR samples in Table A2. Experiments were conducted for various starting HME values, which depend mostly on the sensing distance and the sensor quality.