Chapter 4. Using the Model

In the earlier chapters we've developed a simple cruise controller model and the controlled car model, and then we've tested and fine-tuned the controller model. All the work has been done in Simulink only.

We couldn't perform our model testing on something other than the car model, because we still lack an interface with an external system, outside Simulink. This external system could be the real car for those lucky ones who have a spare 147 GTA in their garage and all the hardware needed to command its engine control unit, or—more realistically—a car simulation software like the one provided with this book.

In this chapter we'll take a first look at the software and understand how it is exchanging the data.

We'll then learn how to develop custom blocks to communicate with the application and use them with our cruise controller model. Those custom blocks will be developed with the MATLAB scripting language, which has the advantage of having a simple and intuitive syntax.

The final part of this chapter, aimed at those who know the C language, discusses the implementation of the same blocks using the C language.

The external software – a Qt5 application

The application is shipped with the code bundle provided with this chapter. It's located in the external_app folder (in code bundle), containing:

  • The 64-bit Linux executable (external_app/Linux/alfa147gta_linux64), which needs the Qt5 libraries to be installed in the system (the package is usually called qt5) and must be marked as executable (by typing chmod +x alfa147gta_linux64 in the terminal)
  • The 32-bit Windows executable (external_appWindowsalfa147gta_win32.exe) built using MinGW; the needed libraries (the .dll files) are located in the same folder to avoid you the hassle of installing Qt5 on Windows
  • The source code (external_app/source/alfa147gta, distributed under the GPL license), made using Qt Creator

Move the application (and every .dll file, if you're on Windows) to the folder we'll use as the MATLAB working folder, and run it by double-clicking on the icon.

You should see the following window:

The external software – a Qt5 application

The application works by reading and writing three text files (whose paths have to be put in the three input fields above the graphs), having only one line with a real number. The vehicle speed and target speed files are written by the application itself, while the throttle command file is read. Throughout this chapter, we'll assume the files to be speed.txt, target.txt, and throttle.txt, located in the same directory as the application itself. Enter their paths in the application input fields.

There is another input field: the simulation step size (in seconds). It must be set equal to the step size used by the cruise controller model, and allow the application to run synchronously with the model. The value we'll use, as discussed in the previous chapter, is 0.1 seconds.

Finally, the slider allows us to vary the target speed easily.

As soon as the Run button is clicked, the application begins simulating the vehicle (assuming an initial throttle input equal to 0) the same way our mathematical Alfa 147 GTA model did.

Try moving the target speed slider: you'll see a red line moving in the speed graph. Of course, since there isn't any controller writing the throttle file, the other graphs will keep their default values.

The first graph, displaying the speeds, will have two lines: the red one is the target speed, and the blue one is the vehicle speed, with the difference between the two highlighted in yellow. The second graph displays the throttle command and the third graph displays the the current gear.

The Stop button will stop the simulation. You'll notice that the speed.txt, target.txt, and throttle.txt files have been automatically created by the application itself.

At this point we know that in order to use this application we've got to develop two custom blocks: a sink block that will write a scalar, one-dimensional, real signal to a file, and a source block that will read a file and output a scalar, one-dimensional, real signal. Custom blocks are developed with S-functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.133.61