%% Log and Replay CAN FD Messages
% This example shows you how to log and replay CAN FD messages using MathWorks
% virtual CAN FD channels in Simulink. You can update this model to 
% connect to supported hardware on your system.

% Copyright 2018 The MathWorks, Inc.

%%
% Load the saved CAN FD message from |sourceFDMsgs.mat| file from the examples
% folder. The file contains CAN FD messages representing a 90 second
% drive cycle around a test track. 
%
% Convert these messages to a format compatible with the CAN FD Replay block
% and save it to a separate file.
load SourceFDMsgs.mat
canFDMsgs = canFDMessageReplayBlockStruct(canFDMsgTimetable);
save DriveReplayFD.mat canFDMsgs
whos

%% CAN FD Replay Model 
% This model contains:
%
% * A CAN FD Replay block that transmits to |MathWorks Virtual Channel 1|. 
% * A CAN FD Receive block that receives the messages on a CAN FD network,
% through |MathWorks Virtual Channel 2|. 
%
% The CAN FD Receive block is configured to block all extended IDs and allow
% only the |WheelSpeed| message with the standard ID |1200| to pass.
%
% <<../CANFDReplayAndLog_Model1.PNG>>
%

%%
% The Wheel Speeds subsystem unpacks the wheel speed information from
% the received CAN FD messages and plots them to a scope. The subsystem also 
% logs the messages to a file.
%
% <<../CANFDReplayAndLog_Model2.PNG>>
%

%% Visualize Wheel Speed Information
% The plot shows the wheel speed for all wheels for the duration of the
% test drive.
% 
% <<../CANFDReplayAndLog_Scope.PNG>>
%

%% Load the Logged Message File
% The CAN FD Log block creates a unique file each time you run the model. Use
% |dir| in the MATLAB Command Window to find the latest log file. 
dir WheelSpeeds*.mat
load WheelSpeeds_2018-Apr-30_132033.mat
whos

%% Convert Logged Messages
% Use |canFDMessageTimetable| to convert messages logged during the simulation to
% a timetable that you can use in the command window. 
%
% To access message signals directly, use the appropriate database file in
% the conversion along with |canSignalTimetable|.
db = canDatabase('VehicleInfoFD.dbc');
wheelSpeedMsgTimetable = canFDMessageTimetable(outFDMsgs, db);
wheelSpeedMsgTimetable(1:15, :)
wheelSpeedSignals = canSignalTimetable(wheelSpeedMsgTimetable);
wheelSpeedSignals(1:15, :)

%%
% MathWorks CAN FD virtual channels were used for this example. You can however
% connect your models to other supported hardware.
