SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorQSTMagnetic Class Reference

#include <SensorQSTMagnetic.hpp>

Inheritance diagram for SensorQSTMagnetic:
[legend]

Public Types

enum  ChipType { CHIP_QMC6310U , CHIP_QMC6310N , CHIP_QMC5883P , CHIP_UNKNOWN }
 Enumeration defining the types of supported magnetic sensor chips. More...
 

Public Member Functions

 SensorQSTMagnetic ()
 Default constructor.
 
 ~SensorQSTMagnetic ()=default
 Destructor.
 
bool readData (MagnetometerData &data) override
 Reads the magnetic field data from the sensor.
 
bool isDataReady () override
 Checks if new data is available from the sensor.
 
bool isDataOverflow ()
 Checks if data overflow has occurred.
 
bool reset () override
 Resets the sensor to its default state.
 
bool selfTest () override
 Checks if the sensor is functioning correctly.
 
bool selfTest (int16_t &x_result, int16_t &y_result, int16_t &z_result)
 Performs a self-test on the sensor.
 
bool setFullScaleRange (MagFullScaleRange range) override
 Sets the full-scale range of the magnetometer.
 
bool setOutputDataRate (float odr) override
 Sets the output data rate for the magnetometer.
 
bool setOperationMode (OperationMode mode) override
 Sets the operation mode of the magnetometer.
 
bool setOversamplingRate (MagOverSampleRatio osr) override
 Sets the oversampling rate of the magnetometer.
 
bool setDownsamplingRate (MagDownSampleRatio dsr) override
 Sets the downsampling rate of the magnetometer.
 
uint8_t getChipID () const
 Gets the chip ID of the magnetometer.
 
bool configMagnetometer (OperationMode mode, MagFullScaleRange range, float odr, MagOverSampleRatio osr, MagDownSampleRatio dsr) override
 Configures the magnetometer with multiple parameters.
 
- Public Member Functions inherited from MagnetometerBase
 MagnetometerBase ()
 Constructor for the MagnetometerBase class.
 
virtual ~MagnetometerBase ()=default
 Destructor for the MagnetometerBase class.
 
virtual float getFullScaleRange () const
 Gets the full-scale range for the magnetometer.
 
virtual OperationMode getOperationMode () const
 Gets the operation mode for the magnetometer.
 
virtual uint16_t getOversamplingRate () const
 Gets the oversampling rate for the magnetometer.
 
virtual void setDeclination (float declination_deg)
 Set the magnetic declination correction value.
 
virtual float getDeclinationDeg () const
 Get the currently set magnetic declination in degrees.
 
virtual float getDeclinationRad () const
 Get the currently set magnetic declination in radians.
 
- Public Member Functions inherited from SensorBase< MagnetometerData >
 SensorBase (const SensorBase &)=delete
 
SensorBaseoperator= (const SensorBase &)=delete
 
void setOffset (int16_t x, int16_t y, int16_t z)
 Set sensor calibration offsets.
 
float getSensitivity () const
 Get sensor sensitivity.
 
SensorInfo getSensorInfo () const
 Get sensor information.
 
virtual SensorConfig getConfig () const
 Get current sensor configuration.
 
- Public Member Functions inherited from I2CDeviceWithHal
bool begin (SensorCommCustom::CustomCallback cb, SensorCommCustomHal::CustomHalCallback hal_cb, uint8_t addr)
 Initialize the sensor using custom callback interface.
 
- Public Member Functions inherited from DeviceBeginCommon
virtual ~DeviceBeginCommon ()
 
uint8_t getAddress () const
 
uint8_t getInterface () const
 
- Public Member Functions inherited from SensorCommWrapper
 SensorCommWrapper ()=default
 
int readReg (uint8_t reg) const
 
int readRegBuff (uint8_t reg, uint8_t *buf, size_t len) const
 
int readBuff (uint8_t *buf, size_t len) const
 
int writeReg (uint8_t reg, uint8_t val)
 
int writeRegBuff (uint8_t reg, uint8_t *buf, size_t len)
 
int writeBuff (uint8_t *buf, size_t len)
 
int writeThenRead (const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len)
 
bool setRegBit (uint8_t reg, uint8_t bit)
 
bool clrRegBit (uint8_t reg, uint8_t bit)
 
bool getRegBit (uint8_t reg, uint8_t bit)
 
