LQR、LQR-MPC、GP-MPC控制倒立摆

2023-05-16

LQR控制倒立摆:

倒立摆状态方程:

目标任务:

 模型参数:

%% LQR for the cart-pole system

load('cp_params.mat');
syms phi phid x xd I u
fr(1) = 0.2;              % friction magnitude
Ts = 0.01;
Duration = 2.5;
Q = [100 0 1 0];          % LQ weights
R = 0.0001;

q1 = [phi; phid; x; xd];
q1 = cp_dynmodel(q1,u,par,fr);

Asym = jacobian(q1,[phi;phid;x;xd]);
A = double(subs(Asym,[phi;phid;x;xd;u],[0;0;0;0;0]));
Bsym = jacobian(q1,u);
B = double(subs(Bsym,[phi;phid;x;xd;u],[0;0;0;0;0]));
A = eye(4)+Ts*A;
B = Ts*B;
clear phi phid x xd I u k1 k2 k3 k4 q1

%% LQR
C = diag([1,1,1,1]);
D = zeros(4,1);
K = dlqr(A,B,diag(Q),R);

%% Simulation
qLQR = zeros(5,Duration/Ts+1);
q = [0.2;0;0;0];
qLQR(:,1) = [q; 0];
for i = 1 : Duration/Ts
    u = -K*q;
    [~,q] = ode45(@(t,q) cp_dynmodel(q,u,par,fr),[0 Ts/2 Ts],q);
    q = q(3,:)';
    qLQR(:,i+1) = [q; u];
end
%% Plots
plotTraj(qLQR,Ts)

首先非线性模型通过雅可比矩阵线性化:

Asym = jacobian(q1,[phi;phid;x;xd]);
A = double(subs(Asym,[phi;phid;x;xd;u],[0;0;0;0;0]));
Bsym = jacobian(q1,u);
B = double(subs(Bsym,[phi;phid;x;xd;u],[0;0;0;0;0]));

离散化模型:

A = eye(4)+Ts*A;
B = Ts*B;

通过LQR计算出反馈矩阵K值:

K = dlqr(A,B,diag(Q),R);

仿真,通过

%% Simulation
qLQR = zeros(5,Duration/Ts+1);
q = [0.2;0;0;0];
qLQR(:,1) = [q; 0];
for i = 1 : Duration/Ts
    u = -K*q;
    [~,q] = ode45(@(t,q) cp_dynmodel(q,u,par,fr),[0 Ts/2 Ts],q);
    q = q(3,:)';
    qLQR(:,i+1) = [q; u];
end
%% Plots
plotTraj(qLQR,Ts)

结果:

 LQR-MPC控制倒立摆:

这里就是使用mpc产生输入补偿LQR输入:

 input = fmincon(@(u) cost_func_lin(u,horizon,q,Q,R,A2,B),input,[],...
        [],[],[],(K*q-40)*ones(horizon,1),(K*q+40)*ones(horizon,1),[],options);
    u = -K*q + input(1);
%% MPC for the cart-pole system

load('cp_params.mat');
syms phi phid x xd I u
fr(1) = 0.2;              % friction magnitude
Ts = 0.01;
horizon = 25;
Duration = 10;
Q = [100 0 1 0];          % MPC weights
R = 0.0001;

q1 = [phi; phid; x; xd];
q1 = cp_dynmodel(q1,u,par,fr);

Asym = jacobian(q1,[phi;phid;x;xd]);
A = double(subs(Asym,[phi;phid;x;xd;u],[0;0;0;0;0]));
Bsym = jacobian(q1,u);
B = double(subs(Bsym,[phi;phid;x;xd;u],[0;0;0;0;0]));
A = eye(4)+Ts*A;
B = Ts*B; %离散化
clear phi phid x xd I u k1 k2 k3 k4 q1

%% LQ feedback gain
C = diag([1,1,1,1]);
D = zeros(4,1);
K = dlqr(A,B,diag(Q),R);

