33 if (!init(sensorsMap))
34 LOG_ERR(logger,
"Initialization failed");
39 : scheduler(scheduler), customScheduler(false)
41 if (!init(sensorsMap))
42 LOG_ERR(logger,
"Initialization failed");
47 for (
auto sampler : samplers)
64 if (samplersMap.find(sensor) != samplersMap.end())
66 samplersMap[sensor]->toggleSensor(sensor,
true);
70 LOG_ERR(logger,
"Can't enable sensor {} it does not exist",
71 static_cast<void*
>(sensor));
77 if (samplersMap.find(sensor) != samplersMap.end())
79 samplersMap[sensor]->toggleSensor(sensor,
false);
83 LOG_ERR(logger,
"Can't disable sensor {}, it does not exist",
84 static_cast<void*
>(sensor));
90 for (
auto sampler : samplers)
91 sampler->enableAllSensors();
96 for (
auto sampler : samplers)
97 sampler->disableAllSensors();
104 if (samplersMap.find(sensor) != samplersMap.end())
105 return samplersMap[sensor]->getSensorInfo(sensor);
107 LOG_ERR(logger,
"Sensor {} not found, can't return SensorInfo",
108 static_cast<void*
>(sensor));
118bool SensorManager::init(
const SensorMap_t& sensorsMap)
120 uint8_t currentSamplerId = getFirstTaskID();
122 if (currentSamplerId != 0)
124 LOG_DEBUG(logger,
"Task scheduler not empty: starting from task ID {}",
128 for (
auto it : sensorsMap)
130 AbstractSensor* sensor = it.first;
131 SensorInfo sensorInfo = it.second;
133 LOG_DEBUG(logger,
"Initializing sensor {}", sensorInfo.id);
135 if (!initSensor(sensor))
137 sensorInfo.isEnabled =
false;
143 "Failed to initialize sensor {} -> Error: {} (period: {} ns)",
144 sensorInfo.id.c_str(), sensor->getLastError(),
145 sensorInfo.period.count());
149 sensorInfo.isInitialized =
true;
154 LOG_DEBUG(logger,
"Adding {} -> period: {} ns, enabled = {}",
155 sensorInfo.id.c_str(), sensorInfo.period.count(),
156 sensorInfo.isEnabled);
160 for (
auto sampler : samplers)
162 if (sensorInfo.period == sampler->getSamplingPeriod())
164 sampler->addSensor(sensor, sensorInfo);
165 samplersMap[sensor] = sampler;
173 SensorSampler* newSampler =
174 createSampler(currentSamplerId, sensorInfo.period);
176 newSampler->addSensor(sensor, sensorInfo);
178 samplers.push_back(newSampler);
179 samplersMap[sensor] = newSampler;
181 if (currentSamplerId == MAX_TASK_ID)
184 "Max task ID (255) reached in task scheduler, IDs "
185 "will start again from 0");
197bool SensorManager::initSensor(AbstractSensor* sensor)
199 return sensor->init() && sensor->selfTest();
202void SensorManager::initScheduler()
206 std::stable_sort(samplers.begin(), samplers.end(),
210 for (
auto& sampler : samplers)
213 { sampler->sampleAndCallback(); });
215 scheduler->
addTask(samplerUpdateFunction, sampler->getSamplingPeriod(),
220uint8_t SensorManager::getFirstTaskID()
222 std::vector<TaskStatsResult> tasksStats = scheduler->
getTaskStats();
224 if (tasksStats.empty())
227 auto max = std::max_element(
228 tasksStats.begin(), tasksStats.end(),
229 [](
const TaskStatsResult& t1,
const TaskStatsResult& t2)
230 { return t1.id < t2.id; });
235SensorSampler* SensorManager::createSampler(uint8_t
id,
236 std::chrono::nanoseconds period)
238 LOG_DEBUG(logger,
"Creating Sampler {} with sampling period {} ns",
id,
241 return new SimpleSensorSampler(
id, period);
#define LOG_WARN(logger,...)
#define LOG_ERR(logger,...)
#define LOG_DEBUG(logger,...)
Base abstract class for sensor drivers.
std::map< AbstractSensor *, SensorInfo > SensorMap_t
bool start()
Starts the task scheduler.
void enableSensor(AbstractSensor *sensor)
Enable sampling for the specified sensor.
void stop()
Starts the task scheduler.
bool areAllSensorsInitialized()
Checks whether all the sensors have been initialized correctly.
std::function< void()> function_t
SensorManager(const SensorMap_t &sensorsMap)
~SensorManager()
Deallocates samplers (through the samplers vector).
void disableSensor(AbstractSensor *sensor)
Disable sampling for the specified sensor.
const SensorInfo getSensorInfo(AbstractSensor *sensor)
const vector< TaskStatsResult > getSamplersStats()
static bool compareByPeriod(SensorSampler *left, SensorSampler *right)
The Task Scheduler allow to manage simple tasks with a single thread. All the task added must not tak...
std::vector< TaskStatsResult > getTaskStats()
bool start() override
Start the thread associated with this active object.
@ RECOVER
Prioritize the number of executions over the period.
void stop() override
Signals the runner thread to terminate and joins the thread.
size_t addTask(function_t function, uint32_t periodMs, Policy policy=Policy::RECOVER, int64_t startTick=Kernel::getOldTick())
Add a millisecond-period task function to the scheduler with an auto generated ID.
This file includes all the types the logdecoder script will decode.
Sensors information struct needed by the SensorManager.