signals-cpp
Loading...
Searching...
No Matches
Models.h
Go to the documentation of this file.
1#pragma once
2#include <optional>
3#include "signals/State.h"
5
6using namespace Eigen;
7
23template<typename DynamicsType>
24class Model
25{
26public:
27 using InputSignalType = typename DynamicsType::InputSignalType;
28 using StateDotSignalType = typename DynamicsType::StateDotSignalType;
29 using StateSignalType = typename DynamicsType::StateSignalType;
30 using ParamsType = typename DynamicsType::ParamsType;
31
40
44 Model() : params_{std::nullopt}
45 {
46 reset();
47 }
48
54 void setParams(const ParamsType& params)
55 {
56 params_ = params;
57 }
58
62 bool hasParams()
63 {
64 return params_.has_value();
65 }
66
70 void reset()
71 {
72 x.reset();
73 xdot.reset();
74 }
75
79 double t() const
80 {
81 return x.t();
82 }
83
92 template<typename IntegratorType>
94 const double& tf,
95 const bool& insertIntoHistory = false,
96 const bool& calculateXddot = false)
97 {
98 if (!hasParams())
99 return false;
100 double t0 = x.t();
101 double dt;
102 if (!signal_utils::getTimeDelta(dt, t0, tf))
103 {
104 return false;
105 }
106 if (!DynamicsType::update(xdot, x, u, t0, tf, params_.value(), insertIntoHistory, calculateXddot))
107 {
108 return false;
109 }
110 if (!IntegratorType::integrate(x, xdot, tf, insertIntoHistory))
111 {
112 return false;
113 }
114 return true;
115 }
116
127 template<typename IntegratorType>
129 const double& tf,
130 const double& dt,
131 const bool& insertIntoHistory = false,
132 const bool& calculateXddot = false)
133 {
134 if (!hasParams())
135 return false;
136 double t_k = x.t();
137 while (t_k < tf)
138 {
139 double dt_k;
140 if (!signal_utils::getTimeDelta(dt_k, t_k, tf, dt))
141 {
142 return false;
143 }
144 double t_kp1 = t_k + dt_k;
145 if (!DynamicsType::update(xdot, x, u, t_k, t_kp1, params_.value(), insertIntoHistory, calculateXddot))
146 {
147 return false;
148 }
149 if (!IntegratorType::integrate(x, xdot, t_kp1, insertIntoHistory))
150 {
151 return false;
152 }
153 t_k = t_kp1;
154 }
155 return true;
156 }
157
158private:
159 std::optional<ParamsType> params_;
160};
161
168{
169 RigidBodyParams1D() : m(1), g(0) {}
173 double m;
179 double g;
180};
181
188{
189 RigidBodyParams2D() : m(1), J(1), g(Vector2d::Zero()) {}
193 double m;
199 double J;
205 Vector2d g;
206};
207
214{
215 RigidBodyParams3D() : m(1), J(Matrix3d::Identity()), g(Vector3d::Zero()) {}
219 double m;
225 Matrix3d J;
231 Vector3d g;
232};
233
237template<typename IST, typename SST, typename SDST, size_t d, typename PT>
239{
240 using InputSignalType = IST;
242 using StateSignalType = SDST;
243
244 using InputType = typename InputSignalType::BaseType;
245 using StateType = typename StateSignalType::BaseType;
246 using StateDotType = typename StateDotSignalType::BaseType;
247 using StateDotDotType = typename StateDotSignalType::TangentType;
248
249 using ParamsType = PT;
250
262 static bool update(StateDotSignalType& xdot,
263 const StateSignalType& x,
264 const InputSignalType& u,
265 const double& t0,
266 const double& tf,
267 const ParamsType& params,
268 const bool& insertIntoHistory = false,
269 const bool& calculateXddot = false)
270 {
271 InputType u_k = u(t0);
272 StateType x_k = x(t0);
273
274 StateDotType xdot_k;
275 xdot_k.pose = x_k.twist;
276 xdot_k.twist = -params.g + u_k / params.m;
277
278 if (calculateXddot)
279 {
280 return xdot.update(tf, xdot_k, insertIntoHistory);
281 }
282 else
283 {
284 return xdot.update(tf, xdot_k, StateDotDotType::identity(), insertIntoHistory);
285 }
286 }
287};
288
289template<typename T>
292
293template<typename T>
296
297template<typename T>
300
304template<typename T>
306{
310
315
317
329 static bool update(StateDotSignalType& xdot,
330 const StateSignalType& x,
331 const InputSignalType& u,
332 const double& t0,
333 const double& tf,
334 const ParamsType& params,
335 const bool& insertIntoHistory = false,
336 const bool& calculateXddot = false)
337 {
338 InputType u_k = u(t0);
339 StateType x_k = x(t0);
340
341 StateDotType xdot_k;
342 xdot_k.pose = x_k.twist;
343 xdot_k.twist = u_k / params.J;
344
345 if (calculateXddot)
346 {
347 return xdot.update(tf, xdot_k, insertIntoHistory);
348 }
349 else
350 {
351 return xdot.update(tf, xdot_k, StateDotDotType::identity(), insertIntoHistory);
352 }
353 }
354};
355
359template<typename T>
361{
365
370
372
384 static bool update(StateDotSignalType& xdot,
385 const StateSignalType& x,
386 const InputSignalType& u,
387 const double& t0,
388 const double& tf,
389 const ParamsType& params,
390 const bool& insertIntoHistory = false,
391 const bool& calculateXddot = false)
392 {
393 InputType u_k = u(t0);
394 StateType x_k = x(t0);
395
396 StateDotType xdot_k;
397 xdot_k.pose = x_k.twist;
398 xdot_k.twist = params.J.inverse() * (-x_k.twist.cross(params.J * x_k.twist) + u_k);
399
400 if (calculateXddot)
401 {
402 return xdot.update(tf, xdot_k, insertIntoHistory);
403 }
404 else
405 {
406 return xdot.update(tf, xdot_k, StateDotDotType::identity(), insertIntoHistory);
407 }
408 }
409};
410
414template<typename T>
416{
420
425
427
439 static bool update(StateDotSignalType& xdot,
440 const StateSignalType& x,
441 const InputSignalType& u,
442 const double& t0,
443 const double& tf,
444 const ParamsType& params,
445 const bool& insertIntoHistory = false,
446 const bool& calculateXddot = false)
447 {
448 InputType u_k = u(t0);
449 StateType x_k = x(t0);
450
451 StateDotType xdot_k;
452 xdot_k.pose = x_k.twist;
453 xdot_k.twist.template block<2, 1>(0, 0) = -(x_k.pose.q().inverse() * params.g) -
454 x_k.twist(2) * Matrix<T, 2, 1>(-x_k.twist(1), x_k.twist(0)) +
455 1.0 / params.m * u_k.template block<2, 1>(0, 0);
456 xdot_k.twist(2) = u_k(2) / params.J;
457
458 if (calculateXddot)
459 {
460 return xdot.update(tf, xdot_k, insertIntoHistory);
461 }
462 else
463 {
464 return xdot.update(tf, xdot_k, StateDotDotType::identity(), insertIntoHistory);
465 }
466 }
467};
468
472template<typename T>
474{
478
483
485
497 static bool update(StateDotSignalType& xdot,
498 const StateSignalType& x,
499 const InputSignalType& u,
500 const double& t0,
501 const double& tf,
502 const ParamsType& params,
503 const bool& insertIntoHistory = false,
504 const bool& calculateXddot = false)
505 {
506 InputType u_k = u(t0);
507 StateType x_k = x(t0);
508
509 // The entire xdot vector is in the body-frame, since we're using it as a local perturbation
510 // vector for the oplus and ominus operations from SE(3). i.e., we increment the translation
511 // vector and attitude jointly, unlike with many filter/controller implementations.
512 StateDotType xdot_k;
513 xdot_k.pose = x_k.twist;
514 xdot_k.twist.template block<3, 1>(0, 0) =
515 -(x_k.pose.q().inverse() * params.g) -
516 x_k.twist.template block<3, 1>(3, 0).cross(x_k.twist.template block<3, 1>(0, 0)) +
517 1.0 / params.m * u_k.template block<3, 1>(0, 0);
518 xdot_k.twist.template block<3, 1>(3, 0) =
519 params.J.inverse() *
520 (-x_k.twist.template block<3, 1>(3, 0).cross(params.J * x_k.twist.template block<3, 1>(3, 0)) +
521 u_k.template block<3, 1>(3, 0));
522
523 if (calculateXddot)
524 {
525 return xdot.update(tf, xdot_k, insertIntoHistory);
526 }
527 else
528 {
529 return xdot.update(tf, xdot_k, StateDotDotType::identity(), insertIntoHistory);
530 }
531 }
532};
533
534#define MAKE_MODEL(ModelBaseName, ModelDOF) \
535 template<typename T> \
536 using ModelBaseName##ModelDOF##Model = Model<ModelBaseName##Dynamics##ModelDOF<T>>; \
537 typedef ModelBaseName##ModelDOF##Model<double> ModelBaseName##ModelDOF##Modeld;
538
539MAKE_MODEL(Translational, 1DOF)
540MAKE_MODEL(Translational, 2DOF)
541MAKE_MODEL(Translational, 3DOF)
542MAKE_MODEL(Rotational, 1DOF)
543MAKE_MODEL(Rotational, 3DOF)
544MAKE_MODEL(RigidBody, 3DOF)
545MAKE_MODEL(RigidBody, 6DOF)
#define MAKE_MODEL(ModelBaseName, ModelDOF)
Definition Models.h:534
TranslationalDynamicsBase< ScalarSignal< T >, ScalarStateSignal< T >, ScalarStateSignal< T >, 1, RigidBodyParams1D > TranslationalDynamics1DOF
Definition Models.h:290
TranslationalDynamicsBase< Vector3Signal< T >, Vector3StateSignal< T >, Vector3StateSignal< T >, 3, RigidBodyParams3D > TranslationalDynamics3DOF
Definition Models.h:298
TranslationalDynamicsBase< Vector2Signal< T >, Vector2StateSignal< T >, Vector2StateSignal< T >, 2, RigidBodyParams2D > TranslationalDynamics2DOF
Definition Models.h:294
VectorSignal< T, 6 > Vector6Signal
Definition Signal.h:945
VectorSignal< T, 1 > Vector1Signal
Definition Signal.h:940
VectorSignal< T, 3 > Vector3Signal
Definition Signal.h:942
ManifoldStateSignal< T, SO3< T >, 4, 3 > SO3StateSignal
Definition State.h:387
VectorStateSignal< T, 6 > Vector6StateSignal
Definition State.h:381
ManifoldStateSignal< T, SE3< T >, 7, 6 > SE3StateSignal
Definition State.h:389
ManifoldStateSignal< T, SO2< T >, 2, 1 > SO2StateSignal
Definition State.h:386
Signal< T, ScalarStateSignalSpec< T >, ScalarStateSignalSpec< T > > ScalarStateSignal
Definition State.h:326
VectorStateSignal< T, 2 > Vector2StateSignal
Definition State.h:377
VectorStateSignal< T, 1 > Vector1StateSignal
Definition State.h:376
VectorStateSignal< T, 3 > Vector3StateSignal
Definition State.h:378
ManifoldStateSignal< T, SE2< T >, 4, 3 > SE2StateSignal
Definition State.h:388
Model()
Initialize a model with no parameters.
Definition Models.h:44
bool hasParams()
Definition Models.h:62
typename DynamicsType::StateSignalType StateSignalType
Definition Models.h:29
double t() const
Definition Models.h:79
void reset()
Zero out the model state and derivative variables and reset simulation time to zero.
Definition Models.h:70
typename DynamicsType::StateDotSignalType StateDotSignalType
Definition Models.h:28
void setParams(const ParamsType &params)
Definition Models.h:54
StateDotSignalType xdot
Definition Models.h:39
StateSignalType x
Definition Models.h:35
bool simulate(const InputSignalType &u, const double &tf, const double &dt, const bool &insertIntoHistory=false, const bool &calculateXddot=false)
Definition Models.h:128
bool simulate(const InputSignalType &u, const double &tf, const bool &insertIntoHistory=false, const bool &calculateXddot=false)
Definition Models.h:93
typename DynamicsType::InputSignalType InputSignalType
Definition Models.h:27
typename DynamicsType::ParamsType ParamsType
Definition Models.h:30
typename VectorSignalSpec< T, d >::Type BaseType
Definition Signal.h:65
bool update(const double &_t, const BaseType &_x, bool insertHistory=false)
Update the signal with a new value at a given time, computing derivative automatically.
Definition Signal.h:270
typename VectorStateSignalSpec< T, d >::Type TangentType
Definition Signal.h:66
bool getTimeDelta(double &dt, const double &t0, const double &tf, const double &dt_max=std::numeric_limits< double >::max())
Definition Utils.h:11
Definition of the dynamics for planar motion of a mass that's allowed to rotate.
Definition Models.h:416
SE2StateSignal< T > StateSignalType
Definition Models.h:418
Vector3StateSignal< T > StateDotSignalType
Definition Models.h:419
Vector3Signal< T > InputSignalType
Definition Models.h:417
typename StateSignalType::BaseType StateType
Definition Models.h:422
typename StateDotSignalType::TangentType StateDotDotType
Definition Models.h:424
typename StateDotSignalType::BaseType StateDotType
Definition Models.h:423
typename InputSignalType::BaseType InputType
Definition Models.h:421
static bool update(StateDotSignalType &xdot, const StateSignalType &x, const InputSignalType &u, const double &t0, const double &tf, const ParamsType &params, const bool &insertIntoHistory=false, const bool &calculateXddot=false)
Update a provided state time derivative given an input and time interval.
Definition Models.h:439
RigidBodyParams2D ParamsType
Definition Models.h:426
Definition of the dynamics for a 3D rigid body that can rotate about any axis.
Definition Models.h:474
typename StateDotSignalType::TangentType StateDotDotType
Definition Models.h:482
Vector6Signal< T > InputSignalType
Definition Models.h:475
typename StateDotSignalType::BaseType StateDotType
Definition Models.h:481
typename InputSignalType::BaseType InputType
Definition Models.h:479
RigidBodyParams3D ParamsType
Definition Models.h:484
SE3StateSignal< T > StateSignalType
Definition Models.h:476
typename StateSignalType::BaseType StateType
Definition Models.h:480
static bool update(StateDotSignalType &xdot, const StateSignalType &x, const InputSignalType &u, const double &t0, const double &tf, const ParamsType &params, const bool &insertIntoHistory=false, const bool &calculateXddot=false)
Update a provided state time derivative given an input and time interval.
Definition Models.h:497
Vector6StateSignal< T > StateDotSignalType
Definition Models.h:477
Parameters for a 1D rigid body model.
Definition Models.h:168
double m
Model mass.
Definition Models.h:173
double g
Gravitational constant.
Definition Models.h:179
RigidBodyParams1D()
Definition Models.h:169
Parameters for a 2D rigid body model.
Definition Models.h:188
double m
Model mass.
Definition Models.h:193
RigidBodyParams2D()
Definition Models.h:189
double J
Moment of inertia.
Definition Models.h:199
Vector2d g
Gravitational vector.
Definition Models.h:205
Parameters for a 3D rigid body model.
Definition Models.h:214
Matrix3d J
Moment of inertia.
Definition Models.h:225
Vector3d g
Gravitational vector.
Definition Models.h:231
RigidBodyParams3D()
Definition Models.h:215
double m
Model mass.
Definition Models.h:219
Definition of the dynamics for planar rotation-only motion of a mass.
Definition Models.h:306
Vector1StateSignal< T > StateDotSignalType
Definition Models.h:309
RigidBodyParams2D ParamsType
Definition Models.h:316
static bool update(StateDotSignalType &xdot, const StateSignalType &x, const InputSignalType &u, const double &t0, const double &tf, const ParamsType &params, const bool &insertIntoHistory=false, const bool &calculateXddot=false)
Update a provided state time derivative given an input and time interval.
Definition Models.h:329
typename StateDotSignalType::TangentType StateDotDotType
Definition Models.h:314
typename StateDotSignalType::BaseType StateDotType
Definition Models.h:313
Vector1Signal< T > InputSignalType
Definition Models.h:307
typename StateSignalType::BaseType StateType
Definition Models.h:312
SO2StateSignal< T > StateSignalType
Definition Models.h:308
typename InputSignalType::BaseType InputType
Definition Models.h:311
Definition of the dynamics for 3D rotation-only motion of a mass.
Definition Models.h:361
SO3StateSignal< T > StateSignalType
Definition Models.h:363
RigidBodyParams3D ParamsType
Definition Models.h:371
typename StateDotSignalType::BaseType StateDotType
Definition Models.h:368
Vector3StateSignal< T > StateDotSignalType
Definition Models.h:364
typename StateDotSignalType::TangentType StateDotDotType
Definition Models.h:369
Vector3Signal< T > InputSignalType
Definition Models.h:362
typename StateSignalType::BaseType StateType
Definition Models.h:367
typename InputSignalType::BaseType InputType
Definition Models.h:366
static bool update(StateDotSignalType &xdot, const StateSignalType &x, const InputSignalType &u, const double &t0, const double &tf, const ParamsType &params, const bool &insertIntoHistory=false, const bool &calculateXddot=false)
Update a provided state time derivative given an input and time interval.
Definition Models.h:384
Base class for defining the dynamics for 1D, 2D, and 3D point masses.
Definition Models.h:239
SDST StateSignalType
Definition Models.h:242
typename StateDotSignalType::BaseType StateDotType
Definition Models.h:246
SST StateDotSignalType
Definition Models.h:241
static bool update(StateDotSignalType &xdot, const StateSignalType &x, const InputSignalType &u, const double &t0, const double &tf, const ParamsType &params, const bool &insertIntoHistory=false, const bool &calculateXddot=false)
Update a provided state time derivative given an input and time interval.
Definition Models.h:262
typename StateDotSignalType::TangentType StateDotDotType
Definition Models.h:247
PT ParamsType
Definition Models.h:249
typename StateSignalType::BaseType StateType
Definition Models.h:245
IST InputSignalType
Definition Models.h:240
typename InputSignalType::BaseType InputType
Definition Models.h:244