Configure sensor parameters

Every sensor supported by the Huxon language provides several configuration parameters tunable according to the application needs. Most sensors have at least the following configuration parameters:

  • Output Data Rate (ODR): defines the sampling frequency of the sensor and the rate at which the sensor outputs its data

  • Full Scale (FS): defines the sensibility of the sensor and the corresponding measurement range

It is possible to declare a configuration for a given sensor type using the HUX_DECLARE_SENSOR_CONFIGURATION construct:

/* Namespace alias to simplify access to STMicroelectronics sensor types */
namespace STM = hux::sensors::STMicroelectronics;
...

/*
 * desired_configuration   : the label of the configuration
 * STM::IIS2DLPC           : the namespace of the sensor type
 * .odr                    : the Output Data Rate (ODR) configuration parameter
 * .fs                     : the Full Scale (FS) configuration parameter
 */
HUX_DECLARE_SENSOR_CONFIGURATION(desired_configuration, STM::IIS2DLPC,
    .odr = STM::IIS2DLPC::configs::odr_25,
    .fs = STM::IIS2DLPC::configs::fs_8
);

Available configuration values for ODR and FS can be retrieved from the sensor model namespace under “configs”:

hux::sensors::MANUFACTURER::SENSOR_MODEL::configs::VALUE

Note

Multiple configuration parameters can be configured within the HUX_DECLARE_SENSOR_CONFIGURATION construct. If a configuration parameter is not specified, its default value is used. For a list of the available configuration parameters and their default values refer to the Sensors documentation.

A sensor configuration can then be assigned to a specific sensor during its declaration:

/*
 * my_sensor                 : the sensor label
 * STM::IIS2DLPC             : the sensor type namespace
 * {}                        : the (empty) simulation dataset
 * desired_configuration     : the configuration for this sensor
 */
HUX_DECLARE_SENSOR(my_sensor, STM::IIS2DLPC, {}, desired_configuration);

Note

A configuration is sensor-type specific and can be assigned to multiple sensors of the same type.

Average temperature example

The following example shows how to write a simple application that computes an average temperature every 60 seconds using the LPS22HB temperature and humidity sensor. To ensure proper timing, we set a specific Output Data Rate (ODR) for the sensor via the corresponding configuration parameter.

In this example, we introduce the “buffer” channel logic. A buffer channel emits a single std::array of N samples for every N samples received as input. The buffer size N is configurable and can be specified together with the channel logic as: buffer<N>.

Note

A buffer channel of size N does not emit any output if less than N samples are given as input.

/* Import Huxon language headers */
#include <huxon/lang.hpp>
#include <huxon/sensors/STMicroelectronics.hpp>

/* Import other libraries */
#include <numeric>

/* Set a namespace alias to ease constants and methods retrieval from vendor's namespace. */
namespace STM = hux::sensors::STMicroelectronics;

/* Declare a sensor configuration for STMicroelectronics HTS221 sensors.
 * The Output Data Rate (ODR) is set to 1 Hz to ensure that the sensor
 * emits one sample every second.
 * */
HUX_DECLARE_SENSOR_CONFIGURATION(desired_configuration, STM::HTS221,
    .odr = STM::HTS221::configs::odr_1
)

/* Declare a new STMicroelectronics HTS221 sensor called "temp_sensor"
 * by providing the custom configuration (the simulation data is left empty).
 * */
HUX_DECLARE_SENSOR(temp_sensor, STM::HTS221, {}, desired_configuration);

/* Declare a "buffer" channel with the sensor temperature source as input.
 * A buffer channel emits a single std::array of N samples for every N
 * samples received as input.
 * Since we receive a temperature sample every second, we set the buffer size N=60, so that
 * the buffer channel produces an array of 60 temperature samples once every minute.
 * */
HUX_DECLARE_CHANNEL(temp_array_ch, buffer<60>,
    temp_sensor.get_temperature()
);

/* Declare a processing node to compute the average temperature over the array generated
 * by the buffer channel. Here, "hux_input" will be of type: std::array<float, 60>.
 * The processing node also prepares the output of the algorithm
 * using "HUX_DECLARE_OUTPUT_VALUE".
 * */
HUX_DECLARE_PROCESSING(average_temperature, temp_array_ch, {

    float temp_sum = std::accumulate(hux_input.begin(), hux_input.end(), 0.0);
    float avg_temperature = temp_sum / hux_input.size();

    HUX_DECLARE_OUTPUT_VALUE(result, Float, "average_temperature", avg_temperature);
    return result;
});

/* Configure the output of the Huxon application. The output must be a processing node
 * (or channel) that produces an output value created with HUX_DECLARE_OUTPUT_VALUE
 * */
HUX_REGISTER_OUTPUT(average_temperature);