Professional Documents
Culture Documents
dSPACE
1. Introduction
2. General Instructions
Create a working directory for your project (use the C, D or E root directories).
Open ControlDesk 3.X.X (this guide is not applicable to ControlDesk Next Generation
(4.X.X))
Open Matlab and choose RTI1103 (long connector board) or RTI1104 (short connector
board) if asked. Verify that dSPACE configuration okay or RTI Platform Support
Activated is displayed.
Switch to your working directory. It is important to stay in this directory during the
whole work session, especially when you compile.
Open Simulink and create a blank diagram. Save it under your working directory.
Go to ControlDesk and create a new experiment (not layout) from the File menu. Name the
experiment and choose your working directory in the Working root field. Optionally, enter
student names in the Authors field. Hit OK.
The interface is able to accept single-ended or differential encoder electronics. Wiring for each case
must be done according to the Hardware Implementation Manual. This guide assumes that the
encoder has been properly wired and connected to the terminal board using a D sub 15-pin
connector.
Open the dSPACE RTI110X / DS 110X Master PPC library and drop an Encoder Setup block
and a DS110X Encoder Position Block C_1 to the Simulink diagram. The Encoder block has 2
outputs: Enc pos and Enc delta pos. The first is used for position and the second for velocity.
Connect a gain block (from Simulink/Math Operations library) and a terminator block (from the
Simulink/Sinks library), so the diagram looks as in the picture:
Open the Encoder setup and enter the settings according to which type of encoders are used
in which channels. In our lab, we use single-ended encoders. Use the channel number where
the connector is attached (Incr1 would be channel 1 and so on).
Open the Encoder Pos_C1 block and select the channel where your encoder is hooked up
and set the initialization value (counts at start time) to zero.
Go to Configuration Parameters/Solver and make sure you have a fixed-step solver with a
step size of Ts.
If using Matlab version 2011, go to Configuration Parameters/Code Generation/RTI
Simulation Options and set Initial Simulation State to STOP (default is RUN).
If using Matlab version 2006, go to Configuration Parameters/Real-Time Workshop/RTI
Simulation Options and set Initial Simulation State to STOP (default is RUN).
Save the Simulink file and press Ctrl B to compile. Make sure the active directory in
Matlab is your working directory before compiling.
Several messages will appear on the Matlab prompt as compilation takes place. After
compilation you should see the message:
### Successful completion of Real-Time Workshop build procedure for model: (your model
name)
*** Finished RTI build procedure for model (your model name)
Return to Control Desk. You will notice that a tab appears showing an sdf file in the
working directory.
From the File menu in Control Desk, create a new Layout (not experiment). Save it right
away in your working directory (at the root, not within the subdirectories created by
ControlDesk)
From the side panel, Data Acquisition tab, drop a Capture Settings block onto the layout
(click the icon and then draw a rectangle on the layout using the mouse).
Click on it and make PPC your project name -HostService active in the Capture box.
Change the Interval Length from the default 0.2 to whatever suits your needs, for
instance 10 seconds. This will be the length of the time axis of graph displays and
numerical displays. Later, if desired you can use a downsampling higher than 1 to thin
down the recorded data.
From the Virtual Instruments tab, select a RadioButton and a Display and add them to
the layout.
Click on the Radio Button and rename RadioButton1 to STOP and RadioButton2 to
RUN. The corresponding values should be 0 for stop and 2 (not 1) for run.
Change the Captions text to something descriptive like Main ON/OFF
Click on the display and change the Captions text to something like Counts or x or
whatever is appropriate.
At this point, your layout should look like this:
Save the layout and return to Simulink. The following step must be done carefully:
you will add labels to variables of interest in Simulink to be displayed in Control
Desk. To do this, double-click on the line that represents the variable and a box will
appear. Enter the variable name. Its important to click ON the line and not next to it.
If you click next to the line, you will create a dead text label. Clicking right ON the
line will create a label that can be linked to a Control Desk display.
For encoders, click on the line after the calibration gain and call it something
descriptive, like x. Do the same for the velocity, call it v.
Compile again with Ctrl B. When done, return to Control Desk.
Click on the tab showing your directory and the sdf file to reveal the project tree:
Drag the simState variable to the Main ON/OFF box. It should flicker briefly.
Click on Labels and you should see all the labels that were properly created in
Simulink (in this case v and x). Drag x onto the Counts display.
Your interface is now ready to run, but its in Edit mode. You can switch between
Edit, Test and Animation modes by clicking at the icons on the top:
To add/remove blocks and modify the layout, you use Edit mode. To run the system
you need to be in Animation Mode. Test mode is for checking the appearance and
operation of the buttons/sliders without running the hardware.
Switch to Animation Mode and click on START in the Main ON/OFF box.
If everything is correct, the number of counts should change when you turn the
encoder shaft.
Stop and proceed to the calibration section.
4. Encoder Cailbration (Position)
Run the basic encoder interface and rotate the encoder shaft by a known amount (for
example 1 revolution). You must have some external means to measure rotation with
good accuracy. With slowly-turning motors, you may make reference marks with a
sharp object. Counting several revolutions will improve precision.
Stop the interface when the encoder completes a specific number of revolutions. Use
the displayed number of counts to arrive at the number of counts/rev. Use this in-
formation to figure out the value of gain to be used so that the display now shows de-
sired units (revolutions, degrees, radians, meters or whatever suits your needs). For
model-based controls, it is recommended to use radians for rotary motion and SI
length units (meters, mm, etc) for linear motion.
Pay attention to the sign of the counts in relation to the physical direction of move-
ment. If this relationship is not the intended one, change the sign of the calibration
gain.
The dSPACE Encoder block provides velocity measurement through the Encoder
Delta Position output. As a general rule, the number of counts per second equals the
number of counts (the same as the position outputs) divided by the sample period Ts.
Therefore, if the position has been calibrated and the calibration gain is Ek, the velo-
city calibration gain must be entered as Ek/Ts, and the resulting velocity will be in
displacement units per second (rad/s, m/s, etc).
6. Analog Output
NOTE: The dSPACE unit has a built-in gain of 10 for analog outputs. Whatever signal
is sent from Simulink to a dSPACE DAC block will be multiplied by 10. To compensate
for this, you must use a gain of 1/10 in front of the DAC block.
Open the dSPACE RTI110X / DS110X Master PPC library and drag Analog output:
DS110XDAC_C?. Set the channel number to the desired value.
Set the initialization and termination voltages according to the situation. NOTE: If
using the SyRen10 power module, set these to 2.5V so the motor wont run un-
expectedly after compiling or stopping a simulation.
Add a gain block from the Mathematical Operations library in Simulink and connect
it to the DAC. Set the gain to 1/10. For testing purposes, add a constant from the
Simulink/Sources library and set the value to something between -10 and 10V (the
range of the DAC).
This portion of the Simulink diagram should look like this (channel 8 example, 4V
output):
Save and compile with Ctrl B. When done, go to ControlDesk and run the system.
Use a voltmeter and a BNC to alligator cable to verify that the channel is putting out
the constant voltage from Simulink. Check that the initialization voltage is correct
after compilation, before running the interface. Also check that the termination
voltage is correct after stopping the interface.
7. Analog Input
Analog input is similar to analog output. Just use the appropriate ADC block and note
the following:
The dSPACE unit has a built-in gain of 0.1 for analog inputs. Whatever signal is read in
Simulink from a dSPACE ADC block will be multiplied by 0.1. To compensate for this,
you must use a gain of 10 at the output of an ADC block.
Procedure:
i. This is crucial: You must adjust the sign of the DAC 0.1 gain and/or the position
and velocity calibration gains so that a positive DAC command results in a
positive displacement (and therefore velocity). To do this, simply apply a
constant voltage to the DAC (say, 3V) and observe the sign of the encoder
display. If positive, no changes are needed. If negative, you can change the sign
if the 0.1 gain before the DAC, or flip the signs of the encoder position and
encoder velocity calibration gains. The choice depends of the physical sense of
rotation, do you want to call CW positive or negative? You can also change
motor polarity in DC motors.
ii. Once you have a positive in positive out system, add blocks to represent a PID
controller. Theres a ready-made PID, but its better to have full control of the
individual blocks. Note that this controller is meant for constant reference inputs
only (amounts of rotation). Therefore, the error derivative equals minus the
velocity. The overall diagram should look like in the figure (with the appropriate
calibration gains).
iii. The Saturation block has been added for protection against inadvertently-created
unstable loops. Set the limits to 0V and 5V to reflect the input range of the
SyRen10. Note that the controller has to be properly tuned so that the control
signal is not deeply saturated
iv. It will be necessary to monitor the calculated control signal and the resulting
position response. Double-click on the line before the saturation block and enter
a label u. With the SyRen10, this signal must be between 0 and 5V.
v. Compile and return to Control Desk. Include 3 sliding controls, 1 per gain. For
the EMG DC motor with SyRen10 power module, the recommended ranges for
the gains are :
a. P gain range:0-10
b. I gain range: 0-25
c. D gain range: 0-0.1
Edit the labels to reflect the function of each slider. Find the appropriate gains in
the model root tab and drag their variables to the sliders. The red borders should
disappear if done correctly.
vi. Add 2 Plotters from the Data Acquistion tab. Right click on them and choose
Plotter Properties. Set the Y scale to fixed (0-5V for u and -5 rad to 5 rad for x).
Find the x and u variables from Model Root/Labels and drag them to the plotters.
The red borders should disappear if done correctly. The final interface should
look as in the figure.
To tune the PID loop, start with zero I and D gains and a small P gain. Increase the P
gain gradually, then work with the I gain to improve the steady error and disturbance
recovery. The D gain may not be very useful with low-resolution encoders.
To properly close a dSPACE / Control Desk session, it is necessary to save the layout
(Ctrl S), save the experiment (Ctrl Shift S) and close the experiment from the File menu.
The experiment can be run on a future session without compilation by opening the
experiment file (cdx extension) from ControlDesk. Matlab is not needed if no
modifications are required.