Controlling an instrument
Last updated on 2025-08-25 | Edit this page
Overview
Questions
- How do I control an instrument?
Objectives
- Add an instrument into a QMI context and control it with RPC commands
Using QMI to control one instrument
In this example we show how to create an instrument object in a context. For this, we have a dummy instrument in the QMI instruments to illustrate. See also the QMI readthedocs documentation about the dummy instrument class call interface. Let’s import the device driver.
So we imported the QMI device driver class called
NoisySineGenerator
from QMI instrument “manufacturer”
dummy
and device noisy_sine_generator
module.
To use this, we start a new QMI context and “make” an instrument object
from the device driver.
Now we have an instrument object nsg
present in Python.
The instrument is also added into the context. This can be checked
with:
OUTPUT
address type
------------ ------------------
nsg_demo.nsg NoisySineGenerator
------------ ------------------
The address “nsg_demo.nsg” means that now there is an instrument object “nsg” present in context “nsg_demo”. The type confirms the instrument object is of expected class type. Alternative way to confirm this is simply to type the object in Python.
OUTPUT
<rpc proxy for nsg_demo.nsg (qmi.instruments.dummy.noisy_sine_generator.NoisySineGenerator)>
As can be seen, the created object is actually an RPC proxy of the actual instrument object. This has a couple of consequences: The first is that the proxy object can be now shared through the context with other contexts, allowing remote control of the instrument. The second is that the proxy object does not have the full class interface of the device driver, but only the variables that are present in QMI proxy class and functions that have been selected to be RPC callable. We can list the variables of the object with
It prints out the class docstring, a listing of its callable RPC methods, signals and class constants.
Here are useful information, like the docstring which is the
documentation string of the class object. Then all entries listed as
“RPC methods” are the callable RPC functions of the object, with their
expected input parameters and return value type. A few methods related
to the proxy locking (lock
, unlock
,
is_locked
and force unlock
are not present,
though. We also won’t handle these methods in the course, but you can
have a look at the tutorial.
You see also empty listings “signals” and “constants”, but for this
instrument class there are none present.
Anyhow, in the printed out listing, a lot of useful methods are present and supported through the proxy. Let’s try one:
OUTPUT
96.18801232566346
So we get returned one sample of the sine wave at a random moment. Let’s do for demonstration purpose a little for loop print out the sine wave of our generator “instrument”:
PYTHON
import time
for i in range(1000):
print(" " * int(40.0 + 0.25 * nsg.get_sample()) + "*")
time.sleep(0.01)
Nice huh? Let’s then close this context with qmi.stop()
and exit Python with exit()
and prepare for next
example.
- Instruments can be added into contexts with
<instrument_object> = qmi.make_instrument("<name>", <ClassName>, <possible_extra_parameters>)
- Instrument class description can be seen with
help(<instrument_object>)
- Detailed information about the object and variables can be obtained
with
dir(<instrument_object>)
- The returned instrument object is an RPC proxy object of the actual class object