%% Simulation
A2 = A-B*K;
u = 0;
qlinMPC = zeros(5,Duration/Ts+1);
q = [0.2; 0; 0; 0];        % initial condition
qlinMPC(:,1) = [q; u];
input = zeros(horizon,1);
options = optimoptions('fmincon','Algorithm','sqp');

for i = 1: Duration / Ts
    input = fmincon(@(u) cost_func_lin(u,horizon,q,Q,R,A2,B),input,[],...
        [],[],[],(K*q-40)*ones(horizon,1),(K*q+40)*ones(horizon,1),[],options);
    u = -K*q + input(1);
    [~,q] = ode45(@(t,q) cp_dynmodel(q,u,par,fr),[0 Ts/2 Ts],q);
    q = q(3,:)';
    qlinMPC(:,i+1) = [q; u];
end

    

%% Plots
plotTraj(qlinMPC,Ts)

关于GPR参考资料:

 Gaussian process 的重要组成部分——关于那个被广泛应用的Kernel的林林总总 - 知乎

Documentation for GPML Matlab Code

%% GPMPC for cart-pole dynamic system

addpath '..\TDK_2020\gp'       % write path of the containing folder
addpath '..\TDK_2020\util'
load('cp_params.mat');
load('cp_trainingpoints.mat');
fr(1) = 0.2;
Ts = 0.01;
horizon = 25;
Duration = 2.5;
Q = [100 0 1 0];
R = 0.0001;

%% GP training
gpmodel.inputs = xtrain';
gpmodel.targets = ytrain';
[gpmodel2, nlml] = train(gpmodel, 1, 100);
%% Precomputation of GP matrices and vectors
N = length(xtrain(1,:));
L = zeros(4,4,4);
Kxx = zeros(N,N,4);
Kinv = Kxx;
alpha = zeros(N,1,4);
sf = 0.1;
sn = 0.01;
for k = 1:4
L(:,:,k) = diag(1./exp(2*gpmodel2.hyp(1:4,k)));

for i=1:N
    for j=1:N
        Kxx(i,j,k)=sf^2*kernel(xtrain(:,i),xtrain(:,j),L(:,:,k));
    end
end
alpha(:,:,k) = (Kxx(:,:,k)+sn^2*eye(N))\ytrain(k,:)';
Kinv(:,:,k) = inv(Kxx(:,:,k)+sn^2*eye(N));
end

%% Discretisation, linearisation
syms phi phid x xd I u

q1 = [phi; phid; x; xd];
q1 = cp_dynmodel(q1,u,par,fr);

Asym = jacobian(q1,[phi;phid;x;xd]);
A = double(subs(Asym,[phi;phid;x;xd;u],[0;0;0;0;0]));
Bsym = jacobian(q1,u);
B = double(subs(Bsym,[phi;phid;x;xd;u],[0;0;0;0;0]));
A = eye(4)+Ts*A;
B = Ts*B;
clear phi phid x xd I u k1 k2 k3 k4 q1

%% LQR
C = diag([1,1,1,1]);
D = zeros(4,1);
K = dlqr(A,B,diag(Q),R);

%% GPMPC Simulation
A2 = A-B*K;
q = [0.2; 0; 0; 0];        % initial condition
qGPMPC = zeros(5,Duration/Ts+1);
qGPMPC(1:4,1) = q;
input = zeros(horizon,1);
options = optimoptions('fmincon','Algorithm','sqp');
for i = 1: Duration / Ts
    input = fmincon(@(u) cost_func_GP(u,horizon,q,Q,R,A2,B,xtrain,L,alpha,Kinv),...
        input,[],[],[],[],(K*q-40)*ones(horizon,1),(K*q+40)*ones(horizon,1),[],options);
    u = -K*q + input(1);
    [~,q] = ode45(@(t,q) cp_dynmodel(q,u,par,fr),[0 Ts/2 Ts],q);
    q = q(3,:)';
    qGPMPC(:,i+1) = [q; u];
end

%% Plots
plotTraj(qGPMPC,Ts)

