📜 ⬆️ ⬇️

Finite automata in the SimInTech dynamic simulation environment. Part 3. Go to the C code.

In the first and second parts, an example of creating a control system based on the logic of "finite automata" was considered and a model of the heater controller was created.

In this part, it will be shown how to generate C code from SimInTech that implements the control program based on the logic of “finite automata”, and then postpone to MS Visual Studio 2015 together with the object model in SimInTech.

The control algorithm based on the finite-state machine logic in the SimInTech environment created at the previous stage can be automatically converted into C code, ready for loading into the controller. I don’t have a controller at hand, so in this example we’ll just collect the dll from the generated code. In this part, we will examine the basic steps for preparing the model and generating the C code from the SimInTech environment.

Save the project created in the previous stage under the name Heater.prt
')
To create a C code, it is necessary to divide our common model into an object model and a control system model. Since at the first stage we packaged the control model in the “Heater Controller” substructure, we need only to render this structure into a separate project.

Create a new project with the name "Controller_Ddll" and save it in the same directory. Then copy the Heater Controller block into it.

To create an external program in SimInTech, you need to use special input-output blocks. These blocks “input contact S3” and “output contact S3” are located in the “Data” tab (see Fig. 45 - 46).

image
Figure 45. Block input contact S3.

image
Figure 46. Block output contact S3.

Place two blocks “input contact S3” and two blocks “output contact S3” on the diagram and connect them to the block “Heater controller”. View of the scheme is shown in Figure 47.

image
Figure 47. Scheme of heater controller algorithms in a separate project.

To configure the inputs, enter the properties of the blocks (double click on the block) and configure the parameters of the input ports as follows:

"Contact Name" - input: 0 for the first block and input: 1 for the second block
“Contact Type” - Float (see Figure 48)

image
Figure 48. Setting the input signal to the program.

To configure the outputs, enter the properties of the blocks and configure the parameters of the output ports as follows:

"Contact Name" - out: 0 for the first block and out: 1 for the second block
“Contact Type” - Float (see Figure 49)

image
Figure 49. Setting the output signals from the control program.

To configure the name of the program or dynamic library, click the “Calculation Parameters” button on the schematic window. In the dialog box that appears, specify the name of the future program. In the "Name (s) of algorithms" parameter, specify controller_dll. (see fig. 50)

image
Figure 50. Configuring the program name.

Save the project as controller_dll.prt.

Let's proceed to setting up the code generation process. In the main menu of the program select the item “Code Generator”. In the dialog box, go to the “Settings” tab and select the template code directory “% codetemplates% VC2015 \” (see. Fig. 51)

image
Figure 51. Setting the code generation pattern.

For code generation there is a set of prepared templates that are configured for different compilers. SimInTech developers offer several variants of code generation templates designed for different hardware platforms and operating systems. Among them - code generation under Windows, Linux, QNX. From the developers site, you can download the MinGW toolkit to create executable files and libraries on Windows.

In this example, we will use the template for the Visual Studio 2015 compiler. To work, the free (“communist”) version of this program is sufficient. The version of Visual Studio Community 2015 can be downloaded from the official website beta.visualstudio.com/en

The main text of the program is created in the form of pure C code conforming to the ANSI standard. This text is placed in a pre-prepared template for various compilers.

To generate the code in the main menu of the program, select the item “Tools” and the sub-item “Generate a program”. When this item is selected, an error message appears at the bottom of the schematic window. Double clicking on the error line leads to switching to the submodel in which the error occurred, and a block is selected for which the program code cannot be generated (see Figure 52).

image
Figure 52. Error message and block allocation with an error.

In this case, the error is due to the fact that in the general project we used the global flash_time signal to change the response time of the transition from the on state to the off state and back. After transferring a part of the circuit to a new project, where there is no such signal, SimInTech shows an error. This error is fixed by creating this signal in a new project. In the SimInTech main menu, select the “Signals” submenu in the “Service” item (see fig. 53)

