AGX Dynamics  2.35.0.1
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 {
100 
101 
112  typedef struct 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 
400 
428  agx::Real time, agx::Real positionThreshold, agx::Real rotationThreshold, const bool stopAtCollision = false, const bool onlyMotionSimulation = false);
429 
451  const agx::RealVector& timestamps, agx::Real positionThreshold, agx::Real rotationThreshold, const bool stopAtCollision = false, const bool onlyMotionSimulation = false);
452 
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 
511 
524 
543 
556 
567  size_t getNumFrames() const;
568 
582 
590 
604 
612 
623  IDJointValueView getJointForces(size_t frameIndex);
624 
631  IDJointValueView getJointAngles(size_t frameIndex);
632 
643 
650 
658 
666 
673 
684 
689 
694 
699 
704 
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 }
#define AGXMODEL_EXPORT
#define m_size
Definition: agx/Vector.h:429
A read-only view into data computed by InverseDynamics.
const T & at(size_t index) const
Fetch value at index. Input index must be less than size.
const T & operator[](size_t index) const
size_t size() const
Number of elements accessible via this view into the data.
Class for computing forces/torques which can be used to reach a desired configuration.
agx::AffineMatrix4x4 getLastTrajectoryTransform()
Return the last transform the solve method reached during the trajectory computation.
agx::Real getLastTrajectoryTimeStamp() const
Returns the last timestamp used during trajectory computation.
InverseDynamicsStatus solve(const agx::RigidBody *body, agx::AffineMatrix4x4 transform, agx::Real time, agx::Real positionThreshold, agx::Real rotationThreshold, const bool stopAtCollision=false, const bool onlyMotionSimulation=false)
Try to find a pose for the structure so the specified body has the requested transform after the spec...
IDUnitValueView getPowerLineValues(size_t frameIndex)
Fetch possible values for agxPowerLine Units.
InverseDynamicsStatus solveLinear(const agx::RigidBody *body, agx::AffineMatrix4x4 transform, agx::Real time, agx::Real positionThreshold, agx::Real rotationThreshold, const bool stopAtCollision=false, const bool onlyMotionSimulation=false)
Try to find a pose for the structure so the specified body has the requested transform after the spec...
agx::UInt getCurrentFrameIndex() const
Return the internal frame count.
bool sync(agxModel::InverseDynamics::SyncLevel level=agxModel::InverseDynamics::FULL)
This method will synchronize transforms and velocities so that solve calls operate on data that is in...
InverseDynamicsStatus gravityCompensation()
Compute joint forces needed to compensate for gravity.
bool setMotionControl(const agx::RigidBody *body, agx::RigidBody::MotionControl mc)
Update the motion control to specified value for the internal bodies that are mirroring the specified...
agx::Real computeRotationalDifference(const agx::RigidBody *body) const
Compute the rotational difference between body and the matching internal clone that is used in dynami...
InverseDynamics::InverseDynamicsStatus finalizeAdaptiveSolveN()
End the adaptive solve.
agx::AffineMatrix4x4 getLastDynamicsTransform()
Return the last transform the solve method reached during the force computations.
agx::Real getLastDynamicsTimeStamp() const
Returns the last timestamp reached during the force computations.
void forceStepDynamicsSimulation()
Will step the dynamics simulation one time step.
@ FULL
Sync everything given the source.
const agxSDK::Assembly * getMotionData() const
Fetch the internal Collection holding the cloned bodies and constraints that are used for motion/traj...
InverseDynamics(const agxSDK::Simulation *sim, const agx::RigidBodyRefVector &bodies, const agx::ConstraintRefVector &constraints)
Create an InverseDynamics instance holding structures matching the input bodies and constraints.
const agxSDK::Assembly * getDynamicsData() const
Fetch the internal Collection holding the cloned bodies and constraints that are used for dynamics/fo...
InverseDynamicsStatus solve(const agx::RigidBody *body, const agx::AffineMatrix4x4Vector &transforms, const agx::RealVector &timestamps, agx::Real positionThreshold, agx::Real rotationThreshold, const bool stopAtCollision=false, const bool onlyMotionSimulation=false)
Similar to solve, but with a series of transforms and delta timestamps.
InverseDynamics(const agxSDK::Simulation *sim, const agx::RigidBodyRefVector &bodies, const agx::ConstraintRefVector &constraints, const agx::ConstraintRefVector &otherConstraints)
This constructor is similar to the three argument constructor but also includes an additional vector ...
InverseDynamics(const agxSDK::Simulation *sim, const agx::RigidBodyRefVector &bodies, const agx::ConstraintRefVector &constraints, const agx::ConstraintRefVector &otherConstraints, const agxPowerLine::PowerLineRefVector &powerlines, const agxPowerLine::UnitRefVector &units)
Create an InverseDynamics instance holding structures matching the input bodies, constraints and powe...
agx::Real computeTranslationalDifference(const agx::RigidBody *body, const agx::AffineMatrix4x4 &transform) const
Compute the translational difference between input transform and the internal body used for dynamics ...
InverseDynamics(const agxSDK::Simulation *sim, const agx::RigidBodyRefVector &bodies, const agx::ConstraintRefVector &constraints, const agxPowerLine::PowerLineRefVector &powerlines, const agxPowerLine::UnitRefVector &units)
Create an InverseDynamics instance holding structures matching the input bodies, constraints and powe...
agx::Real computeTranslationalDifference(const agx::RigidBody *body) const
Compute the translational difference between body and the matching internal clone that is used in dyn...
const agxSDK::Assembly * getSourceData() const
Fetch the internal Collection holding the input bodies and constraints.
InverseDynamics::InverseDynamicsStatus solveLinearN(const InverseDynamics::MoveOperationVector &moves, agx::Real time, const bool stopAtCollision=false, const bool onlyMotionSimulation=false)
This solve method will try to move the specified bodies along individual linear paths,...
agx::Vector< InverseDynamics::MoveOperation > MoveOperationVector
agx::Real computeRotationalDifference(const agx::RigidBody *body, const agx::AffineMatrix4x4 &transform) const
Compute the rotational difference between input transform and the internal body used for dynamics com...
InverseDynamics::InverseDynamicsStatus updateAdaptiveSolveN(const InverseDynamics::MoveOperationVector &moves, agx::Real time)
This update will define new target transforms for the moves already assigned at intitializeAdaptiveSo...
IDJointValueView getJointAngles(size_t frameIndex)
If solve saved angles, fetch the values for specified frame.
InverseDynamicsStatus
Enumeration with status values returned from InverseDynamics.
@ ID_INVALID_TIME
Invalid timestamp provided as argument.
@ ID_BODY_NOT_FOUND
Provided body argument not found.
@ ID_VALID_POSE_NOT_FOUND
A valid pose could not be found with provided thresholds.
@ ID_SUCCESS
Return value for successful operation.
@ ID_BAD_ARGUMENTS
Problem with timestamp and/or transform input values.
void forceStepMotionSimulation()
Will step the motion simulation one time step.
IDJointValueView getJointForces(size_t frameIndex)
Fetch joint forces produced by solve call or gravityCompensation.
InverseDynamics::InverseDynamicsStatus stepAdaptiveSolveN()
Step the adaptive solve one timestep.
size_t getNumFrames() const
Return the number of frames worth of force data that is available.
InverseDynamics::InverseDynamicsStatus initializeAdaptiveSolveN(const InverseDynamics::MoveOperationVector &moves, agx::Real time)
Initialize a solve for N bodies, with desired end transforms.
PowerLine is the base class for a system of power transfer integrated into the rigid body simulation.
Definition: PowerLine.h:71
Pure virtual class.
Definition: Unit.h:73
An assembly is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Assembly.h:67
A Collection is a collection of basic simulation objects, such as rigid bodies, constraints,...
Definition: Collection.h:35
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
The base class for a constraint.
Definition: Constraint.h:89
Constraint that removes all 6 DOF between two bodies, or one body and the world.
Definition: LockJoint.h:29
Base class providing referencing counted objects.
Definition: Referenced.h:114
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
MotionControl
The MotionControl enumeration indicates what makes a RigidBody move.
Definition: RigidBody.h:66
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:38
Vector containing 'raw' data.
Definition: agx/Vector.h:246
Templated vector class.
Definition: agx/Vector.h:53
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
A library of agxPowerLine components designed to model the various parts of a vehicle drive train.
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:37
struct agxModel::IDUnitValue IDUnitValue
Struct holding information about an agxPowerLine::Unit.
struct agxModel::IDJointValue IDJointValue
Struct holding information about a joint.
agxPowerLine contains a collection of connected components that transport power across a system.
Definition: MotorUnit.h:24
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
agx::ref_ptr< Simulation > SimulationRef
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
uint64_t UInt64
Definition: Integer.h:41
uint32_t UInt
Definition: Integer.h:34
double Real
Definition: Real.h:42
AffineMatrix4x4T< Real > AffineMatrix4x4
agx::ref_ptr< const RigidBody > RigidBodyConstRef
Definition: RigidBody.h:47
Struct holding information about a joint.
Struct holding information about an agxPowerLine::Unit.
agxPowerLine::Unit * unit
Struct for defining a move operation during one call to one of the solve functions of InverseDynamics...
MoveOperation(agx::RigidBody *body, const agx::AffineMatrix4x4 &transform, agx::Real translationalCompliance, agx::Real rotationalCompliance, agx::Real translationalThreshold, agx::Real rotationThreshold, bool useRotation=true)