Icarus
Vehicle Simulation as a Transformable Computational Graph, built on Vulcan and Janus
Loading...
Searching...
No Matches
Component.hpp
Go to the documentation of this file.
1#pragma once
2
10
13#include <icarus/core/Error.hpp>
14#include <janus/math/Quaternion.hpp>
15#include <string>
16#include <vector>
17#include <vulcan/time/Epoch.hpp>
18
19namespace icarus {
20
21// Forward declarations
22template <typename Scalar> class Backplane;
23
27struct SignalDecl {
28 std::string name;
29 std::string unit;
30 std::string description;
31 bool is_input = false;
32};
33
47template <typename Scalar> class Component {
48 public:
49 virtual ~Component() = default;
50
51 // =========================================================================
52 // Core Lifecycle (Required)
53 // =========================================================================
54
62 virtual void Provision(Backplane<Scalar> &bp) = 0;
63
72 virtual void Stage(Backplane<Scalar> &bp) = 0;
73
83 virtual void Step(Scalar t, Scalar dt) = 0;
84
85 // =========================================================================
86 // Extended Lifecycle Hooks (Optional)
87 // =========================================================================
88
92 virtual void PreStep(Scalar /*t*/, Scalar /*dt*/) {}
93
97 virtual void PostStep(Scalar /*t*/, Scalar /*dt*/) {}
98
102 virtual void OnError(const SimulationError & /*error*/) {}
103
107 virtual void Shutdown() {}
108
109 // =========================================================================
110 // Identity & Introspection
111 // =========================================================================
112
116 [[nodiscard]] virtual std::string Name() const = 0;
117
123 [[nodiscard]] virtual std::string Entity() const { return ""; }
124
128 [[nodiscard]] virtual std::string TypeName() const { return Name(); }
129
133 [[nodiscard]] std::string FullName() const { return MakeFullPath(Entity(), Name()); }
134
140 [[nodiscard]] virtual std::vector<SignalDecl> DeclareInputs() const { return {}; }
141
147 [[nodiscard]] virtual std::vector<SignalDecl> DeclareOutputs() const { return {}; }
148
152 [[nodiscard]] std::vector<std::string> GetOutputNames() const {
153 std::vector<std::string> names;
154 for (const auto &decl : DeclareOutputs()) {
155 names.push_back(decl.name);
156 }
157 return names;
158 }
159
163 [[nodiscard]] std::vector<std::string> GetInputNames() const {
164 std::vector<std::string> names;
165 for (const auto &decl : DeclareInputs()) {
166 names.push_back(decl.name);
167 }
168 return names;
169 }
170
171 // =========================================================================
172 // Body Attachment API
173 // =========================================================================
174
179 [[nodiscard]] virtual bool HasBodyAttachment() const { return false; }
180
185 [[nodiscard]] virtual Vec3<Scalar> GetBodyPosition() const { return Vec3<Scalar>::Zero(); }
186
195 [[nodiscard]] virtual janus::Quaternion<Scalar> GetBodyOrientation() const {
196 return janus::Quaternion<Scalar>();
197 }
198
199 // =========================================================================
200 // Lifecycle State
201 // =========================================================================
202
206 [[nodiscard]] bool IsProvisioned() const { return provisioned_; }
207
211 [[nodiscard]] bool IsStaged() const { return staged_; }
212
213 // =========================================================================
214 // Configuration Access
215 // =========================================================================
216
220 void SetConfig(ComponentConfig config) { config_ = std::move(config); }
221
227 [[nodiscard]] const ComponentConfig &GetConfig() const { return config_; }
228
238 [[nodiscard]] const vulcan::time::Epoch<Scalar> *GetEpoch() const { return epoch_; }
239
240 protected:
241 // =========================================================================
242 // Configuration Helpers
243 // =========================================================================
244 // These simplify reading config values in Stage() by:
245 // - Eliminating redundant Has/Get patterns
246 // - Automatically handling double→Scalar conversion for vectors
247 // - Providing a clean one-liner API
248
254 template <typename T> T read_param(const std::string &key, const T &default_val) const {
255 return config_.template Get<T>(key, default_val);
256 }
257
263 template <typename T> T require_param(const std::string &key) const {
264 return config_.template Require<T>(key);
265 }
266
272 Vec3<Scalar> read_param_vec3(const std::string &key, const Vec3<Scalar> &default_val) const {
273 if (!config_.template Has<Vec3<double>>(key)) {
274 return default_val;
275 }
276 auto v = config_.template Get<Vec3<double>>(key, Vec3<double>::Zero());
277 return Vec3<Scalar>{static_cast<Scalar>(v(0)), static_cast<Scalar>(v(1)),
278 static_cast<Scalar>(v(2))};
279 }
280
286 Vec3<Scalar> require_param_vec3(const std::string &key) const {
287 auto v = config_.template Require<Vec3<double>>(key);
288 return Vec3<Scalar>{static_cast<Scalar>(v(0)), static_cast<Scalar>(v(1)),
289 static_cast<Scalar>(v(2))};
290 }
291
297 Vec4<Scalar> read_param_vec4(const std::string &key, const Vec4<Scalar> &default_val) const {
298 if (!config_.template Has<Vec4<double>>(key)) {
299 return default_val;
300 }
301 auto v = config_.template Get<Vec4<double>>(key, Vec4<double>::Zero());
302 return Vec4<Scalar>{static_cast<Scalar>(v(0)), static_cast<Scalar>(v(1)),
303 static_cast<Scalar>(v(2)), static_cast<Scalar>(v(3))};
304 }
305
311 Vec4<Scalar> require_param_vec4(const std::string &key) const {
312 auto v = config_.template Require<Vec4<double>>(key);
313 return Vec4<Scalar>{static_cast<Scalar>(v(0)), static_cast<Scalar>(v(1)),
314 static_cast<Scalar>(v(2)), static_cast<Scalar>(v(3))};
315 }
316
317 // =========================================================================
318 // Lifecycle State Management
319 // =========================================================================
320
321 // Called by Simulator to track lifecycle state
322 void MarkProvisioned() { provisioned_ = true; }
323 void MarkStaged() { staged_ = true; }
324 void ResetStaged() { staged_ = false; }
325
326 // Called by Backplane during Stage() to bind epoch reference
327 void BindEpoch(const vulcan::time::Epoch<Scalar> *epoch) { epoch_ = epoch; }
328
329 // Allow Simulator to call Mark* methods
330 template <typename S> friend class Simulator;
331 friend class Simulator; // Non-templated Simulator (Phase 4.0.7)
332 template <typename S> friend class Backplane;
333
334 private:
335 ComponentConfig config_;
336 const vulcan::time::Epoch<Scalar> *epoch_ = nullptr;
337 bool provisioned_ = false;
338 bool staged_ = false;
339};
340
341} // namespace icarus
Configuration container for components with typed accessors.
Core type definitions, concepts, and configuration for Icarus.
Consolidated error handling for Icarus.
Component-facing facade for signal registration and resolution.
Definition Backplane.hpp:32
Base class for all simulation components.
Definition Component.hpp:47
virtual ~Component()=default
void MarkStaged()
Definition Component.hpp:323
virtual std::vector< SignalDecl > DeclareInputs() const
Declared inputs (for documentation/dependency graph).
Definition Component.hpp:140
const ComponentConfig & GetConfig() const
Get component configuration.
Definition Component.hpp:227
virtual std::string TypeName() const
Component type name for data dictionary (e.g., "JetEngine").
Definition Component.hpp:128
const vulcan::time::Epoch< Scalar > * GetEpoch() const
Get simulation epoch (read-only).
Definition Component.hpp:238
Vec3< Scalar > read_param_vec3(const std::string &key, const Vec3< Scalar > &default_val) const
Read a Vec3 config parameter with automatic double→Scalar conversion.
Definition Component.hpp:272
std::vector< std::string > GetInputNames() const
Get list of input signal names.
Definition Component.hpp:163
void ResetStaged()
Definition Component.hpp:324
virtual bool HasBodyAttachment() const
Whether this component has a body attachment (position/orientation).
Definition Component.hpp:179
virtual void Shutdown()
Called during shutdown (cleanup, flush buffers).
Definition Component.hpp:107
Vec4< Scalar > require_param_vec4(const std::string &key) const
Read a required Vec4 config parameter (throws if missing).
Definition Component.hpp:311
virtual void Stage(Backplane< Scalar > &bp)=0
Stage phase - called at start of each run/episode.
virtual janus::Quaternion< Scalar > GetBodyOrientation() const
Get the component's mounting orientation (body-to-component rotation).
Definition Component.hpp:195
virtual Vec3< Scalar > GetBodyPosition() const
Get the component's mounting position in vehicle body frame.
Definition Component.hpp:185
T read_param(const std::string &key, const T &default_val) const
Read a scalar config parameter with default.
Definition Component.hpp:254
virtual void PostStep(Scalar, Scalar)
Called after all component Steps (for post-processing).
Definition Component.hpp:97
std::string FullName() const
Full qualified name: entity.component (or just component).
Definition Component.hpp:133
Vec4< Scalar > read_param_vec4(const std::string &key, const Vec4< Scalar > &default_val) const
Read a Vec4 config parameter with automatic double→Scalar conversion.
Definition Component.hpp:297
bool IsProvisioned() const
Check if Provision has been called.
Definition Component.hpp:206
virtual std::vector< SignalDecl > DeclareOutputs() const
Declared outputs (for documentation/dependency graph).
Definition Component.hpp:147
virtual void Provision(Backplane< Scalar > &bp)=0
Provision phase - called once at application launch.
friend class Backplane
Definition Component.hpp:332
virtual std::string Name() const =0
Component instance name (e.g., "MainEngine").
void MarkProvisioned()
Definition Component.hpp:322
Vec3< Scalar > require_param_vec3(const std::string &key) const
Read a required Vec3 config parameter (throws if missing).
Definition Component.hpp:286
void BindEpoch(const vulcan::time::Epoch< Scalar > *epoch)
Definition Component.hpp:327
void SetConfig(ComponentConfig config)
Set component configuration (called by factory after construction).
Definition Component.hpp:220
friend class Simulator
Definition Component.hpp:330
T require_param(const std::string &key) const
Read a required scalar config parameter (throws if missing).
Definition Component.hpp:263
virtual void OnError(const SimulationError &)
Called when simulation encounters an error.
Definition Component.hpp:102
bool IsStaged() const
Check if Stage has been called.
Definition Component.hpp:211
virtual std::string Entity() const
Entity namespace (e.g., "X15").
Definition Component.hpp:123
virtual void Step(Scalar t, Scalar dt)=0
Step phase - called every time step (hot path!).
virtual void PreStep(Scalar, Scalar)
Called before any component Steps (for pre-processing).
Definition Component.hpp:92
std::vector< std::string > GetOutputNames() const
Get list of output signal names.
Definition Component.hpp:152
Definition AggregationTypes.hpp:13
std::string MakeFullPath(const std::string &entity, const std::string &name)
Build a full path from entity and name.
Definition CoreTypes.hpp:166
Configuration container for components.
Definition ComponentConfig.hpp:37
Signal declaration for introspection.
Definition Component.hpp:27
std::string description
Human-readable description.
Definition Component.hpp:30
std::string name
Local signal name.
Definition Component.hpp:28
std::string unit
Physical unit.
Definition Component.hpp:29
bool is_input
True if input, false if output.
Definition Component.hpp:31
Definition Error.hpp:33