【MATLAB UAV Toolbox】使用指南(三)

可视化自定义飞行日志

通过配置flightLogSignalMapping可从自定义的飞行日志中可视化数据。

加载自定义的飞行日志

在本例中,假设飞行数据已经被解析到MATLAB®中,并存储为M文件。本示例重点介绍如何配置flightLogSignalMapping,以便它能够正确地处理保存在M文件中并将日志数据可视化。customFlightData.mat存储包含3个字段的结构.Fs是信号的抽样频率,它存储在MAT文件里。IMUTrajectory是包含实际飞行信息的矩阵。弹道数据和IMU数据是基于一个模拟飞行器,它沿着xy平面上的一个投影矩形路径飞行。

customData = load("customFlightData.mat");
logData = customData.logData

logData = struct with fields:
IMU: [2785×9 double]
Fs: 100
Trajectory: [2785×10 double]

logData中的IMU字段是一个n × 9矩阵,其中前3列是加速度计读数,单位是m/s^2,第4~6列是以rad/s为单位的陀螺仪读数,最后3列是以μT为单位的磁力计读数。

logData.IMU(1:5, :)

ans = 5×9

0.8208    0.7968   10.7424    0.0862    0.0873    0.0862  327.6000  297.6000  283.8000
0.8016    0.8160   10.7904    0.0883    0.0873    0.0862  327.6000  297.6000  283.8000
0.7680    0.7680   10.7568    0.0862    0.0851    0.0851  327.6000  297.6000  283.8000
0.8208    0.7536   10.7520    0.0873    0.0883    0.0819  327.6000  297.6000  283.8000
0.7872    0.7728   10.7328    0.0873    0.0862    0.0830  327.6000  297.6000  283.8000

LogData中的轨迹字段是一个n×9的矩阵,前3列是m中的XYZ NED坐标,后3列是以m/s为单位的XYZ NED方向的速度,后4列是描述无人机从惯性NED框架到身体框架的旋转的四元数。每一行都是定义了所有这些参数的轨迹的一个点。

logData.Trajectory(1:5,:)

ans = 5×10

0.0200         0   -4.0000    2.0000         0   -0.0036    1.0000         0         0   -0.0000
0.0400         0   -4.0001    2.0000         0   -0.0072    1.0000         0         0   -0.0000
0.0600         0   -4.0002    2.0000         0   -0.0108    1.0000         0         0   -0.0000
0.0800         0   -4.0003    2.0000         0   -0.0143    1.0000         0         0   -0.0000
0.1000         0   -4.0004    2.0000         0   -0.0179    1.0000         0         0   -0.0001

使用预定义的信号格式和图形可视化自定义飞行日志

创建一个不带输入参数的flightLogSignalMapping对象,因为自定义日志格式不遵循标准的“ulog”或“tlog”定义。

customPlotter = flightLogSignalMapping;

该对象有一组可以映射的预定义信号。通过映射这些预定义的信号,您可以访问一组预定义的图。注意,一些信号有一个“#”符号后缀。对于这些信号,您可以选择添加整数作为信号名称的后缀,以便飞行日志绘图仪能够处理多种这类信号。如二次IMU信号和气压计读数。叫info

% Predefined signals
info(customPlotter, "Signal")

ans=18×4 table
SignalName
IsMapped
SignalFields
FieldUnits

