Constraint – Base Class for Holonomic Constraints

class trep.Constraint(system[, name=None, tolerance=1e-10])
Parameters:
  • system (System) – An instance of System to add the constraint to.
  • name – A string that uniquely identifies the constraint.
  • tolerance (Float) – Tolerance to consider the constraint satisfied.

This is the base class for all holonomic constraints in a System. It should never be created directly. Constraints are created by instantiating a specific type of constraint.

See trep.constraints - Holonomic Constraints for the built-in types of constraints. Additional constraints can be added through either the Python or C-API.

Holonomic constraints restrict the allowable configurations of a mechanical system. Every constraint has an associated constraint function \(h(q) : Q \rightarrow R\). A configuration \(q\) is acceptable if and only if \(h(q) = 0\).

Constraint Objects

Constraint.system

The System that this constraint belongs to.

(read-only)

Constraint.name

The name of this constraint or None.

Constraint.index

The index of the constraint in System.constraints. This is also the index of the constraint’s force in any values of \(\lambda\) or its derivatives used through trep.

(read-only)

Constraint.tolerance

The constraint should be considered satisfied if \(|h(q)| < tolerance\). This is primarly used by the variational integrator when it finds the next configuration, or by System.satisfy_constraints().

Constraint.h()
Return type:Float

Return the value of the constraint function at the system’s current state. This function should be implemented by derived Constraints.

Constraint.h_dq(q1)
Parameters:q1 (Config) – Derivative variable
Return type:Float

Return the derivative of h with respect to q1.

Constraint.h_dqdq(q1, q2)
Parameters:
  • q1 (Config) – Derivative variable
  • q2 (Config) – Derivative variable
Return type:

Float

Return the second derivative of h with respect to q1 and q2.

Constraint.h_dqdqdq(q1, q2, q3)
Parameters:
  • q1 (Config) – Derivative variable
  • q2 (Config) – Derivative variable
  • q3 (Config) – Derivative variable
Return type:

Float

Return the third derivative of h with respect to q1, q2, and q3.

Constraint.h_dqdqdqdq(q1, q2, q3, q4)
Parameters:
  • q1 (Config) – Derivative variable
  • q2 (Config) – Derivative variable
  • q3 (Config) – Derivative variable
  • q4 (Config) – Derivative variable
Return type:

Float

Return the fourth derivative of h with respect to q1, q2, q3, and q4.

Verifying Derivatives of the Constraint

It is important that the derivatives of h() are correct. The easiest way to check their correctness is to approximate each derivative using numeric differentiation. These methods are provided to perform this test. The derivatives are only compared at the current configuration of the system. For improved coverage, try running each test several times at different configurations.

Constraint.validate_h_dq(delta=1e-6, tolerance=1e-6, verbose=False)
Constraint.validate_h_dqdq(delta=1e-6, tolerance=1e-6, verbose=False)
Constraint.validate_h_dqdqdq(delta=1e-6, tolerance=1e-6, verbose=False)
Constraint.validate_h_dqdqdqdq(delta=1e-6, tolerance=1e-6, verbose=False)
Parameters:
  • delta – Amount to add to each configuration
  • tolerance – Acceptable difference between the calculated and approximate derivatives
  • verbose – Boolean to print error and result messages.
Return type:

Boolean indicating if all tests passed

Check the derivatives against the approximate numeric derivative calculated from one less derivative (ie, approximate h_dq() from h() and h_dqdq() from h_dq()).

See System.test_derivative_dq() for details of the approximation and comparison.

Visualization

Constraint.opengl_draw()

Draw a representation of this constraint in the current OpenGL context. The OpenGL coordinate frame will be in the System’s root coordinate frame.

This function is called by the automatic visualization tools. The default implementation does nothing.