This post is a primer on how to get started with using the Camera in CODESYS on the Raspberry Pi. The content of the post is taken from a mix of the CODESYS documentation and from reverse engineering sample projects.
Configuring the Raspberry Pi
Plug the Camera in to the connector labelled CAMERA on the Raspberry Pi.
Connect using SSH and run sudo raspi-config to enable the camera, then reboot
Now we need to install camera interface, these instructions are taken from the CODESYS documentation, which appears to have got them from http://elinux.org/RPi-Cam-Web-Interface
At this point I rebooted again.
Clone the code from github and run the installer with the following commands:
When running the installer you get prompted for th name of the install folder – I accepted the default. You also get prompted for the Port to use, I stayed with the default port 80. I chose not to enable web server security. I then rebooted.
Then I browsed to http://192.168.0.38 (the IP address of my RPi) to verify that everything was working.
Capturing a JPG image
Create a new CODESYS project (standard project) selecting the target device as CODESYS Control for Raspberry Pi. Beneath the Camera device right click over the <Empty> device, select plug device
and then select Raspberry Pi Camera, close the dialog.
You should end up with a Camera in the devices tree like this.
You also have a new variable in IoConfig_Globals called Raspberry_Pi_Camera of type Camera.PiCamera. The methods on this function block can be used to save a picture, or video. For documentation check out the online help in CODESYS.
At a minimum you need to specify the name of an output file, the sample below can be driven using the CODESYS debugger, set xTakeStill TRUE and a picture will be saved.
NOTE: On my system when I originally ran this udiResult was set to 70, and no picture was taken. I had to stop the Camera in the RPi Cam Control Application as warned in the CODESYS documentation, by browsing to the IP address of my Pi and selecting Stop Camera
Once I had stopped the Camera the function returned 0 (Errors.ERR_OK), and the picture was taken. Using the device file browser I was able to locate the Picture at the root of the CODESYS file system on my RPi.
NOTE: The function call Raspberry_Pi_Camera.Still takes a long time to execute, by default around 5 seconds. During this time the Program is hung. If you need to make use of this code I strongly suggest having a dedicated, low priority task to execute a program dedicated to taking the picture. I experimented with a timeout of 0, and the program hung for longer than I was prepared to wait. A timeout of 1 still took a long time to complete.