image
Figure 53. Calling up project signals.

Create a new signal (the “add” button at the bottom of the window), set the name flash_time. Set the mode to “Non-directional” and set the default value to 5 (see fig. 54)

image
Figure 54. Adding a project signal.

Close the dialog box with the “OK” button and repeat the code generation process: in the main menu of the program, select the item “Generate a program” in the “Tools” item.

If you have installed the Visual Studio Community 2015 environment correctly, the corresponding text will appear in the schema message window (see Figure 55):

image
Figure 55. Project message window.
[Error]: “The code generation module is not registered. Maximum allowable number of blocks for the scheme: 200 "
[Error]: "The number of blocks in the project: 56"

The first two lines of messages inform about an error - an unregistered module generating code. In an unregistered form, the module allows you to generate code for circuits whose number of blocks does not exceed 200. Our project contains 56 blocks, so the code is generated, despite error messages.

[Information]: "The source text is stored in c: \ simintech \ demo \ automatic \ State maps - state flow \ Heater controller \ controller_dll.inc"
[Information]: "The status section is saved in c: \ simintech \ demo \ automatic \ State maps - state flow \ Heater controller \ controller_dll_state.inc"
[Information]: "The title is saved in c: \ simintech \ demo \ automatic \ State maps - state flow \ Heater controller \ controller_dll.h"
[Information]: "The title is saved in c: \ simintech \ demo \ automatic \ State maps - state flow \ Heater controller \ controller_dll_init.inc"
[Information]: "Source codes of the program are generated"

The following messages inform the user about the creation of source code files and indicate where they are located. Unless otherwise specified specifically in the code generator settings, the files are saved in the same directory where we saved the project.

[Information]: "The code template is C: \ SimInTech \ bin \ CodeTemplates \ VC2015 \"
[Information]: “Running the build script compile.bat„ c: \ simintech \ demo \ automatic \ State maps - state flow \ Heater controller \ controller_dll “root 192.168.1.1 root”

SimInTech then reports which code template is used (in which directory it is located).
The last line reports that the build script controller_dll is running. If Visual Studio 2015 is installed and configured correctly, then opening the explorer, we will see that all the files are in the same folder as the original project. (see Fig. 56)
image
Figure 56. Files created by the code generator and compiler.

Call created dll from the project


Open the original project (we saved it under the name Heater.prt) and save it under the name Heater_loader.prt We will use this project to test the code created by the SimInTech environment.

Remove the “Heater Controller” block and place in its place the “External DLL” block from the “Substructures” tab (see Fig. 57).

image
Figure 57. Setting up the project for testing dll.

By default, this block does not contain input and output ports. To configure this block, double click on the block and set the following parameters in the block properties (see Figure 58):


image
Figure 58. Setting the properties of the block "External dll"

Close the window by pressing the “OK” button, then on the diagram, the “External DLL” block has ports similar to those of the remote “Heater Controller” block. Connect the circuit as shown in Figure 59.

image
Figure 59. Heater circuit for dll testing.

After that, you can run the project on the calculation and see how the code collected in the dll works.
Earlier we specified the name of the project from which the code was generated in the properties of the “External DLL” block, and now, during the simulation, by double-clicking on the image of the “External DLL” block, a project window will open, from which the code was generated and you can see work code in dll. (cf. Fig. 60).


Figure 60. Display on the dll operation scheme.

Graphs of model calculation are shown in Figure 61. The graphs show that the temperature drops at the moment of shutdown and increases at the moment of heating. It is also seen that the mode of operation corresponds to the model of finite automata created in the first stage. First, 40 seconds, the heater is turned off and the temperature decreases with a cooling rate, then 20 seconds is turned on and the temperature rises with a heating rate. And then the cycle repeats.
But the indicator does not work as planned. The indicator correctly shows the status, but the switching frequency does not change. Obviously, there are problems with the flash_time property of indicator modeling blocks (see fig. 61)

