Skyward boardcore
Loading...
Searching...
No Matches
HILSensor.h
Go to the documentation of this file.
1/* Copyright (c) 2024 Skyward Experimental Rocketry
2 * Authors: 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 <type_traits>
28
29#include "HILSimulatorData.h"
30#include "Sensor.h"
31
32namespace Boardcore
33{
34
51template <class T>
52class HILSensor : public T
53{
54public:
55 // A requirement is that the class T must be move constructible
56 static_assert(std::is_move_constructible<T>::value,
57 "T must be move constructible!");
58 // A requirement is that the class T must be a Sensor
59 static_assert(std::is_base_of<Sensor<typename T::DataType>, T>::value,
60 "T must inherit from Sensor<T::DataType>");
61
62 using UpdateFn = std::function<typename T::DataType(void)>;
63
74 : T{std::move(sensor)}, enableHw{enableHw},
76 {
77 }
78
79 bool init() override { return (enableHw ? T::init() : true); }
80
81 bool selfTest() override { return (enableHw ? T::selfTest() : true); }
82
91 typename T::DataType sampleImpl() override
92 {
93 if (enableHw)
94 {
95 auto realSample = T::sampleImpl();
96 {
97 miosix::Lock<miosix::FastMutex> l(mutexRealData);
98 lastRealSample = realSample;
99 }
100 }
101
102 return updateData();
103 }
104
111 typename T::DataType getRealLastSample()
112 {
113 miosix::Lock<miosix::FastMutex> l(mutexRealData);
114 return lastRealSample;
115 }
116
117protected:
118 typename T::DataType lastRealSample; // Last sample of the real sensor
119 bool enableHw; // Flag to enable hardware sampling
120 UpdateFn updateData; // Function to get the lastSample from simulatorData
121
122 // Thread safe mutex to synchronize writes and reads on lastRealSample
123 miosix::FastMutex mutexRealData;
124};
125
133template <typename T>
134static void hillificator(std::unique_ptr<T>& sensor, bool enableHw,
135 typename HILSensor<T>::UpdateFn updateData)
136{
137 if (sensor)
138 {
139 sensor = std::make_unique<Boardcore::HILSensor<T>>(
140 std::move(*sensor), enableHw, std::move(updateData));
141 }
142}
143
144} // namespace Boardcore
Class that wraps a real sensor to perform HIL simulations.
Definition HILSensor.h:53
T::DataType sampleImpl() override
Overridden sampleImpl method so that the data could be actually taken from the simulator.
Definition HILSensor.h:91
T::DataType lastRealSample
Definition HILSensor.h:118
miosix::FastMutex mutexRealData
Definition HILSensor.h:123
T::DataType getRealLastSample()
Method analogous to getLastSample but returns the real sample taken from the hardware sensor.
Definition HILSensor.h:111
bool init() override
Definition HILSensor.h:79
HILSensor(T &&sensor, bool enableHw, UpdateFn updateData)
Constructor of the HILSensor which decorates the passed sensor with the sampling of the simulator dat...
Definition HILSensor.h:73
std::function< typename T::DataType(void)> UpdateFn
Definition HILSensor.h:62
bool selfTest() override
Definition HILSensor.h:81
This file includes all the types the logdecoder script will decode.
Definition WIZ5500.h:318