Advice for creating log files and printing debug messages with simulink compiler

45 views (last 30 days)
A bit of a long post, but I'm wondering if I'm on the right track and if you have any recommended approaches, since I've spent the past few days looking through documentation and other forum posts about Simulink Compiler and haven't found too much information on this subject. The short version is that I would like to display messages in the command prompt while the simulation is running, and once the simulation is over, save those messages to a log file, as well as saving a variety of signals to a different file.
1) I'm working on a simulink project that contains s-functions containing C++ code as well as matlab function blocks. I have been using functions inside my C++ code and matlab code that print messages (sprintf, disp, etc) for the sake of seeing the contents of certain variables, but also as a way to log events occurring inside the blocks. However, it appears that once I've compiled my project into an .exe file and run it from a command prompt window, those printouts don't show up and I haven't found any docs explaining how to print debug/warn/error messages from the .exe file.
2) I know there are a few ways to log signals from the model to the workspace for use during the simulation. From what I gather, doing this by clicking on the signal in the model and selecting 'log signal' doesn't work for Simulink Compiler, so instead I would need to use a block like 'to workspace' or 'record'. I also saw that I can print things to the command prompt in the deploy .m file used by Simulink Compiler, and I can do this during the simulation using a postStepFcn (and other similar callbacks), which lets me print the simulation timestep and state. But for signals, I got this error:
simulink.compiler.getSimulationOutput is not supported with 'LoggingToFile' is set to 'on'"
So it seems like there's a conflict between logging while the sim is running, and saving files containing signals? From what I've seen, there are 3 methods to save signals to a file: set LoggingToFile in the model's import/export settings, use a 'to file' block, or use a 'record' block. Perhaps I should be using the record block? My concern there is that I would like to log a bunch of signals from different blocks, but it seemed like I shouldn't be using more than one record block in a model and so I would need to route many signals into it.

Accepted Answer

Hassaan
Hassaan on 11 Jan 2024
Edited: Hassaan on 20 Jun 2024
Breaking down the issues and potential solutions:
1. Debugging and Logging Messages in Compiled Executable
When your Simulink model is compiled into an executable, the standard MATLAB console output functions (like disp, sprintf) won't work as they do in the MATLAB environment. Here are some strategies:
  • Custom Logging Function: Implement a custom logging function in your C++ code. This function could write messages to a file directly, which you can monitor during the simulation.
  • Inter-process Communication: Use inter-process communication (IPC) mechanisms, such as sockets or shared memory, to send messages from your C++ code to a separate monitoring application that displays or logs these messages.
  • External Libraries: Consider using external logging libraries in C++ that are designed for file logging, such as spdlog or log4cpp.
2. Signal Logging and Saving to File
For logging signals and saving them to files, you have correctly identified the methods. Each method has its trade-offs:
  • To Workspace and To File Blocks: These blocks are straightforward but can become cumbersome if you have many signals to log. They also increase the model's complexity.
  • Record Block: It's more efficient in terms of model complexity, but as you noted, routing many signals into a single record block can be challenging. Consider using bus signals to group related signals, which can then be fed into a single Record block.
  • Model Configuration Parameters: Using the model's configuration parameters for logging (like LoggingToFile) is an integrated solution, but it might not offer the flexibility needed for complex logging requirements.
Recommendations:
  • For Message Logging: Implement a custom logging solution in C++, which writes to a file or communicates with a separate application. This method will give you control over what is logged and how it's formatted.
  • For Signal Logging:
  • Use Record blocks for efficiency, grouping signals with buses if necessary.
  • Alternatively, use To File blocks for individual signals if you need more control over the format and destination of each signal's data.
  • Experiment with model configuration parameters if your logging requirements are straightforward and can be met with the built-in capabilities.
Debugging Tools:
  • Simulink Debugger: Use the Simulink Debugger to step through your model simulation and inspect signal values. This can be especially helpful for understanding the model's behavior before compiling.
  • MATLAB Profiler: For MATLAB Function blocks, use the MATLAB Profiler to understand performance and identify bottlenecks.
your current approach is sound, but enhancing it with custom C++ logging functions and considering the use of bus signals for efficient signal logging might resolve some of the challenges you're facing.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

More Answers (0)

Categories

Find more on Deploy Standalone Applications in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!