classdef QuadPlot < handle
%QUADPLOT Visualization class for quad
properties (SetAccess = public)
k = 0;
qn; % quad number
time = 0; % time
state; % state
des_state; % desried state [x; y; z; xdot; ydot; zdot];
rot; % rotation matrix body to world
color; % color of quad
wingspan; % wingspan
height; % height of quad
motor; % motor position
state_hist % position history
state_des_hist; % desired position history
time_hist; % time history
max_iter; % max iteration
end
properties (SetAccess = private)
h_3d
h_m13; % motor 1 and 3 handle
h_m24; % motor 2 and 4 handle
h_qz; % z axis of quad handle
h_qn; % quad number handle
h_pos_hist; % position history handle
h_pos_des_hist; % desired position history handle
text_dist; % distance of quad number to quad
end
methods
% Constructor
function Q = QuadPlot(qn, state, wingspan, height, color, max_iter, h_3d)
Q.qn = qn;
Q.state = state;
Q.wingspan = wingspan;
Q.color = color;
Q.height = height;
Q.rot = QuatToRot(Q.state(7:10));
Q.motor = quad_pos(Q.state(1:3), Q.rot, Q.wingspan, Q.height);
Q.text_dist = Q.wingspan / 3;
Q.des_state = Q.state(1:6);
Q.max_iter = max_iter;
Q.state_hist = zeros(6, max_iter);
Q.state_des_hist = zeros(6, max_iter);
Q.time_hist = zeros(1, max_iter);
% Initialize plot handle
if nargin < 7, h_3d = gca; end
Q.h_3d = h_3d;
hold(Q.h_3d, 'on')
Q.h_pos_hist = plot3(Q.h_3d, Q.state(1), Q.state(2), Q.state(3), 'r.');
Q.h_pos_des_hist = plot3(Q.h_3d, Q.des_state(1), Q.des_state(2), Q.des_state(3), 'b.');
Q.h_m13 = plot3(Q.h_3d, ...
Q.motor(1,[1 3]), ...
Q.motor(2,[1 3]), ...
Q.motor(3,[1 3]), ...
'-ko', 'MarkerFaceColor', Q.color, 'MarkerSize', 5);
Q.h_m24 = plot3(Q.h_3d, ...
Q.motor(1,[2 4]), ...
Q.motor(2,[2 4]), ...
Q.motor(3,[2 4]), ...
'-ko', 'MarkerFaceColor', Q.color, 'MarkerSize', 5);
Q.h_qz = plot3(Q.h_3d, ...
Q.motor(1,[5 6]), ...
Q.motor(2,[5 6]), ...
Q.motor(3,[5 6]), ...
'Color', Q.color, 'LineWidth', 2);
Q.h_qn = text(...
Q.motor(1,5) + Q.text_dist, ...
Q.motor(2,5) + Q.text_dist, ...
Q.motor(3,5) + Q.text_dist, num2str(qn));
hold(Q.h_3d, 'off')
end
% Update quad state
function UpdateQuadState(Q, state, time)
Q.state = state;
Q.time = time;
Q.rot = QuatToRot(state(7:10))'; % Q.rot needs to be body-to-world
end
% Update desired quad state
function UpdateDesiredQuadState(Q, des_state)
Q.des_state = des_state;
end
% Update quad history
function UpdateQuadHist(Q)
Q.k = Q.k + 1;
Q.time_hist(Q.k) = Q.time;
Q.state_hist(:,Q.k) = Q.state(1:6);
Q.state_des_hist(:,Q.k) = Q.des_state(1:6);
end
% Update motor position
function UpdateMotorPos(Q)
Q.motor = quad_pos(Q.state(1:3), Q.rot, Q.wingspan, Q.height);
end
% Truncate history
function TruncateHist(Q)
Q.time_hist = Q.time_hist(1:Q.k);
Q.state_hist = Q.state_hist(:, 1:Q.k);
Q.state_des_hist = Q.state_des_hist(:, 1:Q.k);
end
% Update quad plot
function UpdateQuadPlot(Q, state, des_state, time)
Q.UpdateQuadState(state, time);
Q.UpdateDesiredQuadState(des_state);
Q.UpdateQuadHist();
Q.UpdateMotorPos();
set(Q.h_m13, ...
'XData', Q.motor(1,[1 3]), ...
'YData', Q.motor(2,[1 3]), ...
'ZData', Q.motor(3,[1 3]));
set(Q.h_m24, ...
'XData', Q.motor(1,[2 4]), ...
'YData', Q.motor(2,[2 4]), ...
'ZData', Q.motor(3,[2 4]));
set(Q.h_qz, ...
'XData', Q.motor(1,[5 6]), ...
'YData', Q.motor(2,[5 6]), ...
'ZData', Q.motor(3,[5 6]))
set(Q.h_qn, 'Position', ...
[Q.motor(1,5) + Q.text_dist, ...
Q.motor(2,5) + Q.text_dist, ...
Q.motor(3,5) + Q.text_dist]);
set(Q.h_pos_hist, ...
'XData', Q.state_hist(1,1:Q.k), ...
'YData', Q.state_hist(2,1:Q.k), ...
'ZData', Q.state_hist(3,1:Q.k));
set(Q.h_pos_des_hist, ...
'XData', Q.state_des_hist(1,1:Q.k), ...
'YData', Q.state_des_hist(2,1:Q.k), ...
'ZData', Q.state_des_hist(3,1:Q.k));
drawnow;
end
end
end
毕业小助手
- 粉丝: 2766
- 资源: 5583
最新资源
- 2009-2023年沪深北证上市公司A股华证ESG评级和得分汇总-最新出炉.zip
- 2009-2023年一季度更新的上市公司华证ESG评级及得分数据-最新出炉.zip
- 2010-2019年地级市商业信用环境数据大揭秘-最新出炉.zip
- 2009年首都大学生成长追踪调查(CSDPS)基线调查数据-最新出炉.zip
- 2010-2020年债券信用利差数据-最新出炉.zip
- 2010-2020年省级工业“三废”排放量数据汇总-最新出炉.zip
- 2010-2021年33家银行绿色信贷余额数据(平安等银行)-最新出炉.zip
- 2010-2021年全国31个省份农业保险数据(含保险深度、密度等多维度)-最新出炉.zip
- 2010-2021年全国各省碳排放和农业碳排放数据合集-最新出炉.zip
- 2010-2021年和讯网上市公司社会责任评分一级指标(2022.7.14原始爬取更新)-最新出炉.zip
- 2010-2021年上市银行风险承担ZSCORE指标计算数据与Stata代码(更新)-最新出炉.zip
- 2010-2021年我国商业银行数字化转型指数(战略、业务、管理)面板数据-最新出炉.zip
- 2010-2021年上市公司和讯网社会责任评级CSR(各分项评级)-最新出炉.zip
- 2010-2024年全国296个地级市房价数据汇总-最新出炉.zip
- 2010-2021年中国商业银行数字化转型指数面板数据(Excel或Stata版)-最新出炉.zip
- 2010-2023.7全国31省份、直辖市专利侵权结案面板数据-最新出炉.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