%% Plot the prediction at a certain time instant and the simulation
% results at that time interval

niter = horizon;
start = 12;                     % t = 0.12 s
mean = zeros(4,niter+1);
mean(:,1) = qGPMPC(1:4,start);
u = qGPMPC(5,start);

Ktest = zeros(1,length(xtrain(1,:)));
M = zeros(4,niter);
S = M;
N = length(xtrain(1,:));

for iter = 1:niter
    for k = 1:4
        for j=1:N
            Ktest(1,j,k)=sf^2*kernel(mean(:,iter),xtrain(:,j),L(:,:,k));
        end
        M(k,iter) = Ktest(:,:,k)*alpha(:,:,k);
        S(k,iter) = sf^2 - Ktest(:,:,k)*Kinv(:,:,k)*Ktest(:,:,k)';
    end
    mean(:,iter+1) = A*mean(:,iter) + B*u + M(:,iter);
end
tsimu = start*Ts:Ts:(start+horizon-1)*Ts;
figure()
subplot(2,1,1)
hold on
plot(tsimu,qGPMPC(1,start:start+niter-1),'r','LineWidth',1)
errorbar(tsimu,mean(1,1:25),2*sqrt(S(1,:)),'k','LineWidth',1)
xlabel('$t$ (s)')
ylabel('$\varphi$ (rad)')
legend('Simulation result','Prediction at $t=0.12$ s')
hold off

subplot(2,1,2)
hold on
plot(tsimu,qGPMPC(3,start:start+niter-1),'r','LineWidth',1)
errorbar(tsimu,mean(3,1:25),2*sqrt(S(3,:)),'k','LineWidth',1)
xlabel('$t$ (s)')
ylabel('$x$ (m)')
hold off
具体代码:

