简单有趣的小游戏(生活中简单有趣的事)

来源:https://github.com/aerojunkie/control-tools/blob/master/ufo_rotate.m一个简单的LQR例子closeall%InitialConditionsx0=[3;%3radians0];%0rad/s%SystemDynamicsA=[01;0.010];B=[0;1];C=[10];D=0;%ControlLaw

大家好,又见面了,我是你们的朋友全栈君。

来源:https://github.com/aerojunkie/control-tools/blob/master/ufo_rotate.m

一个简单的LQR例子
在这里插入图片描述

close all

% Initial Conditions
x0 = [3;  % 3 radians
      0]; % 0 rad/s

% System Dynamics
A = [0    1; 
     0.01 0];
B = [0; 
     1];
C = [1 0];
D = 0;

% Control Law
Q = [1 0;  % Penalize angular error
     0 1]; % Penalize angular rate
R = 1;     % Penalize thruster effort
K = lqr(A,B,Q,R);

% Closed loop system
sys = ss((A - B*K), B, C, D);

% Run response to initial condition
t = 0:0.005:30;
[y,t,x] = initial(sys, x0, t);

%% Everything below this is just for the visualization

% Patch drawings
ufo_data = [
    0.54 .12;
    0.48 0.24;
    0.42 0.31;
    0.3 0.4;
    0.18 0.45;
    0.06 0.48;
    -0.06 0.48;
    -0.18 0.45;
    -0.3 0.4;
    -0.42 0.31;
    -0.48 0.24;
    -0.54 .12;
    0.54 .12;
    0.54 0.06;
    0.6 0.06;
    0.78 0.03; 
    0.91 0.01;
    0.91 0.03;
    1.01 0.03;
    1.01 -0.01;   
    1.1 -0.03; 
    1.26 -0.12; 
    1.36 -0.18; 
    1.40 -0.21;
    1.38 -0.24;
    1.26 -0.27;
    1.1 -0.3;  
    1.01 -0.32;
    1.01 -0.34;
    0.91 -0.34;
    0.91 -0.32;  
    0.72 -0.34;
    0.48 -0.35;
    0.72 -0.6;
    0.62 -0.6;
    0.38 -0.35;    
    0.3 -0.35;    
    -0.3 -0.35;
    -0.38 -0.35;
    -0.62 -0.6;
    -0.72 -0.6;
    -0.48 -0.35;
    -0.91 -0.32;
    -0.91 -0.34;
    -1.01 -0.34;
    -1.01 -0.32;
    -1.1 -0.3;
    -1.26 -0.27;
    -1.38 -0.24;
    -1.40 -0.21;
    -1.36 -0.18;
    -1.26 -0.12;
    -1.1 -0.03;
    -1.01 -0.01;
    -1.01 0.03;
    -0.91 0.03;
    -0.91 0.01;
    -0.78 0.03;
    -0.6 0.06;
    -0.54 0.06;
    0.54 0.06;
];

thrust = [
    0.05 0;
    -.05 0;
    -.06 -0.1;
    -.05 -0.1;
    -.06 -0.2;
    0 -0.25;
    0.06 -0.2;
    0.05 -0.1;
    0.06 -0.1;
    0.05 0];

% yoffset moves the patch up from 0, 0
yoffset = 1;

%% Create Animation

% Rotate to inital condition
ufo_rot = rot(ufo_data, x0(1));
r_thrust_rot = rot(thrust, x0(1));
l_thrust_rot = rot(thrust, x0(1));

% Set up the figure and the subplots
fig = figure('Position', [100 80 670 800]);
h3 = subplot(3, 1, 3);
h3.OuterPosition = [0 0.01 1 .19];
h3a = area(0, 0);
hold on
h3p = plot(0, 0, 'r');
h3.YGrid = 'on';
h3.XGrid = 'on';
h3.LineWidth = 1;
h3.Color = [0 0 0];
h3p.LineWidth = 4;
axis([0 12 -3 3]);
h3.GridColor = 'w';
h3.GridAlpha = 0.5;
text(6.1, 1.5, 'Acceleration (fuel)', 'Color', [1 0 0], 'FontSize', 24)

h2 = subplot(3, 1, 2);
h2.OuterPosition = [0 .2 1 .2];
h2p = plot(0, 0, 'g');
h2p.LineWidth = 4;
h2.YGrid = 'on';
h2.XGrid = 'on';
h2.LineWidth = 1;
h2.GridColor = 'w';
h2.GridAlpha = 0.5;
axis([0 12 -.5 3.5]);
h2.Color = [0 0 0];
text(6.1, 2.5, 'Angular Error (time)', 'Color', [0 1 0], 'FontSize', 24)

