如何在Modelica中创建位置固定但可绕CG自由旋转的实体?
在Modelica中建模绕自身CG自由旋转的3D箱体
核心思路:利用Modelica标准多体库(Modelica.Mechanics.MultiBody)
无需从零实现动力学方程,标准库已封装成熟的多体组件,适合新手快速搭建模型。
步骤1:创建基础模型框架
- 新建Modelica模型,先引入多体库:
within ModelicaByExample; model RotatingBox import Modelica.Mechanics.MultiBody.*; import Modelica.Mechanics.MultiBody.Types; import Modelica.SIunits;
- 添加World坐标系:这是多体模型的根参考系,所有组件都基于它构建。
World world;
步骤2:定义箱体参数与组件
- 定义箱体物理参数(质量、尺寸、转动惯量):
parameter SIunits.Mass m = 10 "箱体质量"; parameter SIunits.Length length = 0.5 "箱体长度"; parameter SIunits.Width width = 0.3 "箱体宽度"; parameter SIunits.Height height = 0.2 "箱体高度"; // 基于长方体公式计算绕自身CG的转动惯量 parameter SIunits.MomentOfInertia I_xx = (m*(width^2 + height^2))/12; parameter SIunits.MomentOfInertia I_yy = (m*(length^2 + height^2))/12; parameter SIunits.MomentOfInertia I_zz = (m*(length^2 + width^2))/12;
- 添加Body组件:代表3D箱体,默认以自身CG为参考点,正好符合需求。
Body box( m = m, I = Types.MomentOfInertia(I_xx, I_yy, I_zz), shape = Types.Shape.Box, lengthX = length, lengthY = width, lengthZ = height, color = {0.8, 0.2, 0.2} "红色箱体");
步骤3:设置自由旋转约束
如果需要固定箱体CG位置,仅允许绕三轴旋转:
- 用
FixedTranslation固定CG在World的指定位置(比如原点) - 用
FreeRotation允许箱体绕自身CG的三个轴自由旋转
FixedTranslation fixedCG( r = {0, 0, 0} "CG固定在World原点"); FreeRotation freeRotation;
然后完成组件连接:
equation connect(world.frame_b, fixedCG.frame_a); connect(fixedCG.frame_b, freeRotation.frame_a); connect(freeRotation.frame_b, box.frame_a);
如果需要允许CG自由平移+旋转,直接用FreeJoint替代上述两个组件:
FreeJoint freeJoint; equation connect(world.frame_b, freeJoint.frame_a); connect(freeJoint.frame_b, box.frame_a);
步骤4:施加扭矩
添加Torque组件,给箱体施加绕各轴的扭矩:
Torque torque( tau = {1, 0.5, 0.2} "绕X/Y/Z轴的扭矩,可修改为时间函数或变量");
连接扭矩组件:
equation connect(world.frame_b, torque.frame_a); connect(box.frame_b, torque.frame_b);
完整示例代码
within ModelicaByExample; model RotatingBox import Modelica.Mechanics.MultiBody.*; import Modelica.Mechanics.MultiBody.Types; import Modelica.SIunits; // 基础坐标系 World world; // 箱体物理参数 parameter SIunits.Mass m = 10 "箱体质量"; parameter SIunits.Length length = 0.5 "箱体长度"; parameter SIunits.Width width = 0.3 "箱体宽度"; parameter SIunits.Height height = 0.2 "箱体高度"; parameter SIunits.MomentOfInertia I_xx = (m*(width^2 + height^2))/12; parameter SIunits.MomentOfInertia I_yy = (m*(length^2 + height^2))/12; parameter SIunits.MomentOfInertia I_zz = (m*(length^2 + width^2))/12; // 箱体组件 Body box( m = m, I = Types.MomentOfInertia(I_xx, I_yy, I_zz), shape = Types.Shape.Box, lengthX = length, lengthY = width, lengthZ = height, color = {0.8, 0.2, 0.2}); // 约束组件:固定CG位置,允许自由旋转 FixedTranslation fixedCG(r = {0, 0, 0}); FreeRotation freeRotation; // 扭矩组件 Torque torque(tau = {1, 0.5, 0.2} "绕X/Y/Z轴的扭矩"); equation // 组件连接 connect(world.frame_b, fixedCG.frame_a); connect(fixedCG.frame_b, freeRotation.frame_a); connect(freeRotation.frame_b, box.frame_a); connect(world.frame_b, torque.frame_a); connect(box.frame_b, torque.frame_b); end RotatingBox;
关键说明
- CG位置:
Body组件的默认参考点就是自身质心,无需额外调整。 - 转动惯量:示例用长方体公式计算,也可根据实际需求手动输入数值。
- 约束调整:若不需要固定CG位置,替换为
FreeJoint即可实现全自由度运动。 - 扭矩修改:
tau参数可设置为时间函数(比如{Modelica.Math.sin(time), 0, 0}),实现随时间变化的扭矩。
内容的提问来源于stack exchange,提问作者finnmezzetti
相关产品推荐
相关产品推荐

