Skyward boardcore
Loading...
Searching...
No Matches
SensorSampler.h
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#pragma once
24
26
27#include <chrono>
28
29#include "Sensor.h"
30#include "SensorInfo.h"
31
32namespace Boardcore
33{
34
43{
44public:
49 SensorSampler(uint8_t id, std::chrono::nanoseconds period);
50
51 virtual ~SensorSampler();
52
53 bool operator==(const SensorSampler& sampler) const
54 {
55 return id == sampler.id && period == sampler.period &&
56 sensors.size() == sampler.sensors.size();
57 }
58
59 static bool compareByPeriod(SensorSampler* left, SensorSampler* right);
60
66 virtual void addSensor(AbstractSensor* sensor, SensorInfo sensorInfo) = 0;
67
75 void toggleSensor(AbstractSensor* sensor, bool isEnabled);
76
80 void enableAllSensors();
81
85 void disableAllSensors();
86
90 void sampleAndCallback();
91
92 uint8_t getID();
93
94 std::chrono::nanoseconds getSamplingPeriod();
95
96 unsigned int getNumSensors();
97
99
100private:
104 virtual void sampleSensor(AbstractSensor* s) = 0;
105
106 uint8_t id;
107 std::chrono::nanoseconds period;
108
109protected:
110 std::vector<std::pair<AbstractSensor*, SensorInfo>> sensors;
111
113};
114
119class SimpleSensorSampler : public virtual SensorSampler
120{
121public:
122 SimpleSensorSampler(uint8_t id, std::chrono::nanoseconds period);
123
125
126 void addSensor(AbstractSensor* sensor, SensorInfo sensorInfo) override;
127
128 void sampleSensor(AbstractSensor* s) override;
129
130private:
132};
133
134} // namespace Boardcore
Base abstract class for sensor drivers.
Definition Sensor.h:52
static PrintLogger getLogger(const string &name)
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.
virtual void addSensor(AbstractSensor *sensor, SensorInfo sensorInfo)=0
Add a sensor to the sensors map.
bool operator==(const SensorSampler &sampler) const
std::chrono::nanoseconds getSamplingPeriod()
void sampleAndCallback()
For each sensor, sample it and call the corresponding callback.
Sampler for simple sensors, those that are simply sampled by calling the sample() method.
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.
Sensors information struct needed by the SensorManager.
Definition SensorInfo.h:42