MBD Toolbox - Tip of the Day

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MBD Toolbox - Tip of the Day

40,319 Views
Daniel_Popa
NXP Employee
NXP Employee

This topic is intended to offer tips and tricks about using Model-Based Design Toolbox, Matlab Simulink and/or any other tools used for loading and debugging the applications created with MBD Toolboxes.

If you have a piece of advice that you want to share with others this is the place to add it.

Comment below and help others to become proficient with NXP's MBD Toolboxes

Summary:

Tip #1: How to use Block Priorities

Tip #2: How to use Model Workspace vs Matlab Workspace

Tip #3: How to add custom code

Tip #4: How to show/hide Block Execution Order

Tip #5: How to use S-Function to inject custom code calls anywhere

Tip #6: How to investigate model fails with build_exception.mat

Tip #7: How to measure code execution time

Tip #8: How to create stand-alone callable functions

Tip #9: How to  Write to/Read from any address

Tip #10: About the Timings & Interrupts of standard models

Tip #11: How to tune any controller in real-time

Tip #12: How to keep a single toolbox visible

Tip #13: How to address licensing errors

Tip #14: How to make visible a missing toolbox in Simulink Library

Tip #15: About Automotive Math and Motor Control Library Set

Tip #16: How to SIL

Tip #17: How to PIL

Tip #18: How to use custom Linker Command Files

Tip #19: How to generate meaningful names for variables in Code Generation

Tip #20: How to control Symbolics

Tip #21: Multitasking in Simulink

Tip #22: How to migrate a Simulink model

Tip #23: New Perspective for Large and Complex Production Software Development

Tip #24: How to Integrate Custom C-code with existing Simulink Models

Labels (1)
40 Replies

23,794 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #24: MBD Toolbox -Integrate Custom C-code with existing Simulink Models

Often we are getting questions about how to mix existing C-code for various functionalities with existing Simulink Models. There are multiple ways of adding/calling your C-functions from Simulink.

Please check this article How to use your own C code in our Toolbox (Battery Management System example) to learn about how to do it.

pastedImage_24.png

23,797 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #23: MBD Toolbox - New Perspective for Large and Complex Production Software Development

Recently, I found this very interesting presentation made by MW Korea during MATLAB EXPO 2018. The subject matter addresses topics related with:

  • efficient software development in Simulink
  • tips and tricks on Embedded Coder configuration for code generation
  • code mapping
  • application partitioning
  • scheduling

The presentation is available here: https://www.matlabexpo.com/content/dam/mathworks/mathworks-dot-com/images/events/matlabexpo/kr/2018/... 

There is also a recording available here (unfortunately only in Korean): New Perspective for Large and Complex Production Software Development Video - MATLAB & Simulink 

For convenience the presentation is also attached.

Hope you will find it useful too and applicable for your applications made with NXP Model-Based Design Toolboxes

23,796 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #22: MBD Toolbox - How to migrate a Simulink model to a newer toolbox version to avoid "Unresolved Link" message

Sometimes, our team is forced to make changes that will break the Simulink library backward compatibility. In this case, models created with previous versions of a toolbox cannot be opened in a newer version due to "Unresolved Link" error message. 

In such cases you will see something like this. The model has been created with MBDT S32K 3.0.0 and imported in MBDT 2018.R1

pastedImage_2.png

How to fix it easily?

Use right click on each Simulink block and select Block Parameters (Reference) menu option

pastedImage_3.png

The following windows will appear that will help you to identify the broken link:

pastedImage_6.png

Edit the Source Block to point to the correct Simulink Library block location. In this case just remove the "/S32K14x" and click Apply

The Simulink Model will be updated and the block will become visible

pastedImage_9.png

0 Kudos
Reply

23,793 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #21: MBD Toolbox - How to do multitasking in Simulink

Thanks to kike‌ for shows how you can create a multitasking application using Simulink standard options: https://community.nxp.com/thread/488575 

0 Kudos
Reply

23,793 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #20: MBD Toolbox - How to control Symbolics

