Coding and Debugging Programs in S-Function Builder by Visual Studio

Contents
  • Software for this post
  • Introduction
  • Coding by S-Function Builder
  • Debugging by Visual Studio
  • Conclusion
Software for this post
  • MATLAB/Simulink (verified by version R2015b);
  • Visual Studio ( verified by Visual Studio Community 2013).
Introduction

Programming by S-Function Builder is an effective way to embed algorithms. There are many comprehensive examples/tutorials provided on Internet. Here, I just try to summarize the main points. The debugging method using Visual Studio will also be included. Before starting this post, readers should have selected proper compiler for C by “mex -setup” in MATLAB command window.

Coding by S-Function Builder

Firstly, go to the Simulink Library browser/User-Defined Functions, drag/drop the “S-Function Builder” block into your Simulink model.

Double click the “S-Function Builder” block in your Simulink model to open a dialog box. Input a proper name for S-Function. Then, set the initial states, parameters (values are from workspace), inputs and outputs (including the dimension of each signal) of the blocks, libraries in sequence. Note that global variables, external source codes can be defined/included in Libraries Tab. After that coding your program in Continuous Dirivatives Tab, Discret Update Tab and Outputs. The names of inputs/outputs, parameters represent address so that they should be called using pointer or array.

After your coding, click the “build” on the right-top of the dialog box. If the build process was successful, four files would be created in the current directory.

Debugging with Visual Studio

In MATLAB command window, input “mex -g ****.c ****_wrapper.c”, where “****” is the S-Function name assigned in last step.

Next, open the Microsoft Visual Studio and choose “Attach to Process” in the TOOLS manu and select the MATLAB process.


Open the “****_wrapper.c” file in the FILE manu, set the breakpoints at the necessary places. The breakpoints may show up with question marks. Then, run the Simulink model and the program will stop at the first breakpoint. The values of the variables are shown in the local tab at the lower part of the window. We can check our algorithms step by step by push F11.

Conclusion

Though the debug is not as simple as MATLAB, writing codes in C will run faster. I will show you how to solve a moderate-scale convex optimization problem with S-Function in real-time in future.

Leave a Reply

Your email address will not be published. Required fields are marked *