int updateBits (uint8_t reg, uint8_t mask, uint8_t value_shifted)
 
void setAddress (uint8_t address)
 
void setAck (bool enable)
 
- Public Member Functions inherited from SensorCommInterface
virtual ~SensorCommInterface ()=default
 

Additional Inherited Members

- Protected Member Functions inherited from SensorBase< MagnetometerData >
 SensorBase (SensorType sensor_type=SensorType::UNKNOWN)
 Construct a new SensorBase object.
 
virtual ~SensorBase ()=default
 Virtual destructor for proper polymorphic cleanup.
 
- Protected Member Functions inherited from I2CDeviceWithHal
bool ensureValid () const override
 Ensure the communication interface is valid.
 
- Protected Member Functions inherited from DeviceBeginCommon
 DeviceBeginCommon ()=default
 
virtual SensorCommBasegetComm () const override
 
virtual void beforeBegin ()
 
virtual void afterCommReady ()
 
virtual void afterInitSuccess (uint8_t param)
 
virtual void onBeginFail ()
 
bool fail ()
 
void clearCommState ()
 
template<typename CreateComm >
bool beginLifecycle (uint8_t addr, uint8_t iface, uint8_t initParam, CreateComm createComm)
 
- Protected Attributes inherited from MagnetometerBase
uint16_t _oversampling_rate
 
float _declination_rad
 
- Protected Attributes inherited from SensorBase< MagnetometerData >
SensorConfig _config
 Current configuration.
 
SensorInfo _info
 Sensor information.
 
int16_t _x_offset
 
int16_t _y_offset
 
int16_t _z_offset
 
float _sensitivity
 
- Protected Attributes inherited from DeviceBeginCommon
std::unique_ptr< SensorCommBasecomm
 
std::unique_ptr< SensorHalhal
 
std::unique_ptr< SensorCommStaticstaticComm
 
uint8_t _addr = 0
 
uint8_t _iface = COMM_CUSTOM
 

Detailed Description

Definition at line 39 of file SensorQSTMagnetic.hpp.

Member Enumeration Documentation

◆ ChipType

Enumeration defining the types of supported magnetic sensor chips.

Enumerator
CHIP_QMC6310U 

Represents the QMC6310U chip type.

CHIP_QMC6310N 

Represents the QMC6310N chip type.

CHIP_QMC5883P 

Represents the QMC5883P chip type.

CHIP_UNKNOWN 

Represents an unknown or unsupported chip type.

Definition at line 45 of file SensorQSTMagnetic.hpp.

Constructor & Destructor Documentation

◆ SensorQSTMagnetic()

SensorQSTMagnetic::SensorQSTMagnetic ( )
inline

Default constructor.

Initializes communication and hardware abstraction pointers to nullptr.

Creates a SensorQSTMagnetic object with no active communication or hardware abstraction instances.

Definition at line 58 of file SensorQSTMagnetic.hpp.

◆ ~SensorQSTMagnetic()

SensorQSTMagnetic::~SensorQSTMagnetic ( )
default

Destructor.

Deinitializes the communication object if it exists.

Ensures proper cleanup by deinitializing the communication interface when the sensor object is destroyed.

Member Function Documentation

◆ configMagnetometer()

bool SensorQSTMagnetic::configMagnetometer ( OperationMode  mode,
MagFullScaleRange  range,
float  odr,
MagOverSampleRatio  osr,
MagDownSampleRatio  dsr 
)
inlineoverridevirtual

Configures the magnetometer with multiple parameters.

Note
This function sets the operation mode, full-scale range, output data rate, oversampling ratio, and downsampling ratio of the magnetometer.
Parameters
modeSUSPEND, NORMAL, SINGLE_MEASUREMENT, CONTINUOUS_MEASUREMENT
rangeFS_2G, FS_8G, FS_12G, FS_30G
odrAllowed values are 10.0, 50.0, 100.0 and 200.0HZ.
osrOSR_8, OSR_4, OSR_2, OSR_1
dsrDSR_1, DSR_2, DSR_4, DSR_8
Return values
Trueif the configuration was successful, false otherwise.

Implements MagnetometerBase.

Definition at line 427 of file SensorQSTMagnetic.hpp.

References setDownsamplingRate(), setFullScaleRange(), setOperationMode(), setOutputDataRate(), and setOversamplingRate().

