SymPy 生物力学建模实战:为人体手臂-杠杆模型添加 DeGroote2016 肌肉肌腱作动器
SymPy 生物力学建模实战为人体手臂-杠杆模型添加 DeGroote2016 肌肉肌腱作动器【免费下载链接】sympyA computer algebra system written in pure Python项目地址: https://gitcode.com/GitHub_Trending/sy/sympy导读本文基于 SymPy 仓库中的官方教程 biomechanical-model-example.rst完整演示如何利用sympy.physics.biomechanics包为一个由sympy.physics.mechanics建立的人体手臂推动杠杆多刚体模型添加肌肉与肌腱力产生元件包括 DeGroote2016 一阶激活动力学、线性肌腱路径、绕圆形凸起的自定义包裹路径以及最终的方程推导与SciPy数值仿真。读完本文你将掌握从多体系统建模 → 肌肉作动器定制 → 运动方程生成 → 数值模拟的完整生物力学建模技术路线。模型描述本文以人体手臂推动一个杠杆的运动为场景演示如何用sympy.physics.biomechanics增强由sympy.physics.mechanics建立的多体模型使其包含模拟肌肉与肌腱的力产生元件。杠杆 A 可绕 $\hat{n}_z$ 轴以角度 $q_1$ 旋转其质心位于旋转轴上。肩部位于点 $P_2$上臂 C 可绕 $\hat{n}_y$ 以角度 $q_2$ 伸展、绕 $\hat{b}_z$ 以角度 $q_3$ 旋转。肘部位于点 $P_3$下臂可绕 $\hat{c}_y$ 以角度 $q_4$ 屈曲手部位于点 $P_4$。通过强制 $\mathbf{r}^{P_4/O} \mathbf{r}^{P_1/O}$ 将手部约束在杠杆上。杠杆、上臂和下臂均按薄圆柱体处理以简化惯性。系统引入两个肌肉肌腱模型分别代表肱二头肌biceps与肱三头肌triceps。两个肌肉附着点 $C_m$ 与 $D_m$ 分别固定在上臂与下臂上。肱二头肌沿从 $C_m$ 到 $D_m$ 的线性路径作用收缩时引起肘部屈曲肱三头肌则绕一个圆心在 $P_3$、法向为 $\hat{c}_y$、半径为 $r$ 的圆弧包裹也附着于同两个点上收缩时引起肘部伸展。首先导入相关模块 import sympy as sm import sympy.physics.mechanics as me import sympy.physics.biomechanics as bm定义变量引入四个坐标 $\mathbf{q} [q_1, q_2, q_3, q_4]^T$分别表示杠杆转角、肩部伸展、肩部旋转与肘部屈曲同时需要广义速度 $\mathbf{u} [u_1, u_2, u_3, u_4]^T$定义为 $\mathbf{u} \dot{\mathbf{q}}$ q1, q2, q3, q4 me.dynamicsymbols(q1, q2, q3, q4, realTrue) u1, u2, u3, u4 me.dynamicsymbols(u1, u2, u3, u4, realTrue)机械系统所需的常量参数如下表参数含义$d_x, l_A$沿 $\hat{n}_x$ 与 $\hat{a}_y$ 方向将 $P_1$ 相对 $O$ 定位$d_y, d_z$沿 N 单位矢量方向将 $P_2$ 相对 $O$ 定位$l_C, l_D$上臂与下臂的长度$m_A, m_C, m_D$杠杆、上臂与下臂的质量$g$重力加速度$k$杠杆线性扭转弹簧系数$c$杠杆线性扭转阻尼系数 dx, dy, dz sm.symbols(dx, dy, dz, realTrue, nonnegativeTrue) lA, lC, lD sm.symbols(lA, lC, lD, realTrue, positiveTrue) mA, mC, mD sm.symbols(mA, mC, mD, realTrue, positiveTrue) g, k, c, r sm.symbols(g, k, c, r, realTrue, positiveTrue)定义运动学定义示意图中的全部参考系与点。$C_o$ 与 $D_o$ 分别为上臂与下臂的质心 N, A, B, C, D sm.symbols(N, A, B, C, D, clsme.ReferenceFrame) O, P1, P2, P3, P4 sm.symbols(O, P1, P2, P3, P4 , clsme.Point) Ao, Co, Cm, Dm, Do sm.symbols(Ao, Co, Cm, Dm, Do, clsme.Point)参考系的方位与角速度定义为 A.orient_axis(N, q1, N.z) B.orient_axis(N, q2, N.y) C.orient_axis(B, q3, B.z) D.orient_axis(C, q4, C.y) A.set_ang_vel(N, u1*N.z) B.set_ang_vel(N, u2*N.y) C.set_ang_vel(B, u3*B.z) D.set_ang_vel(C, u4*C.y)各点的位置与速度定义为 Ao.set_pos(O, dx*N.x) P1.set_pos(Ao, lA*A.y) P2.set_pos(O, dy*N.y dz*N.z) Co.set_pos(P2, lC/2*C.z) Cm.set_pos(P2, 1*lC/3*C.z) P3.set_pos(P2, lC*C.z) Dm.set_pos(P3, 1*lD/3*D.z) Do.set_pos(P3, lD/2*D.z) P4.set_pos(P3, lD*D.z) O.set_vel(N, 0) Ao.set_vel(N, 0) P1.v2pt_theory(Ao, N, A) - lA*u1(t)*A.x P2.set_vel(N, 0) Co.v2pt_theory(P2, N, C) lC*u2(t)*cos(q3(t))/2*C.x - lC*u2(t)*sin(q3(t))/2*C.y Cm.v2pt_theory(P2, N, C) lC*u2(t)*cos(q3(t))/3*C.x - lC*u2(t)*sin(q3(t))/3*C.y P3.v2pt_theory(P2, N, C) lC*u2(t)*cos(q3(t))*C.x - lC*u2(t)*sin(q3(t))*C.y Dm.v2pt_theory(P3, N, D) lC*u2(t)*cos(q3(t))*C.x - lC*u2(t)*sin(q3(t))*C.y lD*(u2(t)*cos(q3(t)) u4(t))/3*D.x - lD*(u2(t)*sin(q3(t))*cos(q4(t)) - u3(t)*sin(q4(t)))/3*D.y Do.v2pt_theory(P3, N, D) lC*u2(t)*cos(q3(t))*C.x - lC*u2(t)*sin(q3(t))*C.y lD*(u2(t)*cos(q3(t)) u4(t))/2*D.x - lD*(u2(t)*sin(q3(t))*cos(q4(t)) - u3(t)*sin(q4(t)))/2*D.y P4.v2pt_theory(P3, N, D) lC*u2(t)*cos(q3(t))*C.x - lC*u2(t)*sin(q3(t))*C.y lD*(u2(t)*cos(q3(t)) u4(t))*D.x - lD*(u2(t)*sin(q3(t))*cos(q4(t)) - u3(t)*sin(q4(t)))*D.y为使手部 $P_4$ 保持在杠杆 $P_1$ 上需要三条完整holonomic约束方程 holonomic (P4.pos_from(O) - P1.pos_from(O)).to_matrix(N)定义惯性将杠杆、上臂与下臂按薄圆柱体处理形成惯性张量与刚体 IA me.Inertia(me.inertia(A, mA/12*lA**2, mA/2*lA**2, mA/12*lA**2), Ao) IC me.Inertia(me.inertia(C, mC/12*lC**2, mC/12*lC**2, mC/2*lC**2), Co) ID me.Inertia(me.inertia(D, mD/12*lD**2, mD/12*lD**2, mD/2*lD**2), Do) lever me.RigidBody(lever, masscenterAo, frameA, massmA, inertiaIA) u_arm me.RigidBody(upper arm, masscenterCo, frameC, massmC, inertiaIC) l_arm me.RigidBody(lower arm, masscenterDo, frameD, massmD, inertiaID)定义力系统处于地球重力场中作用于上臂与下臂的重力为 gravC me.Force(u_arm, mC*g*N.z) gravD me.Force(l_arm, mD*g*N.z)杠杆本身有惯性此外再添加一个线性扭转弹簧-阻尼器为手臂提供额外的按压与拉拽阻力 lever_resistance me.Torque(A, (-k*q1 - c*u1)*N.z)肱二头肌Biceps将肱二头肌建模为一个在两个肌肉附着点 $C_m$ 与 $D_m$ 之间收缩的作动器。它接受指定的激励excitation输入并假设相关肌腱为刚性。肌肉肌腱作动器模型由两部分组成作用路径pathway与激活动力学activation dynamics。肱二头肌沿LinearPathway作用并使用源自 [DeGroote2016] 的肌肉动力学实现。先创建线性路径 biceps_pathway me.LinearPathway(Cm, Dm)激活模型既可以完全符号化创建也可以使用 [DeGroote2016] 中经过调参的数值参数创建推荐 biceps_activation bm.FirstOrderActivationDeGroote2016.with_defaults(biceps)最后命名并构造完整的肌肉肌腱作动器模型 biceps bm.MusculotendonDeGroote2016.with_defaults(biceps, biceps_pathway, biceps_activation)肱三头肌Triceps肱三头肌需要一个自定义路径来处理肌肉与肌腱绕半径 $r$ 圆弧的包裹特性。该路径由两段长度不变的直线段与一段随肘部伸展/屈曲而变化的圆弧组成。作用于上臂与下臂的力可以建模为作用在点 $C_m$ 与 $D_m$ 上、始终平行于直线段的力加上圆弧两端等值反向力在 $P_3$ 处的合力。为此需要继承PathwayBase并实现计算路径长度、路径伸展速度与载荷的方法。该自定义类假设两个刚体之间存在销钉关节两个肌肉附着点分别固定在各自刚体上且销钉点与两个附着点位于同一平面内该平面垂直于销钉轴。同时假设销钉关节坐标与示意图中的 $q_4$ 一致且 $0 \le q_4 \le \pi$圆弧半径为 $r$。 class ExtensorPathway(me.PathwayBase): ... ... def __init__(self, origin, insertion, axis_point, axis, parent_axis, ... child_axis, radius, coordinate): ... A custom pathway that wraps a circular arc around a pin joint. ... ... This is intended to be used for extensor muscles. For example, a ... triceps wrapping around the elbow joint to extend the upper arm at ... the elbow. ... ... Parameters ... ... origin : Point ... Muscle origin point fixed on the parent body (A). ... insertion : Point ... Muscle insertion point fixed on the child body (B). ... axis_point : Point ... Pin joint location fixed in both the parent and child. ... axis : Vector ... Pin joint rotation axis. ... parent_axis : Vector ... Axis fixed in the parent frame (A) that is directed from the pin ... joint point to the muscle origin point. ... child_axis : Vector ... Axis fixed in the child frame (B) that is directed from the pin ... joint point to the muscle insertion point. ... radius : sympyfiable ... Radius of the arc that the muscle wraps around. ... coordinate : sympfiable function of time ... Joint angle, zero when parent and child frames align. Positive ... rotation about the pin joint axis, B with respect to A. ... ... Notes ... ... ... Only valid for coordinate 0. ... ... ... super().__init__(origin, insertion) ... ... self.origin origin ... self.insertion insertion ... self.axis_point axis_point ... self.axis axis.normalize() ... self.parent_axis parent_axis.normalize() ... self.child_axis child_axis.normalize() ... self.radius radius ... self.coordinate coordinate ... ... self.origin_distance axis_point.pos_from(origin).magnitude() ... self.insertion_distance axis_point.pos_from(insertion).magnitude() ... self.origin_angle sm.asin(self.radius/self.origin_distance) ... self.insertion_angle sm.asin(self.radius/self.insertion_distance) ... ... property ... def length(self): ... Length of the pathway. ... ... Length of two fixed length line segments and a changing arc length ... of a circle. ... ... ... ... angle self.origin_angle self.coordinate self.insertion_angle ... arc_length self.radius*angle ... ... origin_segment_length self.origin_distance*sm.cos(self.origin_angle) ... insertion_segment_length self.insertion_distance*sm.cos(self.insertion_angle) ... ... return origin_segment_length arc_length insertion_segment_length ... ... property ... def extension_velocity(self): ... Extension velocity of the pathway. ... ... Arc length of circle is the only thing that changes when the elbow ... flexes and extends. ... ... ... return self.radius*self.coordinate.diff(me.dynamicsymbols._t) ... ... def to_loads(self, force_magnitude): ... Loads in the correct format to be supplied to KanesMethod. ... ... Forces applied to origin, insertion, and P from the muscle wrapped ... over circular arc of radius r. ... ... ... ... parent_tangency_point me.Point(Aw) # fixed in parent ... child_tangency_point me.Point(Bw) # fixed in child ... ... parent_tangency_point.set_pos( ... self.axis_point, ... -self.radius*sm.cos(self.origin_angle)*self.parent_axis.cross(self.axis) ... self.radius*sm.sin(self.origin_angle)*self.parent_axis, ... ) ... child_tangency_point.set_pos( ... self.axis_point, ... self.radius*sm.cos(self.insertion_angle)*self.child_axis.cross(self.axis) ... self.radius*sm.sin(self.insertion_angle)*self.child_axis), ... ... parent_force_direction_vector self.origin.pos_from(parent_tangency_point) ... child_force_direction_vector self.insertion.pos_from(child_tangency_point) ... force_on_parent force_magnitude*parent_force_direction_vector.normalize() ... force_on_child force_magnitude*child_force_direction_vector.normalize() ... loads [ ... me.Force(self.origin, force_on_parent), ... me.Force(self.axis_point, -(force_on_parent force_on_child)), ... me.Force(self.insertion, force_on_child), ... ] ... return loads ...路径长度是两段直线段长度与随销钉关节坐标变化的圆弧长度之和伸展速度即为弧长对时间的导数载荷由三个力组成两个分别推/拉路径直线部分起点与终点的力以及圆弧两端推拉力在肘部的合力效果。PathwayBase抽象基类来自sympy.physics.mechanics.pathwayExtensorPathway通过实现length、extension_velocity与to_loads三个接口完成自定义。自定义路径定义完成后肱三头肌的肌肉肌腱作动器与肱二头肌的构造方式相同 triceps_pathway ExtensorPathway(Cm, Dm, P3, B.y, -C.z, D.z, r, q4) triceps_activation bm.FirstOrderActivationDeGroote2016.with_defaults(triceps) triceps bm.MusculotendonDeGroote2016.with_defaults(triceps, triceps_pathway, triceps_activation)最后将所有载荷组装到一个列表中 loads biceps.to_loads() triceps.to_loads() [lever_resistance, gravC, gravD]运动方程所有载荷定义完毕后即可生成系统运动方程。系统有三条完整约束因此只有一个自由度 kane me.KanesMethod( ... N, ... (q1,), ... (u1,), ... kd_eqs( ... u1 - q1.diff(), ... u2 - q2.diff(), ... u3 - q3.diff(), ... u4 - q4.diff(), ... ), ... q_dependent(q2, q3, q4), ... configuration_constraintsholonomic, ... u_dependent(u2, u3, u4), ... ) ... Fr, Frs kane.kanes_equations((lever, u_arm, l_arm), loads)注意KanesMethod中仅 $q_1$、$u_1$ 为独立坐标/速度$q_2, q_3, q_4$ 与 $u_2, u_3, u_4$ 为依赖变量通过holonomic完整约束与kd_eqs运动学微分方程关联。不含 $\dot{\mathbf{u}}$ 线性项的部分包含肌肉力除坐标与广义速度外还是激活状态变量的函数 me.find_dynamicsymbols(kane.forcing) {a_biceps(t), a_triceps(t), q1(t), q2(t), q3(t), q4(t), u1(t), u2(t), u3(t), u4(t)}它们还包含与肌肉模型相关的新常量参数 kane.forcing.free_symbols {F_M_max_biceps, F_M_max_triceps, c, g, k, lA, lC, lD, l_M_opt_biceps, l_M_opt_triceps, l_T_slack_biceps, l_T_slack_triceps, mC, mD, r, t}这些参数即MusculotendonDeGroote2016暴露的峰值等长力F_M_max、最优纤维长度l_M_opt与肌腱松弛长度l_T_slack对应源码 musculotendon.py 中_F_M_max、_l_M_opt、_l_T_slack符号的生成逻辑。肌肉激活微分方程每块肌肉的激活状态是与两条新的一阶微分方程关联的新状态变量可通过作动器模型访问 biceps.rhs() Matrix([[((1/2 - tanh(10.0*a_biceps(t) - 10.0*e_biceps(t))/2)/(0.0225*a_biceps(t) 0.0075) 16.6666666666667*(3*a_biceps(t)/2 1/2)*(tanh(10.0*a_biceps(t) - 10.0*e_biceps(t))/2 1/2))*(-a_biceps(t) e_biceps(t))]]) triceps.rhs() Matrix([[((1/2 - tanh(10.0*a_triceps(t) - 10.0*e_triceps(t))/2)/(0.0225*a_triceps(t) 0.0075) 16.6666666666667*(3*a_triceps(t)/2 1/2)*(tanh(10.0*a_triceps(t) - 10.0*e_triceps(t))/2 1/2))*(-a_triceps(t) e_triceps(t))]])该表达式来源于FirstOrderActivationDeGroote2016类。从源码 activation.py 可见其动力学方程为$$\frac{da}{dt} \left(\frac{\frac{1}{2} a_0}{\tau_a \left(\frac{1}{2} \frac{3a}{2}\right)} \frac{\left(\frac{1}{2} \frac{3a}{2}\right)\left(\frac{1}{2} - a_0\right)}{\tau_d}\right)\left(e - a\right), \quad a_0 \frac{\tanh(b(e-a))}{2}$$其中with_defaults使用的发布常数为 $\tau_a 0.015$、$\tau_d 0.060$、$b 10$。ActivationBase抽象基类还定义了e/excitation、a/activation、x/state_vars、r/input_vars、p/constants、M、F、rhs()等接口其中M x F是激活动力学的隐式 ODE 形式。类内部使用dynamicsymbols(fe_{name})与dynamicsymbols(fa_{name})生成动态符号这就是 $e_{\text{biceps}}$、$a_{\text{biceps}}$ 命名来源。将两块肌肉的激活微分方程存入同一矩阵 dadt biceps.rhs().col_join(triceps.rhs())数值化系统微分方程系统的完整微分方程组具有如下形式$$\begin{bmatrix} \mathbf{I} \mathbf{0} \mathbf{0} \ \mathbf{0} \mathbf{M}_d \mathbf{0} \ \mathbf{0} \mathbf{0} \mathbf{I} \end{bmatrix} \begin{bmatrix} \dot{\mathbf{q}} \ \dot{\mathbf{u}} \ \dot{\mathbf{a}} \end{bmatrix} \begin{bmatrix} \mathbf{u} \ \mathbf{g}_d(\mathbf{q}, \mathbf{u}, \mathbf{a}) \ \mathbf{g}_a(\mathbf{a}, \mathbf{e}) \end{bmatrix}$$本例中只有动力学微分方程需要求解线性系统转换为显式形式。收集状态、输入与常量为使用lambdify求值需先收集全部状态、输入与常量变量。状态向量由坐标、广义速度与两块肌肉的激活状态组成$\mathbf{x} \begin{bmatrix}\mathbf{q}\\mathbf{u}\\mathbf{a}\end{bmatrix}$。 q, u kane.q, kane.u a biceps.x.col_join(triceps.x) x q.col_join(u).col_join(a) x Matrix([ [ q1(t)], [ q2(t)], [ q3(t)], [ q4(t)], [ u1(t)], [ u2(t)], [ u3(t)], [ u4(t)], [ a_biceps(t)], [ a_triceps(t)]])唯一的指定输入是两块肌肉的激励 e biceps.r.col_join(triceps.r) e Matrix([ [ e_biceps(t)], [ e_triceps(t)]])常量由几何、质量、当地重力常数、杠杆刚度与阻尼系数以及肌肉的各种参数构成 p sm.Matrix([ ... dx, ... dy, ... dz, ... lA, ... lC, ... lD, ... mA, ... mC, ... mD, ... g, ... k, ... c, ... r, ... biceps.F_M_max, ... biceps.l_M_opt, ... biceps.l_T_slack, ... triceps.F_M_max, ... triceps.l_M_opt, ... triceps.l_T_slack, ... ])生成数值函数至此已有全部符号组件可生成求值 $\mathbf{M}_d$、$\mathbf{g}_d$ 与 $\mathbf{g}_a$ 的数值函数从而计算状态的时间导数。还需要一个完整约束的数值函数以确保配置处于合法状态 eval_diffeq sm.lambdify((q, u, a, e, p), ... (kane.mass_matrix, kane.forcing, dadt), cseTrue) eval_holonomic sm.lambdify((q, p), holonomic, cseTrue)数值参数与初始状态为所有常量选取合理的数值各量单位注释如右 import numpy as np p_vals np.array([ ... 0.31, # dx [m] ... 0.15, # dy [m] ... -0.31, # dz [m] ... 0.2, # lA [m] ... 0.3, # lC [m] ... 0.3, # lD [m] ... 1.0, # mA [kg] ... 2.3, # mC [kg] ... 1.7, # mD [kg] ... 9.81, # g [m/s/s] ... 5.0, # k [Nm/rad] ... 0.5, # c [Nms/rad] ... 0.03, # r [m] ... 500.0, # biceps F_M_max [N] ... 0.6*0.3, # biceps l_M_opt [m] ... 0.55*0.3, # biceps l_T_slack [m] ... 500.0, # triceps F_M_max [N] ... 0.6*0.3, # triceps l_M_opt [m] ... 0.65*0.3, # triceps l_T_slack [m] ... ])由于存在三条完整约束其中三个坐标是剩余一个坐标的函数。选择杠杆角 $q_1$ 为独立坐标在给出其余坐标猜测值后用fsolve求解 from scipy.optimize import fsolve q_vals np.array([ ... np.deg2rad(5.0), # q1 [rad] ... np.deg2rad(-10.0), # q2 [rad] ... np.deg2rad(0.0), # q3 [rad] ... np.deg2rad(75.0), # q4 [rad] ... ]) ... def eval_holo_fsolve(x): ... q1 q_vals[0] # specified ... q2, q3, q4 x ... return eval_holonomic((q1, q2, q3, q4), p_vals).squeeze() ... q_vals[1:] fsolve(eval_holo_fsolve, q_vals[1:]) np.rad2deg(q_vals) [ 5. -0.60986636 9.44918589 88.68812842]假设系统初始静止激活与激励均初始为零 u_vals np.array([ ... 0.0, # u1, [rad/s] ... 0.0, # u2, [rad/s] ... 0.0, # u3, [rad/s] ... 0.0, # u4, [rad/s] ... ]) ... a_vals np.array([ ... 0.0, # a_bicep, nondimensional ... 0.0, # a_tricep, nondimensional ... ]) e_vals np.array([ ... 0.0, ... 0.0, ... ])系统方程现在可以数值求值 eval_diffeq(q_vals, u_vals, a_vals, e_vals, p_vals) ([[ 0.00333333 -0.15174161 -0.00109772 -0.00152436] [ 0.19923894 0.31 -0.04923615 0.00996712] [ 0.01743115 0. 0.29585191 0.0011276 ] [ 0. -0.29256885 -0.0005241 -0.29983226]], [[-0.9121071] [ 0. ] [-0. ] [ 0. ]], [[0.] [0.]])返回值分别为质量矩阵 $\mathbf{M}_d$、广义力/惯性力向量 $\mathbf{g}_d$ 与激活方程右端 $\mathbf{g}_a$。模拟肌肉驱动的运动系统方程已可在给定状态与常量下求值接下来通过激励两块肌肉模拟手臂与杠杆的运动。SciPy 的solve_ivp()只需提供显式形式 $\dot{\mathbf{x}}$ 的求值函数即可完成积分。先定义一个激励函数第一次模拟中设为零 def eval_r(t): ... Returns the muscles excitation as a function of time. ... e np.array([0.0, 0.0]) ... return e ... def eval_rhs(t, x, r, p): ... Returns the time derivative of the state. ... ... Parameters ... ... t : float ... Time in seconds. ... x : array_like, shape(10,) ... State vector. ... r : function ... Function f(t) that evaluates e. ... p : array_like, shape(?, ) ... Parameter vector. ... ... Returns ... ... dxdt : ndarray, shape(10,) ... Time derivative of the state. ... ... ... ... q x[0:4] ... u x[4:8] ... a x[8:10] ... ... e r(t) ... ... qd u ... m, f, ad eval_diffeq(q, u, a, e, p) ... ud np.linalg.solve(m, f).squeeze() ... ... return np.hstack((qd, ud, ad.squeeze()))其中状态向量按坐标 $q$4 个→ 广义速度 $u$4 个→ 激活 $a$2 个分片质量矩阵通过np.linalg.solve求解以得到 $\dot{\mathbf{u}}$。给定初始状态 $\mathbf{x}_0$ 与上述函数即可用solve_ivp()模拟 3 秒 from scipy.integrate import solve_ivp t0, tf 0.0, 3.0 ts np.linspace(t0, tf, num301) x0 np.hstack((q_vals, u_vals, a_vals)) sol solve_ivp(lambda t, x: eval_rhs(t, x, eval_r, p_vals), ... (t0, tf), x0, t_evalts)定义状态轨迹绘图函数 import matplotlib.pyplot as plt def plot_traj(t, x, syms): ... Simple plot of state trajectories. ... ... Parameters ... ... t : array_like, shape(n,) ... Time values. ... x : array_like, shape(n, m) ... State values at each time value. ... syms : sequence of Symbol, len(m) ... SymPy symbols associated with state. ... ... ... ... fig, axes plt.subplots(5, 2, sharexTrue) ... ... for ax, traj, sym in zip(axes.T.flatten(), x.T, syms): ... if not sym.name.startswith(a): ... traj np.rad2deg(traj) ... ax.plot(t, traj) ... ax.set_ylabel(sm.latex(sym, modeinline)) ... ... for ax in axes[-1, :]: ... ax.set_xlabel(Time [s]) ... ... fig.tight_layout() ... ... return axes无激活情况下初始杠杆角 5 度的手臂逐渐沉降到平衡位置 plot_traj(ts, sol.y.T, x) [[Axes: ylabel$q_{1}{\\left(t \\right)}$ Axes: ylabel$u_{2}{\\left(t \\right)}$] [Axes: ylabel$q_{2}{\\left(t \\right)}$ Axes: ylabel$u_{3}{\\left(t \\right)}$] [Axes: ylabel$q_{3}{\\left(t \\right)}$ Axes: ylabel$u_{4}{\\left(t \\right)}$] [Axes: ylabel$q_{4}{\\left(t \\right)}$ Axes: ylabel$a_{biceps}{\\left(t \\right)}$] [Axes: xlabelTime [s], ylabel$u_{1}{\\left(t \\right)}$ Axes: xlabelTime [s], ylabel$a_{triceps}{\\left(t \\right)}$]]该模拟显示在重力、杠杆阻力和肌肉肌腱模型的被动特性共同平衡下手臂沉降到平衡位置。接下来以 80% 激励激活肱二头肌 1 秒观察对运动的影响 def eval_r(t): ... if t 0.5 or t 1.5: ... e np.array([0.0, 0.0]) ... else: ... e np.array([0.8, 0.0]) ... return e ... sol solve_ivp(lambda t, x: eval_rhs(t, x, eval_r, p_vals), (t0, tf), x0, t_evalts) plot_traj(ts, sol.y.T, x) [[Axes: ylabel$q_{1}{\\left(t \\right)}$ Axes: ylabel$u_{2}{\\left(t \\right)}$] [Axes: ylabel$q_{2}{\\left(t \\right)}$ Axes: ylabel$u_{3}{\\left(t \\right)}$] [Axes: ylabel$q_{3}{\\left(t \\right)}$ Axes: ylabel$u_{4}{\\left(t \\right)}$] [Axes: ylabel$q_{4}{\\left(t \\right)}$ Axes: ylabel$a_{biceps}{\\left(t \\right)}$] [Axes: xlabelTime [s], ylabel$u_{1}{\\left(t \\right)}$ Axes: xlabelTime [s], ylabel$a_{triceps}{\\left(t \\right)}$]]可以看到手臂先像之前一样趋向平衡随后被激活的肱二头肌向肩部方向拉动杠杆使手臂反抗被动运动肌肉去激活后手臂再次如常沉降。总结与源码对照本文通过一个简单的肌肉骨骼系统模型展示了sympy.physics.biomechanics的核心工作流以sympy.physics.mechanics的多体模型为基础通过路径pathway 激活动力学activation dynamics构造肌肉肌腱作动器并在KanesMethod中纳入其力与微分方程最终用SciPy完成数值仿真。模型运动可由肌肉激励控制仿真结果符合预期。关键实现均可在仓库源码中追溯biomechanics/init.py导出FirstOrderActivationDeGroote2016、MusculotendonDeGroote2016、ActivationBase等公开 APIbiomechanics/activation.py一阶激活动力学的 DeGroote2016 实现含with_defaults$\tau_a0.015$、$\tau_d0.060$、$b10$biomechanics/musculotendon.pyMusculotendonDeGroote2016及F_M_max、l_M_opt、l_T_slack等肌肉参数路径基类PathwayBase与线性路径LinearPathway位于 mechanics/pathway.py本文的ExtensorPathway即通过继承其接口实现自定义包裹路径。参考资料[DeGroote2016] De Groote, F., Kinney, A. L., Rao, A. V., Fregly, B. J., Evaluation of direct collocation optimal control problem formulations for solving the muscle redundancy problem, Annals of biomedical engineering, 44(10), (2016) pp. 2922-2936同系列教程索引见 doc/src/tutorials/physics/biomechanics/index.rstsympy.physics.mechanics下另有经典的肌肉模型示例 doc/src/tutorials/physics/mechanics/muscle_model_example.rst 可作延伸阅读。【免费下载链接】sympyA computer algebra system written in pure Python项目地址: https://gitcode.com/GitHub_Trending/sy/sympy创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

