ssqpy.model
model ¶
Submodule containing classes related to the SSQP model, a respresentation of the
system used for optimization.
SolverMode ¶
Bases: Enum
Solver Mode
The mode the solver uses for the system formulation, corresponding to the
equality constraint of the NLP
This can either be full dynamics, or a kinematics formulation
Options:
- Kinematics: The solver optimizes in a kinematic mode. This means there are no system dynamics. Instead, the generated controls correspond to velocities, while the state corresponds to the system configuration.
- ForwardDynamics: The solver optimizes with a forward dynamics approach. The state corresponds to a stacked vector of the system configuration and velocity. The generated controls correspond to torques which, when applied over time to the system, generate the output trajectory.
- InverseDynamics: The solver optimizes with an inverse dynamics approach. The state corresponds to a stacked vector of the system configuration and velocity. The generated controls correspond to accelerations. This simplifies the LQR formulation and can sometimes lead to more robust solutions. Inverse dynamics is used as a constraint, ensuring the torques producing the generated acceleration controls are within the required effort limits.
- ReducedInverseDynamics: This mode is similar to the Inverse dynamics mode. In this mode, the floating base (if present) and/or some subset of the model's joints are unactuated. While regular Inverse mode enforces this via equality constraints, this mode instead enforces this implicitly through state propagation.
Integration ¶
Bases: Enum
The type of integration used for the system dynamics
Options:
- ExplicitEuler: \(\boldsymbol{x}_{n+1}=\boldsymbol{x}_n+\boldsymbol{v}_ndt\) \(\boldsymbol{v}_{n+1}=\boldsymbol{v}_n+\boldsymbol{a}_ndt\)
- SemiImplicitEuler: \(\boldsymbol{x}_{n+1}=\boldsymbol{x}_n+\boldsymbol{v}_{n+1}dt\) \(\boldsymbol{v}_{n+1}=\boldsymbol{v}_n+\boldsymbol{a}_ndt\)
Preset ¶
Bases: Enum
Preset
Use a preset urdf instead of supplying a path
Mutually exclusive withurdf_path, mjcf_path and free_flyer, since
each preset fixes those
Options:
- NONE: No preset selected (default)
- Go2: Unitree Go2 quadruped
- A1: Unitree A1 quadruped
- Panda: Franka Emika Panda arm
- G1: Unitree G1 humanoid
- Talos: Talos humanoid
- DoublePendulum: Double pendulum
ForceDim ¶
Bases: Enum
The number of control-vector components a Force occupies
Options:
- Linear: A pure point force (e.g. ground contact), no moment
- Wrench: A full spatial force (linear + angular), e.g. rotor thrust plus reaction/drag torque
Force ¶
A external force added to a model stage via Model.addForce/addWrench.
CustomProperties ¶
Optional custom properties for non-XML models.
joint_types
property
writable
¶
The types of joints corresponding to each of the ordered joints.
Model ¶
Defines an SSQP model
An SSQP model object is a prerequisite to performing optimization using SSQP
The model contains the following:
- An optimization horizon, \(H\)
- A cost function for each stage of the horizon, \(\ell_k(\boldsymbol{x}, \boldsymbol{u}) \in \mathcal{R}\), \(0 \leq k \leq H\)
- Dynamics functions for each stage of the horizon, \(\boldsymbol{f}_k(\boldsymbol{x}, \boldsymbol{u}) \in \mathcal{R}^n\), \(0 \leq k \leq H\)
- Constraint functions for each stage of the horizon, \(\boldsymbol{c}_k(\boldsymbol{x}, \boldsymbol{u}) \in \mathcal{R}^c\), \(0 \leq k \leq H\)
Of the above, all stage cost and dynamics functions must be defined, but
constraints may be left empty.
Note that models optimize for \(H\) total steps while considering the initial
state to be constant, whereas the initial controls can be altered. This means
that there are \(H\) total controls (\(\boldsymbol{u}_0\) to \(\boldsymbol{u}_{H-1}\)) and \(H + 1\) total states (\(\boldsymbol{x}_0\) to
\(\boldsymbol{x}_H\)), with \(\boldsymbol{x}_0\) being constant. At the terminal stage (stage \(H\)), there are no
controls and, as such, stagewise functions must be appropriately defined to
ignore them. Predefined functions all have constructors to account for this.
Models may be defined either by passing custom function implementations, or
by using XML description files. Some constructors allow for defining a model
using an XML file, but without passing the required cost functions. This is
done to allow access to robot-related algorithms such as forward dynamics,
which may be used to define model costs. Note that, if such constructors are
used, the finalize method must be called before attempting to use the model
for optimization. The user is responsible for either manually assigning all
required costs, or passing them to the finalize method.
Initialize an SSQP Model.
Positional arguments (required):
horizon (int): Optimization horizon (excluding initial stage)
dt (float): Optimization time step
Keyword arguments (optional):
urdf_path (str): Path to URDF file to use for the system dynamics
mjcf_path (str): Path to MJCF file to use for the system dynamics
free_flyer (bool): Whether the model has a floating base
from_preset (ssqpy.model.Preset): Use a preset urdf instead of
supplying a path; mutually exclusive withurdf_path,
mjcf_path and free_flyer
solver_mode (ssqpy.model.SolverMode): The solver mode to solve in
integration (ssqpy.model.Integration): The integration method to use
ignore_innate_constraints (bool): Whether to ignore XML constraints
actuated_joints (list of int): List of joints to apply controls to;
if non-empty, all joints whose indices are not included will not be
actuated
finalize ¶
Finalize the model with the provided configuration.
Keyword arguments (optional):
costs (list of StageCost): Costs for each optimization stage
stage_cost (StageCost): Cost to add to each non-terminal stage
terminal_cost (StageCost): Cost for the terminal stage
constraints (list of StageConstraints): Constraints for each optimization stage
dynamics (list of StageDynamics): Dynamics for each optimization stage
custom_lower_position_bounds (np.array of float): Lower position bounds to use in place
of the values read from the XML; default XML values are used if left
empty; has no effect if innate constraints are ignored
custom_upper_position_bounds (np.array of float): Upper position bounds to use in place
of the values read from the XML; default XML values are used if left
empty; has no effect if innate constraints are ignored
custom_velocity_bounds (np.array of float): Velocity bounds to use in place of the
values read from the XML; default XML values are used if left empty;
has no effect if innate constraints are ignored
custom_torque_bounds (np.array of float): Torque bounds to use in place of the values
read from the XML; default XML values are used if left empty; has no
effect if innate constraints are ignored
init_states (np.array or list of np.array): Initial states for each optimization stage;
if np.array: initial state to use for every stage
init_controls (np.array or list of np.array): Initial controls for each optimization stage;
if np.array: initial controls to use for every stage
updateDimensions ¶
Updates the model's dimensions and linear dimensions
This is required to update constraint sizes, when constraints' enabled
state changes
addContactConstraint ¶
addContactConstraint(k, frame, mu=0.6, max_normal_force=float('inf'), eq_tol=0.0, no_slip=True, name='')
Add a contact constraint for a frame to a stage.
This includes adding a force for the contact constraint at that stage.
Unless no_slip is false, the constraint also bounds the linear velocity
of the frame to zero so that it does not slip.
getLinearStateSize ¶
Get the state size of the model after state linearization.
getLinearConstraintSize ¶
Get the constraint size of the model after linearization.
getnq ¶
Get the configuration size of the model.
Only defined if the model was initialized using an XML file.
getnv ¶
Get the velocity size of the model.
Only defined if the model was initialized using an XML file.
getnu ¶
Get the non-force control size of the model.
Only defined if the model was initialized using an XML file.
getnjoints ¶
Get the number of actuated joints of the model.
Only defined if the model was initialized using an XML file.
hasFloatingBase ¶
Whether the model has a floating base.
Only defined if the model was initialized using an XML file.
addForce ¶
Add a linear force (ForceDim.Linear) on a frame at a stage.
Only defined if the model was initialized using an XML file.
For this method to be called, the model must not be finalized.
addWrench ¶
Add a full spatial wrench (ForceDim.Wrench, force plus moment) on a
frame at a stage.
Only defined if the model was initialized using an XML file.
For this method to be called, the model must not be finalized.
getTotalForces ¶
Get the current number of forces for a stage.
Only defined if the model was initialized using an XML file.
getOrderedForceFrames ¶
Vector containing frame names, ordered 1-1 with forces for stage k
printFramesAndJoints ¶
Print the frames and joints of the model.
Only defined if the model was initialized using an XML file.
getOrderedActuatedJoints ¶
Get an ordered list of the actuated joints of the model.
Only defined if the model was initialized using an XML file.
getJointTypes ¶
Types for each joint that is part of the state (including unactuated)
forwardKinematics ¶
Perform forward kinematics on the model.
Only defined if the model was initialized using an XML file.
forwardKinematicsSE3 ¶
Perform forward kinematics on the model and return a transformation matrix.
Only defined if the model was initialized using an XML file.
frameJacobian ¶
Get a frame jacobian based on the XML model dynamics
jacobianCenterOfMass ¶
Get model center of mass jacobian for the given configuration
getAllDynamicParameters ¶
Returns a stacked vector of the current values of the loaded model's
dynamic parameters for every link
This consist of n vectors of size 10, containing the following:
\(\boldsymbol{v} = [m, m_{c,x}, m_{c,y}, m_{c_z}, I_{xx}, I_{xy}, I_{yy}, I_{xz}, I_{yz}, I_{zz}]^\top\) for each link
setAllDynamicParameters ¶
Sets the dynamic parameters for every link of the loaded model to the
provided stacked vector
Input:
parameters: A stacked vector of the dynamic parameters for each link of the model
getJointTorqueRegressor ¶
Computes and returns the joint torque regressor matrix linking the joint
torque to the dynamic parameters of each link for the given
configuration, velocity and acceleration
Input:
q: The model configurationv: The model velocitya: The model acceleration
Returns: The joint torque regressor matrix
getFullAccelerationVector ¶
Returns the full acceleration vector for a given stage
Input:
x: The model stateu: The model controlsk: The model stage (only required if model is in Reduced Inverse mode and there are present forces)
Note: For Inverse dynamics mode, this function is trivial, since it only
returns the non-force part of the control vector
Note: XML-based function
getActuatedTorqueVector ¶
Returns the actuated joint torques that realize the given controls
Input:
x: The model stateu: The model controlsk: The model stage (only required if there are forces present at that stage)
The torques come from the inverse dynamics, accounting for the forces
present at stage k. In Reduced Inverse mode, the full acceleration
vector is reconstructed first (see getFullAccelerationVector)
Note: Only defined in Inverse and Reduced Inverse dynamics modes
Note: XML-based function
neutralPose ¶
Get a configuration for the model.
Only defined if the model was initialized using an XML file.