Repository navigation
Expand file tree
/
Copy pathdynamics.py
More file actions
146 lines (116 loc) · 4.82 KB
/
Copy pathdynamics.py
File metadata and controls
146 lines (116 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import pinocchio as pin
import numpy as np
import casadi
from robot_descriptions.loaders.pinocchio import load_robot_description
import pinocchio.casadi as cpin
POLE_LENGTH = 0.5
POLE_RADIUS = 0.02
POLE_MASS = 0.5 # kg
def build_augmented_model():
"""Build KUKA iiwa14 (joint 7 locked) + passive revolute pendulum at the EE.
Returns (model, visual_model, collision_model) with 7 DOF:
joints 1-6 are the actuated KUKA joints, joint 7 is the passive pendulum.
iiwa_joint_7 (EE disk rotation) is locked at q=0.
Convention: pendulum angle theta=0 is upright (rod along +Z).
"""
robot = load_robot_description("iiwa14_description")
# Lock iiwa_joint_7 (the EE disk) at neutral, reducing 7-DOF arm to 6-DOF
q_ref = pin.neutral(robot.model)
model, (visual_model, collision_model) = pin.buildReducedModel(
robot.model,
[robot.visual_model, robot.collision_model],
[7], # iiwa_joint_7
q_ref,
)
ee_frame = model.frames[model.getFrameId("iiwa_joint_ee")]
# 1. Use the EE placement directly (No extra rotation)
# The joint's local Z-axis is now identical to the EE's Z-axis.
pivot_placement = ee_frame.placement.copy()
# 2. Add Joint
pendulum_joint_id = model.addJoint(
ee_frame.parentJoint,
pin.JointModelRZ(), # Rotates around the EE Z-axis
pivot_placement,
"pendulum_joint",
)
# 3. Define the Rod perpendicular to the Z-axis
# If the rod is along Z, it won't swing.
# We put it along local X so it sticks out like a clock hand.
rod_com = np.array([POLE_LENGTH / 2.0, 0.0, 0.0])
I_val = (1.0 / 3.0) * POLE_MASS * POLE_LENGTH**2
# Inertia tensor for rotation around the pivot (Z and Y axes)
inertia_tensor = np.diag([0.001, I_val, I_val])
rod_inertia = pin.Inertia(POLE_MASS, rod_com, inertia_tensor)
model.appendBodyToJoint(pendulum_joint_id, rod_inertia, pin.SE3.Identity())
model.addJointFrame(pendulum_joint_id)
# 4. Visuals (Cylinder along X)
R_visual = pin.utils.rotate('y', np.pi/2)
rod_geom = pin.GeometryObject(
"pendulum_rod",
pendulum_joint_id,
pin.SE3(R_visual, rod_com),
pin.hppfcl.Cylinder(POLE_RADIUS, POLE_LENGTH),
)
rod_geom.meshColor = np.array([1.0, 0.0, 0.0, 1.0])
visual_model.addGeometryObject(rod_geom)
model.effortLimit[-1] = 0.0
return model, visual_model, collision_model
# Add this to dynamics.py
def build_fk_rotation(model):
"""Returns a CasADi function that computes the rotation of the pendulum joint."""
cmodel = cpin.Model(model)
cdata = cmodel.createData()
q = casadi.SX.sym("q", model.nq)
# Perform Forward Kinematics
cpin.framesForwardKinematics(cmodel, cdata, q)
# Get the rotation of the pendulum joint frame
# Note: pendulum_joint_id is joint 7 (index 7 in model.joints for iiwa + 1)
# Or get it by name for robustness:
joint_id = model.getJointId("pendulum_joint")
R_pend = cdata.oMi[joint_id].rotation
return casadi.Function("fk_rot", [q], [R_pend])
def _build_symbolic_forward_dynamics(model):
"""
Automatically builds the forward dynamics using Pinocchio's CasADi bindings.
This ensures the CasADi model EXACTLY matches the Pinocchio model.
"""
# 1. Create a symbolic version of the model
cmodel = cpin.Model(model)
cdata = cmodel.createData()
nq = model.nq
nv = model.nv
# 2. Define symbolic variables
q = casadi.SX.sym("q", nq)
v = casadi.SX.sym("v", nv)
tau = casadi.SX.sym("tau", nv)
# 3. Use Pinocchio's ABA (Articulated Body Algorithm)
# This replaces your entire manual loop and Lagrangian derivation
a = cpin.aba(cmodel, cdata, q, v, tau)
return casadi.Function("fd", [q, v, tau], [a], ["q", "v", "tau"], ["a"])
def step_numpy(model, x, u, dt, method="semi_implicit_euler"):
"""Integrate one time step using Pinocchio ABA.
Args:
method: "semi_implicit_euler" (fast, used by MPC) or "rk4" (accurate, for simulation).
"""
nq = model.nq
tau_full = np.concatenate([u, [0.0]])
data = model.createData()
if method == "rk4":
def xdot(q, v):
a = pin.aba(model, data, q, v, tau_full)
return v, np.asarray(a).copy()
q = x[:nq].copy()
v = x[nq:].copy()
dq1, dv1 = xdot(q, v)
dq2, dv2 = xdot(q + 0.5 * dt * dq1, v + 0.5 * dt * dv1)
dq3, dv3 = xdot(q + 0.5 * dt * dq2, v + 0.5 * dt * dv2)
dq4, dv4 = xdot(q + dt * dq3, v + dt * dv3)
q_next = q + (dt / 6.0) * (dq1 + 2 * dq2 + 2 * dq3 + dq4)
v_next = v + (dt / 6.0) * (dv1 + 2 * dv2 + 2 * dv3 + dv4)
else:
q = x[:nq].copy()
v = x[nq:].copy()
a = pin.aba(model, data, q, v, tau_full)
v_next = v + dt * a
q_next = q + dt * v_next
return np.concatenate([q_next, v_next])