bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
cosim_subsystem.h
Go to the documentation of this file.
1#ifndef BITSIM_COSIM_COSIM_SUBSYSTEM_H
2#define BITSIM_COSIM_COSIM_SUBSYSTEM_H
3
5
6#include <functional>
7#include <vector>
8
9namespace bitsim
10{
11namespace cosim
12{
13
29{
30 public:
31 virtual ~CoSimSubsystem() = default;
32
33 virtual uint_t n_inputs() const = 0;
34
35 virtual uint_t n_outputs() const = 0;
36
43 virtual std::vector<real_t> outputs(const std::vector<real_t> &inputs) const = 0;
44
48 virtual void do_step(real_t t, real_t H, const std::vector<real_t> &u_begin,
49 const std::vector<real_t> &u_end) = 0;
50
54 virtual void save() = 0;
55
59 virtual void restore() = 0;
60};
61
69class OdeSubsystem final : public CoSimSubsystem
70{
71 public:
72 using rhs_type = std::function<std::vector<real_t>(real_t, const std::vector<real_t> &,
73 const std::vector<real_t> &)>;
74 using output_type = std::function<std::vector<real_t>(const std::vector<real_t> &,
75 const std::vector<real_t> &)>;
76
85 OdeSubsystem(std::vector<real_t> x0, uint_t n_inputs, uint_t n_outputs, rhs_type f,
87
88 uint_t n_inputs() const override { return n_inputs_; }
89
90 uint_t n_outputs() const override { return n_outputs_; }
91
92 std::vector<real_t> outputs(const std::vector<real_t> &inputs) const override;
93
94 void do_step(real_t t, real_t H, const std::vector<real_t> &u_begin,
95 const std::vector<real_t> &u_end) override;
96
97 void save() override { saved_ = x_; }
98
99 void restore() override { x_ = saved_; }
100
101 const std::vector<real_t> &state() const noexcept { return x_; }
102
103 uint_t n_substeps() const noexcept { return n_substeps_; }
104
105 private:
106 std::vector<real_t> x_;
107 std::vector<real_t> saved_;
108 uint_t n_inputs_;
109 uint_t n_outputs_;
110 rhs_type f_;
111 output_type h_;
112 uint_t n_substeps_;
113};
114
115} // namespace cosim
116} // namespace bitsim
117
118#endif // BITSIM_COSIM_COSIM_SUBSYSTEM_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
A part of a coupled system that a co-simulation steps on its own: a robot, a trailer,...
Definition cosim_subsystem.h:29
virtual uint_t n_outputs() const =0
virtual std::vector< real_t > outputs(const std::vector< real_t > &inputs) const =0
The outputs at the present time, with the inputs at the present time.
virtual void do_step(real_t t, real_t H, const std::vector< real_t > &u_begin, const std::vector< real_t > &u_end)=0
Advance from t to t + H, the inputs moving linearly from u_begin to u_end.
virtual uint_t n_inputs() const =0
virtual ~CoSimSubsystem()=default
virtual void restore()=0
Go back to the state save() remembered.
virtual void save()=0
Remember the present state.
OdeSubsystem(std::vector< real_t > x0, uint_t n_inputs, uint_t n_outputs, rhs_type f, output_type h, uint_t n_substeps=10)
Definition cosim_subsystem.cpp:11
void save() override
Remember the present state.
Definition cosim_subsystem.h:97
uint_t n_outputs() const override
Definition cosim_subsystem.h:90
const std::vector< real_t > & state() const noexcept
Definition cosim_subsystem.h:101
std::vector< real_t > outputs(const std::vector< real_t > &inputs) const override
The outputs at the present time, with the inputs at the present time.
Definition cosim_subsystem.cpp:22
std::function< std::vector< real_t >(real_t, const std::vector< real_t > &, const std::vector< real_t > &)> rhs_type
Definition cosim_subsystem.h:72
std::function< std::vector< real_t >(const std::vector< real_t > &, const std::vector< real_t > &)> output_type
Definition cosim_subsystem.h:74
uint_t n_inputs() const override
Definition cosim_subsystem.h:88
void do_step(real_t t, real_t H, const std::vector< real_t > &u_begin, const std::vector< real_t > &u_end) override
Advance from t to t + H, the inputs moving linearly from u_begin to u_end.
Definition cosim_subsystem.cpp:32
uint_t n_substeps() const noexcept
Definition cosim_subsystem.h:103
void restore() override
Go back to the state save() remembered.
Definition cosim_subsystem.h:99
Definition co_simulation.cpp:9
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20