AGXDynamics  2.35.0.3
InverseDynamics.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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 }
agx::UInt64
uint64_t UInt64
Definition: Integer.h:41
agxModel::InverseDynamics::MoveOperation::MoveOperation
MoveOperation(agx::RigidBody *body, const agx::AffineMatrix4x4 &transform, agx::Real translationalCompliance, agx::Real rotationalCompliance, agx::Real translationalThreshold, agx::Real rotationThreshold, bool useRotation=true)
Definition: InverseDynamics.h:199
agxModel::IDSpan::size
size_t size() const
Number of elements accessible via this view into the data.
Definition: InverseDynamics.h:868
agxModel::InverseDynamics::MoveOperation::m_useRotation
bool m_useRotation
Definition: InverseDynamics.h:223
agxModel::InverseDynamics::ID_SUCCESS
@ ID_SUCCESS
Return value for successful operation.
Definition: InverseDynamics.h:178
agxModel::InverseDynamics::MoveOperation::m_translationalCompliance
agx::Real m_translationalCompliance
Definition: InverseDynamics.h:219
agx::Real
double Real
Definition: Real.h:42
Vector.h
agxModel
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:36
agxData::transform
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
agx::Vector
Templated vector class.
Definition: Vector.h:52
agx::RigidBody::MotionControl
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:65
agx::RigidBody
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
agx::RigidBodyConstRef
agx::ref_ptr< const RigidBody > RigidBodyConstRef
Definition: RigidBody.h:47
export.h
agxModel::InverseDynamics::FULL
@ FULL
Sync everything given the source.
Definition: InverseDynamics.h:190
agxModel::InverseDynamics
Class for computing forces/torques which can be used to reach a desired configuration.
Definition: InverseDynamics.h:171
agxDriveTrain
A library of agxPowerLine components designed to model the various parts of a vehicle drive train.
agx::AffineMatrix4x4T< Real >
agxModel::InverseDynamics::MoveOperation::m_rotationalCompliance
agx::Real m_rotationalCompliance
Definition: InverseDynamics.h:220
agxSDK::SimulationRef
agx::ref_ptr< Simulation > SimulationRef
Definition: StatisticsTrack.h:26
agxModel::InverseDynamics::MoveOperationVector
agx::Vector< InverseDynamics::MoveOperation > MoveOperationVector
Definition: InverseDynamics.h:226
agxSDK::Collection
A Collection is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Collection.h:34
agxModel::InverseDynamics::ID_INVALID_TIME
@ ID_INVALID_TIME
Invalid timestamp provided as argument.
Definition: InverseDynamics.h:179
agx::VectorPOD
Vector containing 'raw' data.
Definition: Vector.h:245
agx::observer_ptr< RigidBody >
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agxModel::InverseDynamics::MoveOperation::m_body
agx::RigidBodyObserver m_body
Definition: InverseDynamics.h:217
agxModel::IDJointValue::angleIndex
agx::UInt32 angleIndex
Definition: InverseDynamics.h:97
agxModel::InverseDynamics::InverseDynamicsStatus
InverseDynamicsStatus
Enumeration with status values returned from InverseDynamics.
Definition: InverseDynamics.h:177
AffineMatrix4x4.h
agxPowerLine::Unit
Pure virtual class.
Definition: Unit.h:72
agxSDK::Assembly
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:66
agx::ref_ptr
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
agxModel::IDUnitValue
struct agxModel::IDUnitValue IDUnitValue
Struct holding information about an agxPowerLine::Unit.
agxSDK::Simulation
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
agx::SetVector
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:37
agxModel::InverseDynamics::SyncLevel
SyncLevel
Definition: InverseDynamics.h:186
agx::AffineMatrix4x4
AffineMatrix4x4T< Real > AffineMatrix4x4
Definition: AffineMatrix4x4.h:384
agxModel::IDJointValue::jointIndex
agx::UInt32 jointIndex
Definition: InverseDynamics.h:96
agxUtil::setMotionControl
AGXPHYSICS_EXPORT void setMotionControl(agx::RigidBodyPtrVector &bodies, agx::RigidBody::MotionControl motionControl)
Utility function to set motion control for a vector of rigid bodies.
agxModel::IDJointValue::value
agx::Real value
Definition: InverseDynamics.h:98
agxModel::IDUnitValue::gradient
agx::Real gradient
Definition: InverseDynamics.h:116
agxModel::IDUnitValue::dimensionIndex
agx::UInt32 dimensionIndex
Definition: InverseDynamics.h:114
agxPowerLine::PowerLine
PowerLine is the base class for a system of power transfer integrated into the rigid body simulation.
Definition: PowerLine.h:70
m_size
#define m_size
Definition: Vector.h:429
agxModel::InverseDynamics::MoveOperation::m_transform
agx::AffineMatrix4x4 m_transform
Definition: InverseDynamics.h:218
agx::LockJoint
Constraint that removes all 6 DOF between two bodies, or one body and the world.
Definition: LockJoint.h:28
agx::HashSet< agx::RigidBody * >
agxModel::InverseDynamics::ID_BODY_NOT_FOUND
@ ID_BODY_NOT_FOUND
Provided body argument not found.
Definition: InverseDynamics.h:181
ref_ptr.h
agxModel::InverseDynamics::MoveOperation
Struct for defining a move operation during one call to one of the solve functions of InverseDynamics...
Definition: InverseDynamics.h:197
RigidBody.h
agxModel::IDJointValue
struct agxModel::IDJointValue IDJointValue
Struct holding information about a joint.
HashSet.h
agxModel::IDUnitValue
Struct holding information about an agxPowerLine::Unit.
Definition: InverseDynamics.h:112
agxModel::IDSpan::at
const T & at(size_t index) const
Fetch value at index. Input index must be less than size.
Definition: InverseDynamics.h:855
agx::Referenced
Base class providing referencing counted objects.
Definition: Referenced.h:113
agxPowerLine
agxPowerLine contains a collection of connected components that transport power across a system.
Definition: MotorUnit.h:23
agx::UInt
uint32_t UInt
Definition: Integer.h:34
agxModel::InverseDynamics::MoveOperation::m_rotationThreshold
agx::Real m_rotationThreshold
Definition: InverseDynamics.h:222
agxModel::InverseDynamics::ID_VALID_POSE_NOT_FOUND
@ ID_VALID_POSE_NOT_FOUND
A valid pose could not be found with provided thresholds.
Definition: InverseDynamics.h:182
agxModel::IDSpan
A read-only view into data computed by InverseDynamics.
Definition: InverseDynamics.h:127
agx::Constraint
The base class for a constraint.
Definition: Constraint.h:88
agxModel::InverseDynamics::MoveOperation::MoveOperation
MoveOperation()
Definition: InverseDynamics.h:212
AGXMODEL_EXPORT
#define AGXMODEL_EXPORT
Definition: agxModel/export.h:42
agxModel::IDSpan::operator[]
const T & operator[](size_t index) const
Definition: InverseDynamics.h:862
agxSDK
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
agxModel::IDJointValue
Struct holding information about a joint.
Definition: InverseDynamics.h:95
agxModel::InverseDynamics::MoveOperation::m_translationalThreshold
agx::Real m_translationalThreshold
Definition: InverseDynamics.h:221
agxModel::InverseDynamics::ID_BAD_ARGUMENTS
@ ID_BAD_ARGUMENTS
Problem with timestamp and/or transform input values.
Definition: InverseDynamics.h:180
Real.h
agx::UInt32
uint32_t UInt32
Definition: Integer.h:40
agxModel::IDUnitValue::value
agx::Real value
Definition: InverseDynamics.h:115
agxModel::IDUnitValue::unit
agxPowerLine::Unit * unit
Definition: InverseDynamics.h:113