Variables¶
User variables of a running code are exposed by the Project class through its variables collection. The Variable class provides an interface to monitor and modify these variables.
Listing variables¶
To list all user variables available in a project:
for variable in project.variables:
print(variable.name, variable.type_name)
Reading and writing values¶
To read from or write to a specific variable by name:
# Read the current value and write back an updated value
value = project.variables["speed_ref"].read()
project.variables["speed_ref"].write(value + 25.0)
Waiting for variable conditions¶
To block script execution until a user variable reaches a specific threshold or condition:
dc_voltage = project.variables["Vdc"]
# Poll every 100 ms without a timeout until the condition is met
cockpit.wait_until(lambda: dc_voltage.read() >= 750.0, timeout=None, interval=0.1)
API Reference¶
- class imperix.VariableCollection(cockpit: Cockpit, *, project_id: int | None = None, mac: str | None = None)¶
Variables belonging to a project. Accessed via
project.variables.- list(*, timeout: float | None | UseDefaultTimeout = UseDefaultTimeout.TOKEN) list[Variable]¶
Returns a list of the variables belonging to a project.
- Parameters:
timeout – Maximum time to wait for a response in seconds.
- Returns:
A list of
Variableinstances.- Raises:
InvalidStateError – If the project has no linked target.
- class imperix.Variable(_collection: VariableCollection, name: str, type_name: str)¶
Represents a user variable.
- name¶
Variable name shown by Cockpit.
- Type:
str
- type_name¶
Data type (
float,int32, oruint32).- Type:
str
- read(*, timeout: float | None | UseDefaultTimeout = UseDefaultTimeout.TOKEN) Any¶
Reads the variable value.
- Parameters:
timeout – Maximum time to wait for a response in seconds.
- Returns:
Current variable value. The Python type depends on the user variable type.
- Raises:
NotFoundError – If the variable is not available.
InvalidStateError – If Cockpit cannot read the variable in the current target state.
- write(value: Any, *, timeout: float | None | UseDefaultTimeout = UseDefaultTimeout.TOKEN) None¶
Write a value to the variable.
- Parameters:
value – The value to write.
timeout – Maximum time to wait for a response in seconds.
- Raises:
NotFoundError – If the variable is not available.
InvalidStateError – If Cockpit cannot write the variable in the target’s current state.