Sie sind auf Seite 1von 15

MECH 412

System Dynamics and Control

Laboratory 1 Manual

Version 1.3
Ryan J. Caverly, Prof. M. Nahon, Prof. I. Sharf and Prof. J. R. Forbes
Department of Mechanical Engineering
McGill University
Winter 2014

This lab manual serves as a guide to the first laboratory concerning the control of a flexiblejoint designed and built by Quanser Consulting Inc. The prelaboratory questions that you did in
Assignments 1-3 were intended as a guide through the dynamic modelling of the flexible-joint.
They are included in the Appendix of this document. This laboratory manual will guide you
through the first lab.
Also note that this lab manual differs from a traditional lab manual in the sense that every
step includes an instruction and an explanation. The purpose of this is for you, the student, to
obtain a greater understanding of the use of each step.

Laboratory 1: Experimental Open-Loop

System Response
Problem Statement
Using the system model created in Assignment 2, experimentally find the open loop system
response of the actual Quanser flexible-joint system. The Simulink model created in Assignment
2 will be modified to connect to the physical system in real-time using QuaRC software.

Prelaboratory: Modelling of System

The following must be completed before attending the lab:
1. Lab-related questions in Assignments 1 to 3.
2. Bring a copy of your Simulink model from Assignment 2 to the lab.
3. Download the SafetyCheck.mdl file found on MyCourses and bring it to the lab.

Laboratory Procedure
Part 1: Setup
1. Log in to your EMF account on the PC at the lab station.
2. Open up matlab2011a and set the working directory to the one that contains your
Simulink model. It is recommended to save your Simulink model to the H: drive.

3. In matlabs Command Window type mex -setup. Note the space between mex and
-setup. You will be asked Would you like mex to locate installed compilers [y]/n?;
type y. A list of compilers will be listed. Select Microsoft Visual C++ Studio 2010 in
.... Confirm your selection by typing y when prompted once more. (Note, you should
only have to do this once. If you have done this before, you will not have to complete this
step again, nor the next time you run the experiment.)
4. Open your Simulink model from Assignment 2 in matlab. Ask Mario or the TA to check
your Simulink model before continuing.
5. Open up the Simulink Library Browser and add Add, Mux, and Gain blocks to your
Use the Add block to add up the output signals and .
Connect the + signal to one input of the Mux block.
Set the Gain block to 180/pi and connect the input of the gain block to the wire leading
into the state-space block. Connect the output of the gain block to the other input of
the Mux block.
Attach the output of the Mux block to a scope. This scope will now output + and
the input signal.
6. Open up the Simulink Library Browser (located in the toolbar and also in the View tab)
and search for hil initialize (this may take a long time).
You should find a block that says QuaRC on it. If Simulink cannot find the hil
initialize block, save your Simulink model and have your group partner log into
their EMF account on the computer.
Drag this block into your model in a place that it does not interfere with anything else.
The HIL (Hardware In the Loop) Initialize block will allow you to connect to the data
acquisition card and read/write information to the Quanser flexible-joint.
7. Open the HIL Initialize block (double-click) to change the following settings:
In Main, change the Board type to q2 usb.
In Analog Inputs, add analog inputs #0 and #1 to the channels selected.
In Analog Outputs, add analog output #0.
In Encoder Inputs, add encoder inputs #0 and #1 and change the encoder quadrature
to [4].
8. Click and drag to highlight the state-space block and to select blocks shown in Fig. 1.

Right click and choose create subsystem.

Double-click on your new subsystem and you will see the blocks that you just highlighted.
This subsystem will allow you to add blocks to connect to the flexible-joint without crowding
your higher level model.

Fig. 1 Blocks to select for subsystem.

