写一个模糊自适应神经网络滑模控制ABS单轮模型代码
时间: 2023-07-04 12:04:14 浏览: 126
以下是一个简单的模糊自适应神经网络滑模控制ABS单轮模型代码,供参考:
```python
import numpy as np
import skfuzzy as fuzz
# 定义系统参数
m = 250 # 质量
b = 25 # 阻尼系数
g = 9.81 # 重力加速度
r = 0.3 # 轮胎半径
J = 1 # 车轮转动惯量
C = 2000 # 制动器常数
k1 = 1000 # 滑移系数1
k2 = 1500 # 滑移系数2
k3 = 2000 # 滑移系数3
k4 = 2500 # 滑移系数4
k5 = 3000 # 滑移系数5
k6 = 3500 # 滑移系数6
k7 = 4000 # 滑移系数7
k8 = 4500 # 滑移系数8
k9 = 5000 # 滑移系数9
k10 = 5500 # 滑移系数10
k11 = 6000 # 滑移系数11
k12 = 6500 # 滑移系数12
k13 = 7000 # 滑移系数13
k14 = 7500 # 滑移系数14
k15 = 8000 # 滑移系数15
k16 = 8500 # 滑移系数16
# 定义输入变量和输出变量
speed = np.arange(0, 101, 1) # 车速
slip_ratio = np.arange(0, 1.01, 0.01) # 滑移比
brake_force = np.arange(0, 5001, 1) # 制动力
# 定义模糊变量
speed_low = fuzz.trimf(speed, [0, 0, 50])
speed_medium = fuzz.trimf(speed, [0, 50, 100])
speed_high = fuzz.trimf(speed, [50, 100, 100])
slip_ratio_low = fuzz.trimf(slip_ratio, [0, 0, 0.5])
slip_ratio_medium = fuzz.trimf(slip_ratio, [0, 0.5, 1])
slip_ratio_high = fuzz.trimf(slip_ratio, [0.5, 1, 1])
brake_force_weak = fuzz.trimf(brake_force, [0, 0, 2500])
brake_force_medium = fuzz.trimf(brake_force, [0, 2500, 5000])
brake_force_strong = fuzz.trimf(brake_force, [2500, 5000, 5000])
# 建立模糊规则
rule1 = np.fmax(speed_low, slip_ratio_low)
rule2 = np.fmax(speed_low, slip_ratio_medium)
rule3 = np.fmax(speed_low, slip_ratio_high)
rule4 = np.fmax(speed_medium, slip_ratio_low)
rule5 = np.fmax(speed_medium, slip_ratio_medium)
rule6 = np.fmax(speed_medium, slip_ratio_high)
rule7 = np.fmax(speed_high, slip_ratio_low)
rule8 = np.fmax(speed_high, slip_ratio_medium)
rule9 = np.fmax(speed_high, slip_ratio_high)
# 进行模糊推理
brake_force_weakness = np.fmax(fuzz.interp_membership(speed, speed_low, 40), fuzz.interp_membership(slip_ratio, slip_ratio_low, 0.2))
brake_force_mediumness = np.fmax(fuzz.interp_membership(speed, speed_medium, 60), fuzz.interp_membership(slip_ratio, slip_ratio_medium, 0.5))
brake_force_strongness = np.fmax(fuzz.interp_membership(speed, speed_high, 80), fuzz.interp_membership(slip_ratio, slip_ratio_high, 0.8))
# 合并模糊输出
brake_force_combined = np.fmax(brake_force_weakness * brake_force_weak, np.fmax(brake_force_mediumness * brake_force_medium, brake_force_strongness * brake_force_strong))
# 定义滑模控制器
def sgn(x):
if x > 0:
return 1
elif x < 0:
return -1
else:
return 0
def sliding_mode_control(slip_ratio, d_slip_ratio, d2_slip_ratio, brake_force, d_brake_force):
k = 1000
alpha = 3
beta = 1
u = k * sgn(d_slip_ratio) + alpha * d_slip_ratio + beta * d2_slip_ratio
brake_force = brake_force + u
brake_force = np.clip(brake_force, 0, C)
return brake_force
# 定义神经网络控制器
def neural_network_control(speed, slip_ratio, brake_force):
w1 = np.array([0.5, 0.2, 0.3])
w2 = np.array([-0.1, 0.4, -0.2])
w3 = np.array([0.3, -0.2, 0.5])
b1 = 0.2
b2 = -0.1
b3 = 0.3
x = np.array([speed, slip_ratio, brake_force])
y1 = np.sum(x * w1) + b1
y2 = np.sum(x * w2) + b2
y3 = np.sum(x * w3) + b3
y = 1 / (1 + np.exp(-np.array([y1, y2, y3])))
return y[0]
# 模拟ABS单轮模型
dt = 0.1 # 采样时间
t = np.arange(0, 10, dt) # 时间向量
slip_ratio = np.zeros_like(t) # 滑移比
brake_force = np.zeros_like(t) # 制动力
for i in range(1, len(t)):
# 计算滑移比和制动力
slip_ratio[i] = (r * speed[i-1] - r * speed[i]) / (r * speed[i-1])
slip_ratio_error = slip_ratio[i] - slip_ratio[i-1]
slip_ratio_error_dot = (slip_ratio_error - (slip_ratio[i-1] - slip_ratio[i-2])) / dt
brake_force_error = neural_network_control(speed[i-1], slip_ratio[i], brake_force[i-1]) - brake_force[i-1]
brake_force_error_dot = (brake_force_error - (brake_force[i-1] - brake_force[i-2])) / dt
brake_force[i] = sliding_mode_control(slip_ratio[i], slip_ratio_error, slip_ratio_error_dot, brake_force[i-1], brake_force_error_dot)
# 制动器反应延迟
if i > 10:
brake_force[i] = brake_force[i-10]
# 输出结果
print('Time:', t[i], 'Speed:', speed[i-1], 'Slip Ratio:', slip_ratio[i], 'Brake Force:', brake_force[i])
```
此代码仅供参考,具体实现可能需要根据具体问题进行调整。
阅读全文