2025-02-26
7:43 AM
- last edited on
2025-02-26
8:08 AM
by
Lina_DABASINSKA
Hello everyone,
I'm contacting you today in order to understand my error during the calibration of my LSM303AGR sensor located on a PCB. The ultimate goal is to create a compass with tilt compensation but I try to get a good precision with magnetometer first. Of course, I took care to read up on the Internet before opening this post, but all the ideas seem to be divergent and I can't find my way around.
First, I got the raw acceleration and magnitude data from my sensor. So far, so good. As part of my calibration, I generated a data set during which I rotate the sensor in all possible directions to cover all angles. I then measured the hard iron to eliminate offsets on each axis :
def calculate_hard_iron(data):
max_vals = np.max(data, axis=0)
min_vals = np.min(data, axis=0)
return (max_vals + min_vals) / 2
Then I calculated the soft iron by normalizing my data while preserving the centering :
def calculate_soft_iron(data):
# Centering data
mean = np.mean(data, axis=0)
centered_data = data - mean
# Find the scaling factors for each axis
ranges = np.ptp(centered_data, axis=0) # peak to peak range
scale_factors = np.max(ranges) / ranges
# Normalize
corrected_data = centered_data * scale_factors
avg_radius = np.mean(np.sqrt(np.sum(corrected_data**2, axis=1)))
scale = 1.0 / avg_radius
# Apply final scaling
corrected_data = corrected_data * scale
transformation_matrix = np.diag(scale_factors * scale)
return corrected_data, transformation_matrix
I arrive at the following resultt :
Following this, I am able to identify my hard iron vector and my soft iron matrix :
Hard Iron Offset: [-368.0, 390.5, 185.5]
Soft Iron Matrix:
[[0.0020697, 0, 0]
[0, 0.00216311, 0]
[0, 0, 0.00205197]]
Apriori, I should be able to interpret a movement using the parameters calculated in the calibration step. So I decide to generate a dataset in which I perform a 360° turn on XY plan, and here is the result :
What stands out is that the offset problem in the data set subsequently caused a loss of information. However, I did deduce the data from the offset measured during the calibration phase. What did I do wrong ?
Thank you for your attention.