分步实践任务说明
共 3 个核验步骤步骤 1:雅可比转置求静态关节力矩
补全 `static_torques(q, f_end, l1=L1, l2=L2)`(L₁=0.45\ \mathrmm,\ L₂=0.30\ \mathrmm),按虚功原理 \boldsymbol\tau = Jᵀ f 计算维持平衡所需的关节力矩(N·m)。 程序在 q=(0.4, 1.1)\ \mathrmrad、末端对外施力 f=(10, -25)\ \mathrmN 下打印 `tau1` 和 `tau2`。 **物理常识检查**:力矩单位 N·m 等于“力×力臂”,其大小量级应不超过 (L₁+L₂)\|f\| \approx 20.2\ \mathrmN· m。
步骤 2:可操作度与近奇异位形条件数
补全 `manipulability_metrics(q, l1=L1, l2=L2)`,对 J 做奇异值分解,返回 `(w, cond, s_max, s_min)`,其中 w = σ_\maxσ_\min,\mathrmcond = σ_\max/σ_\min(σ_\min \le 10⁻¹² 时返回 `np.inf`)。 程序对比良好位形 q₂=\pi/2 与近伸直奇异位形 q₂=0.05\ \mathrmrad,打印 `w_good`、`cond_good`、`w_near`、`cond_near`。 **物理常识检查**:趋近伸直奇异时可操作度 w = L₁ L₂ |\sin q₂| 趋近于 0,条件数急剧增大(超过 30)。
步骤 3:速度椭球与力椭球对偶绘图
补全 `ellipsoid_points(J, n_pts=100, mode="velocity")`:将单位圆上采样点映射为速度椭球 v = Ju 或力椭球 f = (Jᵀ)⁻¹u,返回 `(n_pts, 2)` 数组。 程序计算速度椭球长半轴与力椭球短半轴的乘积 `duality_prod`,并并排绘制速度椭球与力椭球对比图。 **物理常识检查**:速度椭球与力椭球长短轴正交且互为倒数,故 v_\max · f_\min = σ_\max · (1/σ_\max) = 1!
起步代码框架预览
Pythonimport numpy as np
import matplotlib.pyplot as plt
# 实验 8:平面二连杆静力学、奇异位形与可操作度椭球(第 5 章)
# 约定:平面 x-y,L1 = 0.45 m,L2 = 0.30 m;静力学 τ = J^T F;可操作度 w = sqrt(det(J J^T)) = |det(J)|。
L1, L2 = 0.45, 0.30
def jac_2r(q, l1=L1, l2=L2):
"""平面二连杆 2x2 位置雅可比矩阵。"""
q1, q2 = q
s1, c1 = np.sin(q1), np.cos(q1)
s12, c12 = np.sin(q1 + q2), np.cos(q1 + q2)
return np.array([
[-l1 * s1 - l2 * s12, -l2 * s12],
[l1 * c1 + l2 * c12, l2 * c12],
], dtype=float)
def static_torques(q, f_end, l1=L1, l2=L2):
"""已知末端对环境施加的二维静力 f_end = [fx, fy](N),返回平衡所需关节力矩 τ = J^T f_end(N·m)。"""
# TODO 步骤 1:用 J^T @ f_end 计算关节力矩
raise NotImplementedError("请补全 static_torques")
def manipulability_metrics(q, l1=L1, l2=L2):
"""对 2x2 雅可比做奇异值分解,返回 (w, cond, s_max, s_min):
- w:Yoshikawa 可操作度 |det(J)| = s_max * s_min;
- cond:条件数 s_max / s_min(s_min < 1e-12 时返回 np.inf);
- s_max, s_min:最大、最小奇异值。"""
# TODO 步骤 2:对 J 做 np.linalg.svd 求奇异值,算 w 和 cond
raise NotImplementedError("请补全 manipulability_metrics")
def ellipsoid_points(J, n_pts=100, mode="velocity"):
"""返回单位关节速度(mode='velocity')或单位关节力矩(mode='force')对应的末端椭球边界点(n_pts×2):
- 速度椭球:v = J u,||u|| = 1;
- 力椭球:f = (J^T)^{-1} u,||u|| = 1(速度与力椭球主轴正交对偶)。"""
# TODO 步骤 3:在单位圆上取角度 theta,分别映射得到速度椭球或力椭球点集
raise NotImplementedError("请补全 ellipsoid_points")
def step1():
"""步骤 1:雅可比转置求静态平衡关节力矩"""
tau = static_torques([0.4, 1.1], [10.0, -25.0])
print(f"tau1={tau[0]:.6f}")
print(f"tau2={tau[1]:.6f}")
def step2():
"""步骤 2:可操作度与奇异位形"""
w_good, cond_good, _, _ = manipulability_metrics([0.3, np.pi / 2])
w_near, cond_near, _, _ = manipulability_metrics([0.3, 0.05])
print(f"w_good={w_good:.6f}")
print(f"cond_good={cond_good:.6f}")
print(f"w_near={w_near:.6f}")
print(f"cond_near={cond_near:.6f}")
def step3():
"""步骤 3:速度可操作度椭球与力椭球对偶绘图"""
q_good = [0.3, np.pi / 2]
q_near = [0.3, 0.20]
J_g = jac_2r(q_good)
J_n = jac_2r(q_near)
Pv = ellipsoid_points(J_g, 120, "velocity")
Pf = ellipsoid_points(J_g, 120, "force")
v_max = float(np.max(np.linalg.norm(Pv, axis=1)))
f_min = float(np.min(np.linalg.norm(Pf, axis=1)))
print(f"duality_prod={v_max * f_min:.6f}")
fig, axes = plt.subplots(1, 2, figsize=(8.2, 3.8))
axes[0].plot(Pv[:, 0], Pv[:, 1], label="q2=π/2 速度椭球")
Pv_n = ellipsoid_points(J_n, 120, "velocity")
axes[0].plot(Pv_n[:, 0], Pv_n[:, 1], "--", label="q2=0.20 近奇异")
axes[0].set_title("速度可操作度椭球")
axes[0].set_xlabel("vx / (m/s)")
axes[0].set_ylabel("vy / (m/s)")
axes[0].axis("equal")
axes[0].grid(True)
axes[0].legend(fontsize=8)
axes[1].plot(Pf[:, 0], Pf[:, 1], color="#c2410c", label="q2=π/2 力椭球")
axes[1].set_title("末端力椭球(对偶)")
axes[1].set_xlabel("fx / N")
axes[1].set_ylabel("fy / N")
axes[1].axis("equal")
axes[1].grid(True)
axes[1].legend(fontsize=8)
plt.tight_layout()
plt.show()
for _step in (step1, step2, step3):
try:
_step()
except NotImplementedError as _e:
print(f"【{_step.__doc__}】还没完成:{_e}")