fusion_hat.modules.magnetometer module

class fusion_hat.modules.magnetometer.MagnetometerType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

mag_QMC6310 = 1
mag_QMC5883P = 2
mag_QMC5883L = 3
mag_HMC5883L = 4
fusion_hat.modules.magnetometer.convert_2_int16(value)[source]
fusion_hat.modules.magnetometer.i2c_ack(bus, addr)[source]

Check if a device responds at the specified I2C address

Parameters:
  • bus – SMBus instance

  • addr – I2C address to check

Returns:

True if the device responds, False otherwise

Return type:

bool

class fusion_hat.modules.magnetometer.QMC6310(bus: smbus2.SMBus, addr: int = 28, field_range='8G')[source]

Bases: object

DEF_ADDR = 28

# Define register address of QMC6310 compass sensor

QMC6310_REG_DATA_START = 1
QMC6310_REG_DTAT_X = 1
QMC6310_REG_DTAT_Y = 3
QMC6310_REG_DTAT_Z = 5
QMC6310_REG_STATUS = 9
QMC6310_REG_CONTROL_1 = 10
QMC6310_REG_CONTROL_2 = 11

# Define the register parameter configuration of the sensor

QMC6310_VAL_MODE_SUSPEND = 0
QMC6310_VAL_MODE_NORMAL = 1
QMC6310_VAL_MODE_SINGLE = 2
QMC6310_VAL_MODE_CONTINUOUS = 3
QMC6310_VAL_ODR_10HZ = 0
QMC6310_VAL_ODR_50HZ = 4
QMC6310_VAL_ODR_100HZ = 8
QMC6310_VAL_ODR_200HZ = 12
QMC6310_VAL_OSR1_8 = 0
QMC6310_VAL_OSR1_4 = 16
QMC6310_VAL_OSR1_2 = 32
QMC6310_VAL_OSR1_1 = 48
QMC6310_VAL_OSR2_1 = 0
QMC6310_VAL_OSR2_2 = 64
QMC6310_VAL_OSR2_4 = 128
QMC6310_VAL_OSR2_8 = 192
QMC6310_VAL_MODE_SET_RESET_ON = 0
QMC6310_VAL_MODE_SET_ON = 1
QMC6310_VAL_MODE_SET_RESET_OFF = 2
RANGE = {'12G': 4, '2G': 12, '30G': 0, '8G': 8}
LSB = {'12G': 2.5, '2G': 15, '30G': 1, '8G': 3.75}
QMC6310_VAL_SELF_TEST_ON = 64
QMC6310_VAL_SELF_TEST_OFF = 0
QMC6310_VAL_SOFT_RST_ON = 128
QMC6310_VAL_SOFT_RST_OFF = 0
read_raw()[source]

Read raw magnetometer data

Returns:

[x, y, z] raw magnetic field values

Return type:

list

read_magnet()[source]

Read magnetometer data in Gauss units (consistent with other magnetometer classes)

Returns:

(x, y, z) magnetic field values in Gauss

Return type:

tuple

class fusion_hat.modules.magnetometer.HMC5883L(bus: smbus2.SMBus, addr: int = 30)[source]

Bases: object

HMC5883L 3-axis magnetometer sensor driver class Used to measure Earth’s magnetic field strength, suitable for direction detection and heading angle calculation

DEF_ADDR = 30
REG_CONFIG_A = 0
REG_CONFIG_B = 1
REG_MODE = 2
REG_OUT_X_H = 3
_read_word(reg_h)[source]

Read a signed 16-bit integer from the sensor

Parameters:

reg_h – high-byte register address

Returns:

converted signed 16-bit integer

Return type:

val

read_magnet()[source]

Read magnetometer data

Returns:

X, Y, Z axis magnetic field strength values (unit: Gauss) Note: HMC5883L data register order is X, Z, Y

Return type:

x, y, z

class fusion_hat.modules.magnetometer.QMC5883L(bus: smbus2.SMBus, addr: int = 13)[source]

Bases: object

QMC5883L 3-axis magnetometer sensor driver class Used to measure Earth’s magnetic field strength, is a common alternative to HMC5883L

DEF_ADDR = 13
REG_CONTROL = 9
REG_SET_RESET = 11
REG_OUT_X_L = 0
_read_word_le(reg_l)[source]

Read a signed 16-bit integer from the sensor in little-endian format Note: QMC5883L uses little-endian format to store data

Parameters:

reg_l – Low byte register address

Returns:

Converted signed 16-bit integer

Return type:

val

read_magnet()[source]

Read magnetometer data

Returns:

X, Y, Z axis magnetic field strength values (unit: Gauss) Note: QMC5883L data register order is X, Y, Z (different from HMC5883L)

Return type:

x, y, z

class fusion_hat.modules.magnetometer.QMC5883P(bus: smbus2.SMBus, addr: int = 44)[source]

Bases: object

QMC5883P 3-axis magnetometer sensor driver class Another version of the QMC5883 series with I2C address 0x2C

DEF_ADDR = 44
REG_X_LSB = 1
REG_STATUS = 9
REG_MODE = 10
REG_CONFIG = 11
static _to_i16(lsb, msb)[source]

Convert two bytes to a 16-bit signed integer

Parameters:
  • lsb – Low byte value

  • msb – High byte value

Returns:

Converted 16-bit signed integer

Return type:

v

read_magnet()[source]

Read magnetometer data Reads 6 bytes starting from 0x01 register (little-endian signed format) Features: Returns zero on error, does not interrupt main loop

Returns:

X, Y, Z axis magnetic field strength values (unit: Gauss)

Return type:

x, y, z

class fusion_hat.modules.magnetometer.Magnetometer(mag_type=None, field_range='8G')[source]

Bases: object

Magnetometer sensor driver class Integrates QMC6310, HMC5883L, QMC5883L, and QMC5883P magnetometers

read()[source]

Read magnetometer data

Returns:

(x, y, z) magnetic field values in appropriate units based on sensor type None: if no magnetometer is initialized

Return type:

tuple

get_type()[source]

Get the type of initialized magnetometer

Returns:

Magnetometer type

Return type:

str