Skip to content

[WIP] motion: add support for units

Jose Tiago Macara Coutinho requested to merge units into master

After three errors in the beamline because of unit misinterpretation I thought it was time to propose this.

# simple configuration

add extra key unit: <unit name> to axis to enable units on the axis. If no unit is given, units are completely disabled which means, nothing should change if no units are provided. Example:

- name: my_motor
  unit: mm                # motor user unit is mm 
  acceleration: 40        # (in mm/s/s)
  backlash: 0.2           # (in mm)
  high_limit: 1000        # (in mm)
  low_limit: -1000        # (in mm)
  steps_per_unit: 1000
  velocity: 10            # (in mm/s)

No axis configuration parameter support units. They should be all in the same consistent user unit as provided in unit. Ideal would be to have in the future:

- name: my_motor
  acceleration: 40 m/s/s
  backlash: 0.2 inch
  high_limit: 1000 cm
  low_limit: -1000 cm
  steps_per_unit: 1000
  velocity: 10 km/s

API

Using previous example, where motor units are mm, API looks like:

>>> from bliss import get_axis
>>> from bliss.common.units import ureg as ur
>>> my_motor = get_axis('my_motor') 
>>> print my_motor.position()
12.3 mm
>>> my_motor.move(15)        # permissive API, assume we mean 15mm
>>> print my_motor.position()
15 mm
>>> my_motor.move(10 * ur.inch) 
>>> print my_motor.position()
254 mm
>>> print my_motor.acceleration()
40 mm / s ** 2

Note on permissive implementation: if quantity is not passed as argument to a method, it is assumed it is in motor user unit. A little dangerous but very comfortable API. Otherwise the user would have to type: energy.move(10 * ur.eV) instead of simply energy.move(10) .

Requirements

Instead of implementing our own unit system, better to use an existing one. Problem is there are a lot of units libraries in python.

After some research:

decided for pint module:

  • Efficient
  • numpy support
  • string parsing
  • allows custom contexts (we can define a beamline context where there is a relation between angle and eV (Bragg's law) which would allow to move in degrees/radians or eV. After, can even define specific context for SAXS/WAXS, MX, whatever (I wonder if it would be possible to define a context for 4C diffractometer... H, K, L <=> th, tth, chi, phi)

Merge request reports