%% Real Driving Emissions
% Real Driving Emissions (RDE) is an emissions standard required by the European Union. To meet this standard, a car is driven on public roads and over a wide range of different conditions. Specific equipment installed on the vehicle, the Portable Emission Measuring System (PEMS), collects data to verify that legislative caps for pollutants such as NOx are not exceeded.
% This example shows how to generate an RDE-compliant trip.
%% RDE trip parameters
% For the parameter list definition

DriveCycles = RDE.DriveCycles;
DriveCycles.openParmeterList;

% Initialize the RDE parameters

%driveCycles = RDE;

DriveCycles.dt = 1;                % sampling interval [s]
DriveCycles.StopSpeedTh = 1/3.6;   % stop velocity threshold [m/s]

% urban/rural/motorway velocity range [m/s]

% Parameter defined in the RDE legislation at chapter: 6.3.1, 6.4.1, 6.4.2, 6.5.1
DriveCycles.OperationModeBoundaries = [60 90]/3.6; % Boundaries between urban, rural, motorway [m/s]

% Parameter defined in the RDE legislation at chapter: 6.6.1
DriveCycles.UrbanRatioRange = [0.29 0.43]; % Allowed distance normalized percentage for the urban leg []

% Parameter defined in the RDE legislation at chapter: 6.6.2
DriveCycles.RuralRatioRange = [0.23 0.43]; % Allowed distance normalized percentage for the rural leg []

% Parameter defined in the RDE legislation at chapter: 6.6.3
DriveCycles.MotorwayRatioRange = [0.23 0.43]; % Allowed distance normalized percentage for the motorway leg []

% Parameter defined in the RDE legislation at chapter: 6.7.1
DriveCycles.MotorwayUsualMaxSpeed = 145/3.6;         % Usual max velocity [m/s] (can be occasionally higher, for overpassing, etc.)
DriveCycles.MotorwayAbsoluteSpeedTimeRatio = 0.03;   % Time ratio limit for higher velocities []
DriveCycles.MotorwayAbsoluteMaxSpeed = 160/3.6;      % Absolute max velocity [m/s] (cannot be higher than this)

% Parameter defined in the RDE legislation at chapter: 6.8.1
DriveCycles.UrbanAverageSpeedRange = [15 40]/3.6;    % urban allowed speed range [m/s]

% Parameter defined in the RDE legislation at chapter: 6.8.2
DriveCycles.UrbanStopRatioRange = [0.06 0.3];        % Urban stop normalized percentage range []

% Parameter defined in the RDE legislation at chapter: 6.8.3
DriveCycles.UrbanMinStopTime = 10;   % Urban min stop time [s]
DriveCycles.UrbanMinStopCount = 2;   % Urban min stop occurrences []

% Parameter defined in the RDE legislation at chapter: 6.9.1
DriveCycles.MotorwayUsualMinSpeed = 100/3.6;     % Motorway min velocity [m/s]
DriveCycles.MotorwayUsualMinSpeedTime = 5*60;    % Time allowed for min velocity [s]

% Parameter defined in the RDE legislation at chapter: 6.10.1
DriveCycles.TripDurationRange = [90 120]*60;     % Allowed total trip duration range [s]

% Parameter defined in the RDE legislation at chapter: 6.12
DriveCycles.UrbanMinDistance = 16000;            % Min distance for urban part [m]
DriveCycles.RuralMinDistance = 16000;            % Min distance for rural part [m]
DriveCycles.MotorwayMinDistance = 16000;         % Min distance for motorway part [m]

% rural speeds
DriveCycles.RuralAverageSpeedRange = [60 90]/3.6;     % Rural average speed range [m/s]
DriveCycles.MotorwayAverageSpeedRange = [90 145]/3.6; % Motorway average speed range [m/s]

% Dynamic Boundary Conditions
DriveCycles.PowerToMassRatio = 45;

% Gamma probability distribution shape parameter
DriveCycles.ShapeParameter = 1;

% Number of RDE trips to generate data
DriveCycles.NTrips = 4;

% Output folder for RDE data
DriveCycles.OutputFolder = fullfile(pwd(), 'results');

%% Generate RDE trip and save velocity timeseries as CSV files

DriveCycles.generateDriveCycles(); 

% Check the CSV files

winopen("./results/drive_cycle_1.csv")

%% Plot RDE trips

DriveCycles.plotDriveCycles

%% References
%% RDE package 1
% Basic features of the RDE test, such as characterization of the RDE trip, the vehicle family concept, description of the data evaluation tools, technical requirement of the PEMS equipment, and reporting obligations.
% Official Journal of the European Union, L 82, 31 March 2016. 2016/427
%% RDE package 2
% Determination of the conformity factors and the timetable for RDE implementation. Technical features include the introduction of dynamic boundary conditions and a limit for altitude gain together with a detailed approach to calculating it.
% Official Journal of the European Union, L 109, 26 April 2016. 2016/646
%% RDE package 3
% Particulate number measurement along, provisions for hybrids, and a procedure to include cold starts and regeneration events in the RDE test.
% Official Journal of the European Union, L 175, 7 July 2017. 2017/1151
%% RDE package 4
% In service compliance and surveillance tests along with specific provisions for light commercial vehicles (vans)
% Official Journal of the European Union, L 301, 27 November 2018. 2018/1832
% Copyright 2021 The MathWorks, Inc.