csi

20
37 Learning Adams/Controls with Control System Import from MATLAB Learning Adams/Controls with Control System Import from MATLAB

Upload: bas-ramu

Post on 01-Nov-2014

31 views

Category:

Documents


3 download

DESCRIPTION

good

TRANSCRIPT

Page 1: csi

37Learning Adams/Controls with Control System Import from MATLAB

Learning Adams/Controls with Control System Import from MATLAB

Page 2: csi

Getting Started Using Adams/ControlsOverview

38

OverviewThis 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

Page 3: csi

39Learning Adams/Controls with Control System Import from MATLABAbout the Tutorial

About the TutorialThis 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.

Page 4: csi

Getting Started Using Adams/ControlsStep Three – Setup MATLAB

40

Step Three – Setup MATLABFirst 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_velocity2 azimuth_position.

4. At the prompt, type who to view the list of variables defined in the files.

MATLAB displays the following relevant information:

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

ADAMS_cwd ADAMS_inputs ADAMS_poutput ADAMS_sysdir

ADAMS_exec ADAMS_mode ADAMS_prefix ADAMS_uy_ids

ADAMS_host ADAMS_outputs ADAMS_solver_type

ADAMS_init ADAMS_pinput ADAMS_static

Page 5: csi

41Learning Adams/Controls with Control System Import from MATLABStep Four – Create Adams Target for Real Time Workshop

Step Four – Create Adams Target for Real Time WorkshopIn 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:

Page 6: csi

Getting Started Using Adams/ControlsStep Four – Create Adams Target for Real Time Workshop

42

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 MATLAB’s 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.

Page 7: csi

43Learning Adams/Controls with Control System Import from MATLABStep 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.

Page 8: csi

Getting Started Using Adams/ControlsStep Five – Create Simulink Model

44

Figure 18 Hybrid Control System

Table 1 Hybrid Control System Topology

Quantity Library Block Type

1 Sources Step

2 Continuous Transfer Function

2 Discrete Transfer Function

5 Math Ops Sum

1 Math Ops Gain

2 Ports Import

1 Ports Outport

Step Parameters Continuous Transfer Function Parameters

Page 9: csi

45Learning Adams/Controls with Control System Import from MATLABStep Five – Create Simulink Model

Step Time: 0.001 1 Numerator: [1040]

Initial Value: 0 Denominator: [0.001 1]

Final value: 0.3 Absolute tolerance: auto

Sample time: 0.001 2 Numerator: [950]

[X] Interpret vector parameters as 1-D Denominator: [0.001 1]

[X] Enable zero crossing detection (greyed out) Absolute tolerance: auto

Discrete Transfer Function Parameters Sum Parameters

1 Numerator: [657.4] 1 SumP

Denominator: [1 -.3679] Icon shape: rectangular

Sample time: 0.001 List of signs: +-

2 Numerator: [600 5] [ ] Show additional parameters

Denominator: [1 -.3679] 2 SumV

Sample time: 0.001 Icon shape: rectangular

Gain List of signs: +-

Gain: 0.5 [ ] Show additional parameters

Multiplication: Element-wise(K, *u) 3 SumP1

[ ] Show additional parameters Icon shape: rectangular

Import List of signs: +-

Name: rotor_velocity [ ] Show additional parameters

Port number: 1 4 SumV1

Name: azimuth_position Icon shape: rectangular

Port number: 2 List of signs: +-

Outport [ ] Show additional parameters

Name: control_torque 5 Icon shape: Round

Port number: 1 List of signs: ++

[ ] Show additional parameters

Page 10: csi

Getting Started Using Adams/ControlsStep Six – Code Generation of Control System

46

Step Six – Code Generation of Control SystemFirst 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

Page 11: csi

47Learning Adams/Controls with Control System Import from MATLABStep 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.

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.

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.

Page 12: csi

Getting Started Using Adams/ControlsStep Six – Code Generation of Control System

48

• 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

Page 13: csi

49Learning Adams/Controls with Control System Import from MATLABStep 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.

Page 14: csi

Getting Started Using Adams/ControlsStep Seven – Create GSE from the Simulink Model

50

Step Seven – Create GSE from the Simulink ModelFirst 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

Page 15: csi

51Learning Adams/Controls with Control System Import from MATLABStep 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

Page 16: csi

Getting Started Using Adams/ControlsStep Seven – Create GSE from the Simulink Model

52

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.

Page 17: csi

53Learning Adams/Controls with Control System Import from MATLABStep 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.

Page 18: csi

Getting Started Using Adams/ControlsStep Seven – Create GSE from the Simulink Model

54

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 re-run, 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.

Page 19: csi

55Learning Adams/Controls with Control System Import from MATLABStep Seven – Create GSE from the Simulink Model

Figure 27 Re-run Result

Page 20: csi

Getting Started Using Adams/ControlsStep Seven – Create GSE from the Simulink Model

56