As an introduction to the CarMaker interface blockset and the CarMaker target for Real-Time Workshop, we’d like to demonstrate how easy it is to integrate a Simulink model into CarMaker. We’ll start from scratch with a new Simulink model and lead you through the few steps until the model is running inside CarMaker and its output signals can be watched in diagram window of IPG-CONTROL.
We assume that you’re working in a fresh copy of the template project directory to be found underExamples/Car_Genericin the CarMaker installation directory. Prerequisite on your side is also a basic familiarity with the CarMaker C level interface and you should know how to rebuild and run the CarMaker simulation program and start a simulation.
The example was created with the stand-alone version of CarMaker under Linux, using Mat-lab 6.5.1 (R13 SP 1). However, there should be no difficulty in running the example on another platform with a different version of CarMaker or any other Matlab version supported by CarMaker.
5.2.1 Creating the Simulink model
Start Matlab in themdl subdirectory of the project directory.
Create a new Simulink model like the following one and save it under the name SimuModel.mdl.
In the Simulink library the orange blocks of the CarMaker Interface blockset can be found under Blocksets & Toolboxes, the Integrator block is under Continuous, and the Trigonomet-ric Function comes from Math Operations.
In theBlock Parametersdialog of the From CM Dict, To CM Dict and Def CM Dict blocks, leave all parameters at their default, just enter the name of the quantity. You’ll probably won-der about the notation we use for the quantity names: The $ sign stands for the model name, i.e. SimuModel in our case. Using the model name as a prefix, we can keep all model quantities together, which makes them easier to identify among all the other quantities in CarMaker’s data dictionary. Also, once we decide to give our model a different name, after rebuilding the code all of the model’s quantity names will have changed automatically.
The model does not do something particularly useful. It uses the current simulation time as its input and provides its sine and cosine as outputs. We just want the model to be called by CarMaker once each simulation cycle, so that we can observe its changing output sig-nals.
5.2.2 Setting simulation parameters
Before the C code can be generated with Real-Time Workshop, a few of your model’s sim-ulation parameters need to be changed.
Figure 5.1: The SimuModel.mdl example model
60 Integrating Simulink models
Step-by-step: Integrating a Simulink model
Open the dialogSimulation / Simulation Parametersin the menubar of the model window.
Solver settings
On theSolver tab, the following changes from the default settings should be made:
• Simulation time
Stop time: inf, because CarMaker determines when the simulation stops
• Solver options
Type: Fixed-step, ode1 (Euler), because of the Integrator block in our model Fixed step size: 0.001, because CarMaker runs with a step size of 1 ms Mode: SingleTasking, always use SingleTasking with CarMaker
Your solver settings should now look like this:
Real-Time Workshop settings
On theReal-Time Workshoptab, we first need to set the correct target so that the C code to be generated fits with CarMaker.
• Category: Target configuration
• Configuration
System target file: CarMaker.tlc, use theBrowse... button to set this
Generate code only: on / activated, in this example compilation will be done manually Figure 5.2: Solver settings
61 Integrating Simulink models
Step-by-step: Integrating a Simulink model
The screendump below shows what your target configuration settings should look like:
The code generation options for the selected CarMaker target can be found on the same tab of the dialog:
• Category: CarMaker code generation options
• Matlab include path: /fs/opt/matlab651-r13, change this accordingly
• CarMaker project directory: ../../src, the default value is fine
• CarMaker model wrapper type: Plain, the default value is fine
Except for probably the Matlab include path (the name is bit misleading; what is meant is the directory where to find Matlab’s include directories, i.e. normally the Matlab root direc-tory), the default settings are ok, so everything will probably look like this:
Close the dialog and save your model with the current settings.
Your model is now ready for the C code generation.
Figure 5.3: Real-Time Workshop, target configuration
Figure 5.4: Real-Time Workshop, CarMaker code generation options
62 Integrating Simulink models
Step-by-step: Integrating a Simulink model
5.2.3 Generating and compiling the model C code
Generating the C code
In the menu bar of your model window invokeTools / Real-Time Workshop / Build Model.
R e a l - T i m e W o r k s h o p w i l l n o w a u t o m a t i c a l l y m a k e a n e w s u b d i r e c t o r y SimuModel_CarMaker_rtwand create the model C code files inside that directory. After code generation is complete you will find the following files there:
SimuModel.c
(Number and names of the files actually generated may vary depending on the Matlab ver-sion you’re using for this example.)
Compiling and creating the model library
O p e n a t e r m i n a l w i n d o w ( e . g . x t e r m ) , c h a n g e i n t o t h e n e w l y c r e a t e d SimuModel_CarMaker_rtw directory and compile the code with the following commands:
% cd <path to your project directory>/mdl/SimuModel_CarMaker_rtw
% make -f SimuModel.mk
After the make command has finished, in themdldirectory above, you will find a new file namedlibSimuModel_linux.a , parallel to yourSimuModel.mdl file. This file is called the model library and contains the compiled mode C code that we’re now going to integrate into the CarMaker simulation program.
Here’s what your mdl directory might look like now:
.
| |-- SimuModel_private.h
| |-- SimuModel_types.h
| |-- SimuModel_wrap.c
| |-- SimuModel_wrap.h
| |-- modelsources.txt
| |-- rtmodel.h
| ‘-- rtw_proj.tmw
|-- cmenv.m
‘-- libSimuModel_linux.a
5.2.4 Integrating the model into CarMaker
Integration of a Simulink model into CarMaker means logical and physical integration.
63 Integrating Simulink models
Step-by-step: Integrating a Simulink model
For logical integration, the model code must actually be called from within CarMaker in order to initialize the model code, call it during each simulation cycle, and perform cleanup actions after the simulation.
Physical integration comprises of adding the model library code to the CarMaker simulation program, i.e. linking the simulation program against the model library when rebuilding CarMaker.
The following steps take place in thesrcsubdirectory of your CarMaker project directory.
In the previously started terminal window change to the src directory with the following command:
% cd <path to your project directory>/src
Calling the model from User.c
Edit the fileUser.cwith your favourite ASCII editor for programming and look for the follow-ing line quite at the beginnfollow-ing:
/* #define SIMULINK_EXAMPLE */
Remove the comments as follows, leaving no blanks at the beginning or at the end:
#define SIMULINK_EXAMPLE
That should activate the remaining SIMUMODEL integration code inUser.c. Your model will now be called by CarMaker during the simulation. We included the integration code in User.cto save you some typing when trying to run this example. To find out in detail about all calls tolibSimuModel_linux.a, simply search the file for all occurrences of the string SIMUMODEL. See also the description of the Plain wrapper template in this manual.
Adding the model library to the simulation program
The steps required to build a CarMaker simulation program, i.e. compiling and linking it, are controlled by theMakefile. Edit Makefilewith your favourite ASCII editor for program-ming, locate the "Example of a Simulink model" section and change it as follows:
### Example of a Simulink model
DEF_CFLAGS += -DRT -DUSE_RTMODEL
MATSUPP_MATVER = v6.5.1-r13 LD_LIBS += $(MATSUPP_LIB)
OBJS += ../mdl/libSimuModel_$(ARCH).a
### END (Example of a Simulink model)
Building the simulation program
In the terminal window, still in thesrcdirectory, enter the following command to build the simulation program:
% make
When make has finished you will find a newCarMaker.linuxexecutable file in thesrc directory, which can now be used to test your model running a simulation.
5.2.5 Running the model
Now that everything is ready, try the following steps:
• Start the CarMaker GUI.
• Tell it to use theCarMaker.linux executable file just created.
64 Integrating Simulink models
Step-by-step: Integrating a Simulink model
• InvokeApplication / Start & Connect.
• InvokeFile / Open... and load the Braking testrun.
• InvokeFile / IPG-CONTROL.
• Display SimuModel.quant1 and SimuModel.quant2 in a diagram window.
• Start a simulation by pressing the big greenStart button.
After the simulation, if everything went right, your diagram should look approxi-mately like this one:
Congratulations, you’ve just integrated your first Simulink model into CarMaker.
Figure 5.5: SimuModel output in IPG-CONTROL
65 Integrating Simulink models
The CarMaker interface blockset