Csi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Learning Adams/Controls with Control System Import from MATLAB 37

Learning Adams/Controls with Control System Import from MATLAB

38 Getting Started Using Adams/Controls


Overview

Overview
This chapter teaches you how to generate external system libraries (ESL) from control systems designed in MATLAB/Simulink and import them into Adams. It contains the following sections:
Step Three Setup MATLAB Step Four Create Adams Target for Real Time Workshop Step Five Create Simulink Model Step Six Code Generation of Control System Step Seven Create GSE from the Simulink Model

Learning Adams/Controls with Control System Import from MATLAB 39


About the Tutorial

About the Tutorial


This chapter provides the procedures to import control systems designed in MATLAB/Simulink into Adams. It relies on MATLAB/Real-Time Workshop to convert the control model to C-code. This tutorial is based on the files output from Adams/View in the previous chapter. The antenna model is still used in this chapter. The controller used in last chapter will be modified to represent three different types of controllers: continuous, discrete, and hybrid (continuous/discrete). Those three simulink files (continuous.mdl, discrete.mdl, and hybrid.mdl) are in the examples directory. Copy them to the local directory. If MATLAB/Simulink and Adams cannot be run on the same machine, you will have to perform additional steps to realize the cross-platform control system import. For more information, refer to the online help.

40 Getting Started Using Adams/Controls


Step Three Setup MATLAB

Step Three Setup MATLAB


First you will start MATLAB, and then you will create a simulink model for control system design. You will use the antenna model files from the last section, plus several additional files. To start MATLAB: 1. Start MATLAB in the same directory as the one the model and Simulink files reside. 2. Set up the MEX utility, if not already set up. Enter mex -setup from the MATLAB command window, and then select the appropriate compiler. (see http://support.adams.com under Hardware & Software Requirements for a list of supported compilers) 3. At the prompt (>>), type ant_test. MATLAB displays: %%%INFO:Adams plant actuators names: 1 control_torque %%%INFO:Adams plant sensors names: 1 rotor_velocity 2 azimuth_position. 4. At the prompt, type who to view the list of variables defined in the files. MATLAB displays the following relevant information: ADAMS_cwd ADAMS_inputs ADAMS_poutput ADAMS_prefix ADAMS_static ADAMS_sysdir ADAMS_uy_ids

ADAMS_exec ADAMS_mode ADAMS_init ADAMS_pinput

ADAMS_host ADAMS_outputs ADAMS_solver_type

You can check any of the above variables by entering them at the MATLAB prompt. For example, if you enter Adams_outputs, MATLAB displays all of the outputs defined for your mechanism. For example: ADAMS_outputs = rotor_velocity!azimuth_position

Learning Adams/Controls with Control System Import from MATLAB 41


Step Four Create Adams Target for Real Time Workshop

Step Four Create Adams Target for Real Time Workshop


In order to generate the external system library from the MATLAB/Simulink model, you need to generate some special files for MATLAB/Real-Time Workshop (RTW). You will customize the makefile template and source code template for Adams based on the version of MATLAB your are using. Once this is done, you can use the customized template files for other Simulink models. To create the Real-Time Workshop files for the Adams/Controls model: 1. At the MATLAB prompt (>>), type setup_rtw_for_adams This will automatically detect the version of Matlab you are using and create the makefile template and source code template for Adams. This function will also build template for specific versions of Matlab if desired by entering the desired version token as an argument: setup_rtw_for_adams('<version')) For help with this, enter setup_rtw_for_adams('h'). You should see the following message for success in this step:
%%% Successfully created files for Adams library export from MATLAB/RTW.

You should also confirm that in your working directory that .tlc and .tmf files were created by this step. Alternatively, since the function setup_rtw_for_adams also uses process.py, you can still setup using the old method: (Optional method if not using setup_rtw_for_adams function) 1. Set the MATLAB_ROOT environment variable to the MATLAB installation directory. For example:
On Windows (DOS shell): set MATLAB_ROOT= c:\matlab78\ On UNIX (c shell): setenv MATLAB_ROOT /usr/matlab_78/ On UNIX (korn shell): export MATLAB_ROOT = /usr/matlab_78/ Change the directory paths to match your installation.

2. In the directory where your Adams model resides, issue the following command, where $adams_dir is the directory in which Adams is installed:
On UNIX:

mdadams2010 -c python ($adams_dir)/controls/utils/process.py -v 78 exit


On Windows:

mdadams2010 python ($adams_dir)\controls\utils\process.py -v 78 Alternatively, you can copy the process.py file from the <adams_dir>/controls/utils/ directory on UNIX or <adams_dir>\controls\utils\ on Windows to the current directory and issue the following command:
On UNIX:

