|
1 | 1 | Creating an IOC |
2 | 2 | =============== |
3 | 3 |
|
4 | | -THIS NEEDS UPDATING |
5 | | - |
6 | | -Using ``pythonSoftIoc`` |
7 | | ------------------------ |
8 | | - |
9 | | -Probably the best way to use ``pythonSoftIoc`` is to start by copying fragments |
10 | | -of a simple example such as ``CS-DI-IOC-02``. This consists of the following |
11 | | -elements: |
12 | | - |
13 | | -1. A startup shell script ``start-ioc`` which launches the soft IOC using a |
14 | | - production build of ``pythonSoftIoc``. This script typically looks like |
15 | | - this:: |
16 | | - |
17 | | - #!/bin/sh |
18 | | - |
19 | | - PYIOC=/path/to/pythonSoftIoc/pythonIoc |
20 | | - |
21 | | - cd "$(dirname "$0")" |
22 | | - exec $PYIOC start_ioc.py "$@" |
23 | | - |
24 | | -2. The startup Python script. This establishes the essential component |
25 | | - versions (apart from the ``pythonSoftIoc`` version), performs the appropriate |
26 | | - initialisation and starts the IOC running. The following template is a |
27 | | - useful starting point:: |
28 | | - |
29 | | - from pkg_resources import require |
30 | | - require('cothread==2.12') |
31 | | - require('epicsdbbuilder==1.0') |
32 | | - |
33 | | - # Import the basic framework components. |
34 | | - from softioc import softioc, builder |
35 | | - import cothread |
36 | | - |
37 | | - # Import any modules required to run the IOC |
38 | | - import ... |
39 | | - |
40 | | - # Boilerplate get the IOC started |
41 | | - builder.LoadDatabase() |
42 | | - softioc.iocInit() |
43 | | - |
44 | | - # Start processes required to be run after iocInit |
45 | | - ... |
46 | | - |
47 | | - # Finally leave the IOC running with an interactive shell. |
48 | | - softioc.interactive_ioc(globals()) |
| 4 | +Introduction |
| 5 | +------------ |
49 | 6 |
|
50 | | - Note that the use of ``require`` is specific to DLS, and you may have a |
51 | | - different way of managing your installations. |
| 7 | +Once the module has been installed (see :doc:`installation`) we can create a |
| 8 | +simple EPICS Input/Output Controller (IOC). |
52 | 9 |
|
53 | | -.. _numpy: http://www.numpy.org/ |
54 | | -.. _cothread: https://github.com/dls-controls/cothread |
55 | | -.. _epicsdbbuilder: https://github.com/Araneidae/epicsdbbuilder |
| 10 | +An EPICS IOC created with the help of ``pythonIoc`` and :mod:`softioc` is |
| 11 | +referred to as a "Python soft IOC". The code below illustrates a simple IOC |
| 12 | +with two Process Variables (PVs): |
56 | 13 |
|
| 14 | +.. literalinclude:: ../examples/example_cothread_ioc.py |
57 | 15 |
|
| 16 | +This example script illustrates the following points. |
58 | 17 |
|
59 | | -Introduction |
60 | | ------------- |
| 18 | +.. literalinclude:: ../examples/example_cothread_ioc.py |
| 19 | + :start-after: # Import |
| 20 | + :end-before: # Set |
61 | 21 |
|
62 | | -The Python Soft IOC consists of two components: a command ``pythonIoc`` and an |
63 | | -associated library :mod:`softioc`. The ``pythonIoc`` command consists of a bare |
64 | | -EPICS IOC linked together with the DLS Python interpreter and configured so that |
65 | | -startup arguments are interpreted by the Python interpreter -- this means that |
66 | | -when ``pythonIoc`` is run it behaves the same as running ``dls-python``. |
| 22 | +The :mod:`softioc` library is part of ``pythonIoc``. The two submodules |
| 23 | +:mod:`softioc.softioc` and :mod:`softioc.builder` provide the basic |
| 24 | +functionality for Python soft IOCs and are the ones that are normally used. |
67 | 25 |
|
68 | | -Scripts run from within ``pythonIoc`` differ from standard Python scripts in one |
69 | | -detail: they have the ability to create and publish PVs through the |
70 | | -:mod:`softioc` library. Typically both :mod:`cothread` and |
71 | | -:mod:`epicsdbbuilder` will be recruited to help: :mod:`cothread` is used for |
72 | | -dispatching OUT record processing callback methods, :mod:`epicsdbbuilder` is |
73 | | -used for constructing records during IOC initialisation. |
74 | 26 |
|
75 | | -An EPICS IOC created with the help of ``pythonIoc`` and :mod:`softioc` is |
76 | | -referred to as a "Python soft IOC". The code below illustrates a simple IOC |
77 | | -with one PV:: |
| 27 | +.. literalinclude:: ../examples/example_cothread_ioc.py |
| 28 | + :start-after: # Create |
| 29 | + :end-before: # Boilerplate |
78 | 30 |
|
79 | | - # DLS requires |
80 | | - from pkg_resources import require |
81 | | - require('cothread==2.12') |
82 | | - require('epicsdbbuilder==1.0') |
| 31 | +PVs are normally created dynamically using :mod:`softioc.builder`. All PV |
| 32 | +creation must be done before initialising the IOC. We define `on_update` for |
| 33 | +``ao`` such that whenever we set ``ao``, ``ai`` will be set to the same value. |
83 | 34 |
|
84 | | - # Import basic softioc framework |
85 | | - from softioc import softioc, builder |
| 35 | +.. literalinclude:: ../examples/example_cothread_ioc.py |
| 36 | + :start-after: # Boilerplate |
| 37 | + :end-before: # Start |
86 | 38 |
|
87 | | - # Create PVs |
88 | | - builder.SetDeviceName('TS-TEST-TEST-01') |
89 | | - builder.stringIn('TEST', initial_value = 'This is a test') |
| 39 | +Once PVs have been created then the associated EPICS database can be created |
| 40 | +and loaded into the IOC and then the IOC can be started. |
90 | 41 |
|
91 | | - # Run the IOC. This is boilerplate, and must always be done in this order, |
92 | | - # and must always be done after creating all PVs. |
93 | | - builder.LoadDatabase() |
94 | | - softioc.iocInit() |
| 42 | +.. literalinclude:: ../examples/example_cothread_ioc.py |
| 43 | + :start-after: # Start |
| 44 | + :end-before: # Finally |
95 | 45 |
|
96 | | - softioc.interactive_ioc(globals()) |
| 46 | +We define a long-running operation that will increment the value of ``ai`` once per |
| 47 | +second. This is run as a background process by `cothread`. |
97 | 48 |
|
98 | | -This example script illustrates the following points. |
| 49 | +.. literalinclude:: ../examples/example_cothread_ioc.py |
| 50 | + :start-after: # Finally |
99 | 51 |
|
100 | | -- The use of ``pkg_resources.require`` is standard across all use of the |
101 | | - ``dls-python`` Python interpreter at Diamond, and in this example we are using |
102 | | - both :mod:`cothread` and :mod:`epicsdbbuilder`. Of course, in an officially |
103 | | - published IOC specific versions must be specified, in this example I'm using |
104 | | - the most recent versions at the time of writing. |
| 52 | +Finally the application must refrain from exiting until the IOC is no longer |
| 53 | +needed. The :func:`~softioc.softioc.interactive_ioc` runs a Python |
| 54 | +interpreter shell with a number of useful EPICS functions in scope, and |
| 55 | +passing ``globals()`` through can allow interactive interaction with the |
| 56 | +internals of the IOC while it's running. The alternative is to call something |
| 57 | +like :func:`cothread.WaitForQuit` or some other :mod:`cothread` blocking |
| 58 | +action. |
105 | 59 |
|
106 | | -- The :mod:`softioc` library is part of ``pythonIoc`` and is automatically added |
107 | | - to the path. The two submodules :mod:`softioc.softioc` and |
108 | | - :mod:`softioc.builder` provide the basic functionality for Python soft IOCs |
109 | | - and are the ones that are normally used. |
| 60 | +In this interpreter there is immediate access to methods defined in the |
| 61 | +:mod:`softioc.softioc` module. For example the :func:`~softioc.softioc.dbgf` function |
| 62 | +can be run to observe the increasing value of ``ai``:: |
110 | 63 |
|
111 | | -- PVs are normally created dynamically using :mod:`softioc.builder`. All PV |
112 | | - creation must be done before initialising the IOC. |
| 64 | + >>> dbgf("MY-DEVICE-PREFIX:AI") |
| 65 | + DBF_DOUBLE: 36 |
| 66 | + >>> dbgf("MY-DEVICE-PREFIX:AI") |
| 67 | + DBF_DOUBLE: 37 |
113 | 68 |
|
114 | | -- Once PVs have been created then the associated EPICS database can be created |
115 | | - and loaded into the IOC and then the IOC can be started. |
| 69 | +And the :func:`~softioc.softioc.dbpf` method allows data to be set and to observe |
| 70 | +the functionality of the lambda passed to `on_update` . We set the value on ``ao`` |
| 71 | +and read the value on ``ai`` (exact values may vary based on time between commands):: |
116 | 72 |
|
117 | | -- Finally the application must refrain from exiting until the IOC is no longer |
118 | | - needed. The :func:`~softioc.softioc.interactive_ioc` runs a Python |
119 | | - interpreter shell with a number of useful EPICS functions in scope, and |
120 | | - passing ``globals()`` through can allow interactive interaction with the |
121 | | - internals of the IOC while it's running. The alternative is to call something |
122 | | - like :func:`cothread.WaitForQuit` or some other :mod:`cothread` blocking |
123 | | - action. |
| 73 | + >>> dbpf("MY-DEVICE-PREFIX:AO","999") |
| 74 | + DBF_DOUBLE: 999 |
| 75 | + >>> dbgf("MY-DEVICE-PREFIX:AI") |
| 76 | + DBF_DOUBLE: 1024 |
124 | 77 |
|
125 | 78 |
|
126 | 79 | Creating a Publishable IOC |
@@ -247,3 +200,8 @@ done (:class:`cothread.Spawn` is recommended for initiating persistent backgroun |
247 | 200 | activity) the top level script must pause, as as soon as it exits the IOC will |
248 | 201 | exit. Calling :func:`~softioc.softioc.interactive_ioc` is recommended for this |
249 | 202 | as the last statement in the top level script. |
| 203 | + |
| 204 | + |
| 205 | +.. _numpy: http://www.numpy.org/ |
| 206 | +.. _cothread: https://github.com/dls-controls/cothread |
| 207 | +.. _epicsdbbuilder: https://github.com/Araneidae/epicsdbbuilder |
0 commit comments