fusion_hat.modules.compass module

class fusion_hat.modules.compass.Compass(placement=['x', 'y', 'z'], offset=[0, 0, 0, 0, 0, 0], declination="0°0'W", field_range='8G')[source]

Bases: Magnetometer

Compass module for reading and calculating heading angles from magnetometer data.

This class extends the Magnetometer class to provide compass functionality that can be easily integrated into projects requiring heading information.

Public Methods:

read_raw(): Get raw magnetic field data read(): Get processed compass data including heading angle read_angle(): Get just the heading angle (with optional filtering) set_offset(): Calibrate the compass with offset values set_magnetic_declination(): Adjust for local magnetic declination clear_calibration(): Reset all calibration data

FILTER_SIZE = 30
read_raw()[source]

Read raw magnetometer data.

Returns:

Raw magnetic field data [x, y, z] in mGauss.

Return type:

list

angle_str_2_number(str)[source]

Convert magnetic declination from string format to numerical value.

Parameters:

str (str) – Magnetic declination in format “DD°MM’E” or “DD°MM’W”.

Returns:

Magnetic declination in degrees (positive for East, negative for West).

Return type:

float

angle_number_2_str(number)[source]

Convert magnetic declination from numerical value to string format.

Parameters:

number (float) – Magnetic declination in degrees (positive for East, negative for West).

Returns:

Magnetic declination in format “DD°MM’E” or “DD°MM’W”.

Return type:

str

read()[source]

Read processed compass data.

Returns:

Processed magnetic field data (x, y, z, angle) where:

x, y, z are in mGauss after applying offsets, angle is the heading angle in degrees.

Return type:

tuple

read_angle(filter=False)[source]

Read the current heading angle.

Parameters:

filter (bool, optional) – Whether to apply moving average filter. Defaults to False.

Returns:

Heading angle in degrees (0-360).

Return type:

float

set_offset(offset)[source]

Set calibration offsets for the magnetometer.

Parameters:

offset (list) – Calibration offsets in mGauss. Format: [x_min, x_max, y_min, y_max, z_min, z_max].

Reference:

https://www.appelsiini.net/2018/calibrate-magnetometer/

set_magnetic_declination(angle)[source]

Set magnetic declination.

Parameters:

angle (str or float or int) – Magnetic declination. Can be either: - String format: “DD°MM’E” or “DD°MM’W” - Numeric format: Degrees (positive for East, negative for West)

clear_calibration()[source]

Clear all calibration data.

Resets all minimum/maximum values and offsets to zero.