35static constexpr uint8_t QMC6310U_SLAVE_ADDRESS = 0x1C;
36static constexpr uint8_t QMC6310N_SLAVE_ADDRESS = 0x3C;
37static constexpr uint8_t QMC5883P_SLAVE_ADDRESS = 0x2C;
78 uint8_t buffer[6] = {0};
79 int16_t
x = 0,
y = 0, z = 0;
84 int status =
readReg(REG_0x09_STAT);
109 x = (int16_t)(buffer[1] << 8) | (buffer[0]);
110 y = (int16_t)(buffer[3] << 8) | (buffer[2]);
111 z = (int16_t)(buffer[5] << 8) | (buffer[4]);
161 writeReg(REG_0x0B_CMD2, (uint8_t)0x80);
163 writeReg(REG_0x0B_CMD2, (uint8_t)0x00);
175 int16_t x_result = 0, y_result = 0, z_result = 0;
176 return selfTest(x_result, y_result, z_result);
189 bool selfTest(int16_t &x_result, int16_t &y_result, int16_t &z_result)
203 int16_t x_pre = data.
raw.
x;
204 int16_t y_pre = data.
raw.
y;
205 int16_t z_pre = data.
raw.
z;
214 x_result = data.
raw.
x - x_pre;
215 y_result = data.
raw.
y - y_pre;
216 z_result = data.
raw.
z - z_pre;
233 float full_scale = 0;
234 float sensitivity = 0.0f;
235 uint8_t range_value = 0;
238 sensitivity = 0.001f;
239 range_value = 0x00 << 2;
243 sensitivity = 0.0004f;
244 range_value = 0x01 << 2;
248 sensitivity = 0.00026667f;
249 range_value = 0x02 << 2;
253 sensitivity = 0.00006667f;
254 range_value = 0x03 << 2;
262 if (
updateBits(REG_0x0B_CMD2, 0x0C, range_value) < 0) {
279 int rangeInt =
static_cast<int>(odr * 100 + 0.5);
281 uint8_t regValue = 0;
284 regValue = 0x00 << 2;
287 regValue = 0x01 << 2;
290 regValue = 0x02 << 2;
293 regValue = 0x03 << 2;
299 if (
updateBits(REG_0x0A_CMD1, 0x0C, regValue) < 0) {
316 uint8_t mode_val = 0;
334 if (
updateBits(REG_0x0A_CMD1, 0x03, mode_val) < 0) {
373 return updateBits(REG_0x0A_CMD1, 0x30, osr_val) == 0;
403 return updateBits(REG_0x0A_CMD1, 0xC0, dsr_val) == 0;
449 static constexpr uint8_t REG_0x00_CHIP_ID = 0x00;
450 static constexpr uint8_t REG_0x01_LSB_DX = 0x01;
451 static constexpr uint8_t REG_0x02_MSB_DX = 0x02;
452 static constexpr uint8_t REG_0x03_LSB_DY = 0x03;
453 static constexpr uint8_t REG_0x04_MSB_DY = 0x04;
454 static constexpr uint8_t REG_0x05_LSB_DZ = 0x05;
455 static constexpr uint8_t REG_0x06_MSB_DZ = 0x06;
456 static constexpr uint8_t REG_0x09_STAT = 0x09;
457 static constexpr uint8_t REG_0x0A_CMD1 = 0x0A;
458 static constexpr uint8_t REG_0x0B_CMD2 = 0x0B;
459 static constexpr uint8_t REG_0x29_SIGN = 0x29;
461 static constexpr uint8_t QMC6310_CHIP_ID = 0x80;
462 static constexpr uint8_t QMC5883P_CHIP_ID = 0x80;
463 static constexpr uint8_t QMC6309_CHIP_ID = 0x90;
467 bool initImpl(uint8_t param)
override
481 case QMC6310U_SLAVE_ADDRESS:
484 _chipId = QMC6310_CHIP_ID;
486 case QMC6310N_SLAVE_ADDRESS:
489 _chipId = QMC6310_CHIP_ID;
491 case QMC5883P_SLAVE_ADDRESS:
494 _chipId = QMC5883P_CHIP_ID;
@ MAGNETOMETER
Magnetometer sensor.
OperationMode
Enumeration of sensor operation modes.
MagFullScaleRange
Enumeration defining full-scale range settings for the sensor.
MagDownSampleRatio
Enumeration defining down-sample ratios for the sensor.
@ DSR_8
8x down-sample ratio
@ DSR_2
2x down-sample ratio
@ DSR_4
4x down-sample ratio
@ DSR_1
1x down-sample ratio
MagOverSampleRatio
Enumeration defining over-sample ratios for the sensor.
@ OSR_8
8x over-sample ratio
@ OSR_4
4x over-sample ratio
@ OSR_2
2x over-sample ratio
@ OSR_1
1x over-sample ratio
#define SENSORLIB_LOG_W(...)
#define SENSORLIB_LOG_E(...)
#define SENSORLIB_LOG_D(...)
std::unique_ptr< SensorHal > hal
uint16_t _oversampling_rate
SensorConfig _config
Current configuration.
SensorInfo _info
Sensor information.
bool clrRegBit(uint8_t reg, uint8_t bit)
bool getRegBit(uint8_t reg, uint8_t bit)
int readReg(uint8_t reg) const
int writeReg(uint8_t reg, uint8_t val)
int readRegBuff(uint8_t reg, uint8_t *buf, size_t len) const
int updateBits(uint8_t reg, uint8_t mask, uint8_t value_shifted)
bool setRegBit(uint8_t reg, uint8_t bit)
bool selfTest(int16_t &x_result, int16_t &y_result, int16_t &z_result)
Performs a self-test on the sensor.
bool setOutputDataRate(float odr) override
Sets the output data rate for the magnetometer.
uint8_t getChipID() const
Gets the chip ID of the magnetometer.
bool setFullScaleRange(MagFullScaleRange range) override
Sets the full-scale range of the magnetometer.
bool selfTest() override
Checks if the sensor is functioning correctly.
ChipType
Enumeration defining the types of supported magnetic sensor chips.
@ CHIP_UNKNOWN
Represents an unknown or unsupported chip type.
@ CHIP_QMC5883P
Represents the QMC5883P chip type.
@ CHIP_QMC6310N
Represents the QMC6310N chip type.
@ CHIP_QMC6310U
Represents the QMC6310U chip type.
bool setOversamplingRate(MagOverSampleRatio osr) override
Sets the oversampling rate of the magnetometer.
bool isDataReady() override
Checks if new data is available from the sensor.
bool isDataOverflow()
Checks if data overflow has occurred.
bool setDownsamplingRate(MagDownSampleRatio dsr) override
Sets the downsampling rate of the magnetometer.
bool setOperationMode(OperationMode mode) override
Sets the operation mode of the magnetometer.
~SensorQSTMagnetic()=default
Destructor.
bool configMagnetometer(OperationMode mode, MagFullScaleRange range, float odr, MagOverSampleRatio osr, MagDownSampleRatio dsr) override
Configures the magnetometer with multiple parameters.
bool reset() override
Resets the sensor to its default state.
SensorQSTMagnetic()
Default constructor.
bool readData(MagnetometerData &data) override
Reads the magnetic field data from the sensor.
float calculateHeading(const MagnetometerData &data, float declination=0.0f)
Calculate heading (yaw) angle from magnetometer data in radians.
constexpr bool _isBitSet(T value, uint8_t bit)
Structure representing magnetometer data.
float heading_degrees
heading angle (degrees)
float heading
heading angle (radians)
bool skip_data
skip data flag
SensorVector magnetic_field
magnetic field strength (Gauss)
bool overflow
data overflow flag
int16_t x
X-axis raw value.
int16_t y
Y-axis raw value.
int16_t z
Z-axis raw value.
uint32_t latency
Reporting latency in milliseconds.
float sample_rate
Sample rate in Hz.
float range
Measurement range.
SensorType type
Type of the sensor.
OperationMode mode
Operation mode.
uint8_t version
Hardware/firmware version.
uint8_t i2c_address
Default I2C address.
uint32_t uid
Unique identifier.
SensorType type
Sensor type.
const char * manufacturer
Manufacturer name.
const char * model
Model name/identifier.