Lodestar
An integrated real-time control package in C++
ConstantBlock.hpp
1 //
2 // Created by Hamza El-Kebir on 12/23/21.
3 //
4 
5 #ifndef LODESTAR_CONSTANTBLOCK_HPP
6 #define LODESTAR_CONSTANTBLOCK_HPP
7 
8 #include "Lodestar/blocks/Block.hpp"
9 
10 namespace ls {
11  namespace blocks {
12  namespace std {
13  template<typename TType>
14  class ConstantBlock :
15  public Block<
16  BlockProto::empty,
17  ::std::tuple<TType>,
18  ::std::tuple<TType>
19  > {
20  public:
21  using Base =
22  Block<
23  BlockProto::empty,
24  ::std::tuple<TType>,
25  ::std::tuple<TType>
26  >;
27 
29  {
30  bindEquation();
31  }
32 
33  ConstantBlock(const TType &constant)
34  {
35  this->template p<0>() = constant;
36  bindEquation();
37  }
38 
39  typename ::std::tuple_element<0, typename Base::Params>::type &
40  constant()
41  {
42  return this->template p<0>();
43  }
44 
45  const typename ::std::tuple_element<0, typename Base::Params>::type &
46  constant() const
47  {
48  return this->template p<0>();
49  }
50 
51 #ifdef LS_USE_GINAC
52 
53  const ::std::array<GiNaC::ex, Base::kIns> &inputSymbols()
54  {
55  if (!this->isInitInput_) {
56  for (int i = 0; i < Base::kIns; i++) {
58  GiNaC::lst input;
59  for (int ii = 0; ii <
61  GiNaC::lst row;
62  for (int jj = 0; jj <
64  GiNaC::symbol entry{
65  "blk" + ::std::to_string(this->id) + "_i_" + ::std::to_string(i) +
66  "_r_" + ::std::to_string(ii) + "_c_" + ::std::to_string(jj),
67  "\\text{BLK}^{i, " + ::std::to_string(i) + ", " + ::std::to_string(ii) +
68  ", " + ::std::to_string(jj) + "}_{" + ::std::to_string(this->id) + "}"};
69 
70  row.append(entry);
71  }
72  input.append(row);
73  }
74 
75  this->inputSymbols_[i] = GiNaC::lst_to_matrix(input);
76  } else {
77  this->inputSymbols_[i] = GiNaC::symbol{"blk" + ::std::to_string(this->id) + "_i_" + ::std::to_string(i),
78  "\\text{BLK}^{i, " + ::std::to_string(i) + "}_{" +
79  ::std::to_string(this->id) +
80  "}"};
81  }
82  }
83 
84  this->isInitInput_ = true;
85  }
86 
87  return this->inputSymbols_;
88  }
89 
90  const ::std::array<GiNaC::ex, Base::kOuts> &outputSymbols()
91  {
92  if (!this->isInitOutput_) {
93  for (int i = 0; i < Base::kOuts; i++) {
95  GiNaC::lst output;
96  for (int ii = 0; ii <
98  GiNaC::lst row;
99  for (int jj = 0; jj <
101  GiNaC::symbol entry{
102  "blk" + ::std::to_string(this->id) + "_o_" + ::std::to_string(i) +
103  "_r_" + ::std::to_string(ii) + "_c_" + ::std::to_string(jj),
104  "\\text{BLK}^{i, " + ::std::to_string(i) + ", " + ::std::to_string(ii) +
105  ", " + ::std::to_string(jj) + "}_{" + ::std::to_string(this->id) + "}"};
106 
107  row.append(entry);
108  }
109  output.append(row);
110  }
111 
112  this->outputSymbols_[i] = GiNaC::lst_to_matrix(output);
113  } else {
114  this->outputSymbols_[i] = GiNaC::symbol{"blk" + ::std::to_string(this->id) + "_o_" + ::std::to_string(i),
115  "\\text{BLK}^{i, " + ::std::to_string(i) + "}_{" +
116  ::std::to_string(this->id) +
117  "}"};
118  }
119  }
120 
121  this->isInitOutput_ = true;
122  }
123 
124  return this->outputSymbols_;
125  }
126 
127  const ::std::array<GiNaC::ex, 1> &parameterSymbols()
128  {
129  if (!this->isInitParameter_) {
130 
131  int i = 0;
132 
134  GiNaC::lst output;
135  for (int ii = 0;
136  ii < ls::aux::TemplateTraits::BinaryOperators::parseMatrixLike<TType>::rows; ii++) {
137  GiNaC::lst row;
138  for (int jj = 0; jj <
140  GiNaC::symbol entry{
141  "blk" + ::std::to_string(this->id) + "_p_" + ::std::to_string(i) + "_r_" +
142  ::std::to_string(ii) + "_c_" + ::std::to_string(jj),
143  "\\text{BLK}^{i, " + ::std::to_string(i) + ", " + ::std::to_string(ii) +
144  ", " + ::std::to_string(jj) + "}_{" + ::std::to_string(this->id) + "}"};
145 
146  row.append(entry);
147  }
148  output.append(row);
149  }
150 
151  this->parameterSymbols_[i] = GiNaC::lst_to_matrix(output);
152  } else {
153  this->parameterSymbols_[i] = GiNaC::symbol{"blk" + ::std::to_string(this->id) + "_p_" + ::std::to_string(i),
154  "\\text{BLK}^{i, " + ::std::to_string(i) + "}_{" +
155  ::std::to_string(this->id) +
156  "}"};
157  }
158 
159  this->isInitParameter_ = true;
160  }
161 
162  return this->parameterSymbols_;
163  }
164 
165 #endif
166 
167  protected:
168 #ifdef LS_USE_GINAC
169  ::std::array<GiNaC::ex, 1> parameterSymbols_;
170 #endif
171 
172  void bindEquation()
173  {
174  this->equation = ::std::bind(
176  this,
177  ::std::placeholders::_1
178  );
179 
180 #ifdef LS_USE_GINAC
181  GiNaC::function_options fops("blkf" + ::std::to_string(this->id) + "__", this->blkFunc_NPARAMS);
182  ls::blocks::symbolicEvalFunctionMap[this->id] = [&](const ::std::vector<GiNaC::ex> &exvec) -> GiNaC::ex {
183  return GiNaC::ex{this->parameterSymbols()[0]};
184  };
185 
186  fops.eval_func(ls::blocks::symbolicEval);
187 
188 
189  this->serial = GiNaC::function::register_new(
190  fops
191  );
192 #endif
193  }
194 
195  void triggerFunction(Base &b)
196  {
197  b.template o<0>() = b.template p<0>();
198  }
199  };
200  }
201 
202  template<typename TType>
204  public:
205  static constexpr const BlockType blockType = BlockType::ConstantBlock;
206  enum {
207  directFeedthrough = true
208  };
209 
211  using Base = typename type::Base;
212 
213  enum {
214  kIns = Base::kIns,
215  kOuts = Base::kOuts,
216  kPars = Base::kPars
217  };
218 
219  static const ::std::array<::std::string, kIns> inTypes;
220  static const ::std::array<::std::string, kOuts> outTypes;
221  static const ::std::array<::std::string, kPars> parTypes;
222 
223  static const ::std::array<::std::string, 1> templateTypes;
224  };
225 
226  template<typename TType>
227  const ::std::array<::std::string, BlockTraits<std::ConstantBlock<TType>>::kIns> BlockTraits<std::ConstantBlock<TType>>::inTypes =
228  {};
229 
230  template<typename TType>
231  const ::std::array<::std::string, BlockTraits<std::ConstantBlock<TType>>::kOuts> BlockTraits<std::ConstantBlock<TType>>::outTypes =
232  {demangle(typeid(TType).name())};
233 
234  template<typename TType>
235  const ::std::array<::std::string, BlockTraits<std::ConstantBlock<TType>>::kPars> BlockTraits<std::ConstantBlock<TType>>::parTypes =
236  {demangle(typeid(TType).name())};
237 
238  template<typename TType>
239  const ::std::array<::std::string, 1> BlockTraits<std::ConstantBlock<TType>>::templateTypes =
240  {demangle(typeid(TType).name())};
241  }
242 }
243 
244 
245 #endif //LODESTAR_CONSTANTBLOCK_HPP
ls::blocks::std::ConstantBlock
Definition: ConstantBlock.hpp:14
ls::blocks::BlockTraits
A traits object that exposes information about TBlock.
Definition: BlockTraits.hpp:37
ls::blocks::BlockType
BlockType
Block type information.
Definition: BlockType.hpp:25
ls::aux::TemplateTraits::BinaryOperators::parseMatrixLike
Extracts data from matrix-like types.
Definition: TemplateTraits.hpp:114
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::blocks::BlockProto::id
const unsigned int id
Vector of output signal pointers.
Definition: BlockProto.hpp:28
ls::blocks::BlockType::ConstantBlock
@ ConstantBlock
Constant block.
ls::blocks::Block
Generic base template class for all tuple-based Block instances.
Definition: Block.hpp:45