Motor control for robots. More compact, less object-oriented revision.

Dependencies:   FastPWM3 mbed-dev-f303

Fork of Hobbyking_Cheetah_V1 by Ben Katz

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers math_ops.cpp Source File

math_ops.cpp

00001 
00002 #include "math_ops.h"
00003 
00004 float fmaxf(float x, float y){
00005     /// Returns maximum of x, y ///
00006     return (((x)>(y))?(x):(y));
00007     }
00008 
00009 float fminf(float x, float y){
00010     /// Returns minimum of x, y ///
00011     return (((x)<(y))?(x):(y));
00012     }
00013 
00014 float fmaxf3(float x, float y, float z){
00015     /// Returns maximum of x, y, z ///
00016     return (x > y ? (x > z ? x : z) : (y > z ? y : z));
00017     }
00018 
00019 float fminf3(float x, float y, float z){
00020     /// Returns minimum of x, y, z ///
00021     return (x < y ? (x < z ? x : z) : (y < z ? y : z));
00022     }
00023 
00024 float roundf(float x){
00025     /// Returns nearest integer ///
00026     
00027     return x < 0.0f ? ceilf(x - 0.5f) : floorf(x + 0.5f);
00028     }
00029     
00030 void limit_norm(float *x, float *y, float limit){
00031     /// Scales the lenght of vector (x, y) to be <= limit ///
00032     float norm = sqrt(*x * *x + *y * *y);
00033     if(norm > limit){
00034         *x = *x * limit/norm;
00035         *y = *y * limit/norm;
00036         }
00037     }
00038 
00039 
00040 int float_to_uint(float x, float x_min, float x_max, uint8_t bits){
00041     /// Converts a float to an unsigned int, given range and number of bits ///
00042     float span = x_max - x_min;
00043     float offset = x_min;
00044     return (int) ((x-offset)*((float)((1<<bits)-1))/span);
00045     }
00046     
00047     
00048 float uint_to_float(int x_int, float x_min, float x_max, uint8_t bits){
00049     /// converts unsigned int to float, given range and number of bits ///
00050     float span = x_max - x_min;
00051     float offset = x_min;
00052     return ((float)x_int)*span/((float)((1<<bits)-1)) + offset;
00053     }