I am studying a little bit about the sensor library. Unfortunately, I haven't been able to find any good documentation about sensor library, what function calls are available for each sensor and etc... Is it available?
Anyways, my current problem is with ADXL362 accelerometer. I am trying to set a trigger (instead of polling data like in accel_polling project available at zephyr\samples\sensor\accel_polling)
I want to only read data when the movement threshold has been triggered.
However, it is not fully clear how can this be achieved and could not find any useful information online about it.
My project repository:
github.com/.../thingy91_motion_trigger
I am currently testing on the Thingy 91 board and running the following code:
#include <stdio.h> #include <stdlib.h> #include <zephyr/kernel.h> #include <zephyr/drivers/spi.h> #include <zephyr/device.h> #include <zephyr/drivers/sensor.h> K_SEM_DEFINE(sem, 0, 1); static const enum sensor_channel channels[] = { SENSOR_CHAN_ACCEL_X, SENSOR_CHAN_ACCEL_Y, SENSOR_CHAN_ACCEL_Z, }; #define DEFAULT_ADXL362_NODE DT_ALIAS(adxl362) BUILD_ASSERT(DT_NODE_HAS_STATUS(DEFAULT_ADXL362_NODE, okay), "ADXL362 not specified in DT"); // DEVICE TREE STRUCTURE const struct device *adxl1362_sens = DEVICE_DT_GET(DEFAULT_ADXL362_NODE); static void trigger_handler(const struct device *dev, const struct sensor_trigger *trig) { switch (trig->type) { case SENSOR_TRIG_DATA_READY: if (sensor_sample_fetch(dev) < 0) { printk("Sample fetch error\n"); return; } k_sem_give(&sem); break; case SENSOR_TRIG_THRESHOLD: printk("Threshold trigger\n"); break; default: printk("Unknown trigger\n"); } } void main(void) { struct sensor_value accel[3]; if (!device_is_ready(adxl1362_sens)) { printk("sensor: device %s not ready.\n", adxl1362_sens->name); return 0; } else { printk("sensor: device %s ready.\n", adxl1362_sens->name); } if (IS_ENABLED(CONFIG_ADXL362_TRIGGER)) { struct sensor_trigger trig = { .chan = SENSOR_CHAN_ACCEL_X }; trig.type = SENSOR_TRIG_THRESHOLD; if (sensor_trigger_set(adxl1362_sens, &trig, trigger_handler)) { printk("Trigger set error\n"); return; } trig.type = SENSOR_TRIG_DATA_READY; if (sensor_trigger_set(adxl1362_sens, &trig, trigger_handler)) { printk("Trigger set error\n"); } } while (true) { if (IS_ENABLED(CONFIG_ADXL362_TRIGGER)) { k_sem_take(&sem, K_FOREVER); } else { k_sleep(K_MSEC(1000)); if (sensor_sample_fetch(adxl1362_sens) < 0) { printk("Sample fetch error\n"); return; } } if (sensor_channel_get(adxl1362_sens, SENSOR_CHAN_ACCEL_X, &accel[0]) < 0) { printk("Channel get error\n"); return; } if (sensor_channel_get(adxl1362_sens, SENSOR_CHAN_ACCEL_Y, &accel[1]) < 0) { printk("Channel get error\n"); return; } if (sensor_channel_get(adxl1362_sens, SENSOR_CHAN_ACCEL_Z, &accel[2]) < 0) { printk("Channel get error\n"); return; } printk("x: %.1f, y: %.1f, z: %.1f (m/s^2)\n", sensor_value_to_double(&accel[0]), sensor_value_to_double(&accel[1]), sensor_value_to_double(&accel[2])); } }
my prj.conf is as following:
CONFIG_CBPRINTF_FP_SUPPORT=y # Enable Edge Impulse dependencies CONFIG_CPP=y CONFIG_STD_CPP11=y CONFIG_FP16=n # SPI CONFIG_SPI=y CONFIG_SPI_NRFX=y CONFIG_MAIN_STACK_SIZE=4096 # LOG CONFIG_LOG=y # ADXL362 CONFIG_SENSOR=y CONFIG_ADXL362=y CONFIG_ADXL362_TRIGGER_GLOBAL_THREAD=y CONFIG_ADXL362_INTERRUPT_MODE=1 CONFIG_ADXL362_ABS_REF_MODE=1 CONFIG_ADXL362_ACTIVITY_THRESHOLD=200
and my thingy91_nr9160_ns.overlay:
/ { aliases { adxl372 = &adxl372; adxl362 = &adxl362; }; };
After flasing the nRF9160 on the Thingy 91 with the source code shown above, I am getting the following error:
À*** Booting nRF Connect SDK v2.5.0 *** sensor: device adxl362@0 ready. [00:00:00.270,568] [1B][1;31m<err> ADXL362: Unsupported sensor trigger[1B][0m Trigger set error
I would appreciate if someone could help me:
- By sharing sensor library documentation (especially what function calls are available for each sensor and how to use them correctly)
- Figure out what could be the issue with the code and why I am getting unsupported sensor trigger issue