Closed lizhuang2511 closed 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
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?
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
Kp = 2.0 # 比例增益 Ki = 0.1 # 积分增益 Kd = 0.05 # 微分增益
previous_error = 0.0 integral = 0.0 set_point = 0.5 # 设定滑块的目标位移(例如,0.5米)
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
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)
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)
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 = []
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()
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.
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