Ravelin
DynamicBody.h
1 /****************************************************************************
2  * Copyright 2015 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef DYNAMIC_BODY
8 #error This class is not to be included by the user directly. Use DynamicBodyd.h or DynamicBodyf.h instead.
9 #endif
10 
11 class SINGLE_BODY;
12 
14 class DYNAMIC_BODY : public virtual_enable_shared_from_this<DYNAMIC_BODY>
15 {
16  public:
17  enum GeneralizedCoordinateType { eEuler, eSpatial };
18 
19  virtual ~DYNAMIC_BODY() {}
20 
22  std::string body_id;
23 
25  virtual bool is_enabled() const = 0;
26 
28  virtual MATRIXN& calc_jacobian(boost::shared_ptr<const POSE3> source_pose, boost::shared_ptr<const POSE3> target_pose, boost::shared_ptr<DYNAMIC_BODY> body, MATRIXN& J) = 0;
29  virtual MATRIXN& calc_jacobian_dot(boost::shared_ptr<const POSE3> source_pose, boost::shared_ptr<const POSE3> target_pose, boost::shared_ptr<DYNAMIC_BODY> body, MATRIXN& J) = 0;
30 
32  virtual void validate_position_variables() { };
33 
35  virtual void validate_velocity_variables() { };
36 
38  virtual void set_computation_frame_type(ReferenceFrameType rftype) = 0;
39 
41  ReferenceFrameType get_computation_frame_type() const { return _rftype; }
42 
44  virtual void calc_fwd_dyn() = 0;
45 
47  virtual void reset_accumulators() = 0;
48 
50  virtual void rotate(const QUAT& q) = 0;
51 
53  virtual void translate(const ORIGIN3& o) = 0;
54 
56  virtual REAL calc_kinetic_energy(boost::shared_ptr<const POSE3> P = boost::shared_ptr<const POSE3>()) = 0;
57 
59  virtual boost::shared_ptr<const POSE3> get_gc_pose() const = 0;
60 
62  virtual unsigned num_generalized_coordinates(GeneralizedCoordinateType gctype) const = 0;
63 
65  virtual void set_generalized_forces(const SHAREDVECTORN& gf) = 0;
66 
68  virtual void set_generalized_forces(const VECTORN& gf)
69  {
70  const SHAREDVECTORN gf_shared = gf.segment(0, gf.size()).get();
71  set_generalized_forces(gf_shared);
72  }
73 
75  virtual void add_generalized_force(const SHAREDVECTORN& gf) = 0;
76 
78  virtual void add_generalized_force(const VECTORN& gf)
79  {
80  const SHAREDVECTORN gf_shared = gf.segment(0, gf.size()).get();
81  add_generalized_force(gf_shared);
82  }
83 
85  virtual void apply_generalized_impulse(const SHAREDVECTORN& gj) = 0;
86 
88  virtual void apply_generalized_impulse(const VECTORN& gj)
89  {
90  const SHAREDVECTORN gj_shared = gj.segment(0, gj.size()).get();
91  apply_generalized_impulse(gj_shared);
92  }
93 
96 
99  {
100  const unsigned NGC = num_generalized_coordinates(eEuler);
101  gc.resize(NGC);
102  SHAREDVECTORN gc_shared = gc.segment(0, gc.size());
104  return gc;
105  }
106 
108  virtual SHAREDVECTORN& get_generalized_velocity(GeneralizedCoordinateType gctype, SHAREDVECTORN& gv) = 0;
109 
111  virtual VECTORN& get_generalized_velocity(GeneralizedCoordinateType gctype, VECTORN& gv)
112  {
113  const unsigned NGC = num_generalized_coordinates(gctype);
114  gv.resize(NGC);
115  SHAREDVECTORN gv_shared = gv.segment(0, gv.size());
116  get_generalized_velocity(gctype, gv_shared);
117  return gv;
118  }
119 
121  virtual void set_generalized_acceleration(const SHAREDVECTORN& ga) = 0;
122 
124  virtual void set_generalized_acceleration(const VECTORN& ga)
125  {
126  const SHAREDVECTORN ga_shared = ga.segment(0, ga.size()).get();
127  set_generalized_acceleration(ga_shared);
128  }
129 
132 
135  {
136  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
137  ga.resize(NGC);
138  SHAREDVECTORN ga_shared = ga.segment(0, ga.size());
139  get_generalized_acceleration(ga_shared);
140  return ga;
141  }
142 
144  virtual void set_generalized_coordinates_euler(const SHAREDVECTORN& gc) = 0;
145 
148  {
149  const SHAREDVECTORN gc_shared = gc.segment(0, gc.size()).get();
151  }
152 
154 
158  virtual void set_generalized_velocity(GeneralizedCoordinateType gctype, const SHAREDVECTORN& gv) = 0;
159 
161  virtual void set_generalized_velocity(GeneralizedCoordinateType gctype, const VECTORN& gv)
162  {
163  const SHAREDVECTORN gv_shared = gv.segment(0, gv.size()).get();
164  set_generalized_velocity(gctype, gv_shared);
165  }
166 
169  {
170  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
171  M.resize(NGC, NGC);
172  SHAREDMATRIXN X = M.block(0, NGC, 0, NGC);
174  return M;
175  }
176 
179 
182 
185  {
186  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
187  const SHAREDMATRIXN Bx = B.block(0, B.rows(), 0, B.columns()).get();
188  X.resize(NGC, B.columns());
189  SHAREDMATRIXN Xx = X.block(0, X.rows(), 0, X.columns());
191  return X;
192  }
193 
196  {
197  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
198  X.resize(NGC, B.columns());
199  SHAREDMATRIXN Xx = X.block(0, X.rows(), 0, X.columns());
201  return X;
202  }
203 
206  {
207  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
208  const SHAREDMATRIXN Bx = B.block(0, B.rows(), 0, B.columns()).get();
210  return X;
211  }
212 
215  {
216  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
217  const SHAREDVECTORN bx = b.segment(0, b.rows()).get();
218  x.resize(NGC);
219  SHAREDVECTORN xx = x.segment(0, x.rows());
221  return x;
222  }
223 
226  {
227  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
228  x.resize(NGC);
229  SHAREDVECTORN xx = x.segment(0, x.rows());
231  return x;
232  }
233 
236  {
237  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
238  const SHAREDVECTORN bx = b.segment(0, b.rows()).get();
240  return x;
241  }
242 
245 
248  {
249  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
250  const SHAREDMATRIXN Bx = B.block(0, B.rows(), 0, B.columns()).get();
251  X.resize(NGC, B.rows());
252  SHAREDMATRIXN Xx = X.block(0, X.rows(), 0, X.columns());
254  return X;
255  }
256 
259  {
260  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
261  X.resize(NGC, B.rows());
262  SHAREDMATRIXN Xx = X.block(0, X.rows(), 0, X.columns());
264  return X;
265  }
266 
269  {
270  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
271  const SHAREDMATRIXN Bx = B.block(0, B.rows(), 0, B.columns()).get();
273  return X;
274  }
275 
278 
280 
284 
286 
290  {
291  f.resize(num_generalized_coordinates(DYNAMIC_BODY::eSpatial));
292  SHAREDVECTORN f_shared = f.segment(0, f.size());
293  get_generalized_forces(f_shared);
294  return f;
295  }
296 
298 
305  virtual SHAREDVECTORN& convert_to_generalized_force(boost::shared_ptr<SINGLE_BODY> body, const SFORCE& w, SHAREDVECTORN& gf) = 0;
306 
308 
315  virtual VECTORN& convert_to_generalized_force(boost::shared_ptr<SINGLE_BODY> body, const SFORCE& w, VECTORN& gf)
316  {
317  const unsigned NGC = num_generalized_coordinates(DYNAMIC_BODY::eSpatial);
318  gf.resize(NGC);
319  SHAREDVECTORN gf_shared = gf.segment(0, gf.size());
320  convert_to_generalized_force(body, w, gf_shared);
321  return gf;
322  }
323 
324  protected:
325 
327  ReferenceFrameType _rftype;
328 
330  VECTORN gc, gv, gcgv, xp, xv, xa;
331 
332 }; // end class
333 
334 
virtual void set_generalized_coordinates_euler(const SHAREDVECTORN &gc)=0
Sets the generalized coordinates of this body (using Euler parameters for any rigid body orientations...
virtual MATRIXN & solve_generalized_inertia(const SHAREDMATRIXN &B, MATRIXN &X)
Solves using the inverse generalized inertia.
Definition: DynamicBody.h:195
virtual MATRIXN & calc_jacobian(boost::shared_ptr< const POSE3 > source_pose, boost::shared_ptr< const POSE3 > target_pose, boost::shared_ptr< DYNAMIC_BODY > body, MATRIXN &J)=0
The Jacobian transforms from the generalized coordinate from to the given frame.
virtual SHAREDVECTORN & convert_to_generalized_force(boost::shared_ptr< SINGLE_BODY > body, const SFORCE &w, SHAREDVECTORN &gf)=0
Converts a force to a generalized force.
SHAREDVECTORN segment(unsigned start_idx, unsigned end_idx)
Gets a subvector of this vector as a shared vector.
Definition: VectorN.cpp:245
virtual MATRIXN & resize(unsigned rows, unsigned columns, bool preserve=false)
Resizes this matrix, optionally preserving its existing elements.
Definition: MatrixN.cpp:274
CONST_SHAREDMATRIXN block(unsigned row_start, unsigned row_end, unsigned col_start, unsigned col_end) const
Gets a block as a constant shared matrix.
Definition: MatrixN.h:505
Quaternion class used for orientation.
Definition: Quat.h:21
virtual SHAREDMATRIXN & transpose_solve_generalized_inertia(const MATRIXN &B, SHAREDMATRIXN &X)
Solves the transpose matrix using the inverse generalized inertia.
Definition: DynamicBody.h:268
VECTORN gc
Temporaries for use with integration.
Definition: DynamicBody.h:330
virtual SHAREDVECTORN & solve_generalized_inertia(const VECTORN &b, SHAREDVECTORN &x)
Solves using the inverse generalized inertia.
Definition: DynamicBody.h:235
virtual SHAREDMATRIXN & solve_generalized_inertia(const SHAREDMATRIXN &B, SHAREDMATRIXN &X)=0
Solves using the inverse generalized inertia.
virtual SHAREDVECTORN & get_generalized_velocity(GeneralizedCoordinateType gctype, SHAREDVECTORN &gv)=0
Gets the generalized velocity of this body.
virtual void set_generalized_velocity(GeneralizedCoordinateType gctype, const VECTORN &gv)
Sets the generalized velocity of this body.
Definition: DynamicBody.h:161
Superclass for both rigid and deformable bodies.
Definition: SingleBody.h:14
virtual SHAREDVECTORN & get_generalized_coordinates_euler(SHAREDVECTORN &gc)=0
Gets the generalized coordinates of this body (using Euler parameters for any rigid body orientations...
virtual VECTORN & get_generalized_forces(VECTORN &f)
Gets the external forces on this body.
Definition: DynamicBody.h:289
A generic, possibly non-square matrix.
Definition: MatrixN.h:18
virtual VECTORN & convert_to_generalized_force(boost::shared_ptr< SINGLE_BODY > body, const SFORCE &w, VECTORN &gf)
Converts a force to a generalized force.
Definition: DynamicBody.h:315
virtual VECTORN & get_generalized_velocity(GeneralizedCoordinateType gctype, VECTORN &gv)
Gets the generalized velocity of this body.
Definition: DynamicBody.h:111
virtual SHAREDVECTORN & get_generalized_forces(SHAREDVECTORN &f)=0
Gets the external forces on this body.
virtual void add_generalized_force(const SHAREDVECTORN &gf)=0
Adds a generalized force to the body.
virtual void validate_position_variables()
Validates position-based variables (potentially dangerous for a user to call)
Definition: DynamicBody.h:32
virtual unsigned num_generalized_coordinates(GeneralizedCoordinateType gctype) const =0
Gets the number of generalized coordinates.
std::string body_id
The identifier for this body.
Definition: DynamicBody.h:22
virtual MATRIXN & transpose_solve_generalized_inertia(const SHAREDMATRIXN &B, MATRIXN &X)
Solves the transpose matrix using the inverse generalized inertia.
Definition: DynamicBody.h:258
virtual void set_generalized_forces(const SHAREDVECTORN &gf)=0
Sets the generalized forces on the body.
A generic, possibly non-square matrix using shared data.
Definition: SharedMatrixN.h:59
virtual void translate(const ORIGIN3 &o)=0
Translates the dynamic body by the given translation.
virtual void set_generalized_acceleration(const VECTORN &ga)
Sets the generalized acceleration of this body.
Definition: DynamicBody.h:124
virtual MATRIXN & solve_generalized_inertia(const MATRIXN &B, MATRIXN &X)
Solves using the inverse generalized inertia.
Definition: DynamicBody.h:184
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:15
virtual void set_generalized_forces(const VECTORN &gf)
Sets the generalized forces on the body.
Definition: DynamicBody.h:68
virtual void calc_fwd_dyn()=0
Forces a recalculation of forward dynamics.
virtual bool is_enabled() const =0
Gets whether the body's dynamics are integrated forward.
Superclass for deformable bodies and single and multi-rigid bodies.
Definition: DynamicBody.h:14
virtual SHAREDVECTORN & get_generalized_acceleration(SHAREDVECTORN &ga)=0
Gets the generalized velocity of this body.
ReferenceFrameType get_computation_frame_type() const
Gets the computation frame type for this body.
Definition: DynamicBody.h:41
virtual void apply_generalized_impulse(const VECTORN &gj)
Applies a generalized impulse to the body.
Definition: DynamicBody.h:88
ReferenceFrameType _rftype
The computation frame type.
Definition: DynamicBody.h:327
virtual void validate_velocity_variables()
Validates velocity-based variables (potentially dangerous for a user to call)
Definition: DynamicBody.h:35
virtual void rotate(const QUAT &q)=0
Rotates the dynamic body by the given orientation.
virtual void set_generalized_coordinates_euler(const VECTORN &gc)
Sets the generalized coordinates of this body (using Euler parameters for any rigid body orientations...
Definition: DynamicBody.h:147
virtual void set_generalized_acceleration(const SHAREDVECTORN &ga)=0
Sets the generalized velocity of this body.
A generic N-dimensional floating point vector.
Definition: VectorN.h:16
virtual void apply_generalized_impulse(const SHAREDVECTORN &gj)=0
Applies a generalized impulse to the body.
MATRIXN & get_generalized_inertia(MATRIXN &M)
Gets the generalized inertia of this body.
Definition: DynamicBody.h:168
virtual void set_computation_frame_type(ReferenceFrameType rftype)=0
Sets the computation frame type for this body.
virtual VECTORN & get_generalized_acceleration(VECTORN &ga)
Gets the generalized acceleration of this body.
Definition: DynamicBody.h:134
virtual SHAREDMATRIXN & solve_generalized_inertia(const MATRIXN &B, SHAREDMATRIXN &X)
Solves using the inverse generalized inertia.
Definition: DynamicBody.h:205
A spatial force (a wrench)
Definition: SForce.h:14
A three-dimensional floating point vector used for representing points and vectors in 3D and without ...
Definition: Origin3.h:16
virtual VECTORN & get_generalized_coordinates_euler(VECTORN &gc)
Gets the generalized coordinates of this body (using Euler parameters for any rigid body orientations...
Definition: DynamicBody.h:98
virtual void reset_accumulators()=0
Resets the force and torque accumulators on the dynamic body.
virtual boost::shared_ptr< const POSE3 > get_gc_pose() const =0
Gets the frame for generalized coordinates.
virtual void set_generalized_velocity(GeneralizedCoordinateType gctype, const SHAREDVECTORN &gv)=0
Sets the generalized velocity of this body.
virtual void add_generalized_force(const VECTORN &gf)
Adds a generalized force to the body.
Definition: DynamicBody.h:78
virtual VECTORN & solve_generalized_inertia(const SHAREDVECTORN &b, VECTORN &x)
Solves using the inverse generalized inertia.
Definition: DynamicBody.h:225
virtual VECTORN & solve_generalized_inertia(const VECTORN &b, VECTORN &x)
Solves using the inverse generalized inertia.
Definition: DynamicBody.h:214
virtual MATRIXN & transpose_solve_generalized_inertia(const MATRIXN &B, MATRIXN &X)
Solves the transpose matrix using the inverse generalized inertia.
Definition: DynamicBody.h:247
virtual REAL calc_kinetic_energy(boost::shared_ptr< const POSE3 > P=boost::shared_ptr< const POSE3 >())=0
Calculates the kinetic energy of the body in an arbitrary frame.