utility.c File Reference

Lots of useful board independent functions for use in any module, such as conversions from float to int (bits, not number), and fixed point number functions. More...

Go to the source code of this file.

Functions

fixed fixed_abs (fixed f)
 Returns the absolute value of the given fixed point number.
fixed fixed_mult (fixed a, fixed b)
 Multiplies two fixed point numbers.
long long int fixed_mult_to_long (fixed a, fixed b)
 Multiplies two fixed point numbers as a long long int to hold the overflow.
float fixed_to_float (fixed m)
 Changes from fixed point to a floating point number.
int fixed_to_int (fixed m)
 Changes from fixed point to integer.
fixed float_to_fixed (float m)
 Changes from floating point to fixed point number.
float floatvoid (void)
 A dummy function with void inputs that returns a float.
float floatvoid_noerror (void)
 Doesn't throw an error if called.
fixed int_to_fixed (int m)
 Changes from an int to fixed point number.
int intvoid (void)
 A dummy function with void inputs that returns a int.
int intvoid_noerror (void)
 Doesn't throw an error if called.
fixed linear_to_fixed (int offset, float coeff, int value)
 Calculates the result of plugging in value into the linear equation described by the offset and coeff as a fixed point value.
void tic (void)
 Starts the clock.
void tic_toc_init (VOID_INT_F print_f)
 Initializes the TicToc functionality.
void toc (void)
 Stops the clock and prints out the execution time.
void voidint (int a)
 A dummy function with an int input that returns void.
void voidint_noerror (int a)
 Doesn't throw an error if called.
void voidvoid (void)
 A dummy function with void inputs that returns void.
void voidvoid_noerror (void)
 Doesn't throw an error if called.

Variables

VOID_INT_F tt_print = voidint
 A print or transmit function for the execution time.
int tt_time = 0
 The time it took for the last code block to execute.

Detailed Description

Lots of useful board independent functions for use in any module, such as conversions from float to int (bits, not number), and fixed point number functions.

Author:
Nicolas Williamson
Date:
January 2010

Definition in file utility.c.


Function Documentation

fixed fixed_abs ( fixed  f  ) 

Returns the absolute value of the given fixed point number.

Parameters:
f The fixed point value.
Returns:
The absolute value of the given fixed point number.

Definition at line 98 of file utility.c.

00098                         {
00099   if (f < 0){return -f;}
00100   else{return f;}
00101 }

fixed fixed_mult ( fixed  a,
fixed  b 
)

Multiplies two fixed point numbers.

Parameters:
a The first fixed point number.
b The second fixed point number.
Returns:
The fixed point result of the multiplication of the two given fixed point numbers.

Definition at line 60 of file utility.c.

Referenced by linear_to_fixed(), mc_compliant_control(), mc_get_thermal_limiter(), mc_init(), mc_pid_current(), and mc_smart_check_current().

00060                                   {
00061   return ((fixed)((((long long int)a)*((long long int)b))>>16));
00062 }

long long int fixed_mult_to_long ( fixed  a,
fixed  b 
)

Multiplies two fixed point numbers as a long long int to hold the overflow.

Parameters:
a The first fixed point number.
b The second fixed point number.
Returns:
The fixed point result of the multiplication of the two given fixed point numbers stored in a long long int to hold any overflow.

Definition at line 71 of file utility.c.

Referenced by mc_pid_current().

00071                                                   {
00072   return (((((long long int)a)*((long long int)b))>>16));
00073 }

float fixed_to_float ( fixed  m  ) 

Changes from fixed point to a floating point number.

Parameters:
m A fixed point number.
Returns:
A floating point representation of the given fixed point number.

Definition at line 42 of file utility.c.

Referenced by mc_init(), and mc_smart_check_current().

00042                              {
00043   return ((float)m*FIXED_FLOAT);
00044 }

int fixed_to_int ( fixed  m  ) 

Changes from fixed point to integer.

Doesn't round, only drops the decimal.

Parameters:
m A fixed point number.
Returns:
An integer representation of the given floating point number.

Definition at line 51 of file utility.c.

Referenced by mc_pid_current().

00051                          {
00052   return (((fixed)(m))>>16);
00053 }

fixed float_to_fixed ( float  m  ) 

Changes from floating point to fixed point number.

Parameters:
m A floating point number.
Returns:
A fixed point representation of the given floating point number.

Definition at line 26 of file utility.c.

Referenced by linear_to_fixed(), mc_compliant_control(), mc_init(), mc_set_command_current(), mc_set_dampness(), mc_set_stiffness(), mc_set_target_current(), and mc_set_unsafe_target_current().

