PLC100 + LabVIEW + joystick

Once on my desktop there was a usb-joystick and a PLC (programmable logic controller) of the Aries company - PLC100, while the LabVIEW environment was running on the computer. I thought that all this - at least for fun - can be combined by organizing the control of the PLC (its outputs) using the joystick buttons (later I decided to use not just the buttons, but their combinations - DOWN, FORWARD, Y, for example).

So how does it all work. LabVIEW processes joystick keystrokes and transfers information about the keystroke to the PLC100 (in this case, through the serial port); PLC100, in accordance with the program loaded into it, responds to certain key combinations by turning on / off its outputs.

image

LabVIEW Part


A program that processes joystick button presses and transfers information about them to the PLC100 looks like this in general:

image

Now, in more detail.
1 - Initialization of the joystick. You can find it in the Function Palette under Connectivity-> Input Device Control. If one joystick is connected to the computer, as in my case, it is recommended that the Device Index parameter be set to 0.
2 - Receive data from the input device. Data on the buttons and axes (cross) are stored in clusters, which, using the Unbundle function, need to be divided into elements in order to process them later. The “button” cluster 3 (in the figure it is already divided into elements) consists of Boolean elements; when the button is pressed, the corresponding cluster member takes the value TRUE. With the “axial” cluster 4, the situation is slightly different: it consists of integer elements, and when the UP key is pressed, the first cluster element - X axis - takes the value -32768, the DOWN key corresponds to 32767. With the second cluster element - Y axis - everything is approximately also: BACK = -32768, FORWARD = 32767.
5 - Formation of a line. The Format Into String function forms (or returns) a string of elements of different data types. In my case, these are Boolean elements and there are only 8 of them, so the output will be a string of 8 characters. An explanation is required here: if no button is pressed, the line looks like “00000000”; if, for example, button 1 is pressed, then the line will be “1000000”, if button 2, then “01000000”, etc. Depending on the value of this string, a certain number will be written to the serial port, which the PLC100 will already process. By the way, the “format string” parameter of the Format Into String function is set as “% d% d% d% d% d% d% d% d” to convert the input Boolean FALSE and TRUE to zeros and ones and finally get a string of the form for example, "10000000" and not "TRUEFALSEFALSEFALSE ...". Like this. Could it be easier? You can, but another time.
6 - Configuring and opening the serial port. I left all the default options, adding only a control to select the port number. To work with the COM port, NI-VISA must be installed (here www.ni.com/download/ni-visa-5.4.1/4626/en ).
7 - Case structure. Depending on the value of the string formed by the Format Into String function, it writes a number to the serial port (11, for example, corresponds to button 1, 12 to button 2, etc.). This number is preliminarily converted by the Number To Decimal String function - in the figure, the figure is 8 (without this block, the PLC100 for some unknown reason read some crooks from the port).

PLC program


A bit about the PLC100 ports. As many as three connectors can work as a COM port: these are Debug-232 on the front panel, RS-232 and RS-485 at the bottom. When opening the port, you will need to indicate its number: for Debug-232 it is 4, for RS-232 - 1, for RS-485 - 0. I used RS-485, connecting the PLC to the computer via a cheap Chinese adapter RS-485-> USB (in the photo), respectively, in the program, the port number is 0. The



PLC100 is programmed in Codesys 2.3. In addition, you will need the so-called target - something like a file with a description of the configuration of a programmable device. To initialize and open the port, I used the ComService.lib library (the library and an example for it can be downloaded here www.owen.ru/forum/showthread.php?t=551&page=6) I combined the configuration and opening operations of the port in one function block - see the figure below.



It can be seen that the 'set' object is declared as COMSETTINGS. This is an instance of a COMSETTINGS structure containing port settings; it is already declared in the ComService library (to be precise, in the SysComLib library, which is part of ComService). The settings are fairly standard, you just need to turn to set.Port = 0 - this is RS-485, which was mentioned above.

The COM_SERVICE function block has two parameters: the first is the name of the COMSETTINGS structure, the second is the type of operation, where 0 corresponds to setting and opening.

Next, using the SysComRead function, which is part of the SysLibCom library, we read the data from the port. The dwHandle parameter is the port number (RS-485 in this case), dwBufferAddress is a pointer to the variable where the data will be read, dwBytesToRead is the number of bytes to read (in my case it is 2), dwTimeout is the time in ms after which the function must complete; if you put too little, the function simply will not read anything.



Next is the conversion of the line read from the port into an integer and a comparison of this number: if it, for example, is 10, then no button is pressed, if 11 is pressed X, etc.

To process the key combinations, I programmed 6 function blocks - 3 to enable the first three PLC relay outputs (I think three outputs will be enough for a start) and 3 to turn them off; four combinations of 3 keys and two of 4. Inside, these blocks look like this:



i1, i2, i3 are the first, second and third buttons of the combination, respectively. The function block for a four-button combination is not very different, and getting it from a three-button combination is a technical matter, so I omit its contents.
It should be added that for each function block I declared my instances of R_TRIG, TON, and TOF blocks, because all three outputs would react to one combination. But for the on and off blocks of the same output, I left them the same.

As a result, the program looked like this (I miss the place with the port open):



The video shows how the LEDs on the PLC light up, indicating that the output is active. For clarity, I would like to connect at least a light bulb or a starter to the outputs, but at the time of recording the video, there was only a computer cooler at hand.



I must say that the whole system (I say “system” because I don’t know where the “traffic jam” was) was rather reluctant, if any, to respond to quick presses; unhurried clicks (like in the video) improve the situation a little, but still sometimes there was no reaction to them. But the PLC100 also has an Ethernet connector. So next time I’ll try to make at least a slight improvement by linking the PLC to LabVIEW through an OPC server over Ethernet.

Thanks for attention.

Also popular now: