ANADOLU UNIVERSITY ENGINEERING AND ARCHITECTURE FACULTY ELECTRICAL AND ELECTRONICS ENGINEERING DEPARTMENT ( SPRING – 2008 ) EEM494 Digital Control Systems Laboratory MATLAB AND HARDWARE PROJECT REPORT ( 29/05/08 ) NAME OF INSTRUCTOR : Assist. Prof. Aydın AYBAR NAME OF STUDENT : Deniz KERİMOĞLU STUDENT ID: 18880733086 NAME OF STUDENT : Enes DAYANGAÇ STUDENT ID: 13679160654 TABLE OF CONTENTS Pages Subject ....................................................................................................................................1 1. Introduction .......................................................................................................................1 2. Purpose of the Report 2.1 ADVANTECH PCI 1710 Daq Device............................................................................1 2.2 Fundamentals of Real-Time Toolbox’ in Matlab........................................................2 2.2.1 Real Time Windows Target (RTWT) Toolbox..................................................2 2.2.2 What does Real-Time mean?............................................................................3 2.2.3 Real Time Workshop Toolbox and Real-Time Kernel (OS) built in Matlab.3 2.3 How to interface ADVANTECH PCI 1710 Daq Device with Simulink.......................5 2.4 Difficulties to Use the PCI 1710 DAQ’s Connector .................................................11 2.4.1 Tools...................................................................................................................11 2.4.2 Making Our Own Printed Circuit.....................................................................11 2.4.3 Notes about the Mixture ..................................................................................12 2.5 DC Motor Speed Control via DAQ and MATLAB......................................................13 2.6 Displaying Motor’s Speed (RPM) with Seven Segment...........................................17 2.7 Results: Comparison of the actual system’s outputs and simulation outputs....20 2.8 Gray code......................................................................................................................23 3. Conclusion …....................................................................................................................24 SUBJECT: Interfacing MATLAB and ADVANTECH PCI 1710 Daq Device 1. INTRODUCTION In our project for EEM494 lecture our basic purpose was to use ADVANTECH PCI 1710 Daq Device with Matlab and Simulink. By interfacing the Daq Device with Matlab or Simulink we are able use all the functions supplied by Matlab or Simulink for our control purposes and Matlab supplies us a real-time control of our real world plant by the Real Time Windows Target Toolbox. Basically we have worked on 4 fundamental Simulink blocks which are Analog Input(AI), Analog Output(AO), Digital Input(DI) and Digital Output(DO). We have also written *.m file codes for AI, AO, DI, DO purposes for Daq Device. 2. PURPOSE OF THE REPORT The purpose of this report is to explain the fundamental steps to use ADVANTECH PCI 1710 Daq Device with Matlab and Simulink. 2.1. ADVANTECH PCI 1710 Daq Device In our project we used Advantech’s PCI 1710 Daq Device with the following properties. 16 single-ended or 8 differential or a combination of analog inputs 12-bit A/D converter, with up to 100 kHz sampling rate Programmable gain Automatic channel/gain scanning Onboard FIFO memory (4096 samples) Two 12-bit analog output channels (PCI-1710/1710HG only) 16 digital inputs and 16 digital outputs Onboard programmable counter BoardID™ switch 2.2. Fundamentals of Real-Time Toolbox’ in Matlab Before beginning the adjustment of configurations in Simulink for PCI 1710 we will introduce the fundamental process and idea of how a PC (particularly Matlab) communicates with a PCI Daq device over Windows Operating System in real-time and how Simulink manages real-time application using Real Time Windows Target Toolbox under Windows which is a non-real time Operating System. Then we will issue interfacing the PCI 1710 Daq device with Simulink step by step. 2.2.1 Real Time Windows Target (RTWT) Toolbox Real-Time Windows Target (RTWT) software is a built-in toolbox under Simulink which enables you to run Simulink and Stateflow models in real time on your computer. One can create and control a real-time application through Simulink software. Real-Time Windows Target includes I/O device drivers to support numerous numbers of I/O boards, enabling us to interface to Daq devices, sensors, actuators, and other devices for experimentation, development, and testing of real-time systems. So RTWT supports our ADVANETCH PCI 1710 Daq Device too. Real-Time Windows Target includes a set of I/O blocks that provide connections between the physical I/O board and the real-time model. One can run hardware-in-the-loop simulations (HILS) and quickly observe how the Simulink model responds to real-world behavior. The Block Library lets you connect I/O signals for operation with numerous I/O boards. So RTWT supports our Daq device ADVANTECH PCI 1710 too. Here we have mentioned about the hardware-in-the-loop simulations (HILS). HILS is a device that fools your embedded system into thinking that it's operating with real-world inputs and outputs, in real-time. So we simulate the system as if it is in a real-time environment. Figure shows a simple block diagram of an embedded system being tested using HILS. 2.2.2 What does Real-Time mean? We mentioned about running or simulating real-time applications. But what makes an application a real-time one? And besides what is a real-time application first of all? The answers to these questions are made easy once you have covered the exact meaning of “realtime”. The real-time application is an application that runs concurrently with the inputs supplied and gives the output within a neglectable amount of delay, that is it must respond to the given inputs in no time so that the application does not crash. Here are some real-time applications that we face in our daily life: Videoconference Applications VoIP (Voice Over Internet Protocol) Online Gaming Pendulum-Cart System in the lab The real-time applications, systems must be controlled, observed and simulated in realworld environment concurrently that they must not be faced with unpredictable delays. An important question arises here: Is Windows a Real-Time Operating System (RTOS) that applications under Windows run real-time? Consider the simple example below: You want to listen to some music and you double clicked on your favorite song to be played with Winamp. Even at fast computer you wait 1-2 seconds for the song to be opened and if you are running some other applications besides Winamp this waiting period may be 3-4 seconds. So the system faces with unpredictable delay. As clearly seen from this example Windows is not a Real-Time Operating System (RTOS). 2.2.3 Real Time Workshop Toolbox and Real-Time Kernel (OS) built in Matlab We are to run real-time applications in real-time with Matlab/Simulink under Windows. But as we have mentioned above Windows can not run applications in real-time because of the unpredictable delays. Matlab, particularly Real-Time Windows Target Toolbox uses a small Real-Time Kernel (in the general meaning kernel means Operating System) to ensure that the real-time application runs in real time. The kernel intercepts the interrupt from the PC clock before the Windows operating system receives it. [You may need to revise the lecture notes in Microprocessors I or II courses for the concept of interrupt]. The kernel then uses the interrupt to trigger the execution of the Simulink model. As a result, the kernel is able to give the real-time application the highest priority available. The real-time kernel can use the CPU’s resources directly without the permission of Windows and uses the built-in PC clock as its primary source of time. Because of these properties, applications under the Real-Time Kernel supplied with the Matlab can be run in real-time. You must install the real-time kernel once to use it. Installing the kernel configures it to start running in the background each time you start your computer. To install the kernel: In the MATLAB Command Window, type: rtwintgt –install And precede the installation by pressing “y”. Since we are to run Simulink models under the real-time kernel, Simulink needs to convert the created models which consist of blocks into C code to generate an executable file to be run under the real-time kernel. More precisely, we need to convert the Simulink’s blocks into C code to be used with the real-time kernel. Here the Real-Time Workshop Toolbox comes into the consideration. Real-Time Workshop generates and executes stand-alone C code for developing and testing algorithms modeled in Simulink. The resulting code can be used for many real-time and non-real-time applications. You can tune and monitor the generated code using Simulink blocks and built-in analysis capabilities, or run and interact with the code outside the MATLAB and Simulink environment. Basically Real-Time Workshop provides the utilities to convert our Simulink model into C code by the Real-Time Workshop Code Generation Software. Then it uses Open Watcom C/C++ Compiler to compile and create a real-time executable file with .rwd extension to be run with the real-time kernel. This compiler is supplied with the Real-Time Windows Target software and no other compilers are needed or can be used. Simulink External Mode Interface Module provides communication between Simulink software and the real-time application. This module talks directly to the real-time kernel and is used to start the real-time application, change the block parameters, and retrieve scope data. So Simulink External Mode allows us to change parameters by editing the block diagram and observe the effect of these changes while running a simulation in external mode, in real-time. New parameter values are automatically transferred to real-time application while it is running. That is the exact meaning of the word “real-time”. But note that not all parameter can be changed in real-time. When some parameters have been changed the code needs to be recompiled. Parameters are changed from the Block Parameter Dialog Boxes. Opening a dialog box for a source block causes Simulink to pause. While Simulink is paused, you can edit the parameter values. You must close the dialog box to have the changes take effect and allow Simulink to continue. Example parameters: amplitude of a gain, frequency of a sine wave. Scope blocks can be used to visualize the signals in real-time applications to view the changes. Here is the basic procedure of how a real-time application is run and configured: We create a real-time application in the same way as we create any other Simulink model by using the standard Simulink Blocks. The Real-Time Workshop code generation software creates C code from our Simulink model. Then the Open Watcom C/C++ Compiler compiles the C code to an executable that runs with the real-time kernel. When running our models in real-time , RTWT software captures the sampled data from one or more input channels, uses the data as input to our block diagram model, immediately processes the data and sends it back to the outside world through an output channel on our I/O board. 2.3. How to interface ADVANTECH PCI 1710 Daq Device with Simulink Creating a Model You need to create a Simulink model before you can run a simulation or create a realtime application.[1]. Here is the Simulink Model, let name it deneme.mdl Configuring a Model After you create a Simulink model, you can enter configuration and simulation parameters for use by Real-Time Workshop code generation software for creating C code and building a real-time application.. These parameters control many properties of the model for simulation and code generation. This section contains the essential information you need when setting configuration parameters for a Real-Time Windows Target application. After you create a Simulink model, you can use the rtwinconfigset function to specify a default RealTime Windows Target configuration set for the model.[1]. But for the sake of understanding the procedure we will follow the configuration manually. Entering Configuration and Simulation Parameters Manually The configuration parameters give information to Simulink software for running a simulation in real-time. 1 In the Simulink window, and from the Simulation menu, click Configuration Parameters. In the Configuration Parameters dialog box, click the Solver tab. The Solver pane opens. 2 In the Start time field, enter 0.0. In the Stop time field, enter the amount of time you want your model to run. For example, enter 10.0 seconds. 3 From the Type list, choose Fixed-step. Real-Time Workshop code generation software does not support variable step solvers. 4 From the Solver list, choose a solver. For example, choose the general purpose solver ode5. 5 In the Fixed step size field, enter a sample time. For example, enter 0.001 seconds for a sample rate of 1000 samples/second. 6 From the Tasking Mode list, choose SingleTasking. (For models with blocks that have different sample times, choose MultiTasking.) Your Solver pane looks similar to the next figure. 7 Click the Hardware Implementation node. 8 From the Device type list, choose 32-bit Real-Time Windows Target. 9 Under Emulation hardware, select None. 10 Click the Real-Time Workshop node. The Real-Time Workshop pane opens. 11 In the Target selection section, click the Browse button at the RTW system target file list. The System Target File Browser opens. 12 Select the system target file for building a Real-Time Windows Target application, and click OK. The system target file rtwin.tlc, the template makefile rtwin.tmf, and the make command make_rtw are automatically entered into the Real-Time Workshop pane. Your Real-Time Workshop pane looks similar to the figure. 13 Click Apply to apply the changes to your model and OK for closing the pane.[1] Creating a Real-Time Application After you enter parameters into the Configuration Parameters dialog box for use by the Real-Time Workshop code generation software, you can build a real-time application. 1 In the Simulink window, from the Tools menu, point to Real-Time Workshop, and then click Build Model. (ctrl+B). The build process does the following: • The Real-Time Workshop code generation software creates the C code source files deneme.c and deneme.h. • The make utility make_rtw.exe creates the makefile deneme.mk from the template makefile rtwin.tmf. • The make utility make_rtw.exe builds the real-time application deneme.rwd using the makefile deneme.mk created above. The file rtwin_model.rwd is a binary file that we refer to as your real-time application. You can run the real-time application with the Real-Time Windows Target kernel. 2 Connect your Simulink model to your real-time application. Entering Additional Scope Parameters for Signal Tracing Simulink external mode connects your Simulink model to your real-time application. This connection allows you to use the Simulink block diagram as a graphical user interface to your real-time application. After you have created a real-time application, you can enter scope parameters for signal tracing with Simulink external mode: 1 In the Simulation window, from the Simulation menu, click Configuration Parameters. 2 Select the Real-Time Windows Target node.The Real-Time Windows Target pane opens. 3 Select the External mode check box. The Real-Time Windows Target pane should appear as follows. 4 Click OK. 5 In the Simulation window, and from the Tools menu, click External Mode Control Panel. The External Mode Control Panel dialog box opens. 6 Click the Signal & Triggering button. The External Signal & Triggering dialog box opens. 7 Select the Select all check box. From the Source list, choose manual. From the Mode list, choose normal. 8 In the Duration field, enter the number of sample points in a data buffer. For example, to specify a sample rate of 1000 samples/second and a stop time of 10 seconds, enter 10000 9 Select the Arm when connecting to target check box. The External Signal & Triggering dialog box looks like this: 10 Click Apply to apply the changes to your model and close to close the pane. Running a Real-Time Application You run your real-time application to observe the behavior of your model in real time with the generated code. The process of connecting consists of: • Establishing a connection between your Simulink model and the kernel to allow exchange of commands, parameters, and logged data. • Running the application in real time. After you build the real-time application, you can run your model in real time. 1 From the Simulation menu: • Select External mode simulation. • Choose Connect To Target. The MATLAB Command Window displays the message “Model rtwin_model loaded” 2 From the Simulation menu, choose Start Real-Time Code. The Simulink software runs the execution and plots the signal data in the Scope window. In this example, the Scope window displays 1000 samples in 1 second, increases the time offset, and then displays the samples for the next 1 second. 3 One may perform the followings: • Let the execution run until it reaches the stop time. • From the Simulation menu, click Stop Real-time Code. The real-time application stops. 4 In the Simulation window, and from the Simulation menu, click Disconnect From Target. 2.4. Difficulties to Use the PCI 1710 DAQ’s Connector 68-Pin SCSI Female The PCI DAQ card has 68-pin connector, shown above, which includes; analog ,digital inputs and outputs, grounds and timer/counter pins. Firstly, we had a problem to test it because in order to connect a cable to this connector i.e. a digital meter’s cables were connected to it directly to measure the analog outputs. While doing this, we were trying to be careful to get rid of short-circuit. In short-circuit case between any pins , it will cause to get out of usage of the DAQ card. Furthermore, like that, we cannot connect on it more than two cables. In electronics shopping center in Turkey, an appropriate connectors are not sold. In short, we could not connect the cables to the DAQ card efficiently and we have decided to make a circuit interface connector. 2.4.1. Tools a) 34 double connectors ( klemens ) b) 68-Pin Shielded cable c) A computer d) A laser Printer e) Enough size Copper board d) A clothes Iron e) 300ml Etchant (Hydrogen Peroxide – H2O2) f) 900ml Hydrochloric Acid (Tuz ruhu) g) A plastic cup i ) Any pcb program( we used Ares Professional program) j) A clean A4 photo paper 2.4.2. Making Our Own Printed Circuit We must indicate that it seems easy to implement and not to take long time. But, really it is a hard work. First of all, we bought the connectors and we guessed how long distance is there between connectors pins. Using Ares program, which is together Proteus, we designed the ways for PCB boards and put enough bigger dot to make a hole there. As a second step, we printed it any paper to check whether it suits to our connector and our copper board. After trying a few times, it suited and this was what we wanted and then we cleaned the copper board and the photo paper. All in all, we printed it on the cleaned photo paper. Please note that, just laser printed is worked for it because of its special chemical, we mean that any other printer will not work for PCB. As a next step, we carefully tape to hold the printed photo paper on the cleaned copper board in one position without any movement. After this step, using a very hot iron, for a long time, we put the Iron on our copper board because we waited to make the track pattern to transfer from the paper to the copper board. Actually, we succeed this part at a second time. At first trying, we did not make it enough hot and put the Iron on it for long, and without waiting it gets cool after transferring the track to it, we made the copper board and the printed paper left each other. Fortunately, at the second time, we put the Iron on them for long time, and then, we put it in water, then we observed that in water, the copper left the paper easily. Then, we put it in the mixture of 300ml-Hydrogen Peroxide and 900-Hydrochloric Acid using a enough large plastic cup. Therefore, the tracks are appeared. Finally, we cleaned and dry the board without damaging the tracks. In addition, we made a hole on the dot where we put the Clemens and connect to the cable. After making holes on it, we found out the cables sort to solid it and then we solid it and Clemens on it and tested them whether there is a short circuit or not. In conclusion, there was no problem and we succeed it and its picture is below; 2.4.3. Notes about the Mixture: a) Even though Hydrochloric Acid can be bought at everywhere, Hydrogen Peroxide is just sold in DETSAN company in Eskisehir due to the fact that it is in the group of dangerous chemical. b) Their rate should be 1/3. c) When we put a copper board in this mixture. A chemical reaction starts. This reaction is; Cu + H2O2 + 2HCl -- > CuCl2 + 2H2O That is, after the reaction, water and copper clorid occurs. Copper klorid, CuCl2 is a dangerous gas. Therefore, while doing this step, this gas must be breathed. 2.5 DC Motor Speed Control via DAQ and MATLAB In this part of our project, we control the speed of the DC motor as rpm (root per minute) with PI control. The motor features; R=3.6; which is the total of winding Ra and Terminal Rk, ohm L=5.6*10^-3; which is the inductance, henry J=.055*10^-3; which is the armature inertia, beta=.021; which is the static friction loss K1=.105; which is a constant The motor state-space according to the above values: A=[-R/L 0 -K1/L ; 0 0 1 ; K1/J 0 -beta/J] B=[1/L ; 0 ;0] C=[0 0 1] D=[0] P1=tf(ss(A,B,C,D)) A= 1.0e+003 * -0.6429 0 1.9091 0 0 0 -0.0187 0.0010 -0.3818 B= 178.5714 0 0 C= 0 0 1 D= 0 Transfer function: 3.409e005 -----------------------s^2 + 1025 s + 2.813e005 Proportional-Integrator coefficients: First of all, the coefficients of PI are found without any connections to real motor like the above print-screen picture. P Control - - Increase the transient response - - uP (k ) K Pe(k ) I Control - - Zero steady - state error - - uI (k ) uI (k 1) K I e(k ) Our coefficients are; Kp = 1 Ki = 2/ .3 If we select the step input’s features; Step Time: 1e-3 Initial value: 0 Final Value: 1 In this case, the plots of outpust and errors are; Output Error This table, which is above, may help to find the Kp and Ki coefficients. Designing Simulink model for the real time speed control application: In this part of our studying, we follow this steps; 1-Put the analog output instead of state-space State-space substitute our DC motor 2- Connect the analog input as negative feedback with conversions -Tachometer gives us the speed of the motor as voltage -If its output is 14V, this means it is rotating at 1000rpm -Using this information, we multiply tacho output with 1000/14 -If we want to show the speed as rad/second, then we multiply it again with 1/60 If one wishes, the one does not have to convert rpm to rad/sec, but this time the one should not divide reference rpm with 60. 2. Multiply also analog input with 10 and almost 6. Analog inputs give us a value between 0 and 1, if we multiply with 10, we get the correct decimal value. This correct decimal value is multiplied by 6. Because, our taco out is normally maximum 30V, and our DAQ analog input should be in 5V. By using op-amp, we divide it with 6. In short, to get the this correct value again, as software, we multiply it with 6. This circuit is used as voltage divider which is shown below. For instance, if the input is 24, the output is 4. The first op-amp output is -4V and then the second output is 4V. We have to use the second op-amp to make it positive due to the fact that DAQ’s GNDs are short circuits. So, final our .mdl file is shown below, and it is all mentioned above. 2.6 Displaying Motor’s speed(rpm) with seven segment. In this we have intended to display the speed of the motor on a 4 digit 7 segment display. We have implemented the program using the Simulink Blocks in the below figure. We have also placed the simout block to see the actual system’s error, cotroller output, tachometer outputs and simulative error, cotroller output, tachometer outputs: Here we have implemented the seven segment’s block diagrams in the Seven Segment Block Diagram. We have taken the modulus of the time with respect to 0.1s. Then we have divided the time into 4 equal time periods (0.025 0.050 0.075 0.100). If we are in the period of 0.000 - 0.025 seconds we choose the first seven segment and print the least significant decimal value, then in the second period we choose the second seven segment and print the second least significant value. This is called the scanning and we perform this process in the simulation time interval. We have implemented this scanning process in the so called Subsystem Blocks. Here is the subsystem block that does the scanning process. 2.7 Results: Comparison of the actual system’s outputs and simulation output These values are taken at 500,1000 and 1500rpm respectively. 2.8. Gray code: We build our own simulink model which converts gray code to decimal value. It is a 12 bits binary values and it is got from DAQ digital inputs ; DI 5 - DI 16. This gray code can be used to convert the gray code value from the motor position and convert it to control its position. In order to this, we put the DAQ Digital Input blocks, and we ex or each bit in a algorithm, it is shown the below print-screen pictures. And there is subsystem which check the bit after ex or. If the bit is 1, the output of the bit is one , otherwise, it is zero. Also the subsystem is shown after the next picture. 3. CONCLUSION Digital Control Systems Laboratory challenged us to improve our hardware and software implementation abilities. There were lots of handicaps that we faced and overcame. In the first place we have dealt with the software implementations of Advantech PCI 1710 Daq Device with Matlab. We have firstly tried to use the Daq device with Matlab *.m files but then we have seen that using Simulink’s Block Diagrams gave us more flexibility about interfacing the Daq device and Matlab. Here the Real-Time Windows Target ToolBox under Simulink supplies us Analog Input, Analog Output, Digital Input, Digital Output Blocks to directly use the Daq device. Matlab interacts with the Advantech 1710 Daq device according to the left side figure. This interaction is basically supplied with the Simulink’s Real-Time Windows Target Blocks. In the application we have done, our sensor is Tachometer which gives us the speed as voltage. Our actuator is Current Driver which drives the motor. After we have learnt about Matlab’s abilities we then discovered that lots of real-time applications can be implemented with Matlab and Simulink. Matlab includes lots of tools such as RTW, RTWT, xPC, and many. Such tools connect the applications to the real world easily via Daq device. Actually, these connections can be done with C, but with much more effort. In the next step, that is after we were able to use the Simulink’s I/O blocks we have dealt with the speed control of a DC Motor with the PI control. The simulations and the actual results concerning the system’s error and tachometer output have fitted in the acceptable range. Then we have implemented the software and hardware setup to show the motor’s speed in rpm at 4 digit seven segment display. Referances: [1]RTWT User Guide [2]http://www.biltek.tubitak.gov.tr/merak_ettikleriniz/index.php?kategori_id=6&soru_id=3705 [3]http://homepage.eircom.net/~ei9gq/pcb.html [4]University of Virginia, Course Lectures