dcsext.converter

Converter - converts between common units of measure.

Contents

  1. Description
  2. Functions
    1. _t.convert(value, fromunit, tounit)
    2. _t.register(unitstr, measure, frombase, tobase, tostr, overwrite)
    3. _t.tostring(value, unitstr, precision)
  3. Tables
    1. _t.measure

Description

DCS base units are meters for distance, radians for angle, kilograms for mass, pascals for pressure, kelvins for temperature and meters/second for speed. Positions are the DCS coordinate triple {x, y, z}.

Built-in registered unit strings, conversions are only possible between units of the same measure:

  • LENGTH: “m”, “ft”, “nm” nautical mile, “sm” statute mile, “km”
  • MASS: “kg”, “g”, “lbs”
  • SPEED: “mps”, “knots”, “kph”, “mph”
  • PRESSURE: “pascal”, “inhg”, “mmhg”, “hpa”, “mbar”
  • TEMPERATURE: “kelvin”, “celsius”, “fahrenheit”
  • FREQUENCY: “hz”, “khz”, “mhz”
  • COORDINATES: “dcs”, “dd”, “ddm”, “dms” and “mgrs”

Coordinate units convert between DCS position triples (“dcs”) and {latitude = , longitude = , altitude = } tables (“dd”/”ddm”/”dms”) or MGRS tables (“mgrs”). MGRS tables carry the fields UTMZone, MGRSDigraph, Easting and Northing. The optional precision argument sets the number of decimal places shown by tostring; for ddm/dms it adjusts the minutes/seconds digits.

Functions

_t.convert(value, fromunit, tounit)

Convert between units of measure. Unit strings are case-insensitive and both units must belong to the same measure, see the module description for the built-in units.

Parameters

value
value(number table) to convert
fromunit
units string the value is currently in
tounit
unit of measure to convert the value to

Returns

  • the converted value or nil on error followed by an error message

_t.register(unitstr, measure, frombase, tobase, tostr, overwrite)

Register a new unit of measure with the system.

Parameters

unitstr
the string used to reference this unit, will be converted to uppercase. There cannot be collisions between units.
measure
one of the items in dcsext.converter.measure.
frombase
converts a DCS base unit value to this unit, either a function or a scale factor(number), nil passes values through unchanged.
tobase
converts a value in this unit back to the DCS base unit, either a function or a scale factor(number), nil passes values through unchanged.
tostr
(optional) converts to a human readable representation, called by dcsext.converter.tostring with (value, precision)
overwrite
(optional) flag to overwrite any previous entry.

Returns

  • True if successful otherwise false, error msg

_t.tostring(value, unitstr, precision)

Convert value to a human readable representation. Uses the tostr callback given at registration time, when a unit has none the value is formatted as “ ".

Parameters

value
the value to display
unitstr
unit string that was registered
precision
(optional) defines how many decimal places will be shown, the default is 2.

Returns

  • string representation of value or nil if the unit is unknown

Tables

_t.measure

Supported measures, unit conversion is only possible within one.

Parameters

[LENGTH]
[MASS]
[SPEED]
[PRESSURE]
[TEMPERATURE]
[COORDINATES]
[FREQUENCY]