Working With Units

When working with physical quantities it is important to keep track of the corresponding units. To help keep track of units and convert between supported units the URCap API provides a unit system.

The NPM package “contribution-api” provides definitions for the most common physical quantities like: length, speed, mass, current, time etc. Using the supported units it is possible to define a physical quantity’s value and unit together.

The NPM package “utilities-units” provides a “convertValue” function that will convert between units for any supported physical quantity.

Creating physical quantities and converting units

The following demonstrates how to create physical quantities for length and time and how to convert the units:

import { convertValue, Length, Time } from '@universal-robots/contribution-api';

let distance: Length = { value: 30, unit: 'mm' };
let timeout: Time = { value: 100, unit: 'ms' };
console.log('distance', distance);
console.log('timeout', timeout);
//convert to SI units
distance = convertValue(distance, 'm');
timeout = convertValue(timeout, 's');
console.log('converted distance', distance);
console.log('converted timeout', timeout);

This will result in the following output:

'distance', Object{value: 30, unit: 'mm'}
'timeout', Object{value: 100, unit: 'ms'}
'converted distance', Object{value: 0.03, unit: 'm'}
'converted timeout', Object{value: 0.1, unit: 's'}

Supported units for each physical quantity can be found in the type definition for that quantity. For example, supported units for Acceleration is listed in AccelerationUnits found in the Acceleration definition

Sample

In the GripDistance application sample we convert the limits to match the current value unit. See “gripdistance-application.component.ts” file in the sample.