"Accel#"                  false      "AccelX, AccelY, AccelZ"                                                                                                                                                                                      "m/s^2, m/s^2, m/s^2"                              
"Airspeed#"               false      "PressDiff, IndicatedAirSpeed, Temperature"                                                                                                                                                                   "Pa, m/s, degreeC"                                 
"AttitudeEuler"           false      "Roll, Pitch, Yaw"                                                                                                                                                                                            "rad, rad, rad"                                    
"AttitudeRate"            false      "BodyRotationRateX, BodyRotationRateY, BodyRotationRateZ"                                                                                                                                                     "rad/s, rad/s, rad/s"                              
"AttitudeTargetEuler"     false      "RollTarget, PitchTarget, YawTarget"                                                                                                                                                                          "rad, rad, rad"                                    
"Barometer#"              false      "PressAbs, PressAltitude, Temperature"                                                                                                                                                                        "Pa, m, degreeC"                                   
"Battery"                 false      "Voltage_1, Voltage_2, Voltage_3, Voltage_4, Voltage_5, Voltage_6, Voltage_7, Voltage_8, Voltage_9, Voltage_10, Voltage_11, Voltage_12, Voltage_13, Voltage_14, Voltage_15, Voltage_16, RemainingCapacity"    "v, v, v, v, v, v, v, v, v, v, v, v, v, v, v, v, %"
"GPS#"                    false      "Latitude, Longitude, Altitude, GroundSpeed, CourseAngle, SatellitesVisible"                                                                                                                                  "degree, degree, m, m/s, degree, N/A"              
"Gyro#"                   false      "GyroX, GyroY, GyroZ"                                                                                                                                                                                         "rad/s, rad/s, rad/s"                              
"LocalENU"                false      "X, Y, Z"                                                                                                                                                                                                     "m, m, m"                                          
"LocalENUTarget"          false      "XTarget, YTarget, ZTarget"                                                                                                                                                                                   "m, m, m"                                          
"LocalENUVel"             false      "VX, VY, VZ"                                                                                                                                                                                                  "m/s, m/s, m/s"                                    
"LocalENUVelTarget"       false      "VXTarget, VYTarget, VZTarget"                                                                                                                                                                                "m/s, m/s, m/s"                                    
"LocalNED"                false      "X, Y, Z"                                                                                                                                                                                                     "m, m, m"                                          
"LocalNEDTarget"          false      "XTarget, YTarget, ZTarget"                                                                                                                                                                                   "m, m, m"                                          
"LocalNEDVel"             false      "VX, VY, VZ"                                                                                                                                                                                                  "m/s, m/s, m/s"                                    
  ⋮
% Predefined plots
info(customPlotter,"Plot")

ans=10×4 table
PlotName
ReadyToPlot
MissingSignals RequiredSignals

"Attitude"                    false       "AttitudeEuler, AttitudeRate, Gyro#"    "AttitudeEuler, AttitudeRate, Gyro#"
"AttitudeControl"             false       "AttitudeEuler, AttitudeTargetEuler"    "AttitudeEuler, AttitudeTargetEuler"
"Battery"                     false       "Battery"                               "Battery"                           
"Compass"                     false       "AttitudeEuler, Mag#, GPS#"             "AttitudeEuler, Mag#, GPS#"         
"GPS2D"                       false       "GPS#"                                  "GPS#"                              
"Height"                      false       "Barometer#, GPS#, LocalNED"            "Barometer#, GPS#, LocalNED"        
"Speed"                       false       "GPS#, Airspeed#"                       "GPS#, Airspeed#"                   
"Trajectory"                  false       "LocalNED, LocalNEDTarget"              "LocalNED, LocalNEDTarget"          
"TrajectoryTracking"          false       "LocalNED, LocalNEDTarget"              "LocalNED, LocalNEDTarget"          
"TrajectoryVelTracking"       false       "LocalNEDVel, LocalNEDVelTarget"        "LocalNEDVel, LocalNEDVelTarget"    

在将数据可视化之前,flightLogSignalMapping对象需要知道数据是如何存储在飞行日志中的。为了将信号名与访问logData中相关信息的函数句柄关联,你需要用mapSignal映射信号。每个信号被定义为一个时间戳向量和一个信号值矩阵。
举个例子,为了映射Gyro#信号,基于传感器数据采样频率来定义timeAccess函数句柄。这个函数句柄使用数据的全局时间戳间隔为信号值生成时间戳向量。

timeAccess = @(x)seconds(1/x.Fs*(1:size(x.IMU)));

接下来,使用info检查哪些字段必须为Gyro#信号定义。

info(customPlotter,"Signal","Gyro#")

ans=1×4 table
SignalName
IsMapped
SignalFields
FieldUnits

 "Gyro#"       false      "GyroX, GyroY, GyroZ"    "rad/s, rad/s, rad/s"

Gyro#信号需要包含XYZ轴陀螺仪读数的三列。相应地定义gyroAccess函数句柄,并使用mapSignal将其映射为timeAccess

gyroAccess = @(x)x.IMU(:,4:6);
mapSignal(customPlotter,"Gyro",timeAccess,gyroAccess);

同样,将其他预定义信号映射到飞行日志中的数据。为数据定义value函数句柄。使用相同的timeAccess时间戳向量函数映射信号。

% IMU data stores accelerometer and magnetometer data.
accelAccess = @(x)x.IMU(:,1:3);
magAccess = @(x)x.IMU(:,7:9)*1e-2;

