Main Content

Generate Code for Predicting Remaining Useful Life

This example shows how to deploy an algorithm for predicting remaining useful life (RUL) using MATLAB® Coder™. Such code generation is useful when you have trained an RUL prediction model in MATLAB and are ready to deploy the prediction algorithm to another environment. This example uses MATLAB Coder to generate a MEX file that is executable from MATLAB. You can use a similar procedure to generate code for any target that MATLAB Coder supports.

This example shows how to generate C code for predicting RUL using a linearDegradationModel. You can use the same procedure to generate code for a prediction algorithm with any Predictive Maintenance Toolbox™ RUL model, including degradation models, similarity-based models such as residualSimilarityModel, and survival-based models such as covariateSurvivalModel.

The workflow for generating code for predicting RUL is illustrated in the following diagram. The first step is to fit an RUL model using historical data from your system. You must also write a MATLAB function that receives new data from your system and uses it with the fitted model to predict a new RUL. This function is called the entry-point function. You then generate C/C++ code from the entry-point function.

Fit RUL Model

Before generating code for RUL prediction, you must fit an RUL model using historical data. For this example, load the data in linTrainTables.mat. This file contains measurements of some condition indicator taken over time, organized into tables with column labels "Time" and "Condition". Use this data to train a linear degradation model. (For more information about configuring and training RUL models, see linearDegradationModel or the reference pages for the other RUL model types.)

load('linTrainTables.mat')

mdl = linearDegradationModel;
fit(mdl,linTrainTables,"Time","Condition")

Prepare Model for Code Generation

Once you have a trained RUL model, save the model using saveRULModelForCoder. This function saves the RUL model to a MAT file. Later, in the entry-point function, use loadRULModelForCoder to load and reconstruct the RUL model from that file.

saveMATfilename = 'savedModel.mat';
saveRULModelForCoder(mdl,saveMATfilename);

Define Entry-Point Function

The entry-point function is the function for which you want to generate code. When predicting RUL, your entry-point function might take input data, process it in some way to extract the condition indicator, and then use predictRUL to obtain a new RUL estimate from the model. For an example that shows the complete workflow for identifying condition indicators, processing data, and predicting RUL, see Wind Turbine High-Speed Bearing Prognosis. For this example, create the simple entry-point function degradationRULPredict.m, as shown here.

type degradationRULPredict.m
function [estRUL,ci,pdfRUL] = degradationRULPredict(data)
%#codegen

threshold = 60;

% Load prepared model
mdl = loadRULModelForCoder('savedModel.mat');

% Use input data for new prediction
[estRUL,ci,pdfRUL] = predictRUL(mdl,data,threshold);

end

This function takes as input a data point consisting of a time and a condition-indicator value. The function uses loadRULModelForCoder to load the version of the trained model previously saved with saveRULModelForCoder. The function also includes the required %#codegen directive, which instructs the Code Analyzer to help you diagnose and fix violations that might result in errors during code generation or at runtime. (For more information about the Code Analyzer, see Check Code with the Code Analyzer (MATLAB Coder).)

Capabilities and Limitations of the Entry-Point Function

The simple entry-point function of this example loads the model and obtains the new RUL prediction. Your entry-point function can do other operations, such as further processing on input data to extract a condition indicator for use in the prediction. However, all functions and operations within the entry-point function must support code generation. For degradation-based RUL models, your function can also use the update command to update the prediction model based on new data. When you do so, you can include additional code to preserve the updated model parameters when you shut down and restart the deployed system. For more information, see Generate Code that Preserves RUL Model State for System Restart.

For information about limitations on code generation for RUL models, see the reference pages for predictRUL and for the individual RUL model types, in the Extended Capabilities section.

Generate Code

To generate code, you must provide sample data having the data type and format expected by the entry-point function. For this example, load some test data in the same format as the data you used to train the RUL model, a table of times and condition-indicator values. Because your entry-point function takes as its input one time and one value, extract one row from the table of test data. For code generation, the specific values of the sample data do not matter, only the data types.

load('linTestData.mat','linTestData1')
sampleData = linTestData1(1,:);
sampleData
sampleData=1×2 table
    Time    Condition
    ____    _________

     1       2.1316  

You can now generate code in one of two ways: using the MATLAB Coder app, or at the MATLAB command line.

Generate Code with MATLAB Coder App

In the MATLAB desktop, on the Apps tab, under Code Generation, click MATLAB Coder. The MATLAB Coder app opens on the Select Source Files page. In the Generate code for function box, enter the name of the entry-point function, degradationRULPredict. Then, click Next.

To specify the input data types for the entry-point function, on the Define Input Types page, use sampleData in a call to degradationRULPredict. When you enter the call, MATLAB Coder displays the detected input types and number of outputs. Click Next to confirm.

Optionally, check the entry-point function for issues arising at run time. To do so, click Check for Issues. When you are ready, click Next to advance to the Generate Code page. In this page, you specify the target for code generation. You can generate RUL prediction code for any of the targets that MATLAB Coder supports, including standalone C/C++ code, C/C++ code compiled to a library, or C/C++ code compiled to an executable. For this example, in the Build type list, select MEX. A MEX file is an executable that you can call from within MATLAB.

Click Generate to generate the MEX file, degradationRULPredict_mex. For additional information about MATLAB Coder capabilities and the files it generates, see Generate C Code by Using the MATLAB Coder App (MATLAB Coder).

Generate Code with codegen Command

As an alternative to using the MATLAB Coder app, you can generate code using the following codegen (MATLAB Coder) command.

codegen degradationRULPredict -args {sampleData} -nargout 3
Code generation successful.

Validate Generated Code

To validate the generated code, at the MATLAB command prompt, run the entry-point MATLAB function on the test data. Then, run the generated MEX file on the same data and confirm that the results are the same.

[estRUL,ci,pdfRUL] = degradationRULPredict(sampleData);

[estRUL_mex,ci_mex,pdfRUL_mex] = degradationRULPredict_mex(sampleData);

For example, compare the estimated RUL obtained with the MATLAB function and the generated MEX file.

estRUL
estRUL = 114.2927
estRUL_mex
estRUL_mex = 114.2927

You can now use the generated code as part of your deployed system for predicting remaining useful life.

See Also

| | | | | | | | |

Related Topics