One of the nice features of Model-Based Design approach is the possibility to generate automatically parts of the Simulink models and reuse/import that code wherever you need.

 

Here is a code snippet that looks awful IMHO due to:

  • differences in function calls compared to actual Simulink Subsystem
  • variables prefixes
  • bad indentation

pastedImage_1.png

pastedImage_2.png

MATLAB offers an easy way to customize the global coding styles. Instead of going over the models and modify each subsystem properties you can instruct the code generation about different rules by changing: Identifier Format Control

pastedImage_6.png

Tweaking these arguments produces a legible code:

pastedImage_8.png

What's you preference ?

0 Kudos
Reply

23,800 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #19: MBD Toolbox - How to generate meaningful names for variables in Code Generation

Have you ever wanted to scope or record a variable with FreeMASTER or simply wanted to read the MATLAB generated code but found no meaningful symbol names in C-source or ELF ?

By default, MATLAB/Simulink code generation is going to optimize the program and most of the time the variables might be optimized out or simply defined as locals.

Here is a Simulink model and the associated code generation snippet:

pastedImage_6.png

/* S-Function (GMCLIB_SvmStd_SF_FLT): '<S9>/GMCLIB_SvmStd_SF' */
 {
     SWLIBS_3Syst_FLT Out;
     SWLIBS_2Syst_FLT In;
     In.fltArg1 = rtb_GMCLIB_ParkInv_SF_o1;
     In.fltArg2 = rtb_GMCLIB_ParkInv_SF_o2;
     rtb_GMCLIB_SvmStd_SF_o4 = GMCLIB_SvmStd_FLT(&Out, &In);
     rtb_GMCLIB_SvmStd_SF_o1 = Out.fltArg1;
     rtb_GMCLIB_SvmStd_SF_o2 = Out.fltArg2;
     rtb_GMCLIB_SvmStd_SF_o3 = Out.fltArg3;
 }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

In this case, the inputs and outputs are local variables and can't be visible to FreeMASTER therefore you cannot plot them in real time nor reading/understanding the code faster.

