Skip to content

Commit bd3bc32

Browse files
authored
Updates for R2025a (#40)
1 parent cd126b9 commit bd3bc32

File tree

53 files changed

+3584
-3390
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3584
-3390
lines changed

BEV/BEV_main_script.html

Lines changed: 15 additions & 20 deletions
Large diffs are not rendered by default.

BEV/BEV_main_script.m

Lines changed: 67 additions & 71 deletions
Large diffs are not rendered by default.

BEV/BEV_system_model.mdl

Lines changed: 107 additions & 106 deletions
Large diffs are not rendered by default.

BEV/Utility/BEV_ResultsCompactPlot.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function parent = BEV_ResultsCompactPlot(NameValuePair)
2-
%% Creates plots of simulation results
2+
%% Create plots of simulation results
33
% To see how this function creates a plot, simply run this function without arguments.
44

55
% Copyright 2022-2025 The MathWorks, Inc.
@@ -13,8 +13,8 @@
1313
% After simulation, create a timetable with extractTimetable.
1414
NameValuePair.SimData timetable = timetable(seconds([0;10]), [0;0], [0;0], [0;0], [0;0], [0;0], [0;0], [0;0], [0;0], [0;0], ...
1515
VariableNames = [ ...
16-
"Vehicle Speed (km/hr)", ...
17-
"Reference Vehicle Speed (km/hr)", ...
16+
"Vehicle Speed kph", ...
17+
"Reference Vehicle Speed kph", ...
1818
"G-Force", ...
1919
"Motor Torque Command", ...
2020
"Motor Temperature", ...
@@ -57,11 +57,11 @@
5757

5858
ax = nexttile(tl);
5959

60-
plot(ax, simData, "Time", "Vehicle Speed (km/hr)", LineWidth=2)
60+
plot(ax, simData, "Time", "Vehicle Speed kph", LineWidth=2)
6161
hold(ax, "on")
6262
grid(ax, "on")
63-
plot(ax, simData, "Time", "Reference Vehicle Speed (km/hr)", LineWidth=2)
64-
setMinimumYRange(ax, simData.("Reference Vehicle Speed (km/hr)"), dy_threshold=2);
63+
plot(ax, simData, "Time", "Reference Vehicle Speed kph", LineWidth=2)
64+
setMinimumYRange(ax, simData.("Reference Vehicle Speed kph"), dy_threshold=2);
6565
ylabel(ax, "") % Hide variable name defined in the timetable.
6666
xlim(ax, "tight")
6767
xlabel(ax, "")

BEV/markdown/BEV_main_script.md

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121
This is a simple, fast running BEV model which can estimate the electrical efficiency of the vehicle. It is also suitable for further customizations for more focused analysis of individual components at vehicle system level.
2222

2323

24-
To open the model, navigate Toolstrip > Project Shortcut tab, and click "**BEV system level**" button.
24+
To open the model, navigate Toolstrip > Project Shortcut tab, and click the "**BEV model**" button.
2525

2626

2727
This script shows an example workflow to programmatically open model, run simulation, collect simulation data, visualize result, save data to text\-format file, read saved data file, and analyze. Feel free to modify this script (probably saving as another file first) and experiment to get new results.
2828

2929

30-
You can find more scripts demonstrating other simulation cases in the **SimulationCases** folder.
30+
You can find more scripts demonstrating other simulation cases in the BEV > Model\-\* > SimulationCases folders.
3131

3232
<a id="H_EC484CEF"></a>
3333

3434
# Run Simulation
3535

36-
This section sets up the model and runs simulation. To run this script at once, navigate Toolstrip > Live Editor tab, and click "Run" button. You can also run this section only by clicking "Run Section" button.
36+
This section sets up the model and runs simulation. To run this script at once, navigate Toolstrip > Live Editor tab, and click the Run button. You can also run this section only by clicking the Run Section button.
3737

3838
```matlab
3939
modelName = "BEV_system_model";
@@ -82,9 +82,8 @@ simOut = sim(modelName);
8282
simData = extractTimetable(simOut.logsout);
8383
fig = BEV_ResultsCompactPlot( SimData=simData, PlotTemperature=false );
8484
% Save the plot to a PNG file.
85-
prjRoot = currentProject().RootFolder;
8685
imgFilename = "BEV_SimulationResultPlot.png";
87-
exportgraphics(fig, fullfile(prjRoot, "BEV", "simulation-results", imgFilename))
86+
exportgraphics(fig, fullfile(currentProject().RootFolder, "BEV", "simulation-results", imgFilename))
8887
```
8988

9089
<center><img src="media/BEV_main_script_media/figure_0.png" width="702" alt="figure_0.png"></center>
@@ -100,23 +99,21 @@ Save logged signals to a CSV file for later analysis. CSV text format is used ra
10099
simData = extractTimetable(simOut.logsout);
101100
102101
% Adjust time format so as not to lose the subsecond information.
103-
simData.Time.Format = 'hh:mm:ss.SSSS';
102+
simData.Time.Format = "hh:mm:ss.SSSS";
104103
105104
% Signal names to save in file.
106105
% These must be specified in the model as signal logging names.
107106
% For example, in the BEV system model, see the Measurement subsystem.
108107
dataColumns = [ ...
109108
"HV Battery SOC", "HV Battery Power", "HV Battery Current", ...
110-
"G-Force", "Vehicle Speed (km/hr)" ];
109+
"G-Force", "Vehicle Speed kph" ];
111110
112111
% Select the logged signals to save.
113112
simData = simData(:, dataColumns);
114113
115114
% Add unit information to signal names.
116115
varNames = string(simData.Properties.VariableNames');
117-
% disp(varNames)
118116
varUnits = string(simData.Properties.VariableUnits');
119-
% disp(varUnits)
120117
varNames2 = varNames + " (" + varUnits + ")";
121118
disp(varNames2)
122119
```
@@ -126,15 +123,15 @@ disp(varNames2)
126123
"HV Battery Power (kW)"
127124
"HV Battery Current (A)"
128125
"G-Force (1)"
129-
"Vehicle Speed (km/hr) (km/hr)"
126+
"Vehicle Speed kph (km/hr)"
130127
```
131128

132129
```matlab
133130
simData.Properties.VariableNames = varNames2;
134131
135132
% Save data to CSV file.
136133
simResultFilename = "BEV_SimulationResult_1.csv";
137-
simResultFile_FullPath = fullfile(prjRoot, "BEV", "simulation-results", simResultFilename);
134+
simResultFile_FullPath = fullfile(currentProject().RootFolder, "BEV", "simulation-results", simResultFilename);
138135
writetimetable(simData, simResultFile_FullPath)
139136
```
140137

@@ -147,41 +144,31 @@ Open the saved CSV file in text editor and check that the variable names are sav
147144
This section reads a simulation result CSV file which was saved in the previous section, and do some analysis on the data. This section should work independently without running the previous section as long as the result file exists. At the end of this section, you get the electric efficiency of the vehicle according to the drive cycle for which the data was collected.
148145

149146
```matlab
150-
prjRoot = currentProject().RootFolder;
151147
simResultFilename = "BEV_SimulationResult_1.csv";
152-
simResultFile_FullPath = fullfile(prjRoot, "BEV", "simulation-results", simResultFilename);
148+
simResultFile_FullPath = fullfile(currentProject().RootFolder, "BEV", "simulation-results", simResultFilename);
153149
154150
% Read a CSV file containing simulation result and store it to a timetable.
155151
data = readtimetable(simResultFile_FullPath, VariableNamingRule="preserve");
156152
157153
% Adjust time format.
158-
data.Time.Format = 's';
154+
data.Time.Format = "s";
159155
160156
% Separate variable names and unit strings.
161-
varNames_with_unit = data.Properties.VariableNames';
162-
% disp(varNames_with_unit)
157+
varNames_with_unit = string(data.Properties.VariableNames');
163158
varNames = extractBefore(varNames_with_unit, " (");
164159
disp(varNames)
165160
```
166161

167162
```matlabTextOutput
168-
{'HV Battery SOC' }
169-
{'HV Battery Power' }
170-
{'HV Battery Current'}
171-
{'G-Force' }
172-
{'Vehicle Speed' }
163+
"HV Battery SOC"
164+
"HV Battery Power"
165+
"HV Battery Current"
166+
"G-Force"
167+
"Vehicle Speed kph"
173168
```
174169

175170
```matlab
176-
varUnits = strings(5, 1);
177-
for idx = 1 : numel(varNames_with_unit)
178-
% Return value from extractBetween is an N-by-1 cell array
179-
% for each varNames_with_unit where N could be different.
180-
% We keep the first element only,
181-
% we cannot assign them to varUnits at once outside for-loop.
182-
tmpUnits = extractBetween(varNames_with_unit{idx}, "(", ")");
183-
varUnits{idx} = tmpUnits{1};
184-
end
171+
varUnits = extractBetween(varNames_with_unit, "(", ")");
185172
disp(varUnits)
186173
```
187174

@@ -196,10 +183,6 @@ disp(varUnits)
196183
```matlab
197184
data.Properties.VariableNames = varNames;
198185
data.Properties.VariableUnits = varUnits;
199-
200-
% Define a little utility to manipulate the unit representation
201-
% in Simscape's physical values.
202-
tidyUnit = @(x,u) simscape.Value( value(x,u), u );
203186
```
204187

205188
Time
@@ -212,50 +195,54 @@ dt = diff(t);
212195
Travelled Distance
213196

214197
```matlab
215-
dataVehSpd = data.("Vehicle Speed");
216-
unitStr = varUnits(varNames == "Vehicle Speed");
217-
vehicleSpeed = simscape.Value(dataVehSpd, unitStr{:});
218-
averageSpeed = sum(vehicleSpeed)/numel(vehicleSpeed)
198+
dataVehSpd = data.("Vehicle Speed kph");
199+
unitStr = varUnits(varNames == "Vehicle Speed kph");
200+
vehicleSpeed = simscape.Value(dataVehSpd, unitStr);
201+
averageSpeed = sum(vehicleSpeed)/numel(vehicleSpeed);
202+
disp("Average speed: " + value(averageSpeed) + " " + string(unit(averageSpeed)))
219203
```
220204

221205
```matlabTextOutput
222-
averageSpeed =
223-
37.2886 (km/hr)
224-
206+
Average speed: 37.2886 km/hr
225207
```
226208

227209
```matlab
228-
maxSpeed = max(vehicleSpeed)
210+
maxSpeed = max(vehicleSpeed);
211+
disp("Maximum speed: " + value(maxSpeed) + " " + string(unit(maxSpeed)))
229212
```
230213

231214
```matlabTextOutput
232-
maxSpeed =
233-
70.0078 (km/hr)
234-
215+
Maximum speed: 70.0078 km/hr
235216
```
236217

237218
```matlab
238219
tmpDistance = sum(vehicleSpeed(2:end).*dt);
239-
travelledDistance = tidyUnit( tmpDistance, "km" )
220+
travelledDistance = convert( tmpDistance, "km" );
221+
disp("Travelled distance: " + value(travelledDistance) + " " + string(unit(travelledDistance)))
240222
```
241223

242224
```matlabTextOutput
243-
travelledDistance =
244-
0.9626 (km)
245-
225+
Travelled distance: 0.96263 km
246226
```
247227

248228

249229
G Force
250230

251231
```matlab
252232
G = data.("G-Force");
253-
min(G), max(G)
233+
disp("Minimun G: " + min(G))
234+
```
235+
236+
```matlabTextOutput
237+
Minimun G: -0.077098
238+
```
239+
240+
```matlab
241+
disp("Maximum G: " + max(G))
254242
```
255243

256244
```matlabTextOutput
257-
ans = -0.0771
258-
ans = 0.1985
245+
Maximum G: 0.19849
259246
```
260247

261248

@@ -264,32 +251,41 @@ Battery Power
264251
```matlab
265252
dataBattPwr = data.("HV Battery Power");
266253
unitStr = varUnits(varNames == "HV Battery Power");
267-
batteryPower = simscape.Value(dataBattPwr, unitStr{:}); % J/s
268-
batteryEnergyUsed = sum(batteryPower(2:end).*dt)
254+
batteryPower = simscape.Value(dataBattPwr, unitStr); % J/s
255+
batteryEnergyUsed = sum(batteryPower(2:end).*dt);
256+
batteryEnergyUsed = convert(batteryEnergyUsed, "kWh");
257+
disp("Battery energy used: " + value(batteryEnergyUsed) + " " + string(unit(batteryEnergyUsed)))
269258
```
270259

271260
```matlabTextOutput
272-
batteryEnergyUsed =
273-
518.4123 (kW*s)
274-
261+
Battery energy used: 0.144 kWh
275262
```
276263

277264
```matlab
278-
batteryEnergyUsed = tidyUnit(batteryEnergyUsed, "kW*hr")
265+
energyEfficiency_kWh_per_100km = 100 * value(batteryEnergyUsed / travelledDistance, "kWh/km");
266+
disp("Energy efficiency: " + energyEfficiency_kWh_per_100km + " kWh per 100 km")
279267
```
280268

281269
```matlabTextOutput
282-
batteryEnergyUsed =
283-
0.1440 (hr*kW)
270+
Energy efficiency: 14.9594 kWh per 100 km
271+
```
284272

273+
```matlab
274+
energyEfficiency_km_per_kWh = convert(travelledDistance / batteryEnergyUsed, "km/kWh");
275+
disp("Energy efficiency: " + value(energyEfficiency_km_per_kWh) + " " + string(unit(energyEfficiency_km_per_kWh)))
276+
```
277+
278+
```matlabTextOutput
279+
Energy efficiency: 6.6847 km/kWh
285280
```
286281

287282
```matlab
288-
energyEfficiency_kWh_per_100km = 100 * value(batteryEnergyUsed / travelledDistance, "kW*hr/km")
283+
energyEfficiency_mi_per_kWh = convert(travelledDistance / batteryEnergyUsed, "mi/kWh");
284+
disp("Energy efficiency: " + value(energyEfficiency_mi_per_kWh) + " " + string(unit(energyEfficiency_mi_per_kWh)))
289285
```
290286

291287
```matlabTextOutput
292-
energyEfficiency_kWh_per_100km = 14.9594
288+
Energy efficiency: 4.1537 mi/kWh
293289
```
294290

295291

0 Bytes
Loading
-562 Bytes
Loading
0 Bytes
Loading

BEV/simulation-results/BEV_SimulationResult_1.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Time,HV Battery SOC (%),HV Battery Power (kW),HV Battery Current (A),G-Force (1),Vehicle Speed (km/hr) (km/hr)
1+
Time,HV Battery SOC (%),HV Battery Power (kW),HV Battery Current (A),G-Force (1),Vehicle Speed kph (km/hr)
22
00:00:00.0000,70,0,0,-0,0
33
00:00:00.0000,70,0,0,-0,0
44
00:00:00.1000,70,0,0,-0,0

BEVProjectNavigationApp.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function App = BEVProjectNavigationApp()
2+
%% BEV project navigation app
3+
% This is a project entry-point app to find some key models, scripts, etc. in the project.
4+
5+
% Copyright 2024-2025 The MathWorks, Inc.
6+
7+
arguments (Output)
8+
App (1,:) BEVProjectNavigationAppMain
9+
end % arguments
10+
11+
disp("Starting: BEV Project Navigation App")
12+
13+
project_navigator = BEVProjectNavigationAppMain;
14+
15+
project_navigator.Window.HeaderUI.AppSourceName = mfilename;
16+
17+
if nargout > 0
18+
% Returning the App variable is optional because the app class object persists
19+
% even if it is not returned.
20+
App = project_navigator;
21+
end % if
22+
end % function

0 commit comments

Comments
 (0)