Simulated sensor driver
The simulated sensor driver implements a simulated sensor that is compatible with Zephyr’s Sensors. The sensor provides readouts for predefined set of sensor channels and supports sensor triggers.
Configuration
You can define instances of the sensor simulator in devicetree as shown in the following code snippet:
sensor-sim {
compatible = "nordic,sensor-sim";
acc-signal = "toggle";
};
The sensor driver will automatically be built if CONFIG_SENSOR=y
and any of its instances in devicetree have status set to okay
.
Configuration of generated readouts
The algorithms used to generate simulated sensor readouts are configurable. The following sensor channels and configuration options are available:
Ambient temperature (
SENSOR_CHAN_AMBIENT_TEMP
) - The value is generated as the sum of the value of the devicetree propertybase-temperature
and a pseudo-random number between-1
and1
.Humidity (
SENSOR_CHAN_HUMIDITY
) - The value is generated as the sum of the value of the devicetree propertybase-humidity
and a pseudo-random number between-1
and1
.Pressure (
SENSOR_CHAN_PRESS
) - The value is generated as the sum of the value of the devicetree propertybase-pressure
and a pseudo-random number between-1
and1
.Acceleration in X, Y, and Z axes (
SENSOR_CHAN_ACCEL_X
,SENSOR_CHAN_ACCEL_Y
,SENSOR_CHAN_ACCEL_Z
, for each axis respectively, andSENSOR_CHAN_ACCEL_XYZ
for all axes at once). The acceleration is generated depending on the selected devicetreeacc-signal
property:toggle
- With this choice, the acceleration is toggled on fetch between statically defined values.wave
- With this choice, the acceleration is generated as value of a periodic wave signal. The wave signal value is generated using the Wave generator library. You can use thesensor_sim_set_wave_param()
function to configure generated waves. By default, the function generates a sine wave.
Configuration of sensor triggers
Use CONFIG_SENSOR_SIM_TRIGGER
to enable the sensor trigger.
The simulated sensor supports the SENSOR_TRIG_DATA_READY
trigger.
You can configure the event that generates the trigger in devicetree by using the trigger-gpios
or trigger-timeout
options.
For both triggers, the handler function is called by a dedicated thread. The thread has the following configuration options:
CONFIG_SENSOR_SIM_THREAD_PRIORITY
- This Kconfig option defines the priority.CONFIG_SENSOR_SIM_THREAD_STACK_SIZE
- This Kconfig option defines the stack size.
API documentation
include/drivers/sensor_sim.h
drivers/sensor/sensor_sim/sensor_sim.c
- group sensor_sim
Simulated sensor device driver.
Functions
-
int sensor_sim_set_wave_param(const struct device *dev, enum sensor_channel chan, const struct wave_gen_param *set_params)
Set simulated acceleration parameters.
Note
This function can be used only if acceleration is generated as wave signal. Moreover, although it is thread-safe, it cannot be used in interrupts.
- Parameters:
dev – [in] Sensor device instance.
chan – [in] Selected sensor channel.
set_params – [in] Parameters of wave signal.
- Return values:
0 – If the operation was successful. Otherwise, a (negative) error code is returned.
-
int sensor_sim_set_wave_param(const struct device *dev, enum sensor_channel chan, const struct wave_gen_param *set_params)