clc;
clear;
close all;
warning off;
addpath(genpath(pwd));
r = 0.006; % radius of motor shaft
M = 0.135; % Mass of cart
m = 0.1; % mass of pendulum
I = 0.0007176; % MOI of Pendulum
l = 0.2; % COM of Pendulum
g = 9.81; % Gravity Constant
b = 0.00007892; % viscous damping at pivot of Pendulum
L = 0.046; % Motor Inductance
Rm = 12.5; % Motor Resistance
kb = 0.031; % Motor back emf constant
kt = 0.031; % Motor Torque constant
c = 0.63; % friction coefficient of cart
Er = 2*m*g*l; % Potential Energy of Pendulum
n = 3;
k_swing = 1.2;
% LQR control design
% calculation of A Matrix
AA = I*(M+m) + M*m*(l^2);
aa = (((m*l)^2)*g)/AA;
bb = ((I +m*(l^2))/AA)*(c + (kb*kt)/(Rm*(r^2)));
cc = (b*m*l)/AA;
dd = (m*g*l*(M+m))/AA;
ee = ((m*l)/AA)*(c + (kb*kt)/(Rm*(r^2)));
ff = ((M+m)*b)/AA;
mm = ((I +m*(l^2))*kt)/(AA*Rm*r);
nn = (m*l*kt)/(AA*Rm*r);
A = [0 0 1 0; 0 0 0 1; 0 aa -bb -cc; 0 dd -ee -ff];
B = [0;0; mm; nn];
% calculation of LQR gain
Q = diag([200 1000 0 0]);
R = 0.035;
KK = lqr(A,B,Q,R)
% Close Loop Control simulation
Ts = 0.01; % sample time for simulation
Tf = 9; % simulation end time
X0 = [0; 1*(pi/180); 0;0]; % initial state
X_des = [0; pi; 0; 0]; % desired state
u0 = 0;
i = 0;
sat = @(x, x_max, x_min) min( x_max, max(x_min,x) ); % Saturation Function
for k = 0:Ts:Tf
i = i+1;
new_state = RK4_2nd_order(X0,Ts,u0, M,m,g,l,c,b,I);
if new_state(2) < 0
th = 2*pi-abs(new_state(2));
updated_state = [new_state(1); th; new_state(3); new_state(4)];
else
updated_state = new_state;
end
Xp(i,:) = updated_state'; % for plot
t(i) = k;
X0 = new_state; % update states for simulate system ode
theta = new_state(2);
x_dot = new_state(3);
theta_dot = new_state(4);
% Total Energy of pendulum
E = m*g*l*(1-cos(theta)) + (1/2)*(I + m*l^2)*(theta_dot^2);
% Energy based swing up control
accel = 2*(E-Er)*sign(theta_dot*cos(theta));
accel = k_swing*g*(sat(accel, n*g, -n*g));
% feedback Linearization
u_swing = (M+m)*(accel)+ 0*x_dot-m*l*( (theta_dot)^2)*sin(theta)- m*l*(cos(theta))*( ( b*theta_dot + m*l*accel*cos(theta) + m*g*l*sin(theta) )/(I+m*l^2) );
% LQR control Design
u_volt = -KK*(updated_state-X_des); % u = -kX
u_volt = sat(u_volt,12,-12);
u_lqr = volt2force(u_volt,X0(3),kt,kb,Rm,r);
% Control Switching Condition
if (abs(X_des(2)-updated_state(2)))*(180/pi) <= 30 % condition for lqr control
u0 = u_lqr;
else % condition for swing up control
u0 = u_swing;
end
end
hf = figure()
for i = 1:8:length(Xp)
IP_Animation(Xp(i,1),Xp(i,2));
pause(0.01);
% movieVector(i) = getframe(hf);
hold off
end
figure()
axis(gca,'equal');
subplot(2,2,1);
plot(t,Xp(:,1));
grid on;
ylabel('X (m)');
xlabel('time [sec]');
subplot(2,2,2);
plot(t, (180/pi.*Xp(:,2)));
grid on;
ylabel('\theta (deg)');
xlabel('time [sec]');
subplot(2,2,3);
plot(t,Xp(:,3));
grid on;
ylabel('x dot (m/sec)');
xlabel('time [sec]');
subplot(2,2,4);
plot(t,(180/pi.*Xp(:,4)));
grid on;
ylabel('\theta dot (deg/sec)');
xlabel('time [sec]');
fpga和matlab
- 粉丝: 18w+
- 资源: 2645
最新资源
- 神经网络 使用手写字数据集 实现分割任务 CNN
- 基于maxwell的4极6槽 内转子 11000rpm 输出转矩 156mNm 效率85% 180W 外径 48mm 内径27 轴向长度30mm 直流母线36V 永磁同步电机(永磁直流无刷)模型
- FPGA开发:SDRAM驱动代码,使用串口向sdram写数据,数据环回后被SDRAM送回到串口进行输出,中间使用FIFO进行跨时钟域处理,所用开发板DE2-115,SDRAM型号IS42S16320D
- ZTE C600&C620&C650命令参考
- python-workspace.zip.006
- FX5U FX40SSC 程序 FX5U FX 40SSC运动控制模块程序块 自己整合的针对FX 40SSC模块的功能块,支持点动故障码 状态码 相对定位 绝对定位 直接1指定轴号就可以
- 汽车二、三自由度模型 汽车二、三自由度模型 本人用了三种不同方法搭的汽车线性二自由度simulink模型,文档里包含有具体的车辆数值 适合初学者学习simulink使用(ps.模型输入为前轮转角,输出
- KUKA机器人码垛程序备份
- dbstudio-3.8.5.102.win64 神通数据库连接工具
- 开源TVBox影视盒子 小苹果影视盒子V1.5.7 2025新版
- 基于国产M0核MCU平台,全开源双电阻采样FOC高压 风机量产程序,包含龙博格电机观测器,SVPWM,顺逆风启动,五段式与七段式调制等源码,完全可以移植到别的MCU平台 适合电机算法研究
- 交替迭代法 matlab 无功优化 通过含固态变压器的无功优化算法,形成交替迭代潮流计算,最终计算出符合预期的电压曲线,程序方法包括包括牛拉法 前推回代等,参考性强
- 综合能源耦合微网优化程序matlab 程序基于冷热电联供综合能源耦合模型,采用cchp,并且含有压缩空气储能,采用粒子群优化求解
- DataGrip 2021.3 数据库连接工具
- 考虑碳交易的微网优化模型matlab
- FreeRTOS 是一款开源的、可抢占式的实时操作系统.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页