9. Enlarge your subsystem window and delete everything in the subsystem except for the
input and output ports.
Open up the SafetyCheck.mdl file and copy the SafetyCheck block into your subsystem.
In the Simulink Library locate and add a Gain, Saturation and HIL Write Analog block to your subsystem and connect them all to the input port (In1InputVoltage
of SafetyCheckGainSaturationHIL Write Analog).
Set gain to -1 and the saturation limits to -9 and 9. Verify that the HIL Write Analog
block is set to channel 0 by double-clicking it.
The gain is set to -1 so that a positive voltage turns the flexible-joint counter-clockwise.
The Saturation block is added to ensure that the voltage sent to the motor is no more than
90% of its maximum voltage, even if your input voltage surpasses this. The SafetyCheck
block ensures that the flexible-joint will not rotate past || = 90 degrees and will never
receive a voltage higher than 12 V.
10. Add an HIL Read Encoder, Mux, Demux and Gain block to your subsystem.
Set the gain to 2*pi/4096.

Set the HIL Read Encoder so that it uses channels #0 and #1. If you get an error
regarding the sample time of the HIL Read Encoder block, replace qc get step size
in the Sample time (seconds) with -1.
Connect the blocks as HIL Read EncoderMuxGainDemuxOutput 1 and 2.
The gain is set to 2/4096 in order to convert the angle measured by the encoder into
radians. The encoder quadrature is set to [4], which means that it measures 4096 counts
per revolution.
11. Connect the input theta of the SafetyCheck Block to the wire that corresponds to the measurement of , as seen in Fig. 2(b).
12. You can rename our input Vin and your outputs theta and alpha for convenience by
clicking on In1, Out1 and Out2.
13. Return to your high level model, click on the dropdown box in the upper toolbar that says
Normal and change it to External. Notice that the stop button is replaced by a Connect
to Target button.
By switching to External mode, the model will be run through QuaRC, which will allow
you to perform the experiment in real-time.
14. Delete the To file blocks from your model, if you have any.
In the Simulink Library search for the To host file block and add it to the Simulink
model. Attach it to the wire that connects to the + scope.
Double-click on the To host file block and change the file name to theta plus alpha data.mat
and the variable name to theta plus alpha data.
15. In the QUARC menu select Options. Make sure the following settings are properly set:
Solver menuType: Fixed-Step
Solver menuSolver: ode4 (Runge-Kutta)
Solver menuFixed-step-size: 0.005
Code Generation menuSystem target file
A fixed-step solver is required to perform real-time HIL experiments, since the step size
cannot vary in real time.

Part 2: Open Loop Frequency Response

In part 2 of this lab you will operate the open loop system at various input frequencies in
order to experimentally produce a Bode diagram of the flexible-joint.
1. In Assignment 2, you had set the signal generator for a step (or square wave) input. You
should now set it to provide a sine wave.
Set the signal generator to a 0.05 Hz sine wave.
Set the amplitude of the sine wave to 20 (20 degrees represents the actual angle of
motion of the link) and enter pi/180 in the gain block to convert degrees to radians.
The magnitude of this input sine wave will remain constant at 20 degrees throughout the
2. Set the experiment time to inf in the box beside External in the toolbar.
This will allow you to keep the experiment running until you have collected the required
3. Your Simulink model should now look something like Fig. 2. If there are any discrepancies
with your model, make the necessary changes.

(a) High-level Simulink model block diagram.

(b) Subsystem Simulink model block diagram.

Fig. 2 Simulink model block diagrams.

4. Make sure the matlab working directory is set to the location of your Simulink file. This
should be the H: drive.
5. Build the Simulink model clicking Build (or ctrl B) in the QUARC menu. The model is
finished building once the matlab Command Window says:
### Model [name of your Simulink model] has been downloaded to target shmem://quarctarget:1

