PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter PiXtend with CODESYS – Digital-Analog-Converter Creating a new CODESYS Project Adding PiXtend DAC as a CODESYS Device Creating a simple DAC Test Program Creating a simple Web Visualization Date 18/08/2016, V1.01 Qube Solutions UG (haftungsbeschränkt) Arbachtalstr. 6, 72800 Eningen, GERMANY http://www.qube-solutions.de/ http://www.pixtend.de www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 1 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter Table of Contents 1. Introduction........................................................................................................................3 1.1 General Notes..............................................................................................................4 1.1.1 Copyright of Text and Pictures:............................................................................4 1.1.2 Warning................................................................................................................4 1.1.3 Applications for PiXtend.......................................................................................4 1.2 Disclaimer....................................................................................................................4 2. Prerequisites......................................................................................................................5 3 Step by Step to the first PiXtend DAC Program.................................................................6 3.1 Create a CODESYS standard project for PiXtend..................................................6 3.2 Add SPI Device........................................................................................................8 3.3 Add „PiXtend_DAC“ device...................................................................................11 3.4 Create Global Variable List....................................................................................12 3.5 Mapping of Variables.............................................................................................14 3.6 Task Configuration.................................................................................................16 3.7 Creating the Main Program...................................................................................16 3.8 Connecting to the PiXtend and performing a program download.........................17 4 Creating the CODESYS Webvisu.....................................................................................18 www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 2 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 1. Introduction This manual describes all necessary steps to create a new PiXtend DAC project (www.pixtend.de) in CODESYS (www.codesys.de). You will learn how to use the PiXtend Digital-Analog-Converter as a CODESYS device. With the „PiXtend_DAC“ device you can control both analog outputs on the PiXtend board. The Application Note PiXtend with CODESYS – Create Project describes how the rest of the PiXtend's inputs and outputs can be used. The following three main software components will be used: CODESYS is the leading hardware-independent IEC 61131-3 automation software for developing and engineering controller applications. It is developed and maintained by the company 3S-Smart Software Solutions in Kempten, GERMANY To program a Raspberry Pi using CODESYS, a special Raspberry Runtime Extension for CODESYS is required. The Raspberry Pi Runtime Extension is also developed by the company 3SSmart Software Solutions in Kempten, GERMANY In order to gain direct access to the IO Hardware and interfaces on the PiXtend Board from CODESYS, the PiXtend for CODESYS device drivers are required. The device drivers are provided by the company Qube-Solutions UG (haftungsbeschränkt), Eningen, GERMANY This manual assumes that the three Software components have already been installed correctly. If you have not installed them yet, please refer to the Application Note PiXtend with CODESYS – Installation on how to do so. www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 3 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 1.1 General Notes 1.1.1 Copyright of Text and Pictures: Text and pictures marked with "(3S)" belong to 3S-Smart Systems GmbH in Kempten, Germany – www.codesys.com Text and pictures marked with "(RPI)" belong to the Raspberry Pi Foundation – www.raspberrypi.org Text and pictures which are not marked, or marked with "(QS)" belong to Qube Solutions UG in Eningen, Germany – www.qube-solutions.de 1.1.2 Warning PiXtend must not be used in safety critical systems. Check prior to use the suitability of Raspberry Pi and PiXtend for your application and environment. 1.1.3 Applications for PiXtend The PiXtend can be used for private and commercial projects, such as: • Home Automation, Smart Home • Evaluation of sub-systems, Proof of Concept, pre-series, series • Learning and Teaching platform for control systems and automation • Learning and Teaching platform for microcontroller hard- and software techniques • HAM-Radio, DIY and Maker Applications 1.2 Disclaimer Qube Solutions UG and 3S-Smart Software Solutions can not be held responsible for any potential harm or damage to software, hardware, equipment or connected components, caused by the usage of provided software, drivers or hardware products. www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 4 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 2. Prerequisites This manual assumes that you have read the Application Note PiXtend with CODESYS – Installation and that the three components CODESYS, CODESYS Control for Raspberry Pi, and PiXtend for CODESYS have been successfully installed. For hard- and software requirements please refer to the Application Note PiXtend with CODESYS – Installation www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 5 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 3 Step by Step to the first PiXtend DAC Program 3.1 Create a CODESYS standard project for PiXtend Start the CODESYS Environment. Create a new Project by clicking on Main Menu → File → New Project (Shortcut Ctrl-N). Select "Standard project" from the category "Projects", choose a project name and location (here "D:\Projects\PiXtendTestDAC") and press „OK“. Select "CODESYS Control for Raspberry Pi" as the device, and select "Continuous Function Chart (CFC)" as the program language for the main program PLC_PRG. www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 6 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter You will now see blank CODESYS standard project with the following structure: www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 7 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 3.2 Add SPI Device In the project tree, right click on the „SPI“ item and select „Add Device“ www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 8 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter Select "SPI master" and click the Button "Add Device". In the project tree a new SPI sub entry was created, named "SPI_master (SPI Master)". www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 9 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter As the PiXtend_DAC is connected to the Raspberry Pi via SPI-Port 0.1, we will have to adapt the SPI master's parameters. Double-click on the item „SPI_master“ and change the „SPIport“ Parameter to „/dev/spidev0.1“: NOTE: If you want to use the “PiXtend” device as well as the “PiXtend DAC” device in the same project, make sure that each device is added to a separate SPI Master (see also Demo Project). “PiXtend” always uses /dev/spidev0.0 and “PiXtend DAC” always uses /dev/spidev0.1 www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 10 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 3.3 Add „PiXtend_DAC“ device Select the „SPI_master“ item in the project tree and add another device. (right-click → Add Device). In the vendor drop-down please select the entry „Qube Solutions UG", and select the Device "PiXtend DAC" (not PiXtend). Click on the „Add Device“ button and finally the „Close“ button. Now you can see that the PiXtend DAC appears as a device under "SPI_master (SPI Master)". www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 11 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter A double-click onto „PiXtend_DAC“ in the device tree will open the configuration section for the device. In the tab „SPI devices I/O Mapping“ you can see the two analog outputs of the PiXtend DAC that are accessible from CODESYS. In order for the process image being exchanged periodically, you will have to make two changes here: Please select "Enabled 1 (use bus cycle task if not used in any task)“ and for the bus cycle task select „Main Task“. At a later point when you are „online“ to the controller, you could come back to this tab to directly force output values for testing purposes. As we want to access the two analog outputs from our main program and/or the visualization, in the next step we are going to create a Global Variable List (GVL) to be able to assign global variables to the outputs. 3.4 Create Global Variable List In the project tree perform a right-click on "Application", click „Add Object“ and add a new Global Variable List with the name "GVL". www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 12 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter Now open the GVL and add the following code: //Analog Outputs rAOut0: REAL; rAOut1: REAL; www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 13 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 3.5 Mapping of Variables Now that we have created the required variables in the GVL, we will have to map them, which in this case means assigning them to their respective PiXtend DAC outputs. Therefore please open the already familiar tab "SPI devices I/O mapping" by double clicking the PiXtend DAC device. Expand the "Analog Out" section and assign the two variables. Double-click into an empty row and three dots (…) will appear. Clicking on the three dots will open the input assistant, where we can now select the desired variables: Please select variable Application.GVL.rAOut0 for AnOut0 Please select variable Application.GVL.rAOut1 for AnOut1 www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 14 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter Finally the GPIO24 Bit of the Raspberry Pi has to be configured as an output. Open the Raspberry Pi GPIO configuration tab by double-clicking onto „GPIOs_A_B“ in the project tree. On the „GPIO Parameters“ tab change to „Output“ for GPIO24. After that please switch to the tab „GPIOs I/O Mapping“ and assign „rpi_gpio24“ for Output Bit24. www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 15 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 3.6 Task Configuration If desired you can change the cycle time of the Main Task. To do so please click on Task Configuration → MainTask. With the „PiXtend_DAC“ (Version >= 1.0.0.2) it is possible to reduce the cycle time down to t#1ms. For this project the default cycle time of 20 ms is fast enough. 3.7 Creating the Main Program Double Click „PLC_PRG“ in the project tree to bring up the editor. For the main program "PLC_PRG" we already chose to use "Continuous Function Chart" when we created the CODESYS Project at the beginning of this manual. CFC (Continuous Function Chart) is a graphical programming language. Please drag a „Input“ Block from the toolbox (right) and drop it in the workspace. Do the same with an „Output“ block and connect both. Click in the center of a block to assign a variable or constant, as shown below. A click on the three dots (…) will bring up the Input Assistant. You can either type in the variable name, use auto completion (Ctrl-Spacebar), or select from Input Assistant. This is it. To build the project please click Build → Build in the main menu bar. www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 16 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 3.8 Connecting to the PiXtend and performing a program download Once the program compiles without errors, double-click on "Device (CODESYS Control for Raspberry)" in the project tree and then click the „Scan Network“ button. CODESYS now searches the local network for a Raspberry Pi with the CODESYS Raspberry Pi Runtime on it. If your device does not appear here, please check the following things: • Are you using the correct SD Card Image which has the CODESYS Runtime Extension installed on it? You can download a preconfigured PiXtend CODESYS Image from our download section (www.pixtend.de/downloads). To create your own CODESYS Image please refer to the manual included in the „CODESYS Control for Raspberry Pi“ package provided by 3S. • Is the Raspberry Pi powered on? Did it obtain a valid IP, which you can ping from your local machine? (via ping from the windows command line) • If you don't know the IP, you can use the command „ifconfig“ from the Raspberry Pi command line. • If you can see a valid IP here, but you are not able to ping it, check your network connections to the Raspberry Pi. • Notice: if the Raspberry Pi has been powered up for more than 2 hours, the CODESYS Runtime Extension stops working, unless you have licensed it. After a reboot (sudo shutdown -r now) it will work for another 2 hours. If the Raspberry Pi appears in the list, please select it and click „Online → Login“ in the main menu bar to go online and perform a download of the program. www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 17 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter 4 Creating the CODESYS Webvisu Now we want to add a simple visualization for our project, so that we can control the analog outputs with two sliders from any PC or smartphone / tablet on the network. To add a visualization, right-click on „Application → Add Object → Visualization“ in the project tree. CODESYS now automatically adds the "Visualization-Manager" and a new "Visualization" Object. The manager can be used to change visualization properties, like the default visualization, update rate, URL, etc.. Change the „Scaling options“ to „Isotropic“: www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 18 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter To open the visualization editor, please double-click the "Visualization" object in the project tree. CODESYS comes with a bunch of predefined controls which we can be used out of the box. Open the toolbox, select „Common controls“ and drag 2 sliders into the workspace. Add 2 labels and assign the respective variable name to the sliders „Variable“ property (GVL.rAOut0, GVLrAOut1). Change the property „Scale end“ for both sliders to „10“: www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 19 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter Add two text fields from the toolbox and enter „%2.1f“ for the "Text" property. This will create a placeholder for a REAL Variable with two digits before and one after the decimal point. Assign the value „GVL.rAOut0“ to the „Text variable“ property of the first slider and „GVL.rAOut1“ to the second. Finally we add a caption and an animated CODESYS Logo („Waiting cube symbol“ from the „Special controls“ tab) and arrange the elements nicely. www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 20 / 21 PiXtend Application-Note: PiXtend with CODESYS – Digital-Analog-Converter Compile the project (Build->build) and perform a full download. Press „F5“ to enter RUN Mode. Open a browser of your choice on your PC, smartphone or tablet, and enter the IP of your Raspberry Pi, followed by „:8080/webvisu.htm“ i.e. http://192.168.1.100:8080/webvisu.htm Now you can move the sliders to control the Output Voltage of both channels from 0-10 Volts. We hope you have fun using the PiXtend with CODESYS and are successful with your projects. We are always interested in your feedback. If you use PiXtend in one of your projects please mention PiXtend and our URL www.pixtend.de somewhere. www.pixtend.de Copyright by Qube Solutions UG (haftungsbeschränkt) 21 / 21