% Flight trajectory in local NED coordinates
% XYZ coordinates
nedAccess = @(x)x.Trajectory(:, 1:3);
% XYZ celocities
nedVelAccess = @(x)x.Trajectory(:, 4:6);
% Roll Pitch Yaw rotations converted from a quaternion
attitudeAccess = @(x)flip(quat2eul(x.Trajectory(:, 7:10)),2);

% Configure flightLogSignalMapping for custom data
mapSignal(customPlotter, "Accel", timeAccess, accelAccess);
mapSignal(customPlotter, "Mag", timeAccess, magAccess);
mapSignal(customPlotter, "LocalNED", timeAccess, nedAccess);
mapSignal(customPlotter, "LocalNEDVel", timeAccess, nedVelAccess);
mapSignal(customPlotter, "AttitudeEuler", timeAccess, attitudeAccess);

一旦映射了所有信号,customPlotter就可以根据日志中存储的信号数据生成图形。为了快速检查信号是否被正确映射,调用checkSignal并指定logData

checkSignal(customPlotter,logData);

SignalName: Gyro
Pass
SignalName: Accel
Pass
SignalName: Mag
Pass
SignalName: LocalNED
Pass
SignalName: LocalNEDVel
Pass
SignalName: AttitudeEuler
Pass

要获得映射信号的预览,在checkSignal中选择预览选项

predefinedPlots = show(customPlotter,logData);

【MATLAB UAV Toolbox】使用指南(三)_第1张图片
【MATLAB UAV Toolbox】使用指南(三)_第2张图片
【MATLAB UAV Toolbox】使用指南(三)_第3张图片
【MATLAB UAV Toolbox】使用指南(三)_第4张图片
【MATLAB UAV Toolbox】使用指南(三)_第5张图片
【MATLAB UAV Toolbox】使用指南(三)_第6张图片

【MATLAB UAV Toolbox】使用指南(三)_第7张图片

可视化自定义飞行日志与自定义图

对于模型日志细节分析,定义更多信号并添加更多绘图,而不是存储在flightLogSignalMapping中的预定义绘图。指定过滤大于1的加速度的函数句柄。

