使用Matlab AppDesigner 进行飞行器坐标变换演示

1 原由

学习坐标变换需要有较强的空间想象能力,最好结合实际案例有一定的感性认识,否则坐标轴几经旋转后,凭空想象很难理解。为此,笔者使用Matlab AppDesigner 进行飞行器坐标变换演示GUI,通过界面交互和三维动画演示,可以帮助用户建立坐标变换的感性认识。

2 示意

地面坐标系[公式]:与地面固定,视为固定不动的坐标系;

机体坐标系 [公式] :与飞行器机体固定,随飞行器运动的坐标系;

滚转角(Roll):绕 [公式] 轴旋转角度(从 [公式] 轴看逆时针方向为正);

俯仰角(Pitch):绕 [公式] 轴旋转角度(从 [公式] 轴看逆时针方向为正);

偏航角(Yaw):绕 [公式] 轴旋转角度(从 [公式] 轴看逆时针方向为正);

假设初始状态下(滚转角、俯仰角、偏航角均为0时),地面坐标系与机体坐标系重合。飞机滚转、俯仰或偏航动作后,机体坐标系与地面坐标系出现夹角,根据转动的欧拉角(滚转角、俯仰角、偏航角)可以求出机体坐标系向地面坐标系的坐标变换矩阵,由此可以将机体坐标系中的物理量转换到地面坐标系中描述,反之也可将地面坐标系中的物理量转换到机体坐标系中描述。

3 界面

4 演示

4.1 滚转动作

4.2 俯仰动作

4.3 偏航动作

5 一些方法

5.1 飞机三维模型绘制方法

飞机使用若干三角曲面网格近似绘制,首先获取三角网格各个节点的位置坐标,然后定义三角网格各个节点的连接关系,再使用patch函数绘制填充曲面图,将三角曲面按照节点定义依次连接形成飞机的轮廓。飞机三维模型的绘制程序如下:

