librnr  1.14.5
RoadNarrows Robotics Common Library 1
units.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Common Library 1
4 //
5 // Library: librnr
6 //
7 // File: units.c
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2013-03-18 11:39:24 -0600 (Mon, 18 Mar 2013) $
12  * $Rev: 2765 $
13  *
14  * \brief Units names, conversions, etc.
15  *
16  * \todo Add conversion functions to library.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \pkgcopyright{2010-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
21  */
22 // Permission is hereby granted, without written agreement and without
23 // license or royalty fees, to use, copy, modify, and distribute this
24 // software and its documentation for any purpose, provided that
25 // (1) The above copyright notice and the following two paragraphs
26 // appear in all copies of the source code and (2) redistributions
27 // including binaries reproduces these notices in the supporting
28 // documentation. Substantial modifications to this software may be
29 // copyrighted by their authors and need not follow the licensing terms
30 // described here, provided that the new terms are clearly indicated in
31 // all files where they apply.
32 //
33 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
34 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
35 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
36 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
37 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
38 // THE POSSIBILITY OF SUCH DAMAGE.
39 //
40 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
41 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
42 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
43 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
44 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
45 //
46 ////////////////////////////////////////////////////////////////////////////////
47 
48 #include <sys/types.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "rnr/rnrconfig.h"
53 #include "rnr/assoc.h"
54 #include "rnr/units.h"
55 
56 
57 // ---------------------------------------------------------------------------
58 // Private Interface
59 // ---------------------------------------------------------------------------
60 
61 /*!
62  * Unit short names table. Singular form.
63  */
64 static Nvp_T UnitNamesTbl[] =
65 {
66  {"?", units_undef},
67  {"raw", units_raw},
68  {"%", units_percent},
69  {"%o", units_permil},
70  {"norm", units_norm},
71  {"um", units_um},
72  {"mm", units_mm},
73  {"cm", units_cm},
74  {"m", units_m},
75  {"rad", units_radians},
76  {"deg", units_degrees},
77  {"um/s", units_um_per_s},
78  {"mm/s", units_mm_per_s},
79  {"cm/s", units_cm_per_s},
80  {"m/s", units_m_per_s},
81  {"rad/s", units_rad_per_s},
82  {"deg/s", units_deg_per_s},
83  {"rpm", units_rpm},
84  {"g", units_g},
85  {"m/s^2", units_m_per_s2},
86  {"kgf", units_kgf},
87  {"gf", units_gf},
88  {"newton", units_newton},
89  {"dyne", units_dyne},
90  {"V", units_volt},
91  {"A", units_amp},
92  {"ohm", units_ohm},
93  {"S", units_conductance},
94  {"F", units_compacitance},
95  {"T_F", units_t_f},
96  {"T_C", units_t_c},
97  {"T_K", units_t_k}
98 };
99 
100 // ---------------------------------------------------------------------------
101 // Public Interface
102 // ---------------------------------------------------------------------------
103 
104 /*!
105  * \brief Get the units name.
106  *
107  * \param u Name-Value Pair table
108  *
109  * \return
110  * The associated units short name.
111  */
112 const char *units_shortname(units_t u)
113 {
114  return NvpVal2Name(UnitNamesTbl, arraysize(UnitNamesTbl), u);
115 }
permil &#137; of maximum raw value
Definition: units.h:49
Simple associative map data and operator declarations.
const char * NvpVal2Name(Nvp_T tbl[], size_t nTblEntries, int iVal)
Get the name associated with the value.
Definition: assoc.c:94
ohms
Definition: units.h:86
kilogram-force: 1kgf = 9.80665 N
Definition: units.h:78
revolutions per minute
Definition: units.h:71
radians/s
Definition: units.h:69
undefined units
Definition: units.h:42
const char * units_shortname(units_t u)
Get the units name.
Definition: units.c:112
1 earth g = 9.80665m/s2
Definition: units.h:74
Celsius.
Definition: units.h:92
Definition: assoc.h:151
millimeters/s
Definition: units.h:64
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
volt
Definition: units.h:84
dyne: 1 dyne = N-5
Definition: units.h:81
meters/s2
Definition: units.h:75
degrees/s
Definition: units.h:70
centimeters/s
Definition: units.h:65
Fahrenheit.
Definition: units.h:91
RoadNarrows Robotics common configuration file.
millimeters
Definition: units.h:54
Scientific units.
meters/s
Definition: units.h:66
gram-force: 1gf = 0.001kgf
Definition: units.h:79
micrometers
Definition: units.h:53
percent % of maximum raw value
Definition: units.h:48
degrees
Definition: units.h:60
centimeters
Definition: units.h:55
Kelvin.
Definition: units.h:93
micrometers/s
Definition: units.h:63
normalized units (continuous from -1 to 1)
Definition: units.h:50
radians
Definition: units.h:59
ampheres
Definition: units.h:85
static Nvp_T UnitNamesTbl[]
Definition: units.c:64
newton N = 1 kg x m / s2
Definition: units.h:80
units_t
Unit Types.
Definition: units.h:40
meters
Definition: units.h:56
raw, native device units
Definition: units.h:45