If you see any red text in the matlab Command Window, notify the TA or Mr. Mario
The QuaRC Build command will convert your graphical block diagram code into C code
for matlab to run.
6. Now that the model is built and ready to run, turn on the VoltPAQ-X1 amplifier on the desk.
The switch is on the back of the amplifier, just above the plug input. When the amplifier is
switched on, you will hear it power up. Make sure that there is nothing in the path of
the flexible-joint. The VoltPAQ-X1 amplifier is required to regulate the voltage being sent
to the motor.
7. Before continuing with the experiment, the TA or Mario should check to make sure that
your Simulink model is correctly wired.
8. In the Simulation menu, click Connect to Target to connect to the data acquisition card.
Once you see the blue status bar appear in the lower right of the window, the experiment is
ready to run.
By connecting to the target you are initializing your model to be run through QuaRC.
9. In your Simulink model, open up the scope for + . Make sure you can see the entire
Simulink model and the scope.
10. Manually rotate the arm of the testbed so that the arm points outward and is aligned with
the white mark for zero angle (=0). Now to start the experiment, click the play button.
NOTE: Make sure you keep the mouse over the play button, which turns into the stop
button, while the experiment is running. If at any point during the experiment the system
behaves in an unexpected manner (e.g., nothing happens for more than 5 s when you press
start, there are large oscillations, the motor cuts in and out, there is a loud noise, etc.), press
the stop button immediately. If this happens, verify your numeric values and connections
in Simulink before rerunning the experiment. If something is wrong, ask for assistance.
11. Stop the experiment after you have obtained at least a full period of the output sine wave.
During or after the experiment you can scale the scopes by right-clicking on the scopes and
selecting Autoscale. The Simulink scopes often do not retain all the required information, so it is highly recommended to plot your results from the matlab command window.
You can do this using the commands load theta plus alpha data,
plot(theta plus alpha data(1,:),theta plus alpha data(2,:),
theta plus alpha data(1,:),theta plus alpha data(3,:)), grid on.
You can recall these commands later by pressing the up arrrow on the keyboard while in
the command window.
12. Using a plot of + versus time, note down the peak-to-peak amplitude (Exp. Amplitude)
of the flexible-joint and the phase difference between the flexible-joint and the input sine

wave. You can use the data cursor tool in the matlab figure to find the exact values. This
tool is located three icons to the right of the zoom out icon in the upper toolbar. Also note
the frequency of the input sine wave. It is recommended to use Microsoft Excel or matlab
to store your readings. You should create a table that looks like the one in Table 1.
Freq. (Hz)

Freq. (rad/s) tp input


tp output

Exp. Amplitude (deg)

Gain (dB)

Phase Angle (deg)

Table 1: Suggested table for experimental results.

To calculate the phase difference between the flexible-joint response (output) and the input
sine wave, use the following.
Phase Angle (degrees) = f req (tp input tp output )(360 degrees),


where f req is the frequency of the input sine wave in Hz, tp input is the time of a peak of
the input wave, and tp output is the time of the corresponding peak of the output response.
13. Repeat steps 8 to 12 varying the frequency of the sine wave from 0.1 to 1 Hz in steps of
0.3 Hz and then from 1 to 7 Hz in steps of 2 Hz. If you get an error while attempting to
Connect to Target, try building the file again.
14. Note down the frequency of each test in rad/s by multiplying the frequency in Hz by 2*pi.
15. Find the amplitude gain of the system for each test in dB using

Amplitude (deg)
Gain (dB) = 20 log
40 deg
16. Now plot Gain (dB) versus frequency (rad/s) using a logarithmic scale x-axis.
If you save your data in array form within matlab, you can create your plot using the
command semilogx([name of your data]).
17. Replot the first linear region of the graph using the same logarithmic scale x-axis and find
the equation of the logarithmic trendline of best fit (this will be a straight line on the semilogarithmic plot). In Excel the logarithmic trendline of best fit is given as log base e. You
will have to convert this to log base 10 in order to find the slope in dB/decade.
18. Plot the phase angle of the system in degrees versus frequency using a logarithmic scale


Part 3: Shutdown Procedure

1. To shutdown, turn off the VoltPAC-X1 amplifier (using the same switch on the back of the
amplifier). Close Simulink. Close matlab.
2. Please leave everything as you found it. If anything is broken, or seems wrong, notify
the TA or Mr. Mario Iacobaccio.