Referenced by setup().

◆ getChipID()

uint8_t SensorQSTMagnetic::getChipID ( ) const
inline

Gets the chip ID of the magnetometer.

Note
This function retrieves the chip ID from the sensor.
Return values
Thechip ID.

Definition at line 411 of file SensorQSTMagnetic.hpp.

References SensorBase< MagnetometerData >::_info, and SensorInfo::uid.

◆ isDataOverflow()

bool SensorQSTMagnetic::isDataOverflow ( )
inline

Checks if data overflow has occurred.

Note
This function reads the status register to determine if the magnetic field data has overflowed.
Return values
Trueif data overflow has occurred, false otherwise.

Definition at line 148 of file SensorQSTMagnetic.hpp.

References SensorCommWrapper::getRegBit().

◆ isDataReady()

bool SensorQSTMagnetic::isDataReady ( )
inlineoverridevirtual

Checks if new data is available from the sensor.

Note
This function reads the status register to determine if new magnetic field data is ready to be processed.
Return values
Trueif new data is available, false otherwise.

Implements MagnetometerBase.

Definition at line 137 of file SensorQSTMagnetic.hpp.

References SensorCommWrapper::getRegBit().

Referenced by calibrate().

◆ readData()

bool SensorQSTMagnetic::readData ( MagnetometerData data)
inlineoverridevirtual

Reads the magnetic field data from the sensor.

Note
This function retrieves the latest magnetic field measurements from the sensor and stores them in the provided data structure.
Parameters
dataA reference to a MagnetometerData structure to store the retrieved magnetic field data.
Return values
Trueif the data was read successfully, false otherwise.

Implements MagnetometerBase.

Definition at line 76 of file SensorQSTMagnetic.hpp.

References MagnetometerBase::_declination_rad, sensorlib::_isBitSet(), SensorBase< MagnetometerData >::_sensitivity, SensorBase< MagnetometerData >::_x_offset, SensorBase< MagnetometerData >::_y_offset, SensorBase< MagnetometerData >::_z_offset, MagnetometerUtils::calculateHeading(), MagnetometerData::heading, MagnetometerData::heading_degrees, M_PI, MagnetometerData::magnetic_field, MagnetometerData::overflow, MagnetometerData::raw, SensorCommWrapper::readReg(), SensorCommWrapper::readRegBuff(), SENSORLIB_LOG_E, SENSORLIB_LOG_W, MagnetometerData::skip_data, x, SensorVector::x, RawVector::x, y, SensorVector::y, RawVector::y, SensorVector::z, and RawVector::z.

Referenced by calibrate(), loop(), and selfTest().

◆ reset()

bool SensorQSTMagnetic::reset ( )
inlineoverridevirtual

Resets the sensor to its default state.

Note
This function sends a reset command to the sensor and waits for a short period to allow the reset to take effect.
Return values
Trueif the reset command was successful, false otherwise.

Implements MagnetometerBase.

Definition at line 159 of file SensorQSTMagnetic.hpp.

References DeviceBeginCommon::hal, and SensorCommWrapper::writeReg().

◆ selfTest() [1/2]

bool SensorQSTMagnetic::selfTest ( )
inlineoverridevirtual

Checks if the sensor is functioning correctly.

Note
This function performs a self-test by comparing the magnetic field readings before and after enabling self-test mode.
Return values
Trueif the self-test passed, false otherwise.

Implements MagnetometerBase.

Definition at line 173 of file SensorQSTMagnetic.hpp.

References selfTest().

Referenced by selfTest().

◆ selfTest() [2/2]

bool SensorQSTMagnetic::selfTest ( int16_t &  x_result,
int16_t &  y_result,
int16_t &  z_result 
)
inline

Performs a self-test on the sensor.

Note
This function compares the magnetic field readings before and after enabling self-test mode to determine if the sensor is functioning correctly.
Parameters
&x_resultReturn the difference between before and after self-check.
&y_resultReturn the difference between before and after self-check.
&z_resultReturn the difference between before and after self-check.
Return values
Trueif the self-test passed, false otherwise.

Definition at line 189 of file SensorQSTMagnetic.hpp.

