Hi Nordic Team,
I am currently developing applications for custom boards using the nRF Connect SDK v2.6.0, focusing on the nRF52840 microcontroller.
In our ongoing project, we need to implement a Vibration Monitoring feature that triggers when a threshold level is crossed. I have successfully interfaced the sensor and can obtain X, Y, and Z values using the LIS2DS12 drivers. However, I am encountering difficulties with the sensor_attr_set() function, specifically because the attribute SENSOR_ATTR_SLOPE_TH is not present in the driver.
Below is a sample program for LIS2DH sensor interfacing, where the sensor_attr_set() function uses SENSOR_ATTR_SLOPE_TH for setting the threshold:
/*Enable trigger handler for LIS*/
static void lis_trigger_handler(const struct device *dev, const struct sensor_trigger *trigger)
{
int64_t uptime = k_uptime_get();
if(uptime >= 750)
{
LOG_INF("Vibration is beyond the 750ms\n");
vibration_detected = 1;
}
else
{
vibration_detected = 0;
}
}
/*Threshold for Vibration Monitering*/
void configure_lis_vibration_threshold(const struct device *lis_dev) {
int ret;
struct sensor_value vib_val;
vib_val.val1 = lowG_vib_threshold; // Adjust this threshold value as needed
vib_val.val2 = 0; //(int32_t) SENSOR_G *1.5
struct sensor_trigger lis_trig = {
.type = SENSOR_TRIG_DELTA,
.chan = SENSOR_CHAN_ACCEL_XYZ,
};
// Set sensor attribute for upper threshold
ret = sensor_attr_set(lis_dev, SENSOR_CHAN_ACCEL_X, SENSOR_ATTR_SLOPE_TH, &vib_val);
if (ret) {
LOG_ERR("Failed to set sensor attribute: %d", ret);
return;
}
// Register trigger handler
ret = sensor_trigger_set(lis_dev, &lis_trig, lis_trigger_handler);
if (ret) {
LOG_ERR("Failed to set sensor trigger: %d", ret);
return;
}
}
However, in the LIS2DS12 driver (from zaphyr/driver/sensor/lis2ds12/lis2ds12.c), the SENSOR_ATTR_SLOPE_TH attribute is not available to be set from the lis2ds12_accel_config() function:
static int lis2ds12_accel_config(const struct device *dev,
enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value *val)
{
switch (attr) {
case SENSOR_ATTR_FULL_SCALE:
return lis2ds12_set_range(dev, sensor_ms2_to_g(val));
case SENSOR_ATTR_SAMPLING_FREQUENCY:
LOG_DBG("%s: set odr to %d Hz", dev->name, val->val1);
return lis2ds12_set_odr(dev, LIS2DS12_ODR_TO_REG(val->val1));
default:
LOG_DBG("Accel attribute not supported.");
return -ENOTSUP;
}
return 0;
}
static int lis2ds12_attr_set(const struct device *dev,
enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value *val)
{
switch (chan) {
case SENSOR_CHAN_ACCEL_XYZ:
return lis2ds12_accel_config(dev, chan, attr, val);
default:
LOG_WRN("attr_set() not supported on this channel.");
return -ENOTSUP;
}
return 0;
}
Is there an alternative way to include these attributes in the sensor_attr_set() function, or do I need to write custom drivers?
Regards,
Anmol