image
Figure 61. Schedules of the heater with the controller from dll.

Features of C code generation in SimInTech


The discrepancy between the work of the code and the original model is due to the features of the code generator. In the SimInTech environment in the simulation mode, any property can be set as a variable and calculated using the scripting language, which we did when creating the state machine controlling the display. During simulation, any change in block properties is possible. The mathematical core of SimInTech takes this into account in the process of sorting blocks and in the process of calculation. The operation of the “State shutdown” block in the SimInTech environment is shown in Figure 62.

image
Figure 62. The operation of the “State shutdown” block in the SimInTech environment.

The ability to change the parameters “on the fly” during the simulation significantly increases the flexibility of the system, allows for the selection of block parameters by means of SimInTech optimization, as well as the adjustment of regulators.

When creating a C code, it is necessary to have a one-to-one correspondence between the inputs and outputs of the block for the given parameters. When generating a C code, the ability to change parameters leads to uncertainty with the formation of a code in which there must be a clear separation between the input values, the properties of the blocks and the result of the calculations of the function of the block. To eliminate this uncertainty, all block properties are converted to constants when generating code. The operation scheme of the “State shutdown” block in the C code is shown in Figure 63.

image
Figure 63. Scheme of operation of the “State shutdown” block in the C code

To change block properties when working in C code, it is necessary to explicitly indicate that the flash_time signal changes during the operation of the code. In this case, for the code generator, this property turns into a normal input and the uncertainty with the properties of the block is removed. The operation scheme of the “State shutter” block will look like that shown in Figure 64.

image
Figure 64. Scheme of operation of the block with variable exposure time in the C code.

Let's fix our controller taking into account the above features of C code generation in SimInTech.

Open the project Controller_ dll.prt, go to the “Heater Controller” submodel by double-click, then go to the off state and remove the “Programming language” block from the diagram. Place the block “Signal list entry” from the “Data” tab on the diagram (see Fig. 65)

image
Figure 65. The “off” status diagram after the repair.

In the properties of the "Signal list" block, specify the signal name - flash_time. (see fig. 66)

image
Figure 66. Properties of the “Signal List” block

Repeat this procedure for status enabled. You can copy two blocks “Constant” and “Record to the list of signals” and paste them into the status block enabled. At the entrance to the unit, apply the constant 1 (the switching time in the heating mode). The diagram of the state block included should look as shown in Figure 67.

image
Figure 67. Scheme of work in the enabled state.

Thus, we explicitly stated that we want to change the signal from the state diagram during code generation.

Let us proceed to the configuration of the display state machine. Enter the submodel of the automatic display, and then - in the off state. Delete the “State Shutdown” block and set the “Variable State Shutter” block in its place from the “Automata” tab.

Also place on the diagram the block “Reading from the list of signals” from the “Data” tab. Set flash_time as the signal name. The diagram should look like the one shown in Figure 68.

image
Figure 68. The status diagram is turned on in the timer machine.

Repeat the same for the off state. The diagram should look like the one shown in Figure 69.

image
Figure 69. The status diagram is turned off.

Thus, we explicitly indicate to the code generator that we want to change the value of the exposure time in the state (indicator flashing interval). Save the project.

Repeat the code generation process. In the main menu of the program, select the item “Generate a program” in the item “Tools”. If you have done everything according to the instructions above, then you will have a dll generated from C code.

Open the project Bootable_Driver_DJ. Run the project on the calculation and make sure that the automatic display works correctly and changes the display frequency depending on the mode. (cf. Fig. 70)

image
Figure 70. Heater operation with a controller from dll.

Joint debugging in Visual Studio 2015 and SimInTech


By default, the SimInTech environment is configured in such a way that the process of creating programs from a circuit is fully automated. The user does not need to edit the text of the C program. The user receives the program from the circuit, ready to be loaded into the controller, or in the form of a dll. The example above shows how to create programs without editing the code.