h1 = subplot(3, 1, 1);
h1.OuterPosition = [0 .4 1 0.63];
h1.XTick = [];
h1.YTick = [];
patch([-3 3 3 -3], [-1.7 -1.7 3.3 3.3], 'k');
axis([-3 3 -1.7 3.3]);
line([0 0.28], [1 2.99], 'Color', [0.5 0.3 0.3], 'LineWidth', 1);
line([0 0], [1 -1], 'Color', [0.3 0.5 0.3], 'LineWidth', 1);
H = patch(ufo_rot(:, 1), ufo_rot(:, 2) + yoffset, [0.7 0.7 0.7]);
RBT = patch(r_thrust_rot(:, 1), r_thrust_rot(:, 2), 'r');
LBT = patch(l_thrust_rot(:, 1), l_thrust_rot(:, 2), 'r');
RTT = patch(r_thrust_rot(:, 1), r_thrust_rot(:, 2), 'r');
LTT = patch(l_thrust_rot(:, 1), l_thrust_rot(:, 2), 'r');
RBT.FaceAlpha = 0;
LBT.FaceAlpha = 0;
RTT.FaceAlpha = 0;
LTT.FaceAlpha = 0;
RBT.EdgeColor = 'none';
LBT.EdgeColor = 'none';
RTT.EdgeColor = 'none';
LTT.EdgeColor = 'none';
hold on

% Initial time and fuel values
current_time = 0;
fuel = 0;

% Write time and fuel to the screen
text(-2.5, 3, 'Time: sec', 'Color', [1 1 1], 'FontSize', 24,...
    'HorizontalAlignment', 'left');
text(-2.5, 2.7, 'Fuel: units', 'Color', [1 1 1], 'FontSize', 24,...
    'HorizontalAlignment', 'left');

TME = text(-1.1, 3, num2str(current_time, '%.1f'), 'Color', [0 1 0], ...
'FontSize', 24, 'HorizontalAlignment', 'right');
FUL = text(-1.1, 2.7, num2str(fuel, '%.0f'), 'Color', [1 0 0], ...
'FontSize', 24, 'HorizontalAlignment', 'right');

accel_true = diff(x(:, 2))/(t(2)-t(1));

pause(2)

% Update Animation

for i = 1:5:length(x)-1
    
    % Simulation ending condition
    if and(abs(x(i,1)) < 0.011, abs(x(i,2)) < 0.011)
        x(i,1) = 0;
        pause(1);
        % Turn on tractor beam
        G = patch([0.1 0.8 -0.8 -0.1], [-0.35 -2.3 -2.3 -0.35] + yoffset, 'y');
        G.FaceAlpha = 0.6;
        return
    end
    
    % Scale acceleration to make thruster size look better fuel units
    % just a few digits (asthetics)
    accel_scaled = (x(i+1, 2) - x(i, 2));
    
    fuel = fuel + abs(accel_scaled);
    
    accel_scaled = min(2, max(-2, accel_scaled * 1000));
    
    % Scale thruster based on accleration and direction
    if accel_scaled > 0
        r_scaled(:, 1) = thrust(:, 1) * accel_scaled * 0.5;
        r_scaled(:, 2) = thrust(:, 2) * accel_scaled;
        l_scaled(:, 1) = thrust(:, 1);
        l_scaled(:, 2) = thrust(:, 2);
        
    elseif accel_scaled < 0
        r_scaled(:, 1) = thrust(:, 1);
        r_scaled(:, 2) = thrust(:, 2);
        l_scaled(:, 1) = thrust(:, 1) * -accel_scaled * 0.5;
        l_scaled(:, 2) = thrust(:, 2) * -accel_scaled;
    else
        r_scaled(:, 1) = thrust(:, 1);
        r_scaled(:, 2) = thrust(:, 2);
        l_scaled(:, 1) = thrust(:, 1);
        l_scaled(:, 2) = thrust(:, 2);
    end
    
    % Offset thrusters to place correctly on UFO
    rb_thrust = r_scaled + [0.96 -0.34];
    lb_thrust = l_scaled + [-0.96 -0.34];
    rt_thrust = l_scaled * [1 0; 0 -1] + [0.96, 0.03];
    lt_thrust = r_scaled * [1 0; 0 -1] + [-0.96, 0.03];
    
    % Rotate thrusters with UFO
    rb_thrust_rot = rot(rb_thrust, x(i,1)) + [0 yoffset];
    lb_thrust_rot = rot(lb_thrust, x(i,1)) + [0 yoffset];
    rt_thrust_rot = rot(rt_thrust, x(i,1)) + [0 yoffset];
    lt_thrust_rot = rot(lt_thrust, x(i,1)) + [0 yoffset];
    
    % Update thruster data 
    RBT.XData = rb_thrust_rot(:, 1);
    RBT.YData = rb_thrust_rot(:, 2);
    LBT.XData = lb_thrust_rot(:, 1);
    LBT.YData = lb_thrust_rot(:, 2);
    RTT.XData = rt_thrust_rot(:, 1);
    RTT.YData = rt_thrust_rot(:, 2);
    LTT.XData = lt_thrust_rot(:, 1);
    LTT.YData = lt_thrust_rot(:, 2);
    
   
    % Turn thrusters off if acceleration is low 
    if accel_scaled > 2e-1
        RBT.FaceAlpha = 1;
        LBT.FaceAlpha = 0;
        RTT.FaceAlpha = 0;
        LTT.FaceAlpha = 1;
        
    elseif accel_scaled < -2e-1
        RBT.FaceAlpha = 0;
        LBT.FaceAlpha = 1;
        RTT.FaceAlpha = 1;
        LTT.FaceAlpha = 0;
    else
        RBT.FaceAlpha = 0;
        LBT.FaceAlpha = 0;
        RTT.FaceAlpha = 0;
        LTT.FaceAlpha = 0;
    end
    
    % Rotate ufo
    ufo_rot = rot(ufo_data, x(i,1));
    H.XData = ufo_rot(:, 1);
    H.YData = ufo_rot(:, 2) + yoffset;
    h2p.XData = t(1:i);
    h2p.YData = x(1:i, 1);
    h3p.XData = t(1:i);
    h3p.YData = accel_true(1:i);
    h3a.XData = t(1:i);
    h3a.YData = accel_true(1:i);
    
    current_time = num2str(t(i), '%.1f');

    % Update time and fuel
    TME.String = current_time;
    FUL.String = num2str(fuel*30, '%.0f');
    
    pause(0.0001);
