DevLab_ICM20948 1.0.2 ICM-20948 9-Axis Motion Sensor Driver

Loading...
Searching...
No Matches
mag_test.ino
Go to the documentation of this file.
1/**
2 * @file mag_test.ino
3 * @brief Magnetometer operation-mode sweep for the DevLab ICM-20948.
4 * @author Jonathan Mejorado Lopez
5 * @details Initializes the AK09916 magnetometer through the ICM-20948 internal
6 * I2C master, iterates through continuous measurement modes, and prints CSV-like
7 * magnetic field samples to the Serial monitor.
8 */
9
10#include <Wire.h>
11#include <DevLab_ICM20948.h>
12// -----------------------------------------------------------
13// PINES I2C — ESP32C6 NANO Unit Electronics
14// -----------------------------------------------------------
15/** @brief I2C SDA pin used by the example board. */
16#define SDA_PIN 6
17/** @brief I2C SCL pin used by the example board. */
18#define SCL_PIN 7
19/** @brief I2C bus speed in hertz. */
20#define I2C_FREQ 400000UL
21/** @brief ICM-20948 I2C address selected by the AD0 pin. */
22#define ICM_ADDR 0x69
23
24/** @brief Global ICM-20948 driver instance. */
25 DevLab_ICM20948 imu;
26
27/** @brief Print a separator line to Serial. */
28 void printLinea() { Serial.println(F("------------------------------------------------------------")); }
29/** @brief Print a double separator line to Serial. */
30 void printDobleLinea() { Serial.println(F("============================================================")); }
31
32/** @brief Magnetometer operating modes tested by the sweep. */
33 const ICM20948_Op_Mode opMode[] = {
34 ICM20948_Op_Mode::MODE_POWER_DOWN,
35 ICM20948_Op_Mode::MODE_SINGLE_MEASUREMENT,
36 ICM20948_Op_Mode::MODE_CONTINUOUS_1,
37 ICM20948_Op_Mode::MODE_CONTINUOUS_2,
38 ICM20948_Op_Mode::MODE_CONTINUOUS_3,
39 ICM20948_Op_Mode::MODE_CONTINUOUS_4,
40 ICM20948_Op_Mode::MODE_SELF_TEST
41};
42
43/** @brief Text labels matching the operating modes printed in the sweep. */
44 const char* etiquetasOpModeCfg[] = {
45 "PowerDown","SingleMeasure","Continuo1","Continuo2","Continuo3","Continuo4"
46};
47/** @brief Number of printable magnetometer operation modes. */
48 const uint8_t N_OPM = 6;
49
50/**
51 * @brief Initialize the magnetometer and collect samples for each mode.
52 */
53 void applySettings(){
54 /* Initialize magnetometer. */
55 if (!imu.initMag()) {
56 Serial.println(F("ERROR: initMag() failed"));
57 while (1) delay(200);
58 }
59 for(int i = 2; i < N_OPM; i++){
60 imu.setMagOpMode(opMode[i]);
61 delay(100);
62 Serial.println(F("Magnetometer ready"));
63 float mx, my, mz;
64
65 /* Read magnetometer data
66 * - Output in microtesla (uT)
67 * - Returns true on success
68 */
69 for(int r = 0; r < 500 ; r++){
70 if (imu.readMag(mx, my, mz)) {
71 Serial.printf("[%d]",r);
72 Serial.print(";");
73 Serial.print(etiquetasOpModeCfg[i]);
74 Serial.print(";");
75 Serial.print(mx, 2); Serial.print(F("; "));
76 Serial.print(my, 2); Serial.print(F("; "));
77 Serial.println(mz, 2);
78 } else {
79 Serial.println(F("Mag read failed"));
80 }
81 }
82 Serial.println(F("-----------------------------"));
83 delay(500);
84 }
85
86}
87
88/**
89 * @brief Initialize I2C, verify the IMU, enable sensors, and start the sweep.
90 */
91 void setup() {
92 // put your setup code here, to run once:
93 Serial.begin(115200);
94 delay(200);
95 Serial.println(F("ICM-20948 (I2C) — Magnetometer Example"));
96
97 Wire.begin(SDA_PIN,SCL_PIN);
98 /* Initialize IMU. */
99 if (!imu.beginI2C(ICM_ADDR, Wire, 400000)) {
100 Serial.println(F("ERROR: beginI2C() failed."));
101 while (1) delay(200);
102 }
103
104 Serial.println(F("ICM-20948 ready (I2C)."));
105
106 /* Verify device. */
107 uint8_t who;
108 if (!imu.readWhoAmI(who) || who != 0xEA) {
109 Serial.println(F("ERROR: WHO_AM_I mismatch"));
110 while (1) delay(200);
111 }
112
113 Serial.print(F("WHO_AM_I = 0x"));
114 Serial.println(who, HEX);
115
116 /* Enable required sensors (mag uses internal I2C master)
117 * - accel/gyro not strictly required but safe to keep ON
118 */
119 if (!imu.setSensors(true, true, false)) {
120 Serial.println(F("setSensors failed."));
121 }
122
123 Serial.println(F(" Sensor listo.\n"));
124 Serial.println(F(" Sensor PLANO y QUIETO sobre la mesa."));
125 Serial.println(F(" Iniciando en 5 segundos..."));
126 for (int i = 5; i > 0; i--) {
127 Serial.printf(" %d...\n", i);
128 delay(1000);
129 }
130
131 applySettings();
132
133}
134
135/**
136 * @brief Empty loop; this sketch runs the magnetometer sweep once in setup().
137 */
138 void loop() {
139 // put your main code here, to run repeatedly:
140
141}
DevLab_ICM20948.h
ICM20948_Op_Mode
ICM20948_Op_Mode
Definition DevLab_ICM20948.h:15
ICM20948_Op_Mode::MODE_CONTINUOUS_2
@ MODE_CONTINUOUS_2
ICM20948_Op_Mode::MODE_SELF_TEST
@ MODE_SELF_TEST
ICM20948_Op_Mode::MODE_SINGLE_MEASUREMENT
@ MODE_SINGLE_MEASUREMENT
ICM20948_Op_Mode::MODE_CONTINUOUS_3
@ MODE_CONTINUOUS_3
ICM20948_Op_Mode::MODE_CONTINUOUS_4
@ MODE_CONTINUOUS_4
ICM20948_Op_Mode::MODE_POWER_DOWN
@ MODE_POWER_DOWN
ICM20948_Op_Mode::MODE_CONTINUOUS_1
@ MODE_CONTINUOUS_1
DevLab_ICM20948
Definition DevLab_ICM20948.h:173
DevLab_ICM20948::setMagOpMode
bool setMagOpMode(ICM20948_Op_Mode opMode)
Definition DevLab_ICM20948.cpp:410
DevLab_ICM20948::initMag
bool initMag()
Definition DevLab_ICM20948.cpp:334
DevLab_ICM20948::readWhoAmI
bool readWhoAmI(uint8_t &whoAmI)
Definition DevLab_ICM20948.cpp:118
DevLab_ICM20948::beginI2C
bool beginI2C(uint8_t address=0x69, TwoWire &i2cPort=Wire, uint32_t i2cSpeed=400000)
Definition DevLab_ICM20948.cpp:6
DevLab_ICM20948::readMag
bool readMag(float &x, float &y, float &z)
Definition DevLab_ICM20948.cpp:310
DevLab_ICM20948::setSensors
bool setSensors(bool accel_on, bool gyro_on, bool temp_on)
Definition DevLab_ICM20948.cpp:1006
SCL_PIN
#define SCL_PIN
I2C SCL pin used by the example board.
Definition mag_test.ino:18
printDobleLinea
void printDobleLinea()
Print a double separator line to Serial.
Definition mag_test.ino:30
setup
void setup()
Initialize I2C, verify the IMU, enable sensors, and start the sweep.
Definition mag_test.ino:91
SDA_PIN
#define SDA_PIN
I2C SDA pin used by the example board.
Definition mag_test.ino:16
N_OPM
const uint8_t N_OPM
Number of printable magnetometer operation modes.
Definition mag_test.ino:48
printLinea
void printLinea()
Print a separator line to Serial.
Definition mag_test.ino:28
ICM_ADDR
#define ICM_ADDR
ICM-20948 I2C address selected by the AD0 pin.
Definition mag_test.ino:22
etiquetasOpModeCfg
const char * etiquetasOpModeCfg[]
Text labels matching the operating modes printed in the sweep.
Definition mag_test.ino:44
imu
DevLab_ICM20948 imu
Global ICM-20948 driver instance.
Definition mag_test.ino:25
opMode
const ICM20948_Op_Mode opMode[]
Magnetometer operating modes tested by the sweep.
Definition mag_test.ino:33
applySettings
void applySettings()
Initialize the magnetometer and collect samples for each mode.
Definition mag_test.ino:53
loop
void loop()
Empty loop; this sketch runs the magnetometer sweep once in setup().
Definition mag_test.ino:138