Setting up a real-time digital data acquisition and control interface in dSPACE 1. Introduction dSPACE is a software/hardware platform intended to facilitate interfacing of Simulink models to hardware devices in real-time. With dSPACE, you can develop hardware-in-the-loop (HIL) and rapid control prototyping (RCP) experiments quickly, taking advantage of Matlab and Simulink high-level functions. You can easily read encoders, general digital inputs and analog inputs into a Simulink model. You can also generate digital and analog outputs to control actuators. 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. • Select and declare a sampling period according to application requirements: >>Ts = 1e-3; %set the sample period to 1 ms • Return to Simulink and start adding interfacing blocks according to needs. 3. Encoder Interfacing 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. It’s 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 it’s 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 information to figure out the value of gain to be used so that the display now shows desired 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 movement. If this relationship is not the intended one, change the sign of the calibration gain. 5. Encoder Cailbration (Velocity) • 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 velocity 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 won’t run unexpectedly 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. 8. Modifying parameters in real-time As part of controller tuning, parameter values such as control gains and reference inputs must be changed while the system is in operation. You can easily do this with dSPACE. As an example, we add a gain after the constant input applied to the DAC in Section 6. We will be modifying the values of the gain and the constant while the system is running. Add a gain so that the DAC portion looks like this: • • • • • Compile with Ctrl B and return to Control Desk. Add a Slider and a Numeric Input from the Virtual Instruments tab. As an example, we will be using the slider to change the constant and the numeric input to change the adjustable gain. Edit the captions to reflect this. Edit the range of the slider from -10 to 10 and enable range-checking in the numeric input and set a range of -1 to 1. To link with Simulink, the method is different from when you have labels. Here, you must expand the modelRoot tree in ControlDesk.You should see all the block names from Simulink. Find the one named Constant and click. On the right pane, you should see a variable with a P inside a square labeled Value. Drag this onto the slider. The red border should disappear. Find the adjustable gain in the modelRoot tree and drag the Gain variable with a P • • inside a square (not the Out1) onto the numeric input block. The red border should disappear. If you switch to test mode, the defaults should be seen (4 and 2 in this example). Run the interface with a voltmeter connected to the DAC channel. You should be able to change the overall output by moving the slider and entering values in the box. 9. Simple PID loop This example assumes you have a SyRen10 power module and an EMG small DC motor with encoder. The power module will be driven by DAC using its 0V and S1 connections. Remember that an analog command of 2.5V produces zero motor voltage. 0 V produces maximum voltage (equal to the power supply input in B+, usually +12V) and 5V produces minimum voltage (equal to the power supply input in B-, usually -12V). Because of this, the signals produced by the PID controller must be shifted up by 2.5V. 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. There’s a ready-made PID, but it’s 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. 10. Saving real-time data after a run and closing a session After the interface runs and generates some data, you can save it for later plotting and analysis. To prepare for this, click on the Capture Settings block of your layout and click on the Capture Variables tab. Select the variables you wish to save. Run the experiment and stop it. While in animation mode, press Save in the Capture Settings box. A Matlab (mat) file will be saved. Extracting the data requires additional steps: • Go to the Workspace and load the data file: >>load capture.mat • A structure variable with the same name will be created in the workspace. To see how data is organized type: • The captured variables will be listed in order. Make a note of the variables and their order. Save the contents of the variables to new variables: • • >>capture.Y.Name >>x=capture.Y(1).Data; Do this for all variables. Then save the time variable: >> t= capture.X.Data; • For convenience, save the variables to a new file, including the sampling period: >> save results t x u y Ts This will save variables t, x, u, y and Ts to results.mat. 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.