% X为节点坐标数据
X =  [[0.4 0.025 -0.0433012701892219;0.4 0.05 0;0.4 0.025 0.0433012701892219;0.4 -0.025 0.0433012701892219;0.3 -0.035 0.0606217782649107;0.2 0.035 0.0606217782649107;0.2 0.07 0;0 0.065 0;-0.1 0.03 0.0519615242270663;-0.1 -0.03 0.0519615242270663;-0.2 -0.025 0.0433012701892219;-0.3 -0.04 0;-0.2 -0.05 0;-0.1 -0.06 0;0 -0.065 0;0 -0.0325 0.0562916512459885;0 -0.0325 -0.0562916512459885;-0.1 0.03 -0.0519615242270663;-0.1 0.06 0;-0.1 -0.03 -0.0519615242270663;-0.4 0.015 -0.0259807621135332;-0.4 0.03 0;-0.5 0.02 0;-0.2 -0.025 -0.0433012701892219;-0.4 -0.03 0;-0.4 -0.015 0.0259807621135332;-0.5 -0.01 0.0173205080756888;-0.2 0.025 0.0433012701892219;-0.4 0.015 0.0259807621135332;-0.5 0.01 0.0173205080756888;-0.5 -0.01 -0.0173205080756888;-0.5 -0.02 0;-0.4 -0.015 -0.0259807621135332;-0.3 0.04 0;-0.2 0.025 -0.0433012701892219;0 0.0325 -0.0562916512459885;0.2 -0.035 -0.0606217782649107;0.2 0.035 -0.0606217782649107;0.3 0.035 -0.0606217782649107;0.3 0.07 0;0.5 -0.01 -0.0173205080756888;0.5 -0.02 0;0.5 -0.01 0.0173205080756888;0.4 -0.05 0;0.4 -0.025 -0.0433012701892219;0.3 -0.035 -0.0606217782649107;0.3 -0.07 0;0.2 -0.07 0;0.2 -0.035 0.0606217782649107;0.5 0.01 -0.0173205080756888;0.5 0.02 0;0.3 0.035 0.0606217782649107;0.5 0.01 0.0173205080756888;0 0.0325 0.0562916512459885;-0.2 0.05 0;-0.5 0.01 -0.0173205080756888;0.2 0.0657784834550136 0.0239414100327968;0 0.0562916512459885 -0.0325;0.05 0.5 0;0.2 0.07 -0;0 0.061080020351084 0.0222313093161685;-0.05 0.5 0;0 -0.0562916512459885 -0.0325;0.2 -0.0657784834550136 0.0239414100327968;0.05 -0.5 0;0.2 -0.07 -0;-0.05 -0.5 0;0 -0.061080020351084 0.0222313093161685;-0.45 0.2 0;-0.4 0.0259807621135332 0.015;-0.4 0.0259807621135332 -0.015;-0.5 0.0173205080756888 -0.01;-0.5 0.2 0;-0.5 0.0173205080756888 0.01;-0.4 -0.0259807621135332 0.015;-0.4 -0.0259807621135332 -0.015;-0.45 -0.2 0;-0.5 -0.0173205080756888 -0.01;-0.5 -0.0173205080756888 0.01;-0.5 -0.2 0;-0.4 0.012678547852221 0.0271892336110995;-0.4 -0.012678547852221 0.0271892336110995;-0.45 0 0.15;-0.5 -0.00845236523481399 0.018126155740733;-0.5 0.00845236523481399 0.018126155740733;-0.5 0 0.15]];
% TR定义节点连接关系
TR = [1 50 41;1 51 50;2 51 1;3 51 2;3 53 51;3 43 53;3 4 43;4 3 52;5 4 52;5 47 4;5 49 47;6 49 5;6 54 49;7 54 6;8 54 7;9 54 8;9 16 54;9 10 16;10 9 28;11 10 28;11 14 10;11 13 14;12 13 11;13 12 24;13 24 14;14 24 20;15 14 20;15 10 14;16 10 15;15 48 16;17 48 15;17 37 48;17 36 37;18 36 17;18 8 36;19 8 18;9 8 19;9 19 28;19 55 28;19 35 55;19 18 35;18 20 35;18 17 20;17 15 20;20 24 35;21 35 24;22 35 21;22 34 35;22 28 34;22 29 28;22 23 29;23 22 21;23 21 56;21 33 56;21 24 33;24 25 33;12 25 24;25 12 11;25 11 26;26 32 25;26 27 32;27 26 30;26 29 30;11 29 26;28 29 11;29 23 30;30 23 56;27 30 56;31 27 56;32 27 31;33 32 31;25 32 33;33 31 56;34 28 55;35 34 55;36 8 7;36 7 38;37 36 38;38 46 37;38 39 46;39 38 7;40 39 7;39 40 1;1 45 39;1 41 45;41 42 45;42 41 43;43 4 42;42 4 44;42 44 45;44 47 45;4 47 44;45 47 46;45 46 39;46 47 37;37 47 48;47 49 48;48 49 16;49 54 16;41 50 43;50 53 43;51 53 50;1 40 2;2 40 3;3 40 52;52 40 7;6 52 7;6 5 52;57 60 59;57 58 60;58 57 61;57 62 61;57 59 62;59 58 62;60 58 59;61 62 58;63 64 66;64 63 68;64 68 67;65 64 67;66 64 65;63 66 65;63 65 67;67 68 63;69 71 73;70 71 69;71 70 74;70 73 74;70 69 73;72 74 73;72 71 74;71 72 73;75 76 79;76 75 77;77 75 80;75 79 80;78 80 79;78 76 80;76 78 79;76 77 80;81 82 85;82 81 83;83 81 86;81 85 86;84 86 85;84 82 86;82 84 85;82 83 86];
% 绘图
patch('faces',TR,'vertices',X, 'facecolor','y', 'edgecolor','k');
axis equal;  % 设置坐标轴等比例
axis off;    % 隐藏坐标轴   
view(-10,20) % 设置视角

5.2 坐标变换及三维图形更新

Matlab的导航工具箱(Navigation Toolbox)中提供了丰富的函数来实现坐标变换功能,如eul2quat实现欧拉角向四元数的转换,quat2eul实现四元数向欧拉角的转换,eul2tform实现欧拉角向坐标变换矩阵的转换。

