MBD Toolbox - Tip of the Day

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

MBD Toolbox - Tip of the Day

33,077 Views
dumitru-daniel_
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

14,324 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #11: MBD Toolbox - Change position/speed/current controller gains in real-time

Using FreeMASTER you can change the variables in real-time for any running system.

A good example is the the need for tunning the controllers (position/speed/current). 

By default the Simulink PID controller block uses constant values that can not be changed from outside during simulation or execution. But, if you change the Controller Parameters SOURCE to EXTERNAL you can overcome that limitation and you may change the gains on the fly.

1.jpg

The Simulink model will become:

Capture.JPG

Now, you can add the gains in the FreeMASTER tool and tune your controller in real time.

Capture.JPG

This way you have no excuse for obtaining a "perfect: system response.

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #10: MBD Toolbox - Timings & Interrupts

Did you know that all the blocks that are not coupled with a specific function call/event are still executed on a default interrupt based routine called Model Step Function?

This ***_step() that is triggered based on the settings from Solver/Fixed-Step size

Capture.JPG

This is a code snippet:

1.jpg

There is a conversion mechanism between Simulink Fixed Step Size value to the MCU System Tick that takes into consideration the MCU Clock Frequency.

Next time you design your model, keep in mind that all the generated code is called on a interrupt. Only FreeMASTER callback for data visualization is called on main() infinite loop.

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #9: MBD Toolbox - Write to/Read from any address within the microprocessor address space

Do you need fast access to any memory mapped register of a particular peripheral within the microprocessor address space ? Do you need to perform an initialization of any specific peripheral register ?

With MBD Toolbox and DataMemWrite/DataMemRead Simulink blocks these tasks are very easy. Just check the Reference Manual for the address you need to set and give a try with MBD Toolbox dedicated blocks. No extra code is needed.

data read_write.jpg

0 Kudos

2,570 Views
279814392
Contributor II

I used this module,but have some problems,when I put this mudule in simulink,and input address to read etimer0 CNTR,it couldn't debug in freemaster,the code can not run,please help me!!!

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hi Yuyuan, 

Please open a new thread and add some details so that we can help you: what NXP toolbox are you using and which HW and if posible please attach the model so that we can have a look.

Thank you!

Daniel

0 Kudos

2,570 Views
279814392
Contributor II

Hi,Daniel,

    I have some problems again! please help me.I use 'Automotive Math and Motor Control Library' in simulink to add a module such as 'GMCLIB_SvmStd_F32',and then compile in simulink,and my compiler Selection is 'S32 Design Studio',but it could not complie,the error code as below picture:

pastedImage_1.png

what's the reason?Please!

Yuyuan Wang.

0 Kudos

2,570 Views
279814392
Contributor II

Thank you for your help,I will try later!!!

thanks again!!!

0 Kudos

2,570 Views
279814392
Contributor II

Hi Daniel

I'm Yuyuan Wang,and I will sent you a simulink module,please help me.,thank you !!!

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #8: MBD Toolbox - Use Atomic Subsystem to create stand-alone callable functions

Have you ever faced a situation where you had to read and understand a large amount of C-code generated from a large Simulink model where all the code is inline? Then you probably spend a lot of time to figure out that are the input/output and local data.

Did you know you can transform parts of the model into Atomic Subsystems and then configure the Code Generation to call Reusable Functions

Let's consider the following example that consists in a PI Speed Controller and a Speed Estimator:

1.JPG

Choosing to model the MotorSpeedController as an Atomic Subsystem gives you access to Block Parameters (Subsystem)/Code Generation where you can set different code generation behaviors

Selecting Reusable Function option the generated code will become much more clear.

2.JPG

Here is the snapshot of the reusable function call (on the left) vs Inline Code (on the right):

4.JPG

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #7: MBD Toolbox - Measuring Code Execution Time

Using Model Based Design Toolbox, measuring the execution time of any routine is easy. Just drag-and-drop the Profiler Function block into your models or subsystems you want to check for execution time and read that output values in real time with FreeMASTER or send it over UART/CAN/etc

 

You can find the block within the Utility Blocks group:

profiler.JPG

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #6: MBD Toolbox - Use build_exception.mat to find out why the model failed