MPC/LQR. LQR-MPC.GP-MPC.rar at main · caokaifa/MPC · GitHub

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LQR、LQR-MPC、GP-MPC控制倒立摆 的相关文章

  • MPC,PID,LQR,DDP算法

    算法原理 xff1a
  • LQR控制基本原理(包括Riccati方程具体推导过程)

    全状态反馈控制系统 状态反馈控制器 通过选择K xff0c 可以改变的特征值 xff0c 进而控制系统表现 LQR控制器 最优控制 xff0c 其本质就是让系统以某种最小的代价来让系统运行 xff0c 当这个代价被定义为二次泛函 xff0c
  • PID/LQR/MPC自行总结使用

    PID LQR MPC自行总结使用 自学控制相关知识 xff0c 已经一年多了 xff0c 现在回头看看还是有很多模糊不明确的地方 xff0c 准备借此机会进行总结一下 xff0c 第一次写博客 xff0c 如果错误和不合理之处 xff0c
  • 无人驾驶-控制-LQR(运动学)

    无人驾驶 控制 LQR xff08 运动学 xff09 一 车辆建模 二 参考轨迹 利用泰勒展开 xff0c 进行线性化 xff1a 离散化处理 对离散后的式子进行处理 xff0c 得到X k 43 1 的表达式 综上 xff1a 由于系统
  • LQR控制算法的浅析

    目录 前言 一 知识点补充 1 拉格朗日乘子法 2 积分中值定理 3 向前欧拉法 xff0c 向后欧拉法 xff0c 中点欧拉法 4 向量的导数 5 矩阵求逆引理 记住就好 xff0c 推导见链接 二 连续时间下的LQR推导 1 系统状态方
  • MPC控制笔记(一)

    转自 我的博客 笔记参考1 xff1a Understanding Model Predictive Control Youtube 带自动生成字幕 笔记参考2 xff1a Understanding Model Predictive Co
  • MPC控制笔记(一)

    转自 我的博客 笔记参考1 xff1a Understanding Model Predictive Control Youtube 带自动生成字幕 笔记参考2 xff1a Understanding Model Predictive Co
  • LQR控制算法推导以及简单分析

    首先 xff0c 这篇文章是看了几个大神的博客后 xff0c 自己抄录以及整理的内容 xff0c 其中有些自己的想法 xff0c 但是原理部分基本都是学习大神们的 xff0c 在此先说明一下 1 全状态反馈控制系统 在介绍LQR之前 xff
  • MPC控制

    基于状态空间模型的控制 模型预测控制 xff08 MPC xff09 简介 对基于状态空间模型的控制理解得很到位 在这里我重点讲解一下状态空间 模型 那么什么是状态 xff1f 输出是不是也是状态的一种 xff1f 对的 xff0c 输出也
  • MPC(模型预测控制)控制小车沿轨迹移动——C++实现

    任务说明 要求如下图所示 xff0c 给定一条轨迹 xff0c 要求控制小车沿这条轨迹移动 xff0c 同时可以适用于系统带有延时的情况 注意 xff0c 本篇文章只给出部分C 43 43 代码参考 主要流程 首先用运动学自行车模型 xff
  • PID与MPC控制方法

    记录udacity 无人驾驶工程师课程中控制部分 MPC代码和实践链接https github com udacity CarND MPC Quizzes 本文按照对udacity课程的理解和翻译而来 1 PID P xff1a Propo
  • MPC与LQR的详细对比分析

    从以下几个方面进行阐述 xff1a 一 xff0c 研究对象 xff1a 是否线性 二 xff0c 状态方程 xff1a 离散化 三 xff0c 目标函数 xff1a 误差和控制量的极小值 四 xff0c 工作时域 xff1a 预测时域 x
  • MPC自学资料总结

    1 书籍 xff1a 无人驾驶车辆模型预测控制 2 视频 xff1a https ww2 mathworks cn videos understanding model predictive control part 1 why use m
  • 百度Apollo 2.0 车辆控制算法之LQR控制算法解读

    百度Apollo 2 0 车辆控制算法之LQR控制算法解读 Apollo 中横向控制的LQR控制算法在Latcontroller cc 中实现 根据车辆的二自由度动力学模型 1 根据魔术公式在小角度偏角的情况下有 轮胎的侧向力与轮胎的偏离角
  • 离散LQR:原理,求解与拓展

    该文档用以总结离散LQR的基本原理 xff0c 反馈控制率的求解和一些拓展 xff08 时变系统 xff0c 跟踪命题等 xff09 主要参考的是Stanford的课程EE363 Linear Dynamical Systems的部分课件
  • LQR制导

    LQR制导 引言 在ardupilot中固定翼飞机横航向位置控制 xff08 制导律 xff09 采用L1制导律 xff0c 最近想将一些其他的控制理论用于ardupilot代码中 xff0c 通过ardupilot论坛 xff0c 看到已
  • LQR控制器——简单实现与仿真

    对B站一位良心up主的视频学习总结 安利 https www bilibili com video BV1RW411q7FD spm id from trigger reload 对于可镇定的线性系统 x A x B u dot x Ax
  • [现代控制理论]7_线性控制器设计_Linear Controller Design

    现代控制理论 11 现代控制理论串讲 完结 pdf获取 现代控制理论 10 可观测性与分离原理 观测器与控制器 现代控制理论 9 状态观测器设计 龙伯格观测器 现代控制理论 8 5 线性控制器设计 轨迹跟踪simulink 现代控制理论 8
  • 控制范围和预测范围

    我已经回顾了模型预测控制的参考书目和 Gekko 编程结构 尽管我了解它的编程方式及其目的 例如 我想了解 Gekko 如何根据 Seborg 中的相关内容来管理控制范围和预测范围之间的差异 我看不出代码有什么区别 下面是一个用于说明的 M
  • 在 GEKKO 中使用非线性模型预测控制实现横向控制器

    我正在尝试为由横向动态模型定义的自动驾驶车辆实现横向控制器 好吧 我的问题是 CV 没有达到 SP 设置的所需参考点或目标点 我正在使用以下运动方程和目标函数 我正在使用半经验公式 pacejka 来计算 Fyf Fyr 提供的轮胎力 这里

随机推荐