en:lesson6

# Lesson 06. Magnetometer Calibration

## Raw Data Acquisition for Magnetometer Calibration

By default the magnetometer is not calibrated so it gives inaccurate readings. The adjusted values may be acquired from raw data by calibration that is the defining of the transformation matrix and vector of translation. The raw data may be acquired from magnetometer by running the Python code listed below. During the raw data collection the magnetometer must be chaotically rotated endeavoring to put it in all possible positions.

Python code.

raw_data.py
def control(): #Program’s main function
mgn_result = [0,0,0,0] 		#Initialize mgn_result
num = 1
magnetometer_turn_on(num) 	#Turn magnetometer on
sleep(1)
for i in range(500):		#Perform 500 readings
mgn_result = magnetometer_request_raw(num)
if not mgn_result[0]: #If sensor has not returned error message
print mgn_result[1], mgn_result[2], mgn_result[3]
sleep(0.05)			#Delay five-hundredths of the second
magnetometer_turn_off(num)	#Turn magnetometer off

С code.

raw_data.c
#include <stdio.h>
#include <stdint.h>
#include "libschsat.h"
#define LSS_OK 0
#define LSS_ERROR 1
#define LSS_BREAK 2

void control(void){ //Program’s main function
int16_t mgn_result[] = {0, 0, 0, 0}; //Initialize mgn_result
uint16_t num = 1;
magnetometer_turn_on(num); //Turn magnetometer on
Sleep(1);
int i;
for (i = 0; i < 1000; i++) //Perform 500 readings
{
mgn_result[0] = magnetometer_request_raw(num, &mgn_result[1],&mgn_result[2],&mgn_result[3]);
if(!mgn_result[0]){ //If sensor has not returned error message
printf("%d, %d, %d\n", mgn_result[1], mgn_result[2], mgn_result[3]);
}
Sleep(0.5); //Delay five-hundredths of the second
}
magnetometer_turn_off(num);
}

Connect Host Onboard Computer with Power System and magnetometer (as in previous lesson). Do not fix the magnetometer to the board to make possible the moving of the magnetometer. Download the program to OrbiCraft and run the program. Chaotically rotate the magnetometer to collect raw data from all possible magnetometer’s positions.

Save the acquired data to the file in .txt format. To do so, select all results with Ctrl+A shortcut, copy with Ctrl+C shortcut and save.

## Program performance analysis

To work with magnetometer the program uses such functions:

magnetometer_turn_on(num)

– magnetometer switching’on function, where num is the number of the magnetometer.

magnetometer_request_raw(num)

– the function that returns raw data measured with magnetometer with num number presented as list with 4 digit values.

So the read values will be put into mgn_result list, including 4 values.

mgn_result = [0,0,0,0]
mgn_result = magnetometer_request_raw(num)

The first value of the list returns error message. If the value returned is 0, there is no error; if it is 1, then sensor is not connected; if it is 2, it is the program error.

The program uses cycle statement for i in range(1000) which will be performed 1000 times; so there will be output 1000 values.

These values will be required for the next lesson for magnetometer calibration.

## Acquired Data Analysis with Excel

Open the resulting data file with Excel. Press Ctrl-O and chose the folder containing the file. By default the Excel will not open txt form files, but when you enter * at the enter form and press Enter there will be displayed all files. Choose the data file and press Open.

This will start the import wizard. Press Next. On the second page specify that column divider is space character.

Make sure that in window with example of the data presentation all columns are divided by vertical lines, then press Next.

Column’s data form must be leaved as “general”. Press “Finished”, the data will be written into Excel sheet.

Now there required to plot the diagram, to detect and delete values that were measured with severe errors. These values will stand out at the diagram. Click at the column header to select the whole column.

Click Insert and choose the Point Diagram.

There will be plotted the Point Diagram that clearly shows the points that are standing out.

Put the cursor at the point to see the value.

Find the line with this value in the table, delete the whole line. In similar way delete the erroneous points from second and third columns.

Save the file in .txt form. Answer Yes when asked if you want to save the file as text.

Note where the file is saved; the file will be used at the magnetometer calibration’s next stage.

## Defining the calibration factors

Run the Magneto software.

Enter into the window marked “1” the value “700”, then press Open button and choose the file with raw data cleared of errors. Then press Calibrate button. The Magneto software will calculate and display the modifying factors. At a later stage there will be needed not all factors calculated by Magneto software but three Combined bias (b) values and nine Correction for combined scale factors values only.

## Calibrated magnetometer test