Have you ever faced issues when trying to build your Simulink models and the build console reported something like "Error: Errors occurred - aborting" without any other additional information? Isn't this frustrating?

Did you know you can navigate to the build folder and search for the build_exception.mat file that offers you very detailed information about the error stack, cause and messages that were catch during the build procedure.

By simply loading this build_exception.mat file into the MATLAB workspace you could save a lot of time.

Here is a snapshot of what to expect when loading this file.

build_exception.jpg

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #5: MBD Toolbox - Use S-Function to call your custom code anywhere in the model

Using the S-Function Builder and the Mode Based Design Toolbox - Add Custom Code capabilities (Tip #3) you can call your hand written functions in any place within your models.

S-function builder.JPG

This way you can enhance the Simulink model with multiple capabilities that may not be fully covered by Toolbox Blocks.

Using this approach you can interlace Simulink automatically generated C with your own legacy code.

Here is an example how to achieve this task:  https://community.nxp.com/thread/450048 

Are you using legacy code in the Simulink modelling ? Leave a comment below with the issues you have faced and how you resolved it.

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #4: MBD Toolbox - Show Block Execution Order

In case you want to enable/disable the display of the Simulink Execution Order (discussed in Tip #1) there is an easy way to do that: Display -> Blocks -> Sorted Execution Order

execution_order_enable.jpg

Why would someone hide the execution order number ? Can you think of such scenario ? Leave a reply below.

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #3: MBD Toolbox - Add custom code to your Simulink model

 

Did you know that using NXP Model Based Design Toolbox, one has the possibility to add custom C code into the Simulink model? This C code section will work with NXP blocks for different peripherals ?

There may be cases when one needs to execute some code that is not part of the model but are instead hand coded. In these instances, user specific files will need to be included in the build directory created by toolbox.

You can specify which files to include in the build directory by creating/modifying a file called {toolbox name}_user_copy_required_files.m and locate it somewhere in one of the Matlab paths.

There is an example of this file located in the ‘mscripts’ directory that demonstrates how to include user specific files.

0 Kudos

2,570 Views
260068845
Contributor III

Hello,

   I can not find the 'mscripts', could you share the example to me?  Thanks!!!

0 Kudos

2,570 Views
constantinrazva
NXP Employee
NXP Employee

Hello 260068845@qq.com‌,

Here is an example for MPC toolbox where you can find the mscripts - the same applies for other toolboxes as well:

{toolbox_root}\mbdtbx_mpc574x\mbdtbx_mpc574x\mscripts\utils\mbd_mpc574x_user_copy_required_files.m

The only difference will be the toolbox name/prefixes.

Kind regarsd,

Razvan.

0 Kudos

2,570 Views
raghurajappa
Contributor IV

Hi dumitru-daniel.popa,

What does "locate it somewhere in one of the Matlab paths" mean?? The file that exists within the "rappid" path is a read only (as expected). So where exactly should I place my modified "{toolbox name}_user_copy_required_files.m" file? 

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Hello, 

It does not matter were you place the actual file. All that matters is the MATLAB has that path into it's default search paths. You can add the path to the default ones in MATLAB using:

pastedImage_1.png

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #2: MBD Toolbox - Model Workspace vs. Matlab Workspace

Have you ever faced this kind of Simulink errors ?

Tip2_1.jpg

Usually, the Simulink models refers some parameters that are present in the Matlab workspace. If the workspace is cleared then you need to reload the parameters again form m-file or mat-file.

For some cases it is easy to keep these parameters in the model workspace. This way you can share the Simulink model without any additional m-files/mat-files

Just right click on the Simulink canvas and select Explore:

tip2_3.jpg

Then, add any parameters in the Model Workspace.

Tip2_2.jpg

0 Kudos

2,570 Views
dumitru-daniel_
NXP Employee
NXP Employee

Tip #1: MBD Toolbox - Block Priorities

Did you know that each MBD Toolbox/Simulink block have a Priority field - where you can specify the order of block/generated code execution?

P1.jpg

You can use this option to alter the program flow and optimize the application.

E.g. From this:  reading input data  one by one as 8.0, 8.3, 8.6 

P2.JPG

to this:  reading input data one of the other 8.0, 8.1, 8.2

P3.JPG