三维图形更新的思路也很简单:首先获取用户输入的欧拉角或四元数,再使用上述Matlab函数求取坐标变换矩阵,然后通过坐标转换矩阵将飞机的绘图数据由机体坐标系转换为地面坐标系(相当于绘图坐标系),再使用set函数更新飞机图形对象的坐标数据,形成动画效果。

5.3 关于AppDesigner

这是我第一次使用AppDesigner来制作GUI,以前都是使用GUIde,总的来说,AppDesigner功能比GUIde丰富很多,操作更加便捷,界面相对美观。最大的感受是:

  • App文件由GUIde中的m文件+fig文件方式变为单个mlapp文件,避免了m文件和fig文件不协调问题,且同一界面下编辑更为便捷;
  • 多个控件可以共用同一回调函数,这里三个滑块模块都是共用同一个回调函数,直接在回调函数栏中选用即可。

6 源程序下载

更多细节,欢迎下载源程序。
有Matlab/Simulink方面的技术问题,欢迎发送邮件至944077462@qq.com讨论。更多Matlab/Simulink原创资料,欢迎关注微信公众号:Matlab Fans
使用Matlab AppDesigner 进行飞行器坐标变换演示
在这里插入图片描述

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,下面是一个简单的Matlab App Designer程序,可以将笛卡尔坐标系转换为极坐标系,或将极坐标系转换为笛卡尔坐标系: 1. 创建一个新的Matlab App Designer程序 2. 添加两个输入框和一个按钮,用于输入坐标和进行转换 3. 设置输入框的属性,使其可以接受数字输入 4. 在按钮的回调函数中编写转换代码 5. 运行程序并测试 以下是示例代码: ```matlab classdef CoordinateConverter < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure CartesianEditField matlab.ui.control.NumericEditField PolarEditField matlab.ui.control.NumericEditField ConvertButton matlab.ui.control.Button end % Callbacks that handle component events methods (Access = private) % Button pushed function: ConvertButton function ConvertButtonPushed(app, event) % Get the input value from the Cartesian edit field x = app.CartesianEditField.Value; % Convert Cartesian to polar coordinates [theta, rho] = cart2pol(x(1), x(2)); % Update the polar edit field with the new values app.PolarEditField.Value = [theta, rho]; end end % App initialization and construction methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure app.UIFigure = uifigure; app.UIFigure.Position = [100 100 307 171]; app.UIFigure.Name = 'Coordinate Converter'; % Create CartesianEditField app.CartesianEditField = uieditfield(app.UIFigure, 'numeric'); app.CartesianEditField.Position = [28 83 115 22]; % Create PolarEditField app.PolarEditField = uieditfield(app.UIFigure, 'numeric'); app.PolarEditField.Position = [167 83 115 22]; % Create ConvertButton app.ConvertButton = uibutton(app.UIFigure, 'push'); app.ConvertButton.ButtonPushedFcn = createCallbackFcn(app, @ConvertButtonPushed, true); app.ConvertButton.Position = [121 24 70 22]; app.ConvertButton.Text = 'Convert'; % Create CartesianLabel CartesianLabel = uilabel(app.UIFigure); CartesianLabel.HorizontalAlignment = 'right'; CartesianLabel.Position = [28 112 60 22]; CartesianLabel.Text = 'Cartesian'; % Create PolarLabel PolarLabel = uilabel(app.UIFigure); PolarLabel.HorizontalAlignment = 'right'; PolarLabel.Position = [167 112 33 22]; PolarLabel.Text = 'Polar'; end end % App startup and termination methods (Access = private) % Code that executes after component creation function startupFcn(app) end end % Component initialization and construction methods (Access = public) % Construct app function app = CoordinateConverter % Create and configure components createComponents(app) % Register the app with App Designer registerApp(app, app.UIFigure) % Execute the startup function runStartupFcn(app, @startupFcn) % Show the figure after all components are created app.UIFigure.Visible = 'on'; end end end ``` 这个程序只实现了笛卡尔坐标系到极坐标系的转换,如果需要实现极坐标系到笛卡尔坐标系的转换,只需要在回调函数中调用`pol2cart`函数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MatlabFans_Mfun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值