|
|
The `SensorManager` component manages all the sensors connected to the board.
|
|
|
Periodically, sensors are sampled and their corresponding callback is called, so that further operations can be performed on the retrieved data.
|
|
|
|
|
|
|
|
|
## Component Structure
|
|
|
|
|
|

|
|
|
|
|
|
## Sensor
|
|
|
|
|
|
The `Sensor.h` header file contains the base classes that every new sensor in the system must inherit from it.
|
... | ... | @@ -44,18 +39,76 @@ 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.
|
|
|
|
|
|
## Example
|
|
|
|
|
|
## Component Structure
|
|
|
|
|
|

|
|
|
|
|
|
## Example
|
|
|
### Step 1: Creating a sensor
|
|
|
```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;
|
|
|
};
|
|
|
```
|
|
|
|
|
|
### Step 2: Sampling the sensor
|
|
|
```cpp
|
|
|
#include <miosix.h>
|
|
|
#include <sensors/SensorManager.h>
|
|
|
#include "utils/testutils/TestSensor.h"
|
|
|
#include "TestSensor.h"
|
|
|
|
|
|
SensorManager sensor_manager;
|
|
|
TestSensor* sensor;
|
|
|
|
|
|
void testCallback() {
|
|
|
float data = *(sensor->testDataPtr());
|
|
|
// get the sampled data and
|
|
|
// perform whatever action you want
|
|
|
float data = sensor->getData();
|
|
|
printf("Test data : %f \n", data);
|
|
|
}
|
|
|
|
... | ... | |