|
|
The `SensorManager` component manages all the sensors connected to the board.
|
|
|
|
|
|
The `SensorManager` component manages all the sensors connected to the board.
|
|
|
|
|
|
Sensors are periodically sampled and their corresponding callback is called, so that further operations can be performed on the retrieved data.
|
|
|
|
|
|
## Main Elements
|
|
|
|
|
|
### Sensor Sampler
|
|
|
|
|
|
Sensors are grouped according to the required sampling frequency and to the fact that they use DMA or not.
|
|
|
|
|
|
Sensors are grouped according to the required sampling frequency and to the fact that they use DMA or not.
|
|
|
|
|
|
A `SensorSampler` is characterized by a type (`SIMPLE_SAMPLER` or `DMA_SAMPLER`), the sampling frequency and a set of sensors.
|
|
|
|
|
|
|
|
|
In particular, the `SensorSampler` class maintains a `map<Sensor*, function<void()>>` that maps each sensor to its corresponding callback function.
|
|
|
Every time the `sampleAndCallback()` method of the `SensorSampler` is called, the sampler cycles through all its sensors, samples them and calls their corresponding callback, where these samples can be processed and dispatched.
|
|
|
|
|
|
Since `SensorSampler` is a virtual class, two sub-classes exist:
|
|
|
* `SimpleSensorSampler`: its type is `SIMPLE_SAMPLER`. In order to perform a sampling it calls the sensor's `onSimpleUpdate()` method.
|
|
|
* `DMASensorSampler`: its type is `DMA_SAMPLER`. It only manages sensors that use DMA. In order to perform a sampling it calls the sensor's `onDMAUpdate()` method.
|
|
|
* **SimpleSensorSampler**: its type is `SIMPLE_SAMPLER`. In order to perform a sampling it calls the sensor's `onSimpleUpdate()` method.
|
|
|
* **DMASensorSampler**: its type is `DMA_SAMPLER`. It only manages sensors that use DMA. In order to perform a sampling it calls the sensor's `onDMAUpdate()` method.
|
|
|
|
|
|
### Sensor Manager
|
|
|
In order to add a sensor to the `SensorManager`, the `addSensor()` method is provided: simply pass it the sensor, the required sampling frequency, the callback function and the sampler type (that has default value equal to `SIMPLE_SAMPLER`).
|
|
|
|
|
|
In order to add a sensor to the `SensorManager`, the `addSensor()` method is provided: simply pass it the sensor, the required sampling frequency, the callback function and the sampler type (that has default value equal to `SIMPLE_SAMPLER`).
|
|
|
|
|
|
Every time a sensor is added, the `SensorManager` assigns it to a `SensorSampler`, according to the required sampling frequency and the sampling type.
|
|
|
If a `SensorSampler` for that frequency and type doesn't exist, the `SensorManager` manages its creation.
|
|
|
|
|
|
If a `SensorSampler` for that frequency and type doesn't exist, the `SensorManager` manages its creation.
|
|
|
|
|
|
Note that when a sensor is added, it is also initialized (via `init()`) and `selfTest()` is performed.
|
|
|
The sensor will be added only if both the initialization and the test succeed.
|
|
|
|
|
|
The sensor will be added only if both the initialization and the test succeed.
|
|
|
|
|
|
When `start()` is called, the `SensorManager` initializes the `TaskScheduler`: for each `SensorSampler`, it adds to the scheduler the `sampleAndCallback()` method to be called periodically according to the sampler's frequency.
|
|
|
|
|
|
## Component Behavior
|
... | ... | @@ -45,17 +45,16 @@ For simplicity, each arrow in the diagram describes an action and reports the ph |
|
|
|
|
|
## Example
|
|
|
#### Step 1: Creating a sensor
|
|
|
This example refers to the `TestSensor` class defined in [Sensor](Sensor).
|
|
|
This example refers to the `TestSensor` class defined in [Sensor.h](Sensor).
|
|
|
For more details see how to create a sensor, visit that page.
|
|
|
|
|
|
#### Step 2: Sampling the sensor
|
|
|
Include needed modules, define your sensor and the `SensorManager`.
|
|
|
```cpp
|
|
|
#include <miosix.h>
|
|
|
#include <sensors/SensorManager.h>
|
|
|
#include <sensors`SensorManager`.h>
|
|
|
#include "TestSensor.h"
|
|
|
|
|
|
SensorManager sensor_manager;
|
|
|
`SensorManager` sensor_manager;
|
|
|
TestSensor* sensor;
|
|
|
```
|
|
|
Define the callback that will be passed to the `SensorSampler`, along with the sensor.
|
... | ... | @@ -67,8 +66,8 @@ void testCallback() { |
|
|
}
|
|
|
```
|
|
|
You can use `std::bind()` in order to bind your callback to an `std::function<void()>>` object.
|
|
|
Select the required frequency for the sampling and add the sensor and the callback to the `SensorManager`.
|
|
|
|
|
|
Select the required frequency for the sampling and add the sensor and the callback to the `SensorManager`.
|
|
|
|
|
|
You can now start the `SensorManager`.
|
|
|
```cpp
|
|
|
int main() {
|
... | ... | |