Skyward boardcore
Loading...
Searching...
No Matches
HILPhasesManager.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
28#include <events/Event.h>
29#include <events/EventHandler.h>
30#include <miosix.h>
31#include <utils/Debug.h>
32
33#include <iostream>
34#include <map>
35
36namespace Boardcore
37{
38
40{
41 uint64_t t = 0;
42 float z = 0;
43 float vz = 0;
44
45 Outcomes() : t(0), z(0), vz(0) {}
46 Outcomes(float z, float vz)
47 : t(TimestampTimer::getTimestamp()), z(z), vz(vz)
48 {
49 }
50
51 void print(uint64_t t_start) const
52 {
53 std::cout << "@time : " << (double)(t - t_start) / 1000000
54 << " [sec]\n";
55 std::cout << "@z : " << z << " [m]\n";
56 std::cout << "@vz : " << vz << " [m/s]\n\n";
57 }
58};
59
61{
62public:
68
69 bool isSimulationRunning() const { return (t_start != 0) && (t_stop == 0); }
70
71 virtual void simulationStarted()
72 {
74 LOG_INFO(logger, "SIMULATION STARTED!");
75 }
76
77 virtual void liftoff()
78 {
80 LOG_INFO(logger, "LIFTOFF!");
81 }
82
83 virtual void simulationStopped()
84 {
86 LOG_INFO(logger, "SIMULATION STOPPED!");
87 }
88
89protected:
90 uint64_t t_start = 0;
91 uint64_t t_liftoff = 0;
92 uint64_t t_stop = 0;
94 PrintLogger logger = Logging::getLogger("HILPhasesManager");
95};
96
102template <class FlightPhases, class SimulatorData, class ActuatorData>
104{
105public:
106 using PhasesCallback = std::function<void()>;
107
113
114 bool isFlagActive(const FlightPhases& flag) const
115 {
116 return flagsFlightPhases.at(flag);
117 }
118
119 void registerToFlightPhase(const FlightPhases& flag,
120 const PhasesCallback& func)
121 {
122 callbacks[flag].push_back(func);
123 }
124
125 void setFlagFlightPhase(const FlightPhases& flag, bool isEnable)
126 {
127 flagsFlightPhases[flag] = isEnable;
128 }
129
130 void processFlags(const SimulatorData& hil_flags)
131 {
132 std::vector<FlightPhases> changed_flags;
133
134 processFlagsImpl(hil_flags, changed_flags);
135
136 /* calling the callbacks subscribed to the changed flags */
137 for (unsigned int i = 0; i < changed_flags.size(); i++)
138 for (const auto& callback : callbacks[changed_flags[i]])
139 callback();
140
142 }
143
144 virtual void printOutcomes() = 0;
145
146protected:
147 virtual void processFlagsImpl(const SimulatorData& hil_flags,
148 std::vector<FlightPhases>& changed_flags) = 0;
149
150 virtual void handleEventImpl(const Event& e,
151 std::vector<FlightPhases>& changed_flags) = 0;
152
153 void handleEvent(const Boardcore::Event& e) override
154 {
155 std::vector<FlightPhases> changed_flags;
156
157 handleEventImpl(e, changed_flags);
158
159 /* calling the callbacks subscribed to the changed flags */
160 for (unsigned int i = 0; i < changed_flags.size(); i++)
161 for (const auto& callback : callbacks[changed_flags[i]])
162 callback();
163
165 }
166
167 void registerOutcomes(const FlightPhases& phase)
168 {
170 outcomes[phase] = Outcomes(temp.z, temp.vz);
171 }
172
173 bool isSetTrue(const FlightPhases& phase) const
174 {
175 return flagsFlightPhases.at(phase) && !prev_flagsFlightPhases.at(phase);
176 }
177
178 bool isSetFalse(const FlightPhases& phase) const
179 {
180 return !flagsFlightPhases.at(phase) && prev_flagsFlightPhases.at(phase);
181 }
182
183 std::map<FlightPhases, bool> flagsFlightPhases;
184 std::map<FlightPhases, bool> prev_flagsFlightPhases;
185 std::map<FlightPhases, std::vector<PhasesCallback>> callbacks;
186 std::map<FlightPhases, Outcomes> outcomes;
187};
188} // namespace Boardcore
#define LOG_INFO(logger,...)
std::function< TimedTrajectoryPoint()> getCurrentPosition
HILPhasesManagerBase(std::function< TimedTrajectoryPoint()> getCurrentPosition)
Singleton object that manages all the phases of the simulation. After his instantiation we need to se...
std::function< void()> PhasesCallback
bool isSetTrue(const FlightPhases &phase) const
void registerOutcomes(const FlightPhases &phase)
virtual void handleEventImpl(const Event &e, std::vector< FlightPhases > &changed_flags)=0
std::map< FlightPhases, bool > prev_flagsFlightPhases
std::map< FlightPhases, Outcomes > outcomes
std::map< FlightPhases, std::vector< PhasesCallback > > callbacks
virtual void printOutcomes()=0
void handleEvent(const Boardcore::Event &e) override
void setFlagFlightPhase(const FlightPhases &flag, bool isEnable)
virtual void processFlagsImpl(const SimulatorData &hil_flags, std::vector< FlightPhases > &changed_flags)=0
bool isFlagActive(const FlightPhases &flag) const
std::map< FlightPhases, bool > flagsFlightPhases
void registerToFlightPhase(const FlightPhases &flag, const PhasesCallback &func)
HILPhasesManager(std::function< TimedTrajectoryPoint()> getCurrentPosition)
void processFlags(const SimulatorData &hil_flags)
bool isSetFalse(const FlightPhases &phase) const
static PrintLogger getLogger(const string &name)
Trajectory point with timestamp and velocity module.
float z
Vertical position [m].
float vz
Vertical velocity [m/s].
uint64_t getTimestamp()
Returns the current timer value in microseconds.
Driver for the VN100S IMU.
uint8_t Event
Definition Event.h:30
Outcomes(float z, float vz)
void print(uint64_t t_start) const