00001 /* -*- mode: C++ -*- 00002 * 00003 * Units conversion aids. 00004 * 00005 * Copyright (C) 2007, 2009 Austin Robot Technology 00006 * License: Modified BSD Software License Agreement 00007 * 00008 * $Id: conversions.h 630 2010-09-25 16:20:42Z jack.oquin $ 00009 */ 00010 00011 #ifndef _CONVERSIONS_H 00012 #define _CONVERSIONS_H 00013 00021 #include <math.h> 00022 #include <sys/time.h> 00023 #include <time.h> 00024 00025 00027 const double INCHES_PER_FOOT = 12.0; 00028 const double CM_PER_INCH = 2.54; 00029 const double CM_PER_METER = 100.0; 00030 const double METERS_PER_FOOT = INCHES_PER_FOOT * CM_PER_INCH / CM_PER_METER; // = 0.3048 00031 const double MMETERS_PER_KM = 1000000.0; 00032 const double MMETERS_PER_MILE = 1609344.0; 00033 const double METERS_PER_MILE = MMETERS_PER_MILE / 1000.0; 00034 const long SECONDS_PER_MINUTE = 60; 00035 const long MINUTES_PER_HOUR = 60; 00036 const long SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; 00037 const double RADIANS_PER_DEGREE = M_PI/180.0; 00038 const double DEGREES_PER_RADIAN = 180.0/M_PI; 00039 00041 const double TWOPI = 2.0 * M_PI; 00042 const double HALFPI = M_PI / 2.0; 00043 00044 00046 static inline double mmps2mph(double mm) 00047 { 00048 return mm * SECONDS_PER_HOUR / MMETERS_PER_MILE; 00049 } 00050 00051 static inline double kmph2mmps(double kmph) 00052 { 00053 return kmph * MMETERS_PER_KM / SECONDS_PER_HOUR; 00054 } 00055 00056 static inline double mph2mmps(double mph) 00057 { 00058 return mph * MMETERS_PER_MILE / SECONDS_PER_HOUR; 00059 } 00060 00062 static inline double mph2mps(double mph) 00063 { 00064 return mph * METERS_PER_MILE / SECONDS_PER_HOUR; 00065 } 00066 00068 static inline double mps2mph(double mps) 00069 { 00070 return mps * SECONDS_PER_HOUR / METERS_PER_MILE; 00071 } 00072 00074 static inline double feet2meters(double feet) 00075 { 00076 return feet * METERS_PER_FOOT; 00077 } 00078 00080 static inline double meters2feet(double meters) 00081 { 00082 return meters / METERS_PER_FOOT; 00083 } 00084 00086 static inline double tv2secs(struct timeval *tv) 00087 { 00088 return tv->tv_sec + (tv->tv_usec / 1000000.0); 00089 } 00090 00092 static inline double analog_volts(int data, double maxvolts, int nbits) 00093 { 00094 // clamp value to specified bit range 00095 int limit = (1<<nbits); 00096 data &= (limit - 1); 00097 return (maxvolts * data) / limit; 00098 } 00099 00101 static inline int analog_to_digital(double voltage, 00102 double maxvolts, int nbits) 00103 { 00104 return (int) rintf((voltage / maxvolts) * (1<<nbits)); 00105 } 00106 00107 #endif // _CONVERSIONS_H