Skyward boardcore
Loading...
Searching...
No Matches
SensorSampler.cpp
Go to the documentation of this file.
1/* Copyright (c) 2017-2020 Skyward Experimental Rocketry
2 * Authors: Alain Carlucci, Luca Conterio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "SensorSampler.h"
24// #include <diagnostic/PrintLogger.h>
25
26using namespace std;
27
28namespace Boardcore
29{
30
31SensorSampler::SensorSampler(uint8_t id, std::chrono::nanoseconds period)
32 : id(id), period(period)
33{
34}
35
37
39{
40 return left->getSamplingPeriod() < right->getSamplingPeriod();
41}
42
43void SensorSampler::toggleSensor(AbstractSensor* sensor, bool isEnabled)
44{
45 auto elem = std::find_if(sensors.begin(), sensors.end(),
46 [&](std::pair<AbstractSensor*, SensorInfo> s)
47 { return s.first == sensor; });
48
49 elem->second.isEnabled = isEnabled;
50 LOG_DEBUG(logger, "Sampler {}, toggle Sensor {} ---> enabled = {}", getID(),
51 static_cast<void*>(sensor), elem->second.isEnabled);
52}
53
55{
56 for (auto& s : sensors)
57 s.second.isEnabled = true;
58}
59
61{
62 for (auto& s : sensors)
63 s.second.isEnabled = false;
64}
65
67{
68 for (auto& s : sensors)
69 {
70 // Sample only if that sensor is enabled and initialized
71 if (s.second.isEnabled && s.second.isInitialized)
72 {
73 sampleSensor(s.first);
74
75 if (s.second.callback)
76 s.second.callback();
77 }
78 }
79}
80
81uint8_t SensorSampler::getID() { return id; }
82
83std::chrono::nanoseconds SensorSampler::getSamplingPeriod() { return period; }
84
85unsigned int SensorSampler::getNumSensors() { return sensors.size(); }
86
88{
89 for (auto it = sensors.begin(); it != sensors.end(); ++it)
90 if (it->first == sensor)
91 return it->second;
92
93 LOG_ERR(logger, "Sampler {}: sensor {} not found", this->id,
94 static_cast<void*>(sensor));
95
96 return SensorInfo{};
97}
98
100 std::chrono::nanoseconds period)
101 : SensorSampler(id, period)
102{
103}
104
106
108 SensorInfo sensorInfo)
109{
110 sensors.push_back(make_pair(sensor, sensorInfo));
111}
112
114{
115 sensor->sample();
116}
117
118} // namespace Boardcore
#define LOG_ERR(logger,...)
#define LOG_DEBUG(logger,...)
Base abstract class for sensor drivers.
Definition Sensor.h:52
Virtual sensor sampler class.
SensorSampler(uint8_t id, std::chrono::nanoseconds period)
std::vector< std::pair< AbstractSensor *, SensorInfo > > sensors
void disableAllSensors()
Disable sampling for all the sensors.
static bool compareByPeriod(SensorSampler *left, SensorSampler *right)
const SensorInfo getSensorInfo(AbstractSensor *sensor)
void enableAllSensors()
Enable sampling for all the sensors.
void toggleSensor(AbstractSensor *sensor, bool isEnabled)
Enabled or disable a sensor.
std::chrono::nanoseconds getSamplingPeriod()
void sampleAndCallback()
For each sensor, sample it and call the corresponding callback.
SimpleSensorSampler(uint8_t id, std::chrono::nanoseconds period)
void sampleSensor(AbstractSensor *s) override
Perform the update of all the sensors in the sampler.
void addSensor(AbstractSensor *sensor, SensorInfo sensorInfo) override
Add a sensor to the sensors map.
This file includes all the types the logdecoder script will decode.
Definition WIZ5500.h:318
Sensors information struct needed by the SensorManager.
Definition SensorInfo.h:42