Using PiFace Digital 2 with CODESYS

In this post I look at how to drive the PiFace Digital 2 card with CODESYS. The post is aimed at CODESYS beginners.

The PiFace Digital 2 is provides 8 open collector digital outputs with LED indicators (two of which also drive changeover relays) and 8 digital inputs, four of the inputs are in parallel with on board switches. Fundamentally the guts of the board is a MCP23S17 16 bit I/O expander, so we will need to configure appropriate SPI devices in CODESYS.

First create a new project in CODESYS with ST as the selected language. The PiFace Digital 2 module is based around an SPI latch, so we need to configure the required SPI devices. In the Devices pane right click on the SPI device and select Add Device…

This brings up the Add Device dialog, select SPI master, and click the Add Device button.

Then in the Devices pane select the newly added SPI_master device (do this without closing the Add Device dialog). The Add Device dialog will update to show SPI slave devices that can be added, select the device named PiFace IO driver, click the Add Device button and then close the dialog.
It is possible to connect up to four of these boards to a single Raspberry Pi, to identify individual boards the address is set with a pair of links. If the board is in its default configuration then we should have nothing to do, but we should check, so inspect where JP1 and JP2 are connected, the table below shows what address this represents.

JP1 1-2
JP1 2-3
JP2 1-2
0
1
JP2 2-3
2
3

Open the PiFace_IO_driver device in CODESYS and verify that the Hardware Address parameter matches the value from the table.

We are now all set to test that our PiFace Digital board is working in CODESYS. The easiest way to do this is to create a simple program to copy the Inputs to the Outputs.

The program just copies one VAR of type BYTE to another, to use the actual inputs and outputs change we need to map these VARs onto the IO. To do this use the mapping tab (SPI devices I/O mapping) in the PiFace_IO_driver pane. Double click in the Variable column, and either type in the fully qualified variable name, or click on the elipsis and use the Input Assistant to find the variable.
If you now go online and download this application to your Raspberry PI you should be able to change the state of the outputs by triggering the inputs on the PiFace Digital. You can test the least significant four bits using the switches on the board.

CODESYS with Edimax EW-7811UN on Raspberry PI

Up to this point I have been using wired ethernet, tonight I decided to try with a wireless adapter.

First I followed the instructions here to get the Raspberry PI on my home network. https://www.raspberrypi.org/documentation/configuration/wireless/

Then I tried scanning for the device from CODESYS with no luck. I couldn’t ping the Raspberry PI from my PC, until I pinged the PC from the Raspberry PI. I suspected the Green power saving features of the Edimax adapter.

A quick google found https://www.raspberrypi.org/forums/viewtopic.php?t=61665 I followed the instructions, and created a file /etc/modprobe.d/8192cu.conf with the following content

# Disable power management
options 8192cu rtw_power_mgnt=0 rtw_enusbss=0

and then rebooted.

Having rebooted a Scan Network… from CODESYS found the device.

Getting started with CODESYS visualisation on the Raspberry Pi

This post is intended for those who have never generated any graphics using CODESYS.
CODESYS has a built in fully integrated visualization system. 
I have started by creating a very simple new CODESYS program using ST as the programming language.
Next step is to add a blank visualization as shown below
I name the new visualization Demo.
Adding the Demo visualisation object actually causes three objects to be added to the application.

Before progressing to actually create the visualisation, let’s have a quick look at each of these objects.

Visualization Manager

The Visualization Manager holds global settings such as the initial language, User Management, Hot Keys and language specific fonts. The Visualisation Manager provides a home to add visualization devices underneath. CODEYS supports a number of different visualization device types, the Raspberry Pi adaptation of CODESYS only supports WebVisualization and CODESYS HMI, we will only concern ourselves with WebVisualization in this post.

Web Visualization

The WebVisualization object adds a web server to the application. It also specifies which Visualisation to show when a browser first connects to a specified URL. The defaults are shown below.
Scaling Options are to do with how the visualisation is changed if the size of the window showing them changes. I personally find that the visualisations look best with a Fixed scaling. As I will be testing the visualisations using computers with keyboards and not touchscreen I also change the Default test input to Keyboard.
NOTE: It is possible to have multiple Web Visualization objects each with distinct URLs that can be mapped to different Visualizations. It is equally possible to have multiple Visualizations and access them all through a single URL.

Vizualisation (Demo)

In simplistic terms this object represents a ‘Page’ on which we can add Visualization Elements. These Elements can range from simple lines and rectangles to complex objects such as gauges, alarm tables etc. Visualizations can also be embedded in a Frame element on another Visualisation, or even used to define custom keyboards or dialogs.

Demo Visualization