accelThreshold = @(x)(vecnorm(accelAccess(x)')>11)';
mapSignal(customPlotter, "HighAccel", timeAccess,accelThreshold, "AccelGreaterThan11", "N/A");

updatePlot用于添加自定义图标。第一个参数是飞机日志绘图机对象,第二个参数是图表名字。第三个参数用Timeseries来指定时间数据集。

updatePlot(customPlotter, "AnalyzeAccel","Timeseries",["HighAccel.AccelGreaterThan11", "LocalNEDVel.VX", "LocalNEDVel.VY", "LocalNEDVel.VZ"]);

定义一个自定义函数句柄来生成一个图形句柄(参见下面的函数定义)。该函数使用fft和其他函数对加速度数据生成周期图,并绘制它们。函数返回一个函数句柄。

updatePlot(customPlotter, "plotFFTAccel",@(acc)plotFFTAccel(acc),"Accel");

检查customPlotter现在是否包含一个新的信号和两个新的图表

info(customPlotter, "Signal")

ans=19×4 table
SignalName
IsMapped
SignalFields
FieldUnits

"Accel"                   true       "AccelX, AccelY, AccelZ"                                                                                                                                                                                      "m/s^2, m/s^2, m/s^2"                              
"AttitudeEuler"           true       "Roll, Pitch, Yaw"                                                                                                                                                                                            "rad, rad, rad"                                    
"Gyro"                    true       "GyroX, GyroY, GyroZ"                                                                                                                                                                                         "rad/s, rad/s, rad/s"                              
"HighAccel"               true       "AccelGreaterThan11"                                                                                                                                                                                          "N/A"                                              
"LocalNED"                true       "X, Y, Z"                                                                                                                                                                                                     "m, m, m"                                          
"LocalNEDVel"             true       "VX, VY, VZ"                                                                                                                                                                                                  "m/s, m/s, m/s"                                    
"Mag"                     true       "MagX, MagY, MagZ"                                                                                                                                                                                            "Gs, Gs, Gs"                                       
"Airspeed#"               false      "PressDiff, IndicatedAirSpeed, Temperature"                                                                                                                                                                   "Pa, m/s, degreeC"                                 
"AttitudeRate"            false      "BodyRotationRateX, BodyRotationRateY, BodyRotationRateZ"                                                                                                                                                     "rad/s, rad/s, rad/s"                              
"AttitudeTargetEuler"     false      "RollTarget, PitchTarget, YawTarget"                                                                                                                                                                          "rad, rad, rad"                                    
"Barometer#"              false      "PressAbs, PressAltitude, Temperature"                                                                                                                                                                        "Pa, m, degreeC"                                   
"Battery"                 false      "Voltage_1, Voltage_2, Voltage_3, Voltage_4, Voltage_5, Voltage_6, Voltage_7, Voltage_8, Voltage_9, Voltage_10, Voltage_11, Voltage_12, Voltage_13, Voltage_14, Voltage_15, Voltage_16, RemainingCapacity"    "v, v, v, v, v, v, v, v, v, v, v, v, v, v, v, v, %"
"GPS#"                    false      "Latitude, Longitude, Altitude, GroundSpeed, CourseAngle, SatellitesVisible"                                                                                                                                  "degree, degree, m, m/s, degree, N/A"              
"LocalENU"                false      "X, Y, Z"                                                                                                                                                                                                     "m, m, m"                                          
"LocalENUTarget"          false      "XTarget, YTarget, ZTarget"                                                                                                                                                                                   "m, m, m"                                          
"LocalENUVel"             false      "VX, VY, VZ"                                                                                                                                                                                                  "m/s, m/s, m/s"                                    
  ⋮
info(customPlotter, "Plot")

ans=12×4 table
PlotName
ReadyToPlot
MissingSignals
RequiredSignals

"AnalyzeAccel"                true        ""                       "HighAccel, LocalNEDVel"            
"Attitude"                    true        "AttitudeRate"           "AttitudeEuler, AttitudeRate, Gyro#"
"AttitudeControl"             true        "AttitudeTargetEuler"    "AttitudeEuler, AttitudeTargetEuler"
"Compass"                     true        "GPS#"                   "AttitudeEuler, Mag#, GPS#"         
"Height"                      true        "Barometer#, GPS#"       "Barometer#, GPS#, LocalNED"        
"Trajectory"                  true        "LocalNEDTarget"         "LocalNED, LocalNEDTarget"          
"TrajectoryTracking"          true        "LocalNEDTarget"         "LocalNED, LocalNEDTarget"          
"TrajectoryVelTracking"       true        "LocalNEDVelTarget"      "LocalNEDVel, LocalNEDVelTarget"    
"plotFFTAccel"                true        ""                       "Accel"                             
"Battery"                     false       "Battery"                "Battery"                           
"GPS2D"                       false       "GPS#"                   "GPS#"                              
"Speed"                       false       "GPS#, Airspeed#"        "GPS#, Airspeed#"                   

show可以定义你想指定的图标的名字,"PlotsToShow"用来可视化加速度数据。

accelAnalysisProfile = ["AnalyzeAccel", "plotFFTAccel"];
accelAnalysisPlots = show(customPlotter, logData, "PlotsToShow", accelAnalysisProfile);

【MATLAB UAV Toolbox】使用指南(三)_第8张图片
【MATLAB UAV Toolbox】使用指南(三)_第9张图片
下面是一个完整的例子,展示如何使用flightLogSignalMapping来查看预定义的信号和图标,还有你自己的飞行日志分析图表。
分析加速度数据的函数定义

function h = plotFFTAccel(acc)
    h = figure("Name", "AccelFFT");
    ax = newplot(h);
    v = acc.Values{1};
    Fs = v.Properties.SampleRate;
    N = floor(length(v.AccelX)/2)*2;
    hold(ax, "on");
    for idx = 1:3
        x = v{1:N, idx};
        xdft = fft(x);
        xdft = xdft(1:N/2+1);
        psdx = (1/(Fs*N)) * abs(xdft).^2;
        psdx(2:end-1) = 2*psdx(2:end-1);
        freq = 0:Fs/length(x):Fs/2;
        plot(ax, freq, 10*log10(psdx));
    end
    hold(ax, "off");
    title("Periodogram Using FFT");
    xlabel("f (Hz)");
    ylabel("Power/Frequency (dB/Hz)");
    legend("AccelX", "AccelY", "AccelZ");
end

你可能感兴趣的:(matlab)