pydase/docs/user-guide/Understanding-Units.md
2024-08-19 15:56:57 +02:00

2.0 KiB

Understanding Units

pydase integrates with the pint package to allow you to work with physical quantities within your service. This enables you to define attributes with units, making your service more expressive and ensuring consistency in the handling of physical quantities.

You can define quantities in your pydase.DataService subclass using the pydase.units module. Here's an example:

from typing import Any

import pydase
import pydase.units as u


class ServiceClass(pydase.DataService):
    voltage = 1.0 * u.units.V
    _current: u.Quantity = 1.0 * u.units.mA

    @property
    def current(self) -> u.Quantity:
        return self._current

    @current.setter
    def current(self, value: u.Quantity) -> None:
        self._current = value


if __name__ == "__main__":
    service = ServiceClass()

    service.voltage = 10.0 * u.units.V
    service.current = 1.5 * u.units.mA

    pydase.Server(service=service).run()

In the frontend, quantities are rendered as floats, with the unit displayed as additional text. This allows you to maintain a clear and consistent representation of physical quantities across both the backend and frontend of your service. Web interface with rendered units

Should you need to access the magnitude or the unit of a quantity, you can use the .m attribute or the .u attribute of the variable, respectively. For example, this could be necessary to set the periodicity of a task:

import asyncio
import pydase
import pydase.units as u


class ServiceClass(pydase.DataService):
    readout_wait_time = 1.0 * u.units.ms

    async def read_sensor_data(self):
        while True:
            print("Reading out sensor ...")
            await asyncio.sleep(self.readout_wait_time.to("s").m)


if __name__ == "__main__":
    service = ServiceClass()

    pydase.Server(service=service).run()

For more information about what you can do with the units, please consult the documentation of pint.