SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bmm150_get_data.ino
Go to the documentation of this file.
1
30#include <MagnetometerDrv.hpp>
31
32#ifndef SENSOR_SDA
33#define SENSOR_SDA 33
34#endif
35
36#ifndef SENSOR_SCL
37#define SENSOR_SCL 35
38#endif
39
40#ifndef SENSOR_IRQ
41#define SENSOR_IRQ -1
42#endif
43
44#ifndef SENSOR_RST
45#define SENSOR_RST -1
46#endif
47
48
50
51void setup()
52{
53 Serial.begin(115200);
54 while (!Serial);
55
56 // Using I2C interface
57 if (!magnetometer.begin(Wire, BMM150_I2C_ADDRESS_CSB_HIGH_SDO_LOW, SENSOR_SDA, SENSOR_SCL)) {
58 Serial.print("Failed to init BMM150 - check your wiring!");
59 while (1) {
60 delay(1000);
61 }
62 }
63
64 Serial.println("Init BMM150 Sensor success!");
65
66 // The desired output data rate in Hz. Allowed values are 2.0, 6.0, 8.0, 15.0, 20.0, 25.0 and 30.0HZ.
67 float data_rate_hz = 200.0f;
68 // op_mode: Allowed values are SUSPEND, NORMAL, SINGLE_MEASUREMENT, CONTINUOUS_MEASUREMENT
70 // full_scale: Allowed values are FS_16G
72 // over_sample_ratio: Allowed values are OSR_1, OSR_2, OSR_4, OSR_8
74
75 /* Config Magnetometer */
77 op_mode,
78 full_scale,
79 data_rate_hz,
80 over_sample_ratio)) {
81 Serial.println("Magnetometer configured successfully.");
82 } else {
83 Serial.println("Magnetometer configuration failed.");
84 while (1);
85 }
86
88 Serial.print("Manufacturer: "); Serial.println(info.manufacturer);
89 Serial.print("Model: "); Serial.println(info.model);
90 Serial.print("I2C Address: 0x"); Serial.println(info.i2c_address, HEX);
91 Serial.print("Version: "); Serial.println(info.version);
92 Serial.print("UID: 0x"); Serial.println(info.uid);
93 Serial.print("Type: "); Serial.println(SensorUtils::typeToString(info.type));
94
96 Serial.print("DataRate: "); Serial.println(cfg.sample_rate);
97 Serial.print("FullScaleRange: "); Serial.println(cfg.range);
98 Serial.print("Mode: "); Serial.println((uint8_t)cfg.mode);
99 Serial.println();
100
101 //Find the magnetic declination : https://www.magnetic-declination.com/
102 float declination_deg = MagnetometerUtils::dmsToDecimalDegrees(-3, 20); // -3.3333
103
104 magnetometer.setDeclination(declination_deg);
105
106 Serial.print(" Magnetic Declination: ");
107 Serial.print(declination_deg, 2);
108 Serial.println("°");
109
110 Serial.print(" Sensitivity: ");
111 Serial.print(magnetometer.getSensitivity(), 6);
112 Serial.println(" Gauss/LSB");
113
114 delay(3000);
115
116 Serial.println("Read data now...");
117}
118
119
120void loop()
121{
122 MagnetometerData data;
123
124 if (magnetometer.readData(data)) {
125
126 // Gauss to μT
130
131 Serial.print("Mag:");
132 Serial.print(" X:"); Serial.print(x);
133 Serial.print(" Y:"); Serial.print(y);
134 Serial.print(" Z:"); Serial.print(z);
135 Serial.print(" μT");
136
137 Serial.print(" Sensitivity: ");
138 Serial.print(magnetometer.getSensitivity(), 6);
139 Serial.print(" Gauss/LSB");
140
141 Serial.print(" Metadata:");
142 Serial.print(" X:");
143 Serial.print(data.raw.x);
144 Serial.print(" Y:");
145 Serial.print(data.raw.y);
146 Serial.print(" Z:");
147 Serial.print(data.raw.z);
148
149 Serial.print(" Heading (rad): ");
150 Serial.print(data.heading, 6);
151 Serial.print(" rad");
152
153 Serial.print(" Heading (deg): ");
154 Serial.print(data.heading_degrees, 2);
155 Serial.print("°");
156
158 strength = MagnetometerUtils::gaussToMicroTesla(strength);
159 Serial.print(" Magnetic Strength: ");
160 Serial.print(strength, 2);
161 Serial.println(" μT");
162
163 if (data.overflow) {
164 Serial.println("\tWarning: Data Overflow occurred!");
165 }
166 if (data.skip_data) {
167 Serial.println("\tWarning: Data Skip occurred!");
168 }
169 }
170 delay(50);
171}
@license MIT License
OperationMode
Enumeration of sensor operation modes.
MagFullScaleRange
Enumeration defining full-scale range settings for the sensor.
MagOverSampleRatio
Enumeration defining over-sample ratios for the sensor.
@ OSR_1
1x over-sample ratio
SensorBMM150 magnetometer
void setup()
#define SENSOR_SCL
#define SENSOR_SDA
void loop()
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
virtual void setDeclination(float declination_deg)
Set the magnetic declination correction value.
Driver for Bosch BMM150 magnetometer.
bool readData(MagnetometerData &data) override
Read one magnetometer sample.
bool configMagnetometer(OperationMode mode, MagFullScaleRange range, float odr, MagOverSampleRatio osr, MagDownSampleRatio dsr=MagDownSampleRatio::DSR_1) override
Apply combined magnetometer configuration.
virtual SensorConfig getConfig() const
Get current sensor configuration.
float getSensitivity() const
Get sensor sensitivity.
SensorInfo getSensorInfo() const
Get sensor information.
static const char * typeToString(SensorType type)
Convert sensor type enumeration to string representation.
float dmsToDecimalDegrees(int degrees, int minutes, float seconds=0.0f)
Convert degrees-minutes-seconds to decimal degrees.
float calculateMagneticStrength(const MagnetometerData &data)
Calculate total magnetic field strength.
float gaussToMicroTesla(float gauss)
Converts Gauss to microTesla.
Structure representing magnetometer data.
RawVector raw
raw 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.
Structure representing sensor configuration parameters.
float sample_rate
Sample rate in Hz.
float range
Measurement range.
OperationMode mode
Operation mode.
Structure containing sensor identification information.
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.
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.
int16_t x[5]
int16_t y[5]