However, there are situations when it is necessary to simultaneously debug C code and use SimInTech to test this code. Next, we will show how to configure Visual Studio 2015 to manually edit the C code with testing in the SimInTech environment.

To automate code generation and program creation, SimInTech uses pre-configured templates for different compilers and development environments. In our example, we used the Visual Studio 2015 template, which is located in the folder:
C: \ SimInTech \ bin \ CodeTemplates \ VC2015

This folder contains the command launch files that provide a fully automated build of libraries. When generating code, files with code are placed both in the project directory (or another one specified by the user during setup) and in the template directory. The names in the project folder correspond to the program name in the generation code settings. The names of the files in the template folder are fixed, and the files are overwritten when generating different projects in SimInTech.

The correspondence between the files in the template package and the project folder for the heater controller is given in the following table:

image

To debug and manually edit the source code, you need to move the folder C: \ SimInTech \ bin \ CodeTemplates \ VC2015 \ src to the working directory of the project. For our example, do the following:

  1. Copy C: \ SimInTech \ bin \ CodeTemplates \ VC2015 \ src to the folder where the Heater Controller project is located (I have C: \ SimInTech \ Demo \ Heater Controller).

  2. Open the Visual Studio 2015 project file, example_cpp.vcxproj, located in the C: \ SimInTech \ Demo \ Heater Controller \ src directory in Visual Studio.

  3. In the project settings, specify the paths to the working directories. To do this: in the “Solution Explorer” window, select the file example_cpp and click the “Properties” button. In the settings dialog box, specify the working directory where the project with the dll call is located as the “Output Directory”. (In our example, the working directory is at a level above the directory with source files). The name of the created dll “Target Name” is controller_dll, the extension of the file being created is dll. (see fig. 71)


    Figure 71. Setting up working debug directories

  4. Go to the “Debugging” section and configure the call to the SimInTech environment:
    • "Command" - C: \ SimInTech \ bin \ mmain.exe - the name of the executable file of the program.
    • “Command Arguments” - “C: \ SimInTech \ Demo \ Heater Controller \ Load_heater_D_Leader.prt” is the name of the project with a call to the dll being debugged.
    • “Working Directory” - C: \ SimInTech \ Demo \ Heater Controller \ working directory in which we have the project and the dll being debugged (see fig. 72)


Figure 72. Setting up the SimInTech environment for debugging dll.

Set a breakpoint at the entrance to the main program execution procedure RUN_FUNC and start local debugging by clicking the corresponding button in Visual Studio 2015 (see Figure 73).


Figure 73. Stop point in the main program body

If all the settings are correct, then when you start local debugging, SimInTech will be launched and the Heater_loader_dll.prt project will be opened in it.

After running the simulation of this project in the SimInTech environment, the first step will be caused by the debugged controller_dll.dll and control will pass to Visual Studio 2015 at the breakpoint.

The created project configuration and settings of the working directory allows you to manually change the C code in the Visual Studio 2015 environment and perform step-by-step debugging of the code together with the model in the SimInTech environment. The main text of the program is in the file prog.inc (see Fig. 73)

To adjust the syntax highlighting of a file with the inc extension in Visual Studio, select Tools / Options .. in the main menu, in the dialog box in the Text Editor / File Extention section, add the inc extension.


Figure 74. Configuring the file extension.

To search for parts of the C code associated with the blocks of the design scheme, follow these steps:

  1. Select a block in the diagram.
  2. Call the window for editing block properties (double click).
  3. On the General tab, find the value of the unique identifier (UID).
  4. In the Visual Studio window, the search will find the text UID = ”N”, where N is the number of the unique identifier of the block. (See Fig. 75)


Figure 75. Search for C code by block index in the scheme.

The latest version of SimInTech can be requested here ...
Archive with project files can be located here ...

Source: https://habr.com/ru/post/309296/


All Articles