end

function output = rot(data, angle)
output = size(data);
Q = [cos(angle) -sin(angle); sin(angle) cos(angle)];
for i = 1:length(data)
    output(i, :) = Q * data(i, :)';
end
end
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/127941.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SQLite 下载与安装「建议收藏」

    SQLite 下载与安装「建议收藏」一,安装1.到sqlite官网下载压缩包https://www.sqlite.org/download.html下载后是下面这2个文件:解压到同一目录,如图:”安装”完成后,在cmd下,执行sqlite3验证下是否安装正常。若不正常则需要添加系统环境变量,以便命令行式的操作。二,安装管理工具官网:http://sqlitestudio.pl或直接访问下载https://github.com/pawelsalawa/sqlitestudi…

    2022年10月24日
    0
  • 心血漏洞第二发?SSL v3再曝新漏洞发布预警

    心血漏洞第二发?SSL v3再曝新漏洞发布预警2019独角兽企业重金招聘Python工程师标准>>>…

    2022年7月16日
    7
  • 重建二叉树 python_Python实现重建二叉树的三种方法详解

    重建二叉树 python_Python实现重建二叉树的三种方法详解本文实例讲述了Python实现重建二叉树的三种方法。分享给大家供大家参考,具体如下:学习算法中,探寻重建二叉树的方法:用input前序遍历顺序输入字符重建前序遍历顺序字符串递归解析重建前序遍历顺序字符串堆栈解析重建如果懒得去看后面的内容,可以直接点击此处本站下载完整实例代码。思路学习算法中,python算法方面的资料相对较少,二叉树解析重建更少,只能摸着石头过河。通过不同方式遍历二叉树,可以得…

    2022年4月30日
    50
  • openGL之API学习(六十九)水平同步 垂直同步「建议收藏」

    openGL之API学习(六十九)水平同步 垂直同步「建议收藏」垂直和水平是CRT中两个基本的同步信号,水平同步信号决定了CRT画出一条横越屏幕线的时间,垂直同步信号决定了CRT从屏幕顶部画到底部,再返回原始位置的时间,而恰恰是垂直同步代表着CRT显示器的刷新率水平!垂直同步打开,那么在游戏中,或许强劲的显卡迅速的绘制完一屏的图像,但是没有垂直同步信号的到达,显卡无法绘制下一屏,只有等垂直同步信号到达,才可以绘制。这样fps自然要受到操作系统刷新率运行值的…

    2022年5月21日
    30
  • 电脑蓝屏错误代码0x000000ED_蓝屏代码0x000000ed

    电脑蓝屏错误代码0x000000ED_蓝屏代码0x000000ed电脑蓝屏的原因很多,显示的电脑蓝屏也不一样,对应的修复电脑蓝屏的方法也不同。最近就有网友反映自己的电脑蓝屏代码0x000000ed怎么办,该怎么修复电脑蓝屏呢?今天小编就教下大家电脑蓝屏代码0x000000ed的解决方法。1、蓝屏0x000000ed代码为加载引导时失败,首先先尝试重启。2、如果不能解决的话,重启电脑按住F8,选择进入安全模式,然后进入安全模式桌面。2、进入安全模式后,选择左下角开始菜单,依次选择“所有程序”-“附件”-“命令提示符”右键选择以管理员打开。或者直接快捷键win

    2022年10月8日
    0
  • 中缀表达式转后缀表达式栈的变化_利用栈实现中缀转后缀

    中缀表达式转后缀表达式栈的变化_利用栈实现中缀转后缀这里给出中缀表达式转后缀表达式的算法过程,以及再举两个例子算法过程:1.数字直接加入后缀表达式2.如果是‘(’,入栈3.如果是‘)’,则依次把栈中的运算符加入后缀表达式,直到出现‘(’并从栈中删除它4.如果是运算符+-*/a.栈空或者栈顶元素为‘(’,入栈b.高于栈顶元素优先级,入栈c.否则依次弹出栈顶运算符,直到遇到一个优先级小于它的运算符或者是遇到‘(’为止5.遍历完成后,如果栈非空则依次弹出所有栈顶元素加入到表达式当中例1:…

    2022年10月30日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号