void PWM_control_S32K_step(void)
{
     /* local block i/o variables */
     real32_T rtb_GMCLIB_SvmStd_SF_o1;
     real32_T rtb_GMCLIB_SvmStd_SF_o2;
     real32_T rtb_GMCLIB_SvmStd_SF_o3;
     real32_T rtb_GMCLIB_ParkInv_SF_o1;
     real32_T rtb_GMCLIB_ParkInv_SF_o2;
     uint32_T rtb_GMCLIB_SvmStd_SF_o4;
 ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Wouldn't be nice to have the symbols like U_ALPHA, U_BETA, PWM_A, PWM_B, PWM_C and SECTOR instead of generic Simulink naming convention ?

Good news! that is possible with a single click.

Just right click on the signal, select Signal Properties and then enable "Test Point"

pastedImage_5.png

Do this for all the signals you wish to visualize in FreeMASTER and Simulink is going to highlight all these signal and use them during code generation

pastedImage_6.png

Now the generated code should look like this. 

/* S-Function (GMCLIB_SvmStd_SF_FLT): '<S9>/GMCLIB_SvmStd_SF' */
 {
     SWLIBS_3Syst_FLT Out;
     SWLIBS_2Syst_FLT In;
     In.fltArg1 = PWM_control_S32K_B.U_ALPHA;
     In.fltArg2 = PWM_control_S32K_B.U_BETA;
     PWM_control_S32K_B.SECTOR = GMCLIB_SvmStd_FLT(&Out, &In);
     PWM_control_S32K_B.PWM_A = Out.fltArg1;
     PWM_control_S32K_B.PWM_B = Out.fltArg2;
     PWM_control_S32K_B.PWM_C = Out.fltArg3;
 }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Now, it is easier to read the code and the even more important the Simulink is going to generate a global structure that could be easily visualized in FreeMASTER in real-time.

/* Block signals (auto storage) */
typedef struct {
     real32_T U_ALPHA; /* '<S8>/GMCLIB_ParkInv_SF' */
     real32_T U_BETA; /* '<S8>/GMCLIB_ParkInv_SF' */
     real32_T PWM_A; /* '<S9>/GMCLIB_SvmStd_SF' */
     real32_T PWM_B; /* '<S9>/GMCLIB_SvmStd_SF' */
     real32_T PWM_C; /* '<S9>/GMCLIB_SvmStd_SF' */
     uint32_T SECTOR; /* '<S9>/GMCLIB_SvmStd_SF' */
} B_PWM_control_S32K_T;‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

23,801 Views
279814392
Contributor II

Hi Daniel,

I have some problems and need your help,I use the 'Automotive Math and Motor Control Library' for MPC5744P in Simulink to control PMSM,and I want to use the 'GMCLIB_SvmStd_FLT' module,and I finished the compile in Simulink and then debuged in Freemaster,but I found the code could not run,then I delete this module to have a text,I found the code could run,so I think this module is the core problem.I also used the 'GMCLIB_SvmStd_F32' module,and everything is OK,but I want to use 'GMCLIB_SvmStd_FLT' module because it accord with my demand.So please help me to find the reason.

Thank you.

Yuyuan Wang.

pastedImage_1.pngpastedImage_2.png

0 Kudos
Reply

23,801 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #18: MBD Toolbox - How to use custom Linker Command Files

Did you know you can use your own custom Linker Command Files (*.lcf/*.ld) to control the code and data section placement in the microprocessor FLASH or RAM physical memories ?

By default the MBD Toolbox is deliver with default memory allocation scripts that are located under ../mbdtbx_xxx/src/... folder (e.g.: ..\mbdtbx_MPC574xP\mbdtbx_pnt\src\s32ds_specific_files in case of MPC5744P)

Any linker file that you create in this folder is loaded (one the MATLAB is restarted) into the Model Configuration Parameters and can be selected by the user to link the generated code according with the rules exposed in the file.

Disable the Default Target Memory Definition and you will get access to the User Defined Target Memory Definition option where you can select your custom file.

pastedImage_4.png

This might be useful in case you what to define your own memory arrangement to fit with existing applications or if you want to reuse the generated code with existing IDE based projects.

0 Kudos
Reply

23,802 Views
sagar_salunkhe
Contributor II

Hi Daniel,

I am using S32k144 evb board and MATLAB 2019a version. I have installed NXP tool and I am using it.

I am working on eeporm flash code for s32k148 so I want to replace default linker file with my custom file.

I searched in Toolboxes\NXP_MBDToolbox_S32K1xx\mbdtbx_s32k  and in \ src folder but there is no such fie default linker file, where I can place my custom linker file or its path.

There is no  Default Target Memory Definition  option in Model configuration option. There is no User Defined Target Memory Definition option where I can select my custom file.

sagar1.JPG

Will you please help with s32k148evb board and Matlab2019a version , for use your own custom Linker Command Files

0 Kudos
Reply

23,802 Views
mariuslucianand
NXP Employee
NXP Employee

Hello sagar.salunkhe@faurecia.com‌,

Please go to S32K\src\mbdtbx_s32k14x\src\linker\gcc (if you use GCC) and add the linker file you need here. rename it as S32K148_[memory_size]_flash_[custom_name]. The toolbox will show it in the Build Toolchain tab of the config block if you uncheck the Default Target Memory Definitions. If it fails to show it here, please replace the linker file with the one that you need to use.

Marius

pastedImage_1.png

0 Kudos
Reply

23,802 Views
sagar_salunkhe
Contributor II

Hi Marius,

I tried this solution, but it worked for first time only. After saving setting when I closed the model and opened again it is showing bellow error.

sagar1.JPG

I tried multiple times by closing MATLAB and opening it again ,I found that it is not working always. There is no customized  User Defined Target Memory Definitions in drop down window.

My customize files are bellow

sagar1.JPG

Please suggest the right way to do it.

0 Kudos
Reply

23,802 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #17: MBD Toolbox - How To PIL

Did you know you can tie the standard Simulink model simulation with the actual code executing on the real hardware  to create a co-simulation environment ?

In this co-simulation the outputs of the control algorithm and functions that are executed on the real microprocessor are fed back into the Simulink virtual simulation allowing you to detect and analyze potential issues that can only be detected once the final application is built. 

 

Check this video to see how you can easily convert any Simulink model created with Model Based Design Toolbox to a PIL simulation: Video Link : 7845 

10,559 Views
josephB_otu
Contributor I

Is it possible to use partial PIL implementation? Ie only some model inputs, and use other peripheral inputs?

0 Kudos
Reply

23,802 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #16: MBD Toolbox - How To SIL

Did you know you can test the numerical equivalence between model components and production code that you generate from the those components via the software-in-the-loop (SIL) ?

With a SIL simulation, you test very early in the project design stage, the C-source code on your host computer.  Based on that you can establish the testing strategy  and start testing the functional components by preparing the harness tests to create a functional baseline of the application.

Check this video to see how you can easily convert any Simulink model created with Model Based Design Toolbox to a SIL simulation:  Video Link : 7844 

23,802 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #15: MBD Toolbox - Automotive Math and Motor Control Library supported features

Did you know that using the NXP AMMC Library in conjunction with NXP MBD Toolbox you can design and implement your entire application using just Simulink blocks in a fraction of the time compared other SW development approaches.

No hand-written code is required.

Here is an example of that could be achieved in case of FOC for PMSM.

Untitled.jpg

0 Kudos
Reply

23,802 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #14: MBD Toolbox - Simulink Missing Toolbox Library

Have you tried to use a toolbox that you have installed but is does not seems to be present in the Simulink Library ?

1.JPG

Sometimes the Simulink might not update the information correctly. With a simple F5 you can force a library refresh. If something is broken the Simulik will inform about that:

1.JPG

Just click on Fix and select the "Generate Repositories in Memory"

2.JPG

That should do the trick and made all installed toolboxes available in the Simulink Library.

0 Kudos
Reply

23,802 Views
q380742432
Contributor I

I tried this method, but it didn't work

0 Kudos
Reply

23,805 Views
Daniel_Popa
NXP Employee
NXP Employee

Hello, 

This works only if the toolbox is already installed successfully on that PC. In your particular case - there is a licensing problem. Please confirm this after we will fix the DISK ID issue.

Thank you!

Daniel

0 Kudos
Reply

23,805 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #13: MBD Toolbox - Common license error codes

Have you tried to use the Model Based Design Toolbox but you end-up having a Licensing Error like this ?

Capture.JPG

Most of the times you can fix this by yourself. The NXP MBD Toolbox is using FlexLM license manager from Flexera Software.

FlexLM error codes with description and possible causes can be found here: FLEXlm Error Codes 

The most common source of errors for Model Based Design are: 

Error Code
Description
-1
Cannot find license file.
-2
Invalid license file syntax.
-5
No such feature exists.
-8
Invalid (inconsistent) license key or signature.
The license key/signature and data for the feature do not match. This usually happens when a license file has been altered.
-9
Invalid host.
The hostid of this system does not match the hostid specified in the license file.
-10
Feature has expired.
-11
Invalid date format in license file.
-18
License server does not support this feature.
-21
License file does not support this version.
-30
Cannot read license file.
-31
Feature start date is in the future.
-54
No FEATURESET line in license file.
-55
Incorrect FEATURESET line in license file.
0 Kudos
Reply

23,805 Views
Daniel_Popa
NXP Employee
NXP Employee

Tip #12: MBD Toolbox - Using multiple toolboxes in the same Matlab instance

Even if multiple installations and instantiations of the NXP MBD Toolbox within a single Matlab release is supported there might be situations when you might want to keep only one active and visible toolbox library in Simulink.

You can do that very easy by resetting the Matlab Search Path to Defaults and then load only the Toolbox you are interested in.

Capture.JPG

0 Kudos
Reply