Deployed Application of Parameter Estimation
This example shows how to monitor the condition of an electric vehicle battery in the field, with a deployed version of parameter estimation in Simulink® Design Optimization™, together with Simulink Compiler™.
Battery Monitoring
电池在电动汽车是昂贵的再保险place and need to be monitored and maintained carefully, to ensure they function well for their intended lifetime. In this example, an electric car is driven to work and back on a daily commute. At home, the car is plugged in to a smart charger that monitors both the current and the battery voltage. The charger analyzes the battery data to estimate the battery parameters, using a deployed version of parameter estimation in Simulink Design Optimization, together with Simulink Compiler. The charger relays these parameters to the car manufacturer through an Internet of Things (IoT) connection, so that the manufacturer can monitor the battery health over time.
Battery Model
This example estimates parameters of a simple, rechargeable battery model,sdoBattery
. The input tosdoBattery
is the battery current, and the model output is the battery terminal voltage which is computed from the battery state of charge.
The battery model is based on the equation:
where:
is the battery terminal voltage in Volts.
is the battery constant voltage in Volts.
is the battery polarization resistance in Ohms.
is the maximum battery capacity in Ampere-hours. is the battery initial state of charge in Ampere-hours.
is the battery charge state, with 1 being fully charged and 0 discharged. The battery state of charge is computed from the integral of the battery current with a positive current indicating discharge and a negative current indicating charging.
is the voltage drop when charging, expressed as a fraction of the battery constant voltage.
Use the following command to open the model.
open_system('sdoBattery')
Battery Characteristics
The following battery characteristics are known:
Voltage, 400V
Loss factor, = 0.012
Resistance, 0.32 Ohms.
is known to be 250 Ampere-hours (100 kWh) when the battery is new. As the battery ages, is expected to decrease, and this is monitored to track the health of the battery. The initial state of charge and the new battery capacity need to be estimated.
Steps for Deployed Parameter Estimation
There are two main steps to run parameter estimation in deployed mode:
Make asetupfile, to set up parameter estimation objects for use in deployed mode
Make arunfile, which is a MATLAB function for parameter estimation that can be compiled and run in deployed mode
It is recommended to create thesetupandrunfiles by starting with MATLAB code generated from theParameter Estimator. Copy, split, and modify the generated code to make thesetupandrunfiles as demonstrated in the following section.
Parameter Estimation in Non-Deployed Mode
First generate MATLAB code to estimate and in non-deployed mode. Use the following commands to load the pre-configured estimation session:
loadsdoBattery_spesession_forDeploymentspetool(SDOSessionData)
This step loads and plots the experiment with measured voltage and current data and configures theParameter Estimatorto estimate and .
Navigate to theEstimatebutton in the toolstrip and from the dropdown list, selectGenerate MATLAB Function(seeGenerate MATLAB Code for Parameter Estimation Problems (GUI)). This step generates a MATLAB function which is added to the MATLAB editor, and a MAT-fileparameterEstimation_sdoBattery_Data.mat
. The generated code is available for you in fileparameterEstimationSdoBattery.m
. You can use the generated code to estimate parameters in non-deployed mode.
It is recommended to start with this generated code and copy, split and modify the code to create thesetupandrunfiles described in the following sections.
Setup-File for Deployed Parameter Estimation
To estimate parameters in deployed mode, the code for non-deployed parameter estimation can be split into asetupfile to use in non-deployed mode, and arunfile to use in deployed mode. The setup file is available asparameterEstimationSdoBattery_setup.m
and the main parts are:
Define parameters
Define experiments
Prepare for deployment and save
Define Parameters
Parameters are defined inparameterEstimationSdoBattery_setup.m
in the same way as the generated MATLAB code,parameterEstimationSdoBattery.m
. Use thesdo.getParameterFromModel
command to create a parameter object, containing fields for parameter value, minimum and maximum, and a field ("Free") indicating whether the parameter will be tuned during estimation.
In this example, parameter information is also stored in a database in which cars are identified by a code akin to a pseudo vehicle identification number (VIN). The car manufacturer can use this to monitor the health of the battery over time. TheparameterEstimationSdoBattery_setup.m
file uses the VIN database to update battery parameter values. See theparameterEstimationSdoBattery_setup.m
file for more details.
The initial database is loaded from the MATLAB filesdoBatteryVinDatabase.mat
which has the VIN database stored in variablevinDatabase
. This is acontainers.Map
object, and the VIN key4DEF
is used to look up parameters for the battery in this example.
Run
vinDatabase("4DEF")
to display the following table:
Define Experiments
Experiments are defined inparameterEstimationSdoBattery_setup.m
in the same way as the generated MATLAB code,parameterEstimationSdoBattery.m
. Experiments have measured data and information about specific ports or signals in the model that are associated with the data.
Prepare for Deployment and Save
At the end of theparameterEstimationSdoBattery_setup.m
file, define a simulator which can run the model and compare model output to measured data. Use theprepareToDeploy
command to configure the experiments and simulator so they can be used in deployed mode. Save these prepared objects to a MAT-file.
When running these steps on another model and preparing for deployment, you may be prompted to save the model to continue after running thesetupfunction. Save the model to preserve logging settings that need to be in place for deployed mode.
TherunfileparameterEstimationSdoBattery_run.m
uses the objects saved insdoBatteryObjectsToDeploy.mat
for parameter estimation in deployed mode.
Run-File for Deployed Parameter Estimation
Therunfile is available asparameterEstimationSdoBattery_run.m
and the main parts are:
Load preconfigured deployment objects
Update experiments and parameters
Run optimization
Update Parameter Database
Load Preconfigured Deployment Objects
TheparameterEstimationSdoBattery_run.m
needs a pragma so that the Simulink Compiler includes the model in the compiled code as follows:
Load the preconfigured objects that were saved at the end ofparameterEstimationSdoBattery_setup.m
file as follows:
Update Experiments and Parameters
TheparameterEstimationSdoBattery_run.m
file takes two input arguments:
dataFilename
a data file name for experiment datavin
a vehicle identification number for parameter values
Read the data from the comma-separated-values (CSV) text file specified bydataFilename
. Use theupdateIODatacommand to update the deployed experiments with new input and output data (current and voltage data for this model). Since the data is from a CSV file, you do not need thegetData
function that is present in the generated MATLAB code,parameterEstimationSdoBattery.m
.
使用VIN作为键来查找这个汽车的电池parameters in the parameter database. Use the current value from the database to update the initial parameter values prior to running the new estimation. See theparameterEstimationSdoBattery_run.m
file for more details.
Run Optimization
The next several steps inparameterEstimationSdoBattery_run.m
are very similar to the code inparameterEstimationSdoBattery.m
(for non-deployed estimation). Define a handle to the estimation objective function, specify optimization options, and use thesdo.optimize
function. This step runs the model and compares model output to experiment data. Parameters are tuned to achieve a close match between the model and data.
The objective function is defined in the subfunctionsdoBattery_optFcn
which is also like the objective function inparameterEstimationSdoBattery.m
. However, the name of the signal logging variable needs to be specified since it cannot be queried from the model in deployed mode.
To determine the name of the variable ('logsout
' in this case), query the model from MATLAB in non-deployed mode:
get_param('sdoBattery','SignalLoggingName')
Alternatively, in Simulink use theModelingtab in the toolstrip and clickModel Settings. In the configuration dialog, selectData Import/Exportand find the variable name in theSignal
logging
box.
Update Parameter Database
After callingsdo.optimize
in the main function ofparameterEstimationSdoBattery_run.m
, update the VIN database. For each parameter that is estimated, copy theCurrentValue
to thePreviousValue
and then use the new parameter estimate to update theCurrentValue
. SeeparameterEstimationSdoBattery_run.m
for more details.
Running Parameter Estimation in Deployed Mode
Use themcc
command to compile theparameterEstimationSdoBattery_run.m
function from either the MATLAB command window or the DOS or UNIX command prompt. You need to have MATLAB Runtime installed to complete the following steps. For more information, seeInstall and Configure MATLAB Runtime(MATLAB Compiler).
Run parameter estimation in deployed mode.
In MATLAB, run
vinDatabase("4DEF")
to display the following result:
Tracking Battery Parameters over Time
The table below shows estimates of battery parameters
and
over time. The filesdoBattery_Data1.csv
contains data for the battery when it was new,sdoBattery_Data2.csv
contains data for the battery when it was 1 year old andsdoBattery_Data3.csv
contains data for the battery when it was 2 years old.
Observe that there is degradation in battery capacity over time. There is a high rate of degradation in the first year after which the rate of degradation reduces. When the battery was new, the round-trip commute left the battery state of charge at 61% while after 2 years, the commute left the battery state of charge at 47%. If the state of charge falls below 40%, this condition reduces the number of times the battery can be recharged. By tracking battery parameters over time, the manufacturer can monitor the battery health, and determine if the car needs a new battery.
See Also
prepareToDeploy(Experiment)
|prepareToDeploy(SimulationTest)
|updateIOData(Experiment)
|sdo.Experiment
|sdo.SimulationTest