Using Python to Plot Variables
Using Python to Plot Variables
This example demonstrates how to connect and get values from CDP using Python in CDP Studio, the independent automation software for open PC-based real-time distributed control systems with NoCode setup within the IDE.
The example launches Python script as a standalone program, that connects to CDP application, retrieves Sine output signal values and plots them using Matplotlib module.
Project Overview
The project consists of a very simple CDP application with Sine component added. The project folder also contains a simple Python script named sineplot.py. This script will connect to CDP via StudioAPI protocol, find Sine Output signal and subscribe to its change events. After that, every Sine Output signal change will be detected by the script. The received Sine values will be collected into a an array and plotted using Matplotlib module.
The goal of the example is to demonstrate how easy it is to connect and receive signal value change notifications from CDP using Python cdp-client module. You just have to initialize cdp-client with the network location of the CDP application (host and port), call find_node to connect to the signal and create and hook a function that is called every time when signal value is changed.
Note: This simple Python script is resistant to CDP application restarts or connection failures. While running, it will try automatically to reconnect and will continue receiving and plotting Sine values once reconnected.
How to Run the Example
To run the example from CDP Studio, open Welcome mode and find it under Examples. Next, in Configure mode right-click on the system project and select Run & Connect. See the Running the Example Project tutorial for more information.
PythonRunner Component
To be able to run this example on your computer without any preparation or manual work the Python script is launched automatically on CDP application startup. To achieve this, a separate component named PythonRunner is added into the application. PythonRunner is actually a CDP ExternalControlBroker component, that uses LocalCommandRun request to launch the example Python script as separate process.
How to Prepare to Run the Example in Your Python Environment
This example uses the Python environment bundled with CDP Studio to run the example.
However, the recommended way to run Python scripts is to run them separately from Studio. For that you have to:
- verify that Python interpreter is installed in your computer and install it, when missing.
- install Python CDP client by entering the following command in your computer command prompt
pip install cdp-client
- install Python matplotlib graphics plotting module by entering command in your computer command prompt
pip install matplotlib
- locate the Python script from project folder, for example by typing sineplot.py into the locator box on the lower left area of the CDP Studio window
- copy the file that opened in Code mode editor via copy pasting its text or via menu File->Save "sineplot.py" As...
- start the CDP application again and look in the Application Output pane for a line like
10:40:29.700 StudioAPIServer: listening on 127.0.0.1:7689
- set the IP and port of the StudioAPIServer which the copied script reads from the environment variable StudioAPIURI and run the script, by executing following commands.
Windows:
set StudioAPIURI=127.0.0.1:7689 python sineplot.py
Linux:
export StudioAPIURI=127.0.0.1:7689 python sineplot.py
Troubleshooting
What to do when this example fails to start with errors on line "import matplotlib.pyplot as plt
" ?
When the Python scripts fails to start and CDP application output window contains error lines like these:
DEBUG: PythonRunner: RunSinePlot: Received command output 'Traceback (most recent call last): File "sineplot.py", line 3, in <module> import matplotlib.pyplot as plt File "/opt/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module> _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup() ...
then it can be, that the graphical toolkit package tk that is required by the Python matplotlib module is missing.
On a Linux system this can usually be installed with the package manager, for example on Ubuntu or Debian based systems run
sudo apt-get install tk
and try to run the example again.
What to do when command "pip install cdp-client
" fails?
Some systems might not include setuptools and wheel packages by default. Run
pip install setuptools wheel
and try to install CDP client again.
What to do when command "pip install matplotlib
" fails?
The matplotlib requires a C compiler and Python development headers.
On a Linux system they can usually be installed with the package manager, for example on Ubuntu or Debian based systems run
sudo apt-get install build-essential python-dev
and try to install matplotlib again.
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.