• No results found

The new I/O-card also had problems with the analog output. The operational amplifiers and the supply voltage for these worked better than on the prototype, but the filters for converting the PWM signals to analog voltages didn’t work. They gave out lower voltages

10.4. PROBLEMS WITH ANALOG OUTPUT 85

Figure 10.1: Top layer of final card. (Not to scale)

Figure 10.3: Final version of I/O-card

than they should have. The reason for this is unknown, but the filters was difficult to solder since the SMD resistors and capacitors were very small and were placed on a small surface. Inaccurate assembly may be the reason for the poor performance of the filters. Redesign of this I/O-card should give the filters more space.

Chapter 11

Matlab Real-Time Workshop

11.1

Matlab Real-Time Workshop

Matlab Simulink can be used to design and simulate mathematical models. With the help of Matlab Real-Time Workshop (RTW for short) C-code can be generated from the Simulink model. The code will (if compiled) give a program that does the same simulation as in Simulink. This can be used for rapid prototyping control systems, as described in 2.4. How to use RTW is described in [13]

11.1.1 Target Language Compiler (TLC)

When Matlab Real-Time workshop generates C-code from a Simulink diagram it actually does it in two steps. The first step is to make a .rtw file. This file contains all the information from the Simulink diagram presented in a hierarchy The TLC language is describes in detail in [12].

The TLC is the second step of the code generation, and it generates multiple source files from the information in the .rtw file and several .tlc files. The .rtw file contains information about the Simulink diagram, while the different .tlc files contain information on how the generated code should look like.

It’s two different types of .tlc files. The system target file specify the overall structure of the generated code, and it’s selected before generating code among a list of system target files supplied with RTW. It’s also possible to change existing or make new system target files. For rapid prototyping the general real-time target (grt ) is a good starting point. This makes C-code that can run on most platforms, as shown in 4.3.

Code generated with grt, will as in Simulink execute the simulation as fast as possible. A control system has to execute the simulation with correct time between each time step, or it wouldn’t work as intended. To make this possible with grt, some of the code have to be modified.

Each Simulink block has its own block target file. This file contain the information TLC needs to generate code that does the same as the block would do in Simulink.

11.1.2 S-functions

S-functions are Simulink blocks written in a computer programming language, like Matlabs own language M, C, C++, Ada and FORTRAN. This allows users to add their own blocks in Simulink, that can have functionality that normal Simulink blocks can’t provide. When using Matlab RTW as a rapid prototyping tool for a control system, S-functions are often used to control I/O.

S-functions are written as a code file for the selected computer language, but this file has to follow a specific structure. To use the S-function the code has to be compiled using the Matlab command mex. As for other compiled program, this file has to be compiled again each time changes have been made.

When using RTW, it’s possible to use three types of S-functions, that are described below.

◦ Noninlined S-functions are S-functions that doesn’t have a .tlc file, which means that it’s treated equally by Simulink and RTW. This is the easiest solution, since everything that works in Simulink will work equally in RTW, and it’s not necessary to write any TLC code. Unfortunately this is not very efficient, and will need additional CPU and memory usage when running a program from the generated code.

◦ Fully inlined S-functions have a .tlc (target block) file, and the behavior of the S-function in RTW is defined by this file, and not by the S-function code file used by Simulink. This means that the S-function might not do the same thing when simulating in Simulink as, when running the program made by RTW generated code. This can both be a good and a bad feature. If the block is supposed to do the same in both cases, it will be error-prone and time consuming to upgrade two files at the same time. In some applications like controlling I/O, it might be preferable that the Simulink simulation does something different than the RTW generated code.

◦ Wrapper S-functions are a special type of inlined S-functions, where a third code file is used. This file has functions that both the .tlc file and S-function code files uses. This allows the Simulink and RTW to execute the same code as in a noninlined S-function, but without the loss of performance.