To create our simple application we will add a Meter to out Demo Visualization and get it to indicate the value of the VAR a in the simple program. Double click on the Demo object in the Devices tree to open up the Visualization. Find the Meter Element under Measurement Controls in the Toolbox.
Then Left Click and drag a Meter from the Toolbox onto the top left of the Visualisation area.
At tis point we have a Visualisation including a Meter element, but it is not associated with any variable in our application, so next we must associate a variable from the program with the Meter. In the Properties box with the Meter still selected click in the Value column against the Value Property, click again and then click the ellipsis. 
This will bring up the Input Assistant, select Application->PLC_PRG->a and click OK.
That’s it, we should now be able to go online and verify that our application runs and the visualisation is working. So select Online->Login from the main CODESYS menu, and Login with download. At this pint the Demo Visualization shows as message stating The online visualization is waiting for a connection. Please start the application. Start the application running (Debug->Start, or F5) and the Meter should start to move rapidly.
We should also be able to see the same in a web browser. To do this you need to know the IP address of the Raspberry Pi that you are using. If you can see the console it may have a message that stays something like My IP address is 192.168.0.34, or in CODESYS open the Device object, open the log tab, click the refresh button (shown below), scroll down through the log messages and the IP address will be shown.
or on the Raspberry Pi console type ifconfig, the ip address is shown as inet addr:
The URL to access the Web Visualisation is http://192.168.0.34:8080/webvisu.htm, the captures below show how the meter looks in Internet Explorer and on the browser on the Raspberry Pi.


Starting with CODESYS on the Raspberry Pi

NOTE THIS POST IS OUT OF DATE. Please see starting-with-codesys-on-raspberry-pi

I’ve finally decided to give CODESYS a try on the Raspberry PI. This post tracks what I had to do to get CODESYS functioning, I’ll post more on sample projects in the future.

First of all, my setup: I’m running a fresh install of Windows 8.1 x64 in a virtual machine hosted on vmware Fusion on a Macbook Pro. I have a Raspberry Pi 2 connected by ethernet on my local network, the virtual machine is bridged so that it is on the same subnet as the Raspberry Pi. I am using an 8Gb SanDisk Ultra microSDHC card.

Installing CODESYS on Windows

I downloaded CODESYS V3.5 SP6 Patch 3 from CODESYS download area (You need to register, but then the download is free). I then ran the installer accepting all defaults.

CODESYS on the Raspberry PI

CODESYS for the Raspberry PI is available as a free download from the CODEYS store, again you have to register to get access, and again this is free. I downloaded  CODESYS_Control_for_Raspberry_PI.zip, and unzipped it to give me a folder with the following content.

Installation basically consists of two parts, firstly updating the CODESYS IDE with a package to add support for the Raspberry Pi, and secondly loading a runtime image onto an SDCard. 

Installing the package

I launched CODESYS V3.5 SP6 Patch 3 from the link on the windows desktop, selected the Standard environment when prompted. I then launched the Package Manager from Tools/PackageManager… I then selected Install… and browsed to the CODESYS_Control_for_Raspberry_PI package,

I then accepted the license agreement and all defaults

(Take a note of the target directory), Allow UAC to make changes.
I clicked Finish

Then clicked Close before exiting CODESYS.

Loading the Image

Following the advice in the product data sheet I downloaded and installed Win32diskimager 0.9.5 from http://sourceforge.net/projects/win32diskimager/. I plugged the microSDHC card into an SDCard carrier and then plugged this into a USB SDCard reader (The internal SDXC slot on my MacBook is not recognised by vmware Fusion, see vmware kb1035825). I then launched Win32DiskImager, and browsed to codesysrasp35620.img in the directory I had previously unzipped, selected device G: which corresponds to the SDCard, and clicked Write.

Programming the card took a few minutes, I then clicked Exit. Using Windows Explorer I was able to see the content of the SDCard,

still using Windows Explorer I right clicked on the device and selected Eject, before physically removing the card from the reader.

I then plugged the card into my Raspberry Pi and powered it on. Notice in the startup messages
codesyscontrol started
At the login prompt I entered the default credentials, username pi, password raspberry.
I chose at this point to expand the filesystem to fill the available space on the card, so at the command prompt
sudo raspi-config


I selected the option to Expand Filesystem, then tabbed to Finish and rebooted.

Testing the Installation

I launched CODESYS V3.5 SP6 Patch 3 and then from the Start Page selected New Project…

I selected the default type of a Standard project gave it a name and clicked OK.

I then selected the Device and programming language I wished to use.
This created a project with this content below

I then double clicked the Device (CODESYS Control for Raspberry Pi) which brought up the Communications Settings tab for the device.

I selected Scan Network, this found the device, which I selected and then clicked OK.

Now we are connected to the Raspberry Pi.