3 * @brief Extended accelerometer DLPF, full-scale, and averaging sweep.
4 * @details Runs a larger I2C validation matrix for the 7Semi ICM-20948
5 * accelerometer and prints CSV-like samples for later analysis.
9#include <DevLab_ICM20948.h>
11// -----------------------------------------------------------
12// PINES I2C — ESP32C6 NANO Unit Electronics
13// -----------------------------------------------------------
14/** @brief I2C SDA pin used by the example board. */
16/** @brief I2C SCL pin used by the example board. */
18/** @brief I2C bus speed in hertz. */
19#define I2C_FREQ 400000UL
20/** @brief ICM-20948 I2C address selected by the AD0 pin. */
23// -----------------------------------------------------------
25// -----------------------------------------------------------
26/** @brief Accelerometer DLPF divider and timing configuration. */
28 /** @brief Index into the accelDLPF lookup table. */
30 /** @brief Sample-rate divider written to the IMU register. */
32 /** @brief Noise bandwidth in hertz for reporting. */
34 /** @brief Output data rate in hertz for timing calculations. */
36 /** @brief Text label printed with each captured sample. */
39//id,div,nbw_hz,odr_hz,nombre : {DLPF, NBW , ODR}
40/** @brief Accelerometer DLPF configurations exercised by this sweep. */
41const configDLPF configsDLPF[] = {
42 { 0, 0, 265.0f, 1125.0f, "246 ; 265 ; 1125 " },
43 { 1, 0, 265.0f, 1125.0f, "246 ; 265 ; 1125 " },
44 { 2, 1, 136.0f, 562.5f, "111 ; 136 ; 562 " },
45 { 3, 2, 68.8f, 375.0f, "50 ; 69 ; 375 " },
46 { 4, 7, 34.4f, 140.6f, "24 ; 34 ; 141 " },
47 { 5, 15, 17.0f, 70.3f, "12 ; 17 ; 70 " },
48 { 6, 29, 8.3f, 37.5f, "6 ; 8 ; 35 " },
49 { 7, 0, 499.0f, 1125.0f, "473 ; 499 ; 1125 " },
51/** @brief Number of accelerometer DLPF configurations. */
52const uint8_t N_DLPF = sizeof(configsDLPF) / sizeof(configsDLPF[0]);
54// -----------------------------------------------------------
56// -----------------------------------------------------------
57/** @brief Accelerometer full-scale ranges exercised by the sweep. */
58const ICM20948_Accel_FullScale accelCfg[] = {
59 ICM20948_Accel_FullScale::G_2,
60 ICM20948_Accel_FullScale::G_4,
61 ICM20948_Accel_FullScale::G_8,
62 ICM20948_Accel_FullScale::G_16
64/** @brief Text labels matching accelCfg. */
65const char* etiquetasAccelCfg[] = { "+-2G","+-4G","+-8G","+-16G" };
66/** @brief Number of accelerometer full-scale ranges. */
67const uint8_t N_FS = 4;
69/** @brief Accelerometer DLPF enum values matching configsDLPF. */
70const ICM20948_Accel_DLPFCFG accelDLPF[] = {
71 ICM20948_Accel_DLPFCFG::DLPF0_246HZ,
72 ICM20948_Accel_DLPFCFG::DLPF_246HZ,
73 ICM20948_Accel_DLPFCFG::DLPF_111HZ,
74 ICM20948_Accel_DLPFCFG::DLPF_50HZ,
75 ICM20948_Accel_DLPFCFG::DLPF_24HZ,
76 ICM20948_Accel_DLPFCFG::DLPF_12HZ,
77 ICM20948_Accel_DLPFCFG::DLPF_6HZ,
78 ICM20948_Accel_DLPFCFG::DLPF_473HZ
81/** @brief Accelerometer averaging settings exercised by the sweep. */
82const ICM20948_Accel_Average accelAVG[] = {
83 ICM20948_Accel_Average::AVG_1_OR_4,
84 ICM20948_Accel_Average::AVG_8,
85 ICM20948_Accel_Average::AVG_16,
86 ICM20948_Accel_Average::AVG_32
88/** @brief Text labels matching accelAVG. */
89const char* etiquetasAccelAVG[] = { "14","8","16","32" };
90/** @brief Number of accelerometer averaging settings. */
91const uint8_t N_AVG = 4;
92/** @brief Raw accelerometer sample-rate divider values for experiments. */
93const uint16_t accelSR[] = {
94 0, 1, 3, 5, 7, 10, 15, 22, 31, 63, 127, 255, 513, 1022, 2044, 4095
96/** @brief Text labels matching accelSR output data rates. */
97const char* etiquetasSR[] = {
98 "1125.0","562.5","281.3","187.5","140.6","102.3","70.3",
99 "48.9","35.2","17.6","8.8","4.4","2.2","1.1","0.55","0.27"
102// -----------------------------------------------------------
103/** @brief Global ICM-20948 driver instance. */
105/** @brief Count of validation passes reserved for future checks. */
106uint8_t total_pass = 0;
107/** @brief Count of validation failures reserved for future checks. */
108uint8_t total_fail = 0;
109/** @brief Count of validation warnings reserved for future checks. */
110uint8_t total_warn = 0;
113/** @brief Print a separator line to Serial. */
114void printLinea() { Serial.println(F("------------------------------------------------------------")); }
115/** @brief Print a double separator line to Serial. */
116void printDobleLinea() { Serial.println(F("============================================================")); }
117// -----------------------------------------------------------
119// -----------------------------------------------------------
121 * @brief Verify the IMU identity register.
125 if (!imu.readWhoAmI(who) || who != 0xEA) {
126 Serial.println(F("ERROR: WHO_AM_I mismatch"));
127 while (1) delay(200);
129 Serial.printf("WHO_AM_I = 0x%02X OK\n", who);
132// -----------------------------------------------------------
133// APLICAR CONFIGURACION
135// FIX 1: Se agregan ambas llamadas a setAccelDLPF:
136// Llamada 1 (bypass=false): escribe bits DLPFCFG, deja FCHOICE=0
137// Llamada 2 (bypass=true): activa FCHOICE=1 sin tocar DLPFCFG
139// FIX 2: setAccelDivRate escribe el DIV directo al registro
140// -----------------------------------------------------------
142 * @brief Apply one accelerometer full-scale, DLPF, and averaging configuration.
144 * @param fs_idx Index into accelCfg.
145 * @param cfg DLPF timing and divider configuration.
146 * @param avg Index into accelAVG.
147 * @return true if all configuration writes succeeded, otherwise false.
149bool applySettings(uint8_t fs_idx, const configDLPF &cfg,uint8_t avg) {
150 uint8_t dlpf_val = (uint8_t)accelDLPF[cfg.dlpf_idx];
153 ok &= imu.setAccelScale(accelCfg[fs_idx]);
156 // Paso 1: escribe DLPFCFG en bits [5:3]
157 ok &= imu.setAccelDLPF(dlpf_val, false);
159 ok &= imu.setAccelAveraging(accelAVG[avg]);
160 // Paso 2: activa FCHOICE=1 (DLPF activo) sin tocar DLPFCFG
161 //ok &= imu.setAccelDLPF(0, true);
163 // DIV directo al registro, sin conversion interna
164 ok &= imu.setAccelDivRate(cfg.div);
169// -----------------------------------------------------------
170// SWEEP — MODO VERIFICACION
172// Por cada configuracion DLPF:
173// 1. Aplica la config (FS fijo en +-2g para verificacion)
174// 2. Espera settle del filtro
175// 3. Imprime 3 lecturas reales
177// Cuando las lecturas muestren Az ~1g en reposo,
178// el sensor esta funcionando y se puede activar el sweep completo.
179// -----------------------------------------------------------
181 * @brief Run the full accelerometer validation matrix and print samples.
187 Serial.println(F(" ICM-20948 | UNIT Electronics"));
188 Serial.println(F(" MODO VERIFICACION — FS=+-2g fijo, 3 lecturas por config"));
189 Serial.println(F(" Sensor PLANO y QUIETO | Az esperado: ~+1.000g"));
191 for(uint8_t i = 0; i < N_AVG; i++){
192 for(uint8_t j = 0; j < N_FS;j++){
193 for (uint8_t d = 0; d < N_DLPF; d++) {
197 /*Serial.printf(" Config %d/%d: %s\n", d+1, N_DLPF, configsDLPF[d].nombre);
198 Serial.printf(" ODR=%.1fHz | Periodo=%.1fms\n",
199 configsDLPF[d].odr_hz,
200 1000.0f / configsDLPF[d].odr_hz);*/
202 // --- FIX: aplicar la configuracion antes de leer ---
203 bool cfg_ok = applySettings(j, configsDLPF[d],i); // FS=0 → +-2g
205 Serial.println(F(" ERROR: applySettings() fallo"));
209 // Esperar que el filtro estabilice (10 periodos del ODR, min 50ms)
210 uint32_t settle_ms = max((uint32_t)(10000.0f / configsDLPF[d].odr_hz),
214 // Tomar 3 lecturas espaciadas por el periodo del ODR
215 uint32_t periodo_ms = max((uint32_t)(1000.0f / configsDLPF[d].odr_hz),
218 for (uint8_t r = 0; r < 100; r++) {
221 if (imu.readAccel(ax, ay, az)) {
222 Serial.printf("%d ; %s ; %s ; %s ; %+.4f ; %+.4f ; %+.4f\n",
224 configsDLPF[d].nombre, // "246 ; 265 ; 1125"
225 etiquetasAccelCfg[i], // "+-2G"
226 etiquetasAccelAVG[j], // "8"
229 Serial.println(F(" [?] readAccel() fallo"));
237 Serial.println(F(" VERIFICACION COMPLETA"));
238 Serial.println(F(" Si Az ~ +1.0g en todos los bloques:"));
239 Serial.println(F(" el sensor funciona correctamente."));
240 Serial.println(F(" Activar sweep completo cuando este listo."));
244// -----------------------------------------------------------
246// -----------------------------------------------------------
248 * @brief Initialize I2C, reset/wake the IMU, enable sensors, and start sweep.
251 Serial.begin(115200);
253 Serial.println(F("ICM-20948 — Verificacion"));
256 Serial.println(F(" ICM-20948 VALIDACION ACELEROMETRO"));
259 Wire.begin(SDA_PIN, SCL_PIN);
261 if (!imu.beginI2C(ICM_ADDR, Wire, I2C_FREQ)) {
262 Serial.println(F("ERROR: beginI2C() fallo"));
263 while (1) delay(200);
268 // softReset pone el chip a dormir
269 // Despues del reset hay que despertar el dispositivo
272 imu.sleep(false); // limpia SLEEP bit, CLKSEL=1
275 if (!imu.setSensors(true, true, true)) {
276 Serial.println(F("ERROR: setSensors() fallo"));
279 Serial.println(F(" Sensor listo.\n"));
280 Serial.println(F(" Sensor PLANO y QUIETO sobre la mesa."));
281 Serial.println(F(" Iniciando en 5 segundos..."));
282 for (int i = 5; i > 0; i--) {
283 Serial.printf(" %d...\n", i);
287 //for (int runs = 0; runs < 20 ; runs++){
292// -----------------------------------------------------------
293// LOOP — lectura simple post-sweep
294// -----------------------------------------------------------
296 * @brief Optional post-sweep loop for manual accelerometer reads.
300 if (imu.readAccel(ax, ay, az)) {
301 Serial.printf("X:%+.3f Y:%+.3f Z:%+.3f\n", ax, ay, az);