hello welcome back in this video we'll see how we can automatically generate C code from the
classification model we built before MATLAB and deployed on a real piece of hardware in
particular what we'll do we will first validate our algorithm and the desktop to see if performs
well and then you know once we make sure that this works well we can automatically generate
code from it and test it on a PLC so let me just show you exactly what we're gonna do next
so we're gonna start from this model and then what we will do we will automatically generate
the code from the classification algorithm that we will bring the Simulink through a matlab
function block so what we do we just leverage code generation and we deploy the algorithm on a PLC so
after this instead of testing this algorithm directly with the real piece of hardware with
the real flow PAC machine what we do we actually test this against a real-time representation of
the model and in particular what we do we deploy the plant model and the controller on a speed good
machine and this pitbull machine essentially will represent the real-time behavior of the
model and in this way by connecting the speak goat and the peel seam through an Ethernet power
link connection the algorithm running on the PLC actually things that is being tested against the
real piece of hardware ok so let me show you exactly what this looks like so here we have
the PLC where we will deploy the classification algorithm and then here you can see the Ethernet
power link connection and this is connected to the P code machine and on this big machine we
will deploy the Simulink model represents the flowback machine so all of this is basically
made possible through simulate real time so Simulink real time is a tool that allows us to
quickly and very easily run any Simulink model in real time on a real time machine and also what we
can do with Siemens real time is leverage MATLAB API is in particular we can build a GUI without
designer like in this case and then quickly use it to test algorithms real-time so here we start
the real-time simulation and then we can see how the signals are collected in real-time and here
we can watch the desired position in blue and the actual position in red on the first plot
and then we also measure torque producing the shaft and then speed of the motor so we also
have a couple of scalar values for the failures on the mechanical side and the electrical side
and then essentially what we can do from here from this app we can inject folds in the model
so for instance what we can do we can inject a fault on the electrical side and then we can
immediately see how this impacts the behavior of the flow of pac machine so we can watch this
in the GUI and then we can see how now we have a red lamp for the electrical failure and this
means that the algorithm that is now running on the PLC and there was deployed directly from
Simulink is actually behaving well and is capable of spotting a failure on the electrical side
so once again before we switch to MATLAB let me just recap so here on the PLC we are running
the classification algorithm and here on the speaker machine we are running the plant model
so that being said let's see how we actually achieve this okay so here we are in Simulink so
what we see here we see exactly the same model that we used before to generate data and then
here we have added another subsystem in this subsystem we will perform the fault detection
so if we go inside we have a couple of variants so let's take a look at the first one so in this
one we have a MATLAB function and with this MATLAB function what we do we perform first
of all features extraction from the data that we measure during simulation then we
normalize it and then we perform prediction so we load the model that we have built before with
the classification learner app and then we call the predict function that gives
us the label for the full condition that is predicted okay and then we also have another
variant which is the real-time variant and then as you can see here we don't have any
MATLAB function block but we simply manage the communication between the speaker machine where
the plant model runs and the PLC where the classification algorithm runs okay
you
so instead of injecting faults directly from the Simulink model we can use an app and in particular
this app was built with app designer and it's a very convenient way to essentially control
Semenuk models and in this case modify parameters inject folds plots and so on so for example from
here we can inject folds we can switch from desktop to real-time simulation we can watch
possible failures of our system and so on so if we start the simulation we can see how the
classification algorithm behaves so essentially what we are doing now we are just testing the
classification algorithm that we have built before with the classification learner app and
we want to see how this behaves on the desktop so we want to see how this behaves in Simulink
so at the beginning we have no faults and so we'll have green lights both on the electrical side and
on the mechanical side so as the simulation go on we can just start to inject failures so for
example what we do here we just decrease the gearbox efficiency and then we can see that we
have a yellow lamp for the mechanical failure so this means that the classification algorithm
is actually doing okay at spotting this kind of failure but we can further you know make it
worse and then we can actually further reduce the efficiency of the gearbox and then you know we can
see whether the algorithm is able to spot this and then after a bit of seconds you know after
the features are extracted and the prediction is done we can see that we have a red lamp on
the mechanical failure okay so at this point we are confident that the model works well on the
desktop so we can generate code from it so first of all let's open a new project in automation
studio which is the IDE to program B and our PLC's let's select the hardware that we can use and then
let's go back to simulate so at this point you will recognize that here we have exactly the
same function block that we have tested before on the desktop and then all we need to do is
just map the Simulink signals to variables in automation studio so here we use these blocks
from the beyond our target for Simulink and then here we select whether we want to generate C or
C++ code if you want to use similar encoder or embedded coder then we give a name to the task
that will generate select the hardware and then we point to the project that we previously created so
at this point we only need to build the model and once we do this we essentially use embedded
coder to automatically generate C code from the classification algorithm so once we click on
OK and close this we will see that the code has been automatically imported in automation studio
and here what I want to point out is that we don't need to use necessarily this kind
of target we could use any other supported target as for example Siemens or back off
or we could simply generate MC standard C code and import this into another embedded
system so you know from Simulink we are quite independent on the hardware chosen
okay so at this point after deploying the code on the PLC we can just run the algorithm in real-time
and once we switch from desktop to real-time we are essentially changing the variant for the fault
classification that I showed you before so at this point we connect to the speed code machine
and then we build a model so what we're doing now we are just generating again C code from
the emailing model but now from the plant and this code will be automatically deployed on the speak
of machine so to do this we just click on download you then now we are ready to run the algorithm
in real time so you know since the speak code is connected to the PLC where we have recently in the
point code for the classification algorithm we're essentially now testing the real-time behavior of
the classification algorithm and we're testing how the C code is behaving in real-time so instead of
testing this directly with actual with the real flowback machine it is very convenient to run
this against the real-time representation of the flowback machine itself and this is because now we
can inject false in the model without actually breaking the real physical system and this is
you know very useful to test the classification algorithm on the plc and as you can see now also
here in real time the c code that is running on the plc is actually behaving well and is capable
of spotting the failure on the mechanical side and then once again we can further make things
worse we can adjust inject faults in the model without breaking the actual physical system and
then we can see that the model works well so this brings us to the end of the pretty maintenance
workflow for the condition monitoring aspect so in the next videos we will actually take a
step back and then we will go back to the train model part and then we will see how we can use
the condition indicators to build different kind of algorithm in particular instead of building a
classification algorithm we will use a different model to estimate how much time we have before the
bank system might fail so we will take a look at prognostics and remaining useful life estimation