WORKSHOP 3 GSE IMPORT
Overview MATLAB’s Real Time Workshop package will be used to create a compiled library (.dll) representation of the Simulink model. This .dll file will be integrated with the Adams model using the Control System Import functionality of Adams/Controls. This will create a General State Equation (GSE) in Adams that interacts with the compiled library (.dll) code.
MATLAB Mex Setup The MATLAB external interface compiler (MEX) must be set up properly on the system. To initiate setup: Issue the command: >> mex –setup Let MEX locate the installed compilers on the system. Select the Microsoft Visual C++ compiler choice; other compilers will not work properly with the Adams components.
Adams Model Setup Create new plant export files by: Start Adams/View in the working directory, load the model named ‘spring_start.cmd’. Export the plant into the working directory, using a plant export prefix of ‘rtw_model’ to create a file named rtw_model.m in the working directory. Select Target Software to Matlab This file will be used to set the plant input/output information for the Simulink model.
Adams Files For RTW Creation Adams files needed for the RTW creation process must be copied to the working directory before any compilation in MATLAB. The necessary files are copied to the working directory by first reading in the plant information and then running an Adams/Controls command like so: Read in the previously-created rtw_model.m file. This sets up the proper Adams paths in the MATLAB session. Run the following script to copy necessary files into the MATLAB working directory: >> setup_rtw_for_adams Check the working directory: several .tlc and .tmf files, needed for the RTW compilation process, should now exist.
Modify Simulink Model The Simulink model from the previous workshop will be used, but it must be cleaned up for use with RTW. To do this: Load the matlab_spring.mdl Simulink model from the previous workshop. All interactive blocks in the Simulink model must be removed for use with RTW, so go through and eliminate all ‘scope’ blocks along with any associated elements (i.e. summer blocks, connection lines, etc.).
Modify Simulink Model (Cont.) The Simulink model should then appear as:
Modify Simulink Model (Cont.) The ‘adams_sub’ block is not used for RTW creation, instead proper input and output elements (inport/outport) must be created to replace the adams_sub block. To create the needed inports/outports and fix up the model: Issue the following command on the MATLAB command line: >> setio Delete the adams_sub block. Hook up the inports and outports to the existing model. Use Terminator blocks to ‘ground’ the two unused outputs from Adams.
Modify Simulink Model (Cont.) The resulting Simulink model should appear as:
Modify Simulink Model (Cont.) Finally, create variables in MATLAB for reference in the Simulink model. To do this: Create variables on the command line of MATLAB like: K_stiffness = -8.0 C_damping = -0.033 Modify (double-click) the two Gain blocks in the model and substitute these variables for the current values. These variables will be accessed later to tune the model. Save the Simulink model before proceeding further.
RTW Configuration and Build Access the RTW configuration settings using the menu picks in Simulink: Tools -> Real-Time Workshop -> Options. Make the following changes in the appropriate tree view categories: Real-Time Workshop: RTW system target file = rsim.tlc Make command = make_rtw Template makefile = rsim_default_tmf Solver: Stop time = 0.25 Type = Fixed-step Solver = ode1 (Euler)
RTW Configuration and Build (Cont.) Optimization: Inline parameters = true (checked) Select Configure button, then add variables k_stiffness and c_damping to the list of Global (Tunable) Parameters. Ensure that the Storage Class is set to SimulinkGlobal (Auto). Apply all of the above changes, then save the Simulink model to disk. Build the RTW component using the menu picks in Simulink: Tools -> Real-Time Workshop -> Build Model
RTW Configuration and Build (Cont.) The build process should yield the message: ### Successful completion of Real-Time Workshop build procedure for model: rtw_controller Two sub-directories and several output files should be created under the working directory. The file matlab_spring.dll (prefix is the same as the Simulink model) should exist in the working directory. If this file is not created, ensure that: The working directory contains several .tlc and .tmf files. The MEX -setup process was followed and the Microsoft Visual C++ compiler was selected. The .m file from the Adams was properly loaded
Control System Import (Adams/View) Having created the .dll representation of the controller, the system can now be imported into Adams/View as a General State Equation (GSE) element. To import the RTW .dll file as a GSE: Start Adams/View in the working directory that contains the .dll file. Load the .cmd file that was created in the Plant Export process (rtw_model.cmd). Ensure that the Adams/Controls plugin is loaded. Import the Control System via: Controls -> Control System -> Import
Control System Import (Adams/View) (cont.) Use the following settings for the Control System Import dialog box: GSE Name = rtw_force External System Library = rtw_controller.dll Use the ‘Import I/O Signals …’ button and select the existing Controls Plant in the model to fill out the Input/Output lists. Click OK to create the GSE and associated elements in the model. Verify creation using the Database Navigator: Use the menu picks: Tools -> Database Navigator Verify that a General State Equation element named ‘rtw_force’ has been created in the model. Other elements such as ARRAYs, STRINGs and Variables have also been created as supporting elements for the GSE.
Simulate, Compare Results Run a dynamic simulation in Adams/view for 2 seconds, 200 steps. The two masses in the model should oscillate in a similar fashion with respect to one another. Compare the displacement of the two masses by switching to Adams/Postprocessor (F8 key) and plotting the part center-of-mass (cm) displacement in the y-direction. The displacements should match up closely, looking similar to the following:
Test RTW Parametrics Ensure that the parametrics built into the RTW component are functioning as expected by varying the spring stiffness. To do this, modify the Design Variable in the Adams/View session via: Build -> Design Variable -> Modify Select the K_stiffness variable that is a child of the rtw_force element in the model. Alter the spring stiffness for the RTW force from a standard value of “-8” to “-18” and re-run the simulation. Plot the new results in Adams/PostProcessor. Does the variable appear to be functioning as expected? _________________________(yes/no) (i)
Optional Tasks Inspect the .adm file that Adams/View writes out for this model (File > Export > Adams/Solver Dataset) in a text editor. Attempt to locate the STRING element that specifies the name of the .dll file prefix that is used for the RTW code. Do you think that the STRING value must match up exactly with the .dll file prefix? __________________(yes/no) (ii)
Answers Yes, the spring stiffness variable should alter the behavior of the system. Yes, the string value must be the same as the name of the .dll file.