|
5楼
楼主 |
发表于 2023-5-21 19:50:34
|
只看该作者
你没做积分限幅吗?
# 定义电流环参数
kp_current = 0.1 # 电流环比例增益
ki_current = 0.05 # 电流环积分增益
# 定义速度环参数
kp_velocity = 0.2 # 速度环比例增益
ki_velocity = 0.1 # 速度环积分增益
# 定义积分限幅
current_integral_limit = 10.0
velocity_integral_limit = 20.0
# 初始化变量
current_error = 0.0
current_integral = 0.0
velocity_error = 0.0
velocity_integral = 0.0
control_output = 0.0
# 循环运行控制
while True:
# 读取速度传感器值
velocity_sensor_value = readVelocitySensor()
# 计算速度环误差
velocity_error = desired_velocity - velocity_sensor_value
# 计算速度环积分项
velocity_integral += velocity_error
# 限制速度环积分项
velocity_integral = max(min(velocity_integral, velocity_integral_limit), -velocity_integral_limit)
# 计算速度环输出
velocity_output = kp_velocity * velocity_error + ki_velocity * velocity_integral
# 读取电流传感器值
current_sensor_value = readCurrentSensor()
# 计算电流环误差
current_error = velocity_output - current_sensor_value
# 计算电流环积分项
current_integral += current_error
# 限制电流环积分项
current_integral = max(min(current_integral, current_integral_limit), -current_integral_limit)
# 计算电流环输出
current_output = kp_current * current_error + ki_current * current_integral
# 执行控制输出
executeControlOutput(current_output) |
|