00026                              {
00027   return ((fixed)((m) * FIXED_ONE));
00028 }

float floatvoid ( void   ) 

A dummy function with void inputs that returns a float.

Use this as the default for function pointers.

Definition at line 124 of file utility.c.

References error_occurred().

00124 {error_occurred(ERROR_UTIL_DUMMY);return 0.0;}

float floatvoid_noerror ( void   ) 

Doesn't throw an error if called.

See also:
floatvoid

Definition at line 126 of file utility.c.

00126 {return 0.0;}

fixed int_to_fixed ( int  m  ) 

Changes from an int to fixed point number.

Parameters:
m An integer.
Returns:
A fixed point representation of the given int.

Definition at line 34 of file utility.c.

Referenced by hs_look4hup(), and linear_to_fixed().

00034                          {
00035   return ((fixed)(m<<16));
00036 }

int intvoid ( void   ) 

A dummy function with void inputs that returns a int.

Use this as the default for function pointers.

Definition at line 131 of file utility.c.

References error_occurred().

00131 {error_occurred(ERROR_UTIL_DUMMY);return 0;}

int intvoid_noerror ( void   ) 

Doesn't throw an error if called.

See also:
intvoid

Definition at line 133 of file utility.c.

00133 {return 0;}

fixed linear_to_fixed ( int  offset,
float  coeff,
int  value 
)

Calculates the result of plugging in value into the linear equation described by the offset and coeff as a fixed point value.

Used in converting between units.

 retval = (coeff * value) + offset 
Parameters:
offset The offset of the linear equation. Often called b.
coeff The coefficient of the linear equation. Often called m.
value The value to be plugged into the linear equation. Often called x.
Returns:
The fixed point result of plugging value into the linear equation.

Definition at line 84 of file utility.c.

References fixed_mult(), float_to_fixed(), and int_to_fixed().

00084                                                          {
00085   int a, b, c, d, e;
00086   a = value;
00087   b = a - offset; //subtract the offset
00088   c = -1 * int_to_fixed(b); //convert to fixed point
00089   d = float_to_fixed(coeff); //convert coefficient to fixed point
00090   e = fixed_mult(c,d); //multiply gain to get amps in fixed point
00091   return e;
00092 }

void tic ( void   ) 

Starts the clock.

Put this function call right before the block of code for which you wish to know the execution time.

Definition at line 151 of file utility.c.

References tt_time.

00151               {
00152   tt_time = T0TC;
00153 }

void tic_toc_init ( VOID_INT_F  print_f  ) 

Initializes the TicToc functionality.

Note:
Must call this before using tic or toc.
Parameters:
print_f A function which takes an int and returns void that will be used to print out the execution time.

Definition at line 144 of file utility.c.

References tt_print.

00144                                      {
00145   tt_print = print_f;
00146 }

void toc ( void   ) 

Stops the clock and prints out the execution time.

Call this function right after the block of code for which you wish to know the execution time. Will use the print function passed at initialization.

Definition at line 160 of file utility.c.

References tt_print, and tt_time.

00160               {
00161   tt_time = (T0TC - tt_time);
00162   tt_print(tt_time);
00163   tt_time = 0;
00164 }

void voidint ( int  a  ) 

A dummy function with an int input that returns void.

Use this as the default for function pointers.

Parameters:
a Dummy integer.

Definition at line 117 of file utility.c.

References error_occurred().

00117 {error_occurred(ERROR_UTIL_DUMMY);}

void voidint_noerror ( int  a  ) 

Doesn't throw an error if called.

See also:
voidint

Definition at line 119 of file utility.c.

00119 {;}

void voidvoid ( void   ) 

A dummy function with void inputs that returns void.

Use this as the default for function pointers.

Definition at line 109 of file utility.c.

References error_occurred().

00109 {error_occurred(ERROR_UTIL_DUMMY);} 

void voidvoid_noerror ( void   ) 

Doesn't throw an error if called.

See also:
voidvoid

Definition at line 111 of file utility.c.

00111 {;} 


Variable Documentation

VOID_INT_F tt_print = voidint

A print or transmit function for the execution time.

Definition at line 137 of file utility.c.

Referenced by tic_toc_init(), and toc().

int tt_time = 0

The time it took for the last code block to execute.

Definition at line 136 of file utility.c.

Referenced by tic(), and toc().

Generated on Tue Jun 29 16:36:15 2010 by  doxygen 1.6.3