42 Getting Started Using Adams/Controls


Step Four Create Adams Target for Real Time Workshop

mdadams2010 -c python process.py -v 78 exit


On Windows:

mdadams2010 python process.py -v 78 The argument -v 78 stands for MATLAB 7.8 (R2009a). This command customizes several files from the MATLAB installation for the Adams target and your computer setup. You should notice several new files in your working directory with a .tlc extension and two new files with a .tmf extension. These are files required by MATLABs Real Time Workshop in the steps that follow. For help with process.py, use the -h flag (i.e., process.py -h). Note: The value for MATLAB_ROOT should have no quote, no spaces (on Windows, get short names with command dir /x), and a final slash on the path.

Learning Adams/Controls with Control System Import from MATLAB 43


Step Five Create Simulink Model

Step Five Create Simulink Model


To create the Simulink template for the control system: 1. Enter setio at the MATLAB prompt. MATLAB creates a template model with the inport(s) and outport(s) defined, as shown in Figure
17.

Figure 17

Simulink Template

Based on this template, you can design continuous, discrete, or hybrid control systems. For this tutorial, this step is information only. Since you already have these files copied into the local directory, you can delete this "io" model and use one of the examples. 2. To open hybrid.mdl, from the File menu, select Open. Or, double-click the file in the file browser. Optionally, you may also open discrete.mdl and/or continuous.mdl and run the rest of the tutorial with those for comparison. In the following context, the hybrid control system will be used as the example to illustrate the process. The Figure 18 shows the hybrid system and Table 1 shows the topology.

44 Getting Started Using Adams/Controls


Step Five Create Simulink Model

Figure 18 Table 1 Quantity 1 2 2 5 1 2 1

Hybrid Control System Hybrid Control System Topology Library Sources Continuous Discrete Math Ops Math Ops Ports Ports Block Type Step Transfer Function Transfer Function Sum Gain Import Outport Continuous Transfer Function Parameters

Step Parameters

Learning Adams/Controls with Control System Import from MATLAB 45


Step Five Create Simulink Model

Step Time: 0.001 Initial Value: 0 Final value: 0.3 Sample time: 0.001 [X] Interpret vector parameters as 1-D [X] Enable zero crossing detection (greyed out) Discrete Transfer Function Parameters 1 Numerator: [657.4] Denominator: [1 -.3679] Sample time: 0.001 2 Numerator: [600 5] Denominator: [1 -.3679] Sample time: 0.001 Gain Gain: 0.5 Multiplication: Element-wise(K, *u) [ ] Show additional parameters Import Name: Name: Outport Name: control_torque rotor_velocity Port number: 1 azimuth_position Port number: 2

Numerator: [1040] Denominator: [0.001 1] Absolute tolerance: auto

Numerator: [950] Denominator: [0.001 1] Absolute tolerance: auto

Sum Parameters 1 SumP Icon shape: rectangular List of signs: +[ ] Show additional parameters 2 SumV Icon shape: rectangular List of signs: +[ ] Show additional parameters 3 SumP1 Icon shape: rectangular List of signs: +[ ] Show additional parameters 4 SumV1 Icon shape: rectangular List of signs: +[ ] Show additional parameters 5 Icon shape: Round List of signs: ++ [ ] Show additional parameters

Port number: 1

46 Getting Started Using Adams/Controls


Step Six Code Generation of Control System

Step Six Code Generation of Control System


First you will configure MATLAB/Real-Time Workshop and then you will create the external system library from the Simulink model. Given a controller designed with the appropriately designated inports and outports, the following steps are required to export the model using RTW. 1. From the Tools menu, point to Real-Time Workshop, and then select Options. The Simulation Parameters dialog box appears. 2. Verify that Generate code only is not selected. 3. Complete the Simulink Parameters dialog box for MATLAB, as shown Figure 19.

Figure 19

Simulation Parameters

4. From the treeview on the left side of the window, select Solver. The dialog box displays the Solver options as shown in Figure 20

Learning Adams/Controls with Control System Import from MATLAB 47


Step Six Code Generation of Control System

Figure 20

Simulation Parameters - Solver Options

5. Set Solver options Type to Variable-Step. (If selecting Fixed-step solver, Set Mode to SingleTasking.). 6. Under zero-crossing options, set Zero-crossing control to Disable All. Note: Zero-crossing support is experimental in Adams/Controls, and not available in Adams/Mechatronics. If you have zero-crossing in your model, and you would like try to use them in Adams/Controls, simply select Use local settings or Enable All to turn them on, and Adams SENSOR's will be created via Controls System Import that use the zero-crossings functions.

