... | ... | @@ -3,22 +3,6 @@ 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
|
|
|
|
|
|
The `Sensor.h` header file contains the base classes used to define sensors: every new sensor must inherit from one of them.
|
|
|
|
|
|
`Sensor` is the top-level virtual class.
|
|
|
Sub-classes are provided for different types of sensors, e.g. `GyroSensor`, `AccelSensor`, `PressureSensor` and some others.
|
|
|
Since these classes are a bit limited, you can always directly extend `Sensor`, defining your own data structures to store sensor's samples and methods to read them.
|
|
|
|
|
|
Each `Sensor` object has to override some methods:
|
|
|
* `init()`: this is the first method to be called before using the sensor. It sets up the sensor, e.g. registers.
|
|
|
* `onSimpleUpdate()`: this method is called every time the sensor has to be sampled. Data read from the sensor is stored in local variables or structures. Override this method if you directly want to read data from the sensor.
|
|
|
* `onDMAUpdate()`: this method is called every time the sensor has to be sampled using DMA. Data read from DMA is stored in local variables or structures. Override this method if the communication with the sensor is performed through DMA.
|
|
|
* `selfTest()`: check if the sensor is correctly working.
|
|
|
All these methods return a boolean value indicating whether the operation succeded or not.
|
|
|
|
|
|
The `getLastError()` method can be useful to get the last error generated by the sensor driver.
|
|
|
|
|
|
### Sensor Sampler
|
|
|
|
... | ... | @@ -61,56 +45,8 @@ For simplicity, each arrow in the diagram describes an action and reports the ph |
|
|
|
|
|
# Example
|
|
|
### Step 1: Creating a sensor
|
|
|
For more details see how to write a sensor driver.
|
|
|
```cpp
|
|
|
#include <cmath>
|
|
|
#include "Common.h"
|
|
|
#include "sensors/Sensor.h"
|
|
|
|
|
|
using miosix::getTick;
|
|
|
using miosix::TICK_FREQ;
|
|
|
|
|
|
class TestSensor : public virtual Sensor
|
|
|
{
|
|
|
public:
|
|
|
TestSensor() : sample(0) {}
|
|
|
|
|
|
~TestSensor() {}
|
|
|
|
|
|
bool init() override
|
|
|
{
|
|
|
// initialize the sensor here
|
|
|
// e.g. set config registers and check whoami
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool onSimpleUpdate() override
|
|
|
{
|
|
|
// read data and store it in local variable "sample"
|
|
|
sample = 10 * sin(PI * static_cast<float>(getTick()) /
|
|
|
static_cast<float>(TICK_FREQ));
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
bool selfTest() override
|
|
|
{
|
|
|
// check that the sensor is properly working
|
|
|
// e.g. check values tolerance over a set of samples
|
|
|
// or what the datasheet suggests
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// not a Sensor class method: you should
|
|
|
// define methods to get the sampled data
|
|
|
float getData()
|
|
|
{
|
|
|
return sample;
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
float sample;
|
|
|
};
|
|
|
```
|
|
|
This example refers to the `TestSensor` class defined in [Sensor](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`.
|
... | ... | |