Skyward boardcore
Loading...
Searching...
No Matches
HIL.h
Go to the documentation of this file.
1/* Copyright (c) 2021-2024 Skyward Experimental Rocketry
2 * Authors: Luca Conterio, 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 <Singleton.h>
27#include <utils/KernelTime.h>
28#include <utils/TimeUtils.h>
29
30#include "HILPhasesManager.h"
31#include "HILTransceiver.h"
32
33namespace Boardcore
34{
35
39template <class FlightPhases, class SimulatorData, class ActuatorData>
41{
42 using PhasesCallback =
43 typename HILPhasesManager<FlightPhases, SimulatorData,
44 ActuatorData>::PhasesCallback;
45
46public:
56 HIL(HILTransceiver<FlightPhases, SimulatorData, ActuatorData>*
58 HILPhasesManager<FlightPhases, SimulatorData, ActuatorData>*
60 std::function<ActuatorData()> updateActuatorData, int simulationPeriod)
61 : Boardcore::ActiveObject(Boardcore::STACK_MIN_FOR_SKYWARD,
62 miosix::PRIORITY_MAX - 1),
64 updateActuatorData(updateActuatorData),
65 simulationPeriod(simulationPeriod)
66 {
67 }
68
72 [[nodiscard]] bool start() override
73 {
74 bool initOk = true;
75
76 if (!hilTransceiver->start())
77 {
78 LOG_ERR(logger, "hilTransceiver started with errors");
79 initOk = false;
80 }
81
82 if (!hilPhasesManager->start())
83 {
84 LOG_ERR(logger, "hilPhasesManager started with errors");
85 initOk = false;
86 }
87
89 {
90 LOG_ERR(logger, "hil started with errors");
91 initOk = false;
92 }
93
94 return initOk;
95 }
96
97 void stop()
98 {
99 hilTransceiver->stop();
100 hilPhasesManager->stop();
102 LOG_INFO(logger, "HIL framework stopped");
103 }
104
106 {
107 LOG_INFO(logger, "Waiting for simulation to start...");
108 while (!hilPhasesManager->isSimulationRunning())
109 miosix::Thread::sleep(1);
110 }
111
112 void registerToFlightPhase(const FlightPhases& flag,
113 const PhasesCallback& func)
114 {
115 hilPhasesManager->registerToFlightPhase(flag, func);
116 }
117
118 int getSimulationPeriod() const { return simulationPeriod; }
119
121 {
122 return hilTransceiver->getTimestampSimulatorData();
123 }
124
125 const SimulatorData* getSensorData() const
126 {
127 return hilTransceiver->getSensorData();
128 }
129
130protected:
131 HILTransceiver<FlightPhases, SimulatorData, ActuatorData>* hilTransceiver;
132 HILPhasesManager<FlightPhases, SimulatorData, ActuatorData>*
134
135private:
136 void run() override
137 {
138 uint64_t ts = miosix::getTime();
139 while (!shouldStop())
140 {
141 ts += msToNs(simulationPeriod);
142 hilTransceiver->setActuatorData(updateActuatorData());
143 miosix::Thread::nanoSleepUntil(ts);
144 }
145 }
146
148
149 std::function<ActuatorData()> updateActuatorData;
150 int simulationPeriod; // Simulation period in milliseconds
151};
152} // namespace Boardcore
#define LOG_INFO(logger,...)
#define LOG_ERR(logger,...)
bool shouldStop()
Tells whether or not the ActiveObject should stop its execution.
virtual void stop()
Signals the runner thread to terminate and joins the thread.
virtual bool start()
Start the thread associated with this active object.
Single interface to the hardware-in-the-loop framework.
Definition HIL.h:41
const SimulatorData * getSensorData() const
Definition HIL.h:125
int getSimulationPeriod() const
Definition HIL.h:118
HIL(HILTransceiver< FlightPhases, SimulatorData, ActuatorData > *hilTransceiver, HILPhasesManager< FlightPhases, SimulatorData, ActuatorData > *hilPhasesManager, std::function< ActuatorData()> updateActuatorData, int simulationPeriod)
Constructor of the HIL framework.
Definition HIL.h:56
HILPhasesManager< FlightPhases, SimulatorData, ActuatorData > * hilPhasesManager
Definition HIL.h:133
void stop()
Signals the runner thread to terminate and joins the thread.
Definition HIL.h:97
bool start() override
Start the needed hardware-in-the-loop components.
Definition HIL.h:72
int64_t getTimestampSimulatorData() const
Definition HIL.h:120
void registerToFlightPhase(const FlightPhases &flag, const PhasesCallback &func)
Definition HIL.h:112
HILTransceiver< FlightPhases, SimulatorData, ActuatorData > * hilTransceiver
Definition HIL.h:131
void waitStartSimulation()
Definition HIL.h:105
Singleton object that manages all the phases of the simulation. After his instantiation we need to se...
static PrintLogger getLogger(const string &name)
This file includes all the types the logdecoder script will decode.
constexpr long long msToNs(long long ms)
Convert milliseconds to nanoseconds.
Definition TimeUtils.h:56