MATLAB: Is there an example available that demonstrates how to gain access to global signals using the C-API in Real-Time Workshop

accessapicc++ apicodeexternalglobalinterfaceloggingmonitoringrtwsignalsimulink coder

I want to use the C-API in Real-Time Workshop to interface to an external signal monitoring application with certain signals from my model. I would like to know if there is an example available that demonstrates how to gain access to the global signals in my model from external C code.

Best Answer

This enhancement has been incorporated in Release 2008b (R2008b) in the following demo 'Target-Based Data Interface: C-API Data Definition File' which can be opened by typing 'rtwdemo_capi' on MATLAB Command Prompt.
For previous product releases, read below for any possible workarounds:
The attached example application logs values of all global signals in a model to a text file. This code is intended as a starting point for gaining access to signal addresses by using the C-API in Real-Time Workshop. You can extend the code to perform signal logging and monitoring. The application:
  • Uses the "rtmGetDataMapInfo" macro to gain access to the model mapping information (MMI) substructure of the real-time model structure. In the following macro call, rtM is the pointer to the real-time model structure in model.c or model.cpp.
rtwCAPI_ModelMappingInfo* mmi = &(rtmGetDataMapInfo(rtM).mmi);
  • Uses custom function "capi_StartBlockIOLogging" to initialize signal logging, custom function "capi_UpdateBlockIOLogging" to log a signal value at each time step, and custom function "capi_StopBlockIOLogging" to terminate logging and write the values to a text file.
The custom functions are integrated with model code by using the TLC file capitestsigfile.tlc. The TLC file places the following function calls in model.c:
* Call to "capi_UpdateBlockIOLogging()" in the "model_output" function
* Call to "capi_StartBlockIOLogging()" in the "model_initialize" function
* Call to "capi_StopBlockIOLogging()" in the "model_terminate" function
To use the example, download the attached files and place them in your current working directory. The attached files include
* capitestsigfile.tlc
* capi_BIOSignalLog.h
* capi_BIOSignalLog.c
* example_capi.mdl
To configure and build the example, using Real-Time Workshop 6.1 (R14SP1) do the following:
1. Open the model "example_capi.mdl."
2. In the model window, click Simulation > Configuration Parameters.
2. Choose a target file. If you want to use a grt.tlc target file instead of the default ert.tlc target file:
a. In the left pane of the Configuration Parameters dialog box, click Real-Time Workshop.
b. Click Browse to open the System Target File Browser.
c. Select a grt.tlc target.
3. Integrate the custom application code with the generated code, by including the following string in the TLC options field of the Build process section of the Real-Time Workshop pane :
-acapiSigTestFile="capitestsigfile.tlc"
4. Click Real-Time Workshop > Interface and select "C-API" for the Interface field under Data exchange. For the purpose of this example, you can limit the data exchange to “Signals in C API” only.
5. If you are using an ert.tlc file, select the following options on the Real-Time Workshop Interface pane:
* Support complex numbers
* MAT-file logging
6. Return to the Real-Time Workshop pane and clear the "Generate code only" check box. The Build button appears.
7. Click Apply.
8. Click Build. Real-Time Workshop generates an executable file.
To run the executable, type "!example_capi" at the MATLAB prompt. Real-Time Workshop creates the text file example_capi_ BlockIOLog.txt in your working directory. Examine this text file using an editor, such as the MATLAB Editor.
For more information about using the C-API to interface with signals, see the documentation accessed by typing the following at the MATLAB 7.0.1 (R14SP1) prompt:
web([docroot,'/toolbox/rtw/rtw_ug/f6010dfi2.html'])