References SensorCommWrapper::clrRegBit(), CONTINUOUS_MEASUREMENT, DeviceBeginCommon::hal, MagnetometerData::raw, readData(), SENSORLIB_LOG_E, setOperationMode(), SensorCommWrapper::setRegBit(), SUSPEND, RawVector::x, RawVector::y, and RawVector::z.

◆ setDownsamplingRate()

bool SensorQSTMagnetic::setDownsamplingRate ( MagDownSampleRatio  dsr)
inlineoverridevirtual

Sets the downsampling rate of the magnetometer.

Note
This function configures the magnetometer's downsampling rate, affecting the trade-off between noise and measurement speed.
Parameters
dsrDSR_1, DSR_2, DSR_4, DSR_8
Return values
Trueif the downsampling rate was set successfully, false otherwise.

Implements MagnetometerBase.

Definition at line 383 of file SensorQSTMagnetic.hpp.

References DSR_1, DSR_2, DSR_4, DSR_8, SENSORLIB_LOG_E, and SensorCommWrapper::updateBits().

Referenced by configMagnetometer().

◆ setFullScaleRange()

bool SensorQSTMagnetic::setFullScaleRange ( MagFullScaleRange  range)
inlineoverridevirtual

Sets the full-scale range of the magnetometer.

Note
This function configures the magnetometer to operate within a specific range, affecting the sensitivity and maximum measurable magnetic field strength.
Parameters
rangeFS_2G, FS_8G, FS_12G, FS_30G
Return values
Trueif the range was set successfully, false otherwise.

Implements MagnetometerBase.

Definition at line 231 of file SensorQSTMagnetic.hpp.

References SensorBase< MagnetometerData >::_config, SensorBase< MagnetometerData >::_sensitivity, FS_12G, FS_2G, FS_30G, FS_8G, SensorConfig::range, SENSORLIB_LOG_E, and SensorCommWrapper::updateBits().

Referenced by configMagnetometer().

◆ setOperationMode()

bool SensorQSTMagnetic::setOperationMode ( OperationMode  mode)
inlineoverridevirtual

Sets the operation mode of the magnetometer.

Note
This function configures the magnetometer to operate in a specific mode, affecting its power consumption and measurement behavior.
Parameters
modeSUSPEND, NORMAL, SINGLE_MEASUREMENT, CONTINUOUS_MEASUREMENT
Return values
Trueif the mode was set successfully, false otherwise.

Implements MagnetometerBase.

Definition at line 314 of file SensorQSTMagnetic.hpp.

References SensorBase< MagnetometerData >::_config, CONTINUOUS_MEASUREMENT, SensorConfig::mode, NORMAL, SENSORLIB_LOG_E, SINGLE_MEASUREMENT, SUSPEND, and SensorCommWrapper::updateBits().

Referenced by configMagnetometer(), and selfTest().

◆ setOutputDataRate()

bool SensorQSTMagnetic::setOutputDataRate ( float  odr)
inlineoverridevirtual

Sets the output data rate for the magnetometer.

Note
This function should be called to configure the sensor's output data rate.
Parameters
odrThe desired output data rate in Hz. Allowed values are 10.0, 50.0, 100.0 and 200.0HZ.
Return values
Trueif the output data rate was set successfully, false otherwise.

Implements MagnetometerBase.

Definition at line 277 of file SensorQSTMagnetic.hpp.

References SensorBase< MagnetometerData >::_config, SensorConfig::sample_rate, SENSORLIB_LOG_D, SENSORLIB_LOG_E, and SensorCommWrapper::updateBits().

Referenced by calibrate(), and configMagnetometer().

◆ setOversamplingRate()

bool SensorQSTMagnetic::setOversamplingRate ( MagOverSampleRatio  osr)
inlineoverridevirtual

Sets the oversampling rate of the magnetometer.

Note
This function configures the magnetometer's oversampling rate, affecting the trade-off between noise and measurement speed.
Parameters
osrOSR_8, OSR_4, OSR_2, OSR_1
Return values
Trueif the oversampling rate was set successfully, false otherwise.

Implements MagnetometerBase.

Definition at line 349 of file SensorQSTMagnetic.hpp.

References MagnetometerBase::_oversampling_rate, OSR_1, OSR_2, OSR_4, OSR_8, SENSORLIB_LOG_E, and SensorCommWrapper::updateBits().

Referenced by configMagnetometer().


The documentation for this class was generated from the following file: