Skyward boardcore
Loading...
Searching...
No Matches
Sensor.h
Go to the documentation of this file.
1/* Copyright (c) 2016-2020 Skyward Experimental Rocketry
2 * Authors: Alain Carlucci, Luca Conterio, Davide Mor, Emilio Corigliano
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
25#include <miosix.h>
26
27#include <array>
28#include <memory>
29#include <type_traits>
30
31#include "SensorData.h"
32
33namespace Boardcore
34{
35
40template <class T, class ExpectedDataType>
42 : std::is_base_of<ExpectedDataType,
43 typename std::remove_reference<
44 decltype(std::declval<T>().getLastSample())>::type>
45{
46};
47
52{
53protected:
55
56public:
57 virtual ~AbstractSensor() {}
58
63 virtual bool init() = 0;
64
69 virtual bool selfTest() = 0;
70
74 virtual void sample() = 0;
75
81};
82
89template <typename Data>
90class Sensor : public virtual AbstractSensor
91{
92public:
93 using DataType = Data;
94
95protected:
97
104 virtual DataType sampleImpl() = 0;
105
106 // Thread safe mutex to synchronize writes and reads
107 miosix::FastMutex mutex;
108
109public:
111
112 Sensor(Sensor&& other) : lastSample{std::move(other.lastSample)}, mutex{} {}
113
114 virtual ~Sensor() {}
115
116 void sample() override
117 {
118 // Sampling outside of the protected zone ensures that the sampling
119 // action cannot cause locks or delays
120 DataType d = sampleImpl();
121
122 {
123 miosix::Lock<miosix::FastMutex> l(mutex);
124 lastSample = d;
125 }
126 }
127
131 virtual Data getLastSample()
132 {
133 miosix::Lock<miosix::FastMutex> l(mutex);
134 return lastSample;
135 }
136};
137
147template <typename Data, uint32_t FifoSize>
148class SensorFIFO : public Sensor<Data>
149{
150 using Super = Sensor<Data>;
151
152protected:
153 std::array<Data, FifoSize> lastFifo;
154 uint16_t lastFifoLevel = 1; //< number of samples in lastFifo
157 0; //< delta between previous interrupt
158 // timestamp and the last received one
159
160public:
162
164 : lastFifo{std::move(other.lastFifo)},
165 lastFifoLevel{std::move(other.lastFifoLevel)},
168 {
169 }
170
175 const std::array<Data, FifoSize> getLastFifo(uint16_t& lastFifoSize)
176 {
177 miosix::Lock<miosix::FastMutex> l(Super::mutex);
178 lastFifoSize = lastFifoLevel;
179 return lastFifo;
180 }
181
189 inline virtual void IRQupdateTimestamp(uint64_t ts)
190 {
193 }
194};
195
196} // namespace Boardcore
Base abstract class for sensor drivers.
Definition Sensor.h:52
virtual void sample()=0
Sample the sensor.
virtual bool selfTest()=0
Check if the sensor is working.
SensorErrors lastError
Definition Sensor.h:54
SensorErrors getLastError()
Get last error for debugging purposes. Avoid silent fails.
Definition Sensor.h:80
virtual ~AbstractSensor()
Definition Sensor.h:57
virtual bool init()=0
Initialize the sensor.
Interface for sensor that implement a FIFO.
Definition Sensor.h:149
const std::array< Data, FifoSize > getLastFifo(uint16_t &lastFifoSize)
Definition Sensor.h:175
std::array< Data, FifoSize > lastFifo
Definition Sensor.h:153
uint64_t interruptTimestampDelta
Definition Sensor.h:156
uint16_t lastFifoLevel
Definition Sensor.h:154
uint64_t lastInterruptTimestamp
Definition Sensor.h:155
SensorFIFO(SensorFIFO &&other)
Definition Sensor.h:163
virtual void IRQupdateTimestamp(uint64_t ts)
Called by the interrupt handling routine: provides the timestamp of the last interrupt (if FIFO is di...
Definition Sensor.h:189
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
virtual DataType sampleImpl()=0
Read a data sample from the sensor. In case of errors, the method should return the last available co...
DataType lastSample
Definition Sensor.h:96
virtual ~Sensor()
Definition Sensor.h:114
void sample() override
Sample the sensor.
Definition Sensor.h:116
miosix::FastMutex mutex
Definition Sensor.h:107
virtual Data getLastSample()
Definition Sensor.h:131
Sensor(Sensor &&other)
Definition Sensor.h:112
This file includes all the types the logdecoder script will decode.
SensorErrors
Generic error codes that a sensor can generate.
Definition SensorData.h:38
Definition WIZ5500.h:318
Check that a given type has a method called getData() and that the return type of this method is a su...
Definition Sensor.h:45