jgerstmayr / EXUDYN

Multibody Dynamics Simulation: Rigid and flexible multibody systems
https://www.youtube.com/channel/UCsQD2bIPBXB_4J23WtqKkVw/playlists
Other
173 stars 23 forks source link

how to simulation pid control #78

Closed lizhuang2511 closed 1 month ago

lizhuang2511 commented 1 month ago

how to do setup force or driver speed in same simulation setp,that i can simulation pid control ,i dont see example ,who can give me a example

lizhuang2511 commented 1 month ago

how to do setup force or driver speed in simulation setp,that i can simulation pid control ,i dont see example ,who can give me a example

jgerstmayr commented 1 month ago

There are several ways to add simple control-like behavior (PD-control):

PID control is more involved, as the integrator requires an integration part, an additional state and usually a technique to avoid wind-up problems. In this case, I can only sketch:

Indeed it would be nice to have a simple mass with PID controller example. Maybe, you volunteer to create one?

lizhuang2511 commented 1 month ago

ai generate code in below .it use 'setprestepuserfuntion' to set every step setup,i run it not work , do you want this method can do?

import exudyn as exu import numpy as np import matplotlib.pyplot as plt

定义PID控制器的参数

Kp = 2.0 # 比例增益 Ki = 0.1 # 积分增益 Kd = 0.05 # 微分增益

初始化PID控制相关的变量

previous_error = 0.0 integral = 0.0 set_point = 0.5 # 设定滑块的目标位移(例如,0.5米)

定义一个函数,用于在每个仿真步中计算PID控制器的输出

def pid_controller(current_displacement, current_time, dt): global previous_error, integral, set_point

# 计算误差
error = set_point - current_displacement

# 计算积分项
integral += error * dt

# 计算微分项
derivative = (error - previous_error) / dt

# 计算PID控制器的输出
control_signal = Kp * error + Ki * integral + Kd * derivative

# 更新上一个误差
previous_error = error

return control_signal

定义一个函数,作为Exudyn的预步骤回调函数,用于在每个仿真步中应用PID控制

def pre_step_callback(mbs, t):

获取滑块的当前位移

slider_displacement = mbs.GetObjectOutput(sliderMarker, exu.OutputVariableType.Position)[1]  # 假设滑块在y方向移动

# 计算PID控制器的输出
control_signal = pid_controller(slider_displacement, t, mbs.sys['dt'])

# 设置曲柄的旋转速度(这里假设曲柄是通过一个旋转关节驱动的)
mbs.SetObjectParameter(crankJoint, 'rotationVelocity', control_signal)

创建Exudyn系统

mbs = exu.MainSystem()

添加地面

ground = mbs.AddObject(exu.ObjectGround())

添加曲柄和连杆(简化模型,仅用于演示)

inertiaCrank = exu.InertiaCuboid(mass=1.0, sideLengths=[0.1, 0.1, 0.5]) crank = mbs.AddObject(exu.ObjectRigidBody(physics=inertiaCrank, referencePosition=[0, 0, 0])) crankJoint = mbs.AddObject(exu.ObjectJointRotation(markerNumbers=[ground.GetMarker(0), crank.GetMarker(0)], rotationAxis=[0, 0, 1]))

添加滑块

inertiaSlider = exu.InertiaCuboid(mass=1.0, sideLengths=[0.1, 0.1, 0.1]) slider = mbs.AddObject(exu.ObjectRigidBody(physics=inertiaSlider, referencePosition=[0.5, 0, 0])) sliderMarker = slider.GetMarker(0)

添加约束,使滑块只能在y方向移动(这里使用平移关节来简化模型)

注意:在实际情况中,你可能需要使用更复杂的约束来模拟滑块与导轨之间的相互作用

translationJoint = mbs.AddObject(exu.ObjectJointTranslation(markerNumbers=[ground.GetMarker(0), sliderMarker ], axis=[0, 1, 0]))

设置预步骤回调函数

mbs.SetPreStepUserFunction(pre_step_callback)

设置仿真参数

simulationSettings = exu.SimulationSettings() simulationSettings.timeIntegration.numberOfSteps = 1000 simulationSettings.timeIntegration.endTime = 5.0 simulationSettings.solutionSettings.writeSolutionToFile = False

存储滑块位移的数据

slider_displacements = []

定义一个函数,作为Exudyn的后处理回调函数,用于在每个仿真步后存储滑块位移

def post_processing_callback(mbs, t): slider_displacement = mbs.GetObjectOutput(sliderMarker, exu.OutputVariableType.Position)[1] slider_displacements.append(slider_displacement)

设置后处理回调函数

mbs.SetPostProcessingUserFunction(post_processing_callback)

启动仿真

mbs.Assemble() mbs.SolveDynamic(solverType=exu.DynamicSolverType.RK44, simulationSettings=simulationSettings)

绘制滑块位移图

time_steps = np.linspace(0, simulationSettings.timeIntegration.endTime, len(slider_displacements)) plt.plot(time_steps, slider_displacements, label='Slider Displacement') plt.xlabel('Time [s]') plt.ylabel('Displacement [m]') plt.legend() plt.grid() plt.show()

jgerstmayr commented 1 month ago

AI is able to provide simulation code for some models, but the output has to be verified by the user. In particular, more difficult questions cannot be solved 100% . I will not provide error corrections here for AI-generated results.