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
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.
The Simulink model will become:
Now, you can add the gains in the FreeMASTER tool and tune your controller in real time.
This way you have no excuse for obtaining a "perfect: system response.
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
This is a code snippet:
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.
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.
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!!!
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
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:
what's the reason?Please!
Yuyuan Wang.
Thank you for your help,I will try later!!!
thanks again!!!
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:
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.
Here is the snapshot of the reusable function call (on the left) vs Inline Code (on the right):
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:
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.
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.
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.
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
Why would someone hide the execution order number ? Can you think of such scenario ? Leave a reply below.
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.
Hello,
I can not find the 'mscripts', could you share the example to me? Thanks!!!
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.
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?
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:
Tip #2: MBD Toolbox - Model Workspace vs. Matlab Workspace
Have you ever faced this kind of Simulink errors ?
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:
Then, add any parameters in the Model Workspace.
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?
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
to this: reading input data one of the other 8.0, 8.1, 8.2