相关新闻

SpringBoot+SSM构建酒店VIP管理系统实战

SpringBoot+SSM构建酒店VIP管理系统实战

1. 项目背景与核心价值酒店行业在数字化浪潮中正经历着深刻变革,VIP客户管理作为提升复购率和客户黏性的关键环节,传统手工登记Excel表格的管理方式已显露出明显短板。去年帮某连锁酒店做系统升级时,他们前台经理给我看了一沓皱巴巴的会员登记…

2026/11/5 16:29:18 阅读更多 →
IoT-For-Beginners 的 Raspberry Pi 摄像头未被检测到,如何启用摄像头接口并测试拍照

IoT-For-Beginners 的 Raspberry Pi 摄像头未被检测到,如何启用摄像头接口并测试拍照

IoT-For-Beginners 的 Raspberry Pi 摄像头未被检测到,如何启用摄像头接口并测试拍照 【免费下载链接】IoT-For-Beginners 12 Weeks, 24 Lessons, IoT for All! 项目地址: https://gitcode.com/GitHub_Trending/io/IoT-For-Beginners 在 IoT-For-Beginners 课…

2026/11/8 22:50:33 阅读更多 →
LifeOS Fabric 技能实战:用 extract_latest_video 模式从 YouTube RSS 精准提取最新视频链接

LifeOS Fabric 技能实战:用 extract_latest_video 模式从 YouTube RSS 精准提取最新视频链接

LifeOS Fabric 技能实战:用 extract_latest_video 模式从 YouTube RSS 精准提取最新视频链接 【免费下载链接】LifeOS ⛰️ The Life Operating System — an intent engineering platform that moves you from your current state to your ideal state, in life an…

2026/10/25 1:25:21 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/16 8:22:12 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/16 6:41:34 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/16 8:22:12 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/16 8:22:12 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/16 7:30:18 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/16 8:22:12 阅读更多 →

日新闻

周新闻

月新闻