1. In Assignment 3, you obtained the transfer function G2 (s). Use Matlab to draw the Bode
plot of that transfer function. How does your experimental Bode diagram match the one
obtained theoretically? Provide your experimental and theoretical Bode plots in the same
2. What is the slope of the low frequency region of the Bode diagram magnitude plot you
found experimentally (slope of logarithmic base 10 trendline of best fit)? Does it seem to
match the slope of the Bode diagram of the theoretical transfer function?
3. How reliable are your measurements at the higher frequencies? What factors could be
affecting your high-frequency measurements?

To Submit
Submit a brief lab report (5-7 pages recommended) comprising of the following sections:
Introduction: provide context for the lab. What were the objectives of this experiment?
Write 2-3 paragraphs in your own words (do not copy the manual).
Answers to all laboratory questions: provide one paragraph of text and supporting figures
as required. Keep the figure presentation concise.
Conclusion: discuss your results and sources of error. What can be said about the success of
your experiment, and the validity of your theoretical model of the system? (2-3 paragraphs
recommended length).



Prelab 1. Figure 1(a) shows the Quanser flexible-link experimental testbed. It consists of an
electric motor driving a square-shaped rotor. A beam is attached to the rotor with springs. Later
in this course, we will be designing controllers for this system to minimize the beams vibration.
Figure 1(b) shows a schematic of the testbed, in which are shown the rotor and the beam. We
denote the rotation of the motor by and the rotation of the beam, relative to the rotor, by . The
rotor is considered to have a moment of inertia Jeq, and the beam has a moment of inertia JL, both
about the origin shown.

Figure 1(b)

Figure 1(a)

Figure 1(c) shows another schematic showing the same components, but also showing the torque
applied by the motor, , the damping internal to the rotor assembly, Beq, the aerodynamic
damping acting on the arm, BL, and the stiffness of the rotational spring attaching the beam to the
rotor, KS. The torque applied by the motor on the rotor can be written as
Vm is the voltage applied to the motor.

, where

Figure 1(c)

In our mission to design a motor controller for this system, we will start off, in this assignment,
by deriving the equations of motion for this system. Derive the two coupled second-order
differential equations, in and , that govern the motion of this system. In these equations, the
input voltage should appear on the right-hand side, and all terms containing the variables and
(and their derivatives) should appear on the left-hand side.

Prelab 2. We now continue toward our objective of designing a controller for the Quanser flexible-link
experimental testbed that we started in Assignment #1, using the following coupled equations of motion
(found in Assignment #1) as a starting point.

The numerical values for the flexible-link testbed parameters are given below.
Flexible-Link Properties

Motor Properties

For this assignment you must:


Formulate the state-space equations for the system using the provided equations of motion. Use a
state vector


, an input

and an output

C and D, in numerical form.

Write a corresponding Simulink program to solve the system and provide a screenshot of your
block diagram.
Run your Simulink model with an initial condition of
provide a screenshot of your resulting plots of


. Give the state space matrices A, B,

and .
and a step input of

Run your Simulink model with initial conditions of

5V. Provide a screenshot of your resulting plots of

and .

Refer to the Simulink example.mdl file which is a model of a 2 degree-of-freedom translational
system , posted in MyCourses.
In order to run the example .mdl file or your own program, click on the play button in the toolbar.
In your Simulink model of the flexible-link system, the state space equations will only work with
angles in radians. You should convert the output angles into degrees before you plot them.
To give an initial condition to the system, when you click on the State-Space Block to fill in the
A, B, C and D matrices, also give initial conditions in the state vector form.

Simulink has a step block in its library(

). Drag blocks from the library into your model.

You can use the Simulink manual provided on MyCourses for additional information.

Prelab 3. This question represents the next step towards our objective of designing a motor
controller for the Quanser flexible-link device that we previously started in Assignments 1 and 2.
We will use the following state-space equations for the system as a starting point.



For this assignment you must:


Formulate the transfer function



using the above state space equation with an output

. This represents the case where we are interested in the

as the output .

Formulate the transfer function


using the above state space equation with an output

. This represents the case where we are interested in the

angle ( + ) as the output .

Note: You may use Matlab to solve for


, or do it by hand.