Download to OrbiCraft the program that will perform 60 measurement reading with 1 sec step. Use the print-out of the angle protractor and magnetometer with ruler. Factors for def mag_calibrated function must be taken from Magneto software.

Copy the rounded-off values such way:

Python code.

calibr.py
import math

time_step = 1		# Measurement time step
mag_num = 1		# Magnetometer number

# Функция mag_calibrated function will adjust the magnetometer’s readings considering the calibration factors
def mag_calibrated(magx,magy,magz):
magx_cal = 1.06*(magx + -7.49) + -0.01*(magy + -23.59) + 0.07*(magz + -108.24)
magy_cal = -0.01*(magx + -7.49) + 1.11*(magy + -23.59) + 0.09*(magz + -108.24)
magz_cal = 0.07*(magx + -7.49) + 0.09*(magy + -23.59) + 1.00*(magz + -108.24)
return magx_cal, magy_cal, magz_cal

def initialize_all():
print "Enable magnetometer", mag_num
magnetometer_turn_on(mag_num)
sleep(1)

def switch_off_all():
print "Disable magnetometer", mag_num
magnetometer_turn_off(mag_num)

def control(): # Program’s main function which is calling all other functions main function
initialize_all()
mag_state = 0 		# Initialize magnetometer’s status
alpha_goal = 0		# Target angle
omega_goal = 0 		# Target angular velocity

for i in range(60):
# magnetometer data request
mag_state, magx_raw, magy_raw, magz_raw = magnetometer_request_raw(mag_num)

if not mag_state: # if magnetometer returned error code 0 (no error)
magx_cal, magy_cal, magz_cal = mag_calibrated(magx_raw,magy_raw,magz_raw)
mag_alpha = math.atan2(magy_cal, magx_cal)/math.pi*180
print "mag_alpha atan2= ", mag_alpha

elif mag_state == 1:
print "Fail because of access error, check the connection"

elif mag_state == 2:
print "Fail because of interface error, check your code"

sleep(time_step)

switch_off_all()

С code.

calibr.c
#include <stdio.h>
#include <stdint.h>
#include "libschsat.h"
#define LSS_OK 0
#define LSS_ERROR 1
#define LSS_BREAK 2

#include <math.h>
const int time_step = 1;
const uint16_t mag_num = 1;

int mag_calibrated(int16_t *magx, int16_t *magy, int16_t *magz ){

float magx_cal;
magx_cal = 1.06*(*magx + -7.49) + -0.01*(*magy + -23.59) + 0.07*(*magz + -108.24);
float magy_cal;
magy_cal = -0.01*(*magx + -7.49) + 1.11*(*magy + -23.59) + 0.09*(*magz + -108.24);
float magz_cal;
magz_cal = 0.07*(*magx + -7.49) + 0.09*(*magy + -23.59) + 1.00*(*magz + -108.24);
*magx = (int16_t) magx_cal;
*magy = (int16_t) magy_cal;
*magz = (int16_t) magz_cal;

return 0;
}

void initialize_all(){
printf("Enable magnetometer %d\n", mag_num);
magnetometer_turn_on(mag_num);
Sleep(1);
}

void switch_off_all(){
printf("Disable magnetometer %d\n", mag_num);
magnetometer_turn_off(mag_num);
}

int control(){
initialize_all();
int mag_state = 0;
int16_t mgx_cal=0;
int16_t mgy_cal=0;
int16_t mgz_cal=0;
int16_t *magx_cal = &mgx_cal;
int16_t *magy_cal = &mgy_cal;
int16_t *magz_cal = &mgz_cal;
int i;

for ( i= 0; i < 60; i++){
mag_state = magnetometer_request_raw(mag_num, magx_cal,magy_cal,magz_cal);
float mag_alpha;
if (!mag_state){
mag_calibrated(magx_cal,magy_cal,magz_cal);
mag_alpha = atan2(mgy_cal, mgx_cal)/ M_PI *180;
printf("mag_alpha atan2 = %f\n", mag_alpha);
}
else if (mag_state == 1){
printf("Fail because of access error, check the connection");
}
else if (mag_state == 2){
printf("Fail because of interface error, check your code");
}
Sleep(time_step);
}
switch_off_all();
return 0;
}

Turn the magnetometer with 30 step, record the readings for the angle values to EXCEL sheet. There will be the table something like this.

## Data Analysis with Excel

Analyze the acquired data as in previous lesson.

Average value for the remainder of the angles measured is 30.

Standard deviation of the calculated remainders from average value is 1.3.

Percentage ratio for average value and standard deviation is 4%.

The percentage ratio measured in the previous lesson was 17%; therefor, the calibrated magnetometer will measure 4 times more accurately.