7. From the treeview on the left side of the window, select Optimization. The dialog box displays the Advanced options as shown in the Figure 21. 8. Verify that Inline parameters is selected. Enabling Inline parameters has the following effects:
Real-Time Workshop uses the numerical values of model parameters, instead of their

symbolic names, in generated code.

48 Getting Started Using Adams/Controls


Step Six Code Generation of Control System

Reduces global RAM usage, because parameters are not declared in the global parameters

structure.

Figure 21

Simulation Parameters - Advanced Options

9. Select Apply. 10. Select the Real-Time Workshop tab. 11. To begin code generation and build the RTW library, select Build. Messages will appear in the MATLAB command window indicating successful code generation and RTW library creation. You should see messages that end with the following:
Creating library ..\hybrid.lib and object ..\hybrid.exp *** Created ADAMS dll C:\adams_controls_getting_started\antenna\hybrid_rsim_rtw>exit /B 0

Learning Adams/Controls with Control System Import from MATLAB 49


Step Six Code Generation of Control System

### Successful completion of Real-Time Workshop build procedure for model: hybrid

The library you created will be in your working directory.

50 Getting Started Using Adams/Controls


Step Seven Create GSE from the Simulink Model

Step Seven Create GSE from the Simulink Model


First you will start Adams/View and import the command file, and then simulate your Adams model containing the GSE for the control system. To start Adams/View and load the command file: 1. Launch Adams/View and import the file ant_test.cmd. 2. Load the Adams/Controls plugin, if not already loaded. 3. From the Controls menu, point to Control System, and then select Import. The Adams/Controls System Import dialog box appears. 4. Select Import I/O Signals from Existing Controls Plant. 5. From the Database Navigator, select ant_test for the plant. The values for the Output Signals and Input Signals text boxes appear. 6. Complete the dialog box as shown in Figure 22, and then select OK.

Figure 22

Adams/Controls System Import Dialog Box

Learning Adams/Controls with Control System Import from MATLAB 51


Step Seven Create GSE from the Simulink Model

Adams/View displays information on the GSE created (See Figure 23). The Database Navigator shown in Figure 24 shows the GSE and its associated arrays.

Figure 23

Information Window

52 Getting Started Using Adams/Controls


Step Seven Create GSE from the Simulink Model

Figure 24

Database Navigator

To simulate your model: 1. From the Settings menu, point to Solver, and then select Dynamics. The Solver Setting dialog box displays. 2. Change Formulation to SI2. 3. Set Category to Executable. 4. Set Hold License to No. This setting is critical to using the RTW dll in repeated simulations in Adams/View. When you rewind/stop a simulation in Adams/View, the RTW dll is released and reacquired, thereby resetting the discrete states and counters to their original state. Changing this behavior by issuing commands in Adams/View causes unexpected simulations after the first one. 5. Select Close. 6. Run a simulation with a step size of .001s and duration of .25s. During the simulation, the antenna motion behaves the same as the one in the co-simulation.

Learning Adams/Controls with Control System Import from MATLAB 53


Step Seven Create GSE from the Simulink Model

7. Press F8 to open Adams/PostProcessor. 8. Plot and animate, as desired. Adams/PostProcessor displays the plot of azimuth position versus time, as shown in below figure.

Figure 25

Plot of Azimuth Position vs. Time

A comparison of the results of the above simulation, Co-simulation, and Function Evaluation simulation is conducted (the latter two not performed in this tutorial). In all cases, the output step (sampling time in discrete simulation) is set to .001 second. The control torque versus time from three simulations is plotted in Figure 26. As shown, the result from the simulation with imported GSE is almost the same as that from Function Evaluation simulation. The control torque from the Co-simulation is slightly larger in magnitude because the one-step delay introduced by the discrete control system results in a control-mechanical system with less damping.

54 Getting Started Using Adams/Controls


Step Seven Create GSE from the Simulink Model

Your RTW dll can contain discrete states and counters. When performing repeated simulations (for example, design of experiments or design optimization), these entities need to be reset to their original values at the beginning of each simulation. When you rewind a simulation in Adams/View, the RTW dll is released and reacquired, thereby resetting the discrete states and counters to their original state. Changing this behavior by issuing commands in Adams/View causes unexpected simulations after the first one.

Figure 26

Control Torque vs. Time

Optionally, if you repeat the tutorial to create ESL's for discrete and continuous models, and rerun, you should see a plot like Figure 27. Note that the step excitation is slightly different for each model, and delays are caused by discrete states, so you should see differences in the responses accordingly.

Learning Adams/Controls with Control System Import from MATLAB 55


Step Seven Create GSE from the Simulink Model

Figure 27

Re-run Result

56 Getting Started Using Adams/Controls


Step Seven Create GSE from the Simulink Model

You might also like