AGX Dynamics  2.37.3.2
InverseDynamics.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agxModel/export.h>
20 
21 #include <agx/ref_ptr.h>
22 #include <agx/Real.h>
23 #include <agx/AffineMatrix4x4.h>
24 #include <agx/Vector.h>
25 #include <agx/HashSet.h>
26 #include <agx/RigidBody.h>
27 
28 namespace agx {
31 
32  template<typename T1, typename T2>
33  class SetVector;
34 
35  class Constraint;
41 
42  class LockJoint;
44 
47 }
48 
49 namespace agxSDK {
50  class Simulation;
52 
53  class Assembly;
56 
57  class Collection;
59 }
60 
61 namespace agxPowerLine {
62  class Unit;
65 
66  class PowerLine;
69 }
70 
71 namespace agxDriveTrain {
72  class VelocityConstraint;
75 }
76 
77 
78 namespace agxModel
79 {
95  typedef struct IDJointValue {
99  } IDJointValue;
100 
101 
112  typedef struct IDUnitValue {
117  } IDUnitValue;
118 
119 
126  template<typename T>
127  class IDSpan
128  {
129  public:
131  const T& at( size_t index ) const;
132 
133  const T& operator[]( size_t index ) const;
134 
136  size_t size() const;
137 
138  private:
139  friend class InverseDynamics;
140 
141  IDSpan(const T* ptr, size_t size);
142 
143  const T* m_ptr;
144  size_t m_size;
145  };
146 
149 
150 
172  {
173  public:
183  ID_COLLISION_OCCURRED
184  };
185 
186  enum SyncLevel {
187  INTERNAL = 0,
188  MOTION = 1 << 0,
189  DYNAMIC = 1 << 1,
190  FULL = MOTION + DYNAMIC
191  };
192 
198  {
201  agx::Real translationalCompliance,
202  agx::Real rotationalCompliance,
203  agx::Real translationalThreshold,
204  agx::Real rotationThreshold,
205  bool useRotation = true // If set use entire target transform, if false just move to the position
206 
207  )
208  : m_body(body), m_transform(transform), m_translationalCompliance(translationalCompliance),
209  m_rotationalCompliance(rotationalCompliance), m_translationalThreshold(translationalThreshold),
210  m_rotationThreshold(rotationThreshold), m_useRotation(useRotation)
211  {}
213  m_body(nullptr), m_transform(agx::AffineMatrix4x4()), m_translationalCompliance(agx::Real(1)),
214  m_rotationalCompliance(agx::Real(1)), m_translationalThreshold(agx::Real(1)),
215  m_rotationThreshold(agx::Real(1)), m_useRotation(true)
216  {}
224  };
225 
227 
246  const agx::RigidBodyRefVector& bodies,
247  const agx::ConstraintRefVector& constraints);
248 
269  const agx::RigidBodyRefVector& bodies,
270  const agx::ConstraintRefVector& constraints,
271  const agx::ConstraintRefVector& otherConstraints);
272 
297  const agx::RigidBodyRefVector& bodies,
298  const agx::ConstraintRefVector& constraints,
299  const agxPowerLine::PowerLineRefVector& powerlines,
300  const agxPowerLine::UnitRefVector& units);
301 
328  const agx::RigidBodyRefVector& bodies,
329  const agx::ConstraintRefVector& constraints,
330  const agx::ConstraintRefVector& otherConstraints,
331  const agxPowerLine::PowerLineRefVector& powerlines,
332  const agxPowerLine::UnitRefVector& units);
333 
343 
368 
399  InverseDynamicsStatus gravityCompensation();
400 
428  agx::Real time, agx::Real positionThreshold, agx::Real rotationThreshold, const bool stopAtCollision = false, const bool onlyMotionSimulation = false);
429 
450  InverseDynamicsStatus solve(const agx::RigidBody* body, const agx::AffineMatrix4x4Vector& transforms,
451  const agx::RealVector& timestamps, agx::Real positionThreshold, agx::Real rotationThreshold, const bool stopAtCollision = false, const bool onlyMotionSimulation = false);
452 
473  InverseDynamicsStatus solveLinear(const agx::RigidBody* body,
475  agx::Real time, agx::Real positionThreshold, agx::Real rotationThreshold,
476  const bool stopAtCollision = false,
477  const bool onlyMotionSimulation = false);
489  agx::Real time,
490  const bool stopAtCollision = false,
491  const bool onlyMotionSimulation = false);
492 
510  agx::Real computeTranslationalDifference(const agx::RigidBody* body) const;
511 
523  agx::Real computeTranslationalDifference(const agx::RigidBody* body, const agx::AffineMatrix4x4& transform) const;
524 
542  agx::Real computeRotationalDifference(const agx::RigidBody* body) const;
543 
555  agx::Real computeRotationalDifference(const agx::RigidBody* body, const agx::AffineMatrix4x4& transform) const;
556 
567  size_t getNumFrames() const;
568 
581  agx::Real getLastTrajectoryTimeStamp() const;
582 
589  agx::AffineMatrix4x4 getLastTrajectoryTransform();
590 
603  agx::Real getLastDynamicsTimeStamp() const;
604 
611  agx::AffineMatrix4x4 getLastDynamicsTransform();
612 
623  IDJointValueView getJointForces(size_t frameIndex);
624 
631  IDJointValueView getJointAngles(size_t frameIndex);
632 
642  IDUnitValueView getPowerLineValues(size_t frameIndex);
643 
649  const agxSDK::Assembly* getSourceData() const;
650 
657  const agxSDK::Assembly* getMotionData() const;
658 
665  const agxSDK::Assembly* getDynamicsData() const;
666 
673 
684 
688  InverseDynamics::InverseDynamicsStatus stepAdaptiveSolveN();
689 
693  InverseDynamics::InverseDynamicsStatus finalizeAdaptiveSolveN();
694 
698  agx::UInt getCurrentFrameIndex() const;
699 
703  void forceStepMotionSimulation();
704 
708  void forceStepDynamicsSimulation();
709 
710  private:
711  using PowerLineRefPair = std::pair<agxPowerLine::PowerLineRef, agxPowerLine::PowerLineRef>;
713 
714  using PowerLineUnitPair = std::pair<agxPowerLine::Unit*, agxPowerLine::Unit*>;
716 
717  void reserveData(const size_t numReserveFrames);
718 
723  agx::RigidBody* createKinematicTrajectoryBody(agxSDK::SimulationRef& motionSimulation);
724 
725  void createKinematicTrajectoryBodies(agxSDK::SimulationRef& motionSimulation, agx::RigidBodyRefVector& kinematicTrajectoryBodies, const size_t numBodies);
726 
728 
729  // Return true if did brake on impact
730  bool solveMotionSimulation(const agx::Vec3Vector& endPositions,
731  agx::Real time,
732  const bool stopAtCollision = false,
733  const bool onlyMotionSimulation = false);
734 
735  void interpolateVelocity(const agx::Vec3Vector& endPositions, agx::Real t);
736 
737  // Return true if did brake on impact
738  bool stepInternalSimulations(const agx::ConstraintRefSetVector& motionJoints,
739  const agx::ConstraintRefSetVector& dynamicsJoints,
740  const bool stopAtCollision = false,
741  const bool onlyMotionSimulation = false);
742 
744 
745  void removeKinematicObjects();
746 
750  static agxSDK::Collection* createSourceContainer(const agx::RigidBodyRefVector& bodies,
751  const agx::ConstraintRefVector& constraints,
752  const agx::ConstraintRefVector* otherConstraint = nullptr,
753  agx::UInt64* numControlledConstraints = nullptr,
754  const agxPowerLine::PowerLineRefVector* powerlines = nullptr );
755 
759  const agxSDK::Assembly* assembly,
760  const agxPowerLine::PowerLineRefVector* powerlines);
761 
765  void setupInternalPowerlines(
766  const agxPowerLine::PowerLineRefVector& powerlines,
767  const agxPowerLine::UnitRefVector& units);
768 
772  bool haveConstraints(const agxPowerLine::PowerLine* powerline,
773  const agx::ConstraintRefSetVector& constraints);
774 
779  void markConstraints(const agxPowerLine::PowerLine* powerline,
780  const agx::ConstraintRefSetVector& constraints,
781  agx::ConstraintConstPtrSet& hashset);
782 
786  agxSDK::Collection* buildCopy(const agxSDK::Assembly* source,
787  const agxPowerLine::PowerLineRefVector& sourcePowerLines,
788  PowerLineMapping& mapping);
789 
793  InverseDynamicsStatus doSolve(const agx::RigidBody* body,
794  const agx::AffineMatrix4x4* transforms,
795  const agx::Real* timestamps,
796  size_t N,
797  agx::Real positionThreshold,
798  agx::Real rotationThreshold,
799  const bool stopAtCollision = false,
800  const bool onlyMotionSimulation = false);
801 
802  // Output data
803  agx::VectorPOD<IDJointValue> m_jointForces;
804  agx::VectorPOD<IDJointValue> m_jointAngles;
805  agx::VectorPOD<IDUnitValue> m_unitValues;
806 
807  // Source objects
808  agxSDK::AssemblyConstRef m_sourceData;
809  agxPowerLine::PowerLineRefVector m_sourcePowerLines;
810  agx::UInt64 m_sourceNumControlledConstraints;
811 
812  // Dynamics objects
813  agxSDK::SimulationRef m_dynamicsSimulation;
814  agxSDK::AssemblyRef m_dynamicsData;
815  agx::RigidBodyPtrHashSet m_dynamicsJointedBodies;
816 
817  PowerLineMapping m_dynamicsPowerLines;
818  PowerLineUnitMapping m_dynamicsPowerLineUnits;
819  agx::ConstraintConstPtrSet m_dynamicsPowerLineControlledConstraints;
820 
821  agxDriveTrain::VelocityConstraintRefVector m_dynamicsVelocityConstraints;
822 
823  // Motion objects
824  agxSDK::SimulationRef m_motionSimulation;
825  agxSDK::AssemblyRef m_motionData;
826  agx::RigidBodyPtrHashSet m_motionJointedBodies;
827  PowerLineMapping m_motionPowerLines;
828 
829  agx::RigidBodyRefVector m_kinematicTrajectoryBodies;
830  agx::Vector<agx::ConstraintRef> m_trajectoryConstraints;
831  agx::Vec3Vector m_startPositions;
832  agx::Vec3Vector m_endPositions;
833  agx::RigidBodyRefVector m_trajectoryBodies;
834 
835  // Misc
836  agx::AffineMatrix4x4 m_solveTransform1;
837  agx::AffineMatrix4x4 m_solveTransform2;
838 
839  agx::Real m_largestCompliance;
840  agx::Real m_adaptiveTime;
841  agx::UInt m_numJointValuesPerTimestep;
842  agx::UInt m_numPowerLineValuesPerTimestep;
843  agx::UInt m_currentFrameIndex;
844  };
845 
846 
847  template<typename T>
848  inline IDSpan<T>::IDSpan(const T* ptr, size_t size) :
849  m_ptr(ptr), m_size(size)
850  {
851  }
852 
853 
854  template<typename T>
855  inline const T& IDSpan<T>::at( size_t index ) const
856  {
857  return *(m_ptr + index);
858  }
859 
860 
861  template<typename T>
862  inline const T& IDSpan<T>::operator[]( size_t index ) const
863  {
864  return *(m_ptr + index);
865  }
866 
867  template<typename T>
868  inline size_t IDSpan<T>::size() const
869  {
870  return m_size;
871  }
872 
873 }
AffineMatrix4x4T< Real > AffineMatrix4x4
const T & operator[](size_t index) const
struct agxModel::IDUnitValue IDUnitValue
Struct holding information about an agxPowerLine::Unit.
Pure virtual class.
Definition: Unit.h:72
#define m_size
Definition: Vector.h:429
Struct holding information about a joint.
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
The base class for a constraint.
Definition: Constraint.h:88
InverseDynamicsStatus
Enumeration with status values returned from InverseDynamics.
Problem with timestamp and/or transform input values.
A library of agxPowerLine components designed to model the various parts of a vehicle drive train...
agxPowerLine contains a collection of connected components that transport power across a system...
Definition: MotorUnit.h:23
Class for computing forces/torques which can be used to reach a desired configuration.
Base class providing referencing counted objects.
Definition: Referenced.h:119
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:36
Provided body argument not found.
Templated vector class.
Definition: Vector.h:52
Constraint that removes all 6 DOF between two bodies, or one body and the world.
Definition: LockJoint.h:28
uint32_t UInt
Definition: Integer.h:34
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Struct holding information about an agxPowerLine::Unit.
agxPowerLine::Unit * unit
agx::ref_ptr< VelocityConstraint > VelocityConstraintRef
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
MoveOperation(agx::RigidBody *body, const agx::AffineMatrix4x4 &transform, agx::Real translationalCompliance, agx::Real rotationalCompliance, agx::Real translationalThreshold, agx::Real rotationThreshold, bool useRotation=true)
struct agxModel::IDJointValue IDJointValue
Struct holding information about a joint.
A valid pose could not be found with provided thresholds.
Struct for defining a move operation during one call to one of the solve functions of InverseDynamics...
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
AGXPHYSICS_EXPORT void setMotionControl(agx::RigidBodyPtrVector &bodies, agx::RigidBody::MotionControl motionControl)
Utility function to set motion control for a vector of rigid bodies.
A read-only view into data computed by InverseDynamics.
uint64_t UInt64
Definition: Integer.h:41
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
Sync everything given the source.
size_t size() const
Number of elements accessible via this view into the data.
#define AGXMODEL_EXPORT
Return value for successful operation.
double Real
Definition: Real.h:42
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:65
PowerLine is the base class for a system of power transfer integrated into the rigid body simulation...
Definition: PowerLine.h:70
A Collection is a collection of basic simulation objects, such as rigid bodies, constraints, geometries.
Definition: Collection.h:34
agx::Vector< InverseDynamics::MoveOperation > MoveOperationVector
agx::ref_ptr< Simulation > SimulationRef
Invalid timestamp provided as argument.
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
agx::ref_ptr< const RigidBody > RigidBodyConstRef
Definition: RigidBody.h:47
uint32_t UInt32
Definition: Integer.h:40
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:37
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints, geometries.
Definition: Assembly.h:66
const T & at(size_t index) const
Fetch value at index. Input index must be less than size.