Skip to content

Commit 4a10019

Browse files
authored
Merge pull request #18 from dls-controls/improve-docs
Improve the docs and tests for initially undefined values
2 parents c73bdd1 + 95492db commit 4a10019

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

docs/explanations/why-use-pythonIoc.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ allows you to write this as:
1414
.. code-block::
1515
1616
import numpy as np
17-
from cothread.catools import caget, camonitor
17+
from cothread.catools import camonitor
1818
from softioc import builder, softioc
1919
20-
# The PVs we want to average and their initial values
20+
# The PVs we want to average and initial values
2121
PVs = [f"DEVICE{i}:CURRENT" for i in range(100)]
22-
values = np.array(caget(PVs))
22+
values = np.empty(len(PVs)) * np.nan
2323
24-
# The PV we want to serve
25-
avg = builder.aOut("AVERAGE:CURRENT", np.mean(values))
24+
# The PV we want to serve, initially undefined
25+
avg = builder.aOut("AVERAGE:CURRENT")
2626
2727
# Start the IOC
2828
builder.LoadDatabase()
@@ -31,13 +31,22 @@ allows you to write this as:
3131
# Make a monitor on the PVs to keep the value up to date
3232
def update_avg(value: float, index: int):
3333
values[index] = value
34-
avg.set(np.mean(values))
34+
mean = np.mean(values)
35+
# If all PVs have returned a value, set the mean
36+
if not np.isnan(mean)
37+
avg.set(mean)
3538
3639
camonitor(PVs, update_avg)
3740
3841
# Leave the IOC running with an interactive shell.
3942
softioc.interactive_ioc(globals())
4043
44+
.. note::
45+
46+
If using `asyncio` then you would use `aioca.camonitor` instead of
47+
`cothread.catools.camonitor`::
48+
49+
from aioca import camonitor
4150

4251
Dynamically created PVs
4352
-----------------------

tests/sim_asyncio_ioc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async def callback(value):
2323
sys.stdout.flush()
2424
sim_records.t_ai.set(value)
2525

26-
t_ao = builder.aOut('AO2', initial_value=12.45, on_update=callback)
26+
t_ao = builder.aOut('AO2', on_update=callback)
2727

2828
# Run the IOC
2929
builder.LoadDatabase()

tests/test_asyncio.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def asyncio_ioc():
3333
@pytest.mark.asyncio
3434
async def test_asyncio_ioc(asyncio_ioc):
3535
import asyncio
36-
from aioca import caget, caput, camonitor, CANothing, _catools
36+
from aioca import caget, caput, camonitor, CANothing, _catools, FORMAT_CTRL
3737
# Unregister the aioca atexit handler as it conflicts with the one installed
3838
# by cothread. If we don't do this we get a seg fault. This is not a problem
3939
# in production as we won't mix aioca and cothread, but we do mix them in
@@ -48,7 +48,10 @@ async def test_asyncio_ioc(asyncio_ioc):
4848
m = camonitor(PV_PREFIX + ":SIN", q.put, notify_disconnect=True)
4949
assert len(await asyncio.wait_for(q.get(), 1)) == 4
5050
# AO
51-
assert await caget(PV_PREFIX + ":AO2") == 12.45
51+
ao_val = await caget(PV_PREFIX + ":AO2", format=FORMAT_CTRL)
52+
assert ao_val == 0
53+
assert ao_val.severity == 3 # INVALID
54+
assert ao_val.status == 17 # UDF
5255
await caput(PV_PREFIX + ":AO2", 3.56, wait=True)
5356
await asyncio.sleep(0.1)
5457
assert await caget(PV_PREFIX + ":AI") == 12.34
@@ -70,7 +73,7 @@ async def test_asyncio_ioc(asyncio_ioc):
7073
# check closed and output
7174
assert "%s:SINN.VAL 1024 -> 4" % PV_PREFIX in out
7275
assert 'update_sin_wf 4' in out
73-
assert "%s:AO2.VAL 12.45 -> 3.56" % PV_PREFIX in out
76+
assert "%s:AO2.VAL 0 -> 3.56" % PV_PREFIX in out
7477
assert 'async update 3.56' in out
7578
assert 'Starting iocInit' in err
7679
assert 'iocRun: All initialization complete' in err

0 commit comments

Comments
 (0)