Real-time Simulation in the SIMULINK environment

Source: Internet
Author: User
Tags add time
As we all know, Simulation Based on Simulink is a pseudo real-time simulation. The simulation time is not the same as the actual time synchronization, but related to the machine execution speed and model complexity. So how can we implement real-time simulation in the simulation environment of Simulink. MATLAB provides a real-time working environment (RTW) for converting a simulation model into a C Program In the external mode, this C program can exchange data with Simulink. If a clock interrupt is added to the C program, real-time simulation of the simulation can be realized in the Simulink. The following describes the specific implementation steps of this method. Step 1: create a model. The model must contain the scope module for Signal display. Step 2: set model parameters.
1. solver parameter settings
Solver-> solver options-> type = Fixed-step (required) Simulation Algorithm Set ode5 fixed step size to any value (0 ~ 1)
2. Real-time workspace parameter settings
When category is target configuration, Click Browse to select generate Code Select the common real-time malloc code format -- grt_malloc.tlc. Modify the template makefile to grt_malloc_vc.tmf. Switch category to grt code generation options and select the external mode check box to enable the generated code to support the external mode. Step 3: Modify the grt_main.c file and add the clock interruption code to it.
Grt_malloc_main.c is the main function implementation file of the common real-time malloc C program. During the code generation process, RTW automatically links it to the model code compilation to an executable file. Grt_malloc_main.c is located in the <MATLAB root> \ RTW \ c \ grt_malloc directory. For more information about how to add the clock interruption code, see "add time interruption code. Step 4: generate code.
After saving the parameter settings, click the build button or click the build all icon on the toolbar to generate the code. Step 5. Run the simulation program and view the simulation results.
Run the simulation program at the command prompt. The command is mdlname-TF-INF-w.
Mdlname is the model created in step 1. The parameter-TF specifies the simulation time. inf indicates unlimited execution, and the specific time can be specified (unit: S ); the-W parameter indicates the command to wait for the simulation to start in the simulation. then, switch the Simulink to the external mode (external, which defaults to normal). Then, you can see that the toolbar icon has changed a bit, click Connect to real-time target to establish a link with the real-time simulation program. After the link is established, click Start the Real-Time code to start the real-time simulation program. At this time, mdlname.exe receives the command to start simulation in the chain, and starts to execute Model Code until it receives the command to stop simulation in the chain or the specified simulation time. During the simulation, the dynamic real-time simulation results are displayed in the model's signal display module. The external Mode Control Panel can also be used to adjust online parameters and other functions. The time interrupt code is added.
Windows provides four timer Methods: timer, multimedia timer, high-precision running counter, and real-time clock directly managed by virtual device drivers. The timer resolution is not high and cannot meet the requirements of a slightly higher sampling frequency. The real-time clock directly managed by the virtual device driver must be written into the virtual device driver, which is complicated in programming and difficult to implement. The following provides the code for implementing time interruption using a multimedia timer and a high-precision counter. Grt_malloc_main.c 1. Use the multimedia timer // The callback function of the multimedia timer-push the simulation at a fixed frequency
Void callback dosimulation (uint uid, uint umsg, DWORD dwuser, DWORD dw1, DWORD dw2 ){
Rt_onestep (s );
} Add the time interrupt code int_t main (int_t argc, const char_t * argv []) to the main function.
{
Uint wtimerres = timer_accuracy; // defines the time interval,
Uint waccuracy; // defines the resolution and takes the simulation step of the model.
Uint timerid; // defines the timer handle timecaps TC ;............
The following code is added to the Model initialization code after initializemodel (S, finaltime ).
If (timegetdevcaps (& TC, sizeof (timecaps) = timerr_noerror)
{
Waccuracy = min (max (TC. wperiodmin, timer_accuracy), TC. wperiodmax );
Timebeginperiod (waccuracy); // sets the timer resolution.
}

If (timerid = timesetevent (wtimerres, waccuracy, (lptimecallback) dosimulation, 0, time_periodic) = 0)
{
Exit (0 );
}

.............
............

} Timer_accuracy is defined at the beginning of the file.
# Define timer_accuracy 100 // 100 ms 2. Use High Resolution performance counter int_t main (int_t argc, const char_t * argv [])
{
Large_integer frequency, lastcount, currentcount ;...........
...........

// Add the following code to the corresponding part of the execution model code
If (! Queryperformancefrequency (& frequency) // gets the system's high-precision running counter frequency
{
Exit (exit_failure );
} Queryperformancecounter (& lastcount); // query the Count value of the high-precision running counter and save the result in lastcount while (! Gblbuf. stopexecutionflag &&
(Rtmgettfinal (S) = run_forever |
Rtmgettfinal (S)-rtmgett (s)> rtmgett (s) * dbl_epsilon ))
{Rtextmodepauseifneeded (rtmgetrtwextmodeinfo (s ),
(Boolean_t *) & rtmgetstoprequested (s); If (rtmgetstoprequested (s) break; do
{
Queryperformancecounter (& currentcount); // queries the Count value of the current high-precision running counter
} While (currentcount. QuadPart-LastCount.QuadPart)/frequency. quadpart <0.1); // 0.1 is the simulation step size
Rt_onestep (s );
Queryperformancecounter (& lastcount );
}
..........
}

Note:
1. Whether it is a multimedia timer or a high-precision running counter, windowsapi is used, so include <windows. h> In grt_malloc_main.c;
2. the Multimedia Timer must be supported by winmm. Lib. Therefore, you must add winmm. lib to the libs path of the compilation link. The specific method is as follows:
Replace winmm in the (vc_root) \ lib directory. copy the Lib file to the directory (maid) \ RTW \ c \ Lib \ Win32, and then modify the grt_malloc_vc.tmf in the directory (maid) \ RTW \ c \ grt_malloc) \ RTW \ c \ Lib \ Win32 \ winmm. add lib to libs. the contents of the modified additional libraries are as follows:
Libs =
|> Start_precomp_libraries <|
! If "$ (opt_opts)" = "$ (default_opt_opts )"
Libs = $ (libs) |> expand_library_location <|\|> expand_library_name <| _ VC. Lib $ (Maid) \ RTW \ c \ Lib \ Win32 \ winmm. Lib
! Else
Libs = $ (libs) |> expand_library_name <|. Lib $ (Maid) \ RTW \ c \ Lib \ Win32 \ winmm. Lib
! Endif |> end_precomp_libraries <|
|> Start_expand_libraries <|
Libs = $ (libs) |> expand_library_name <|. Lib |> end_expand_libraries <|
3. When a high-precision counter is used to run a simulation task, an error will occur when a new thread is created to execute the simulation task. If a multimedia timer is used, the callback function is executed in a new thread.
4. the simulation step size can be set by rtmgetstepsize (s) or (S)-> timing. stepsize. the preceding example uses the common real-time malloc format code to generate real-time simulation programs in other formats.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.