Chetan Sharma
/
HKCC_Controller_MBed_OS
Modifying the HKCC for no readily apparent reason
math_ops.cpp@53:e9e1d70532ef, 2021-02-19 (annotated)
- Committer:
- cactode
- Date:
- Fri Feb 19 22:27:52 2021 -0800
- Revision:
- 53:e9e1d70532ef
- Parent:
- 47:e1196a851f76
Porting to MBed OS, initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 20:bf9ea5125d52 | 1 | |
benkatz | 47:e1196a851f76 | 2 | #include "../math_ops.h" |
benkatz | 20:bf9ea5125d52 | 3 | |
benkatz | 20:bf9ea5125d52 | 4 | |
benkatz | 20:bf9ea5125d52 | 5 | float fmaxf3(float x, float y, float z){ |
benkatz | 25:f5741040c4bb | 6 | /// Returns maximum of x, y, z /// |
benkatz | 20:bf9ea5125d52 | 7 | return (x > y ? (x > z ? x : z) : (y > z ? y : z)); |
benkatz | 20:bf9ea5125d52 | 8 | } |
benkatz | 20:bf9ea5125d52 | 9 | |
benkatz | 20:bf9ea5125d52 | 10 | float fminf3(float x, float y, float z){ |
benkatz | 25:f5741040c4bb | 11 | /// Returns minimum of x, y, z /// |
benkatz | 20:bf9ea5125d52 | 12 | return (x < y ? (x < z ? x : z) : (y < z ? y : z)); |
benkatz | 20:bf9ea5125d52 | 13 | } |
benkatz | 20:bf9ea5125d52 | 14 | |
benkatz | 20:bf9ea5125d52 | 15 | void limit_norm(float *x, float *y, float limit){ |
benkatz | 25:f5741040c4bb | 16 | /// Scales the lenght of vector (x, y) to be <= limit /// |
benkatz | 20:bf9ea5125d52 | 17 | float norm = sqrt(*x * *x + *y * *y); |
benkatz | 20:bf9ea5125d52 | 18 | if(norm > limit){ |
benkatz | 20:bf9ea5125d52 | 19 | *x = *x * limit/norm; |
benkatz | 20:bf9ea5125d52 | 20 | *y = *y * limit/norm; |
benkatz | 20:bf9ea5125d52 | 21 | } |
benkatz | 20:bf9ea5125d52 | 22 | } |
benkatz | 47:e1196a851f76 | 23 | |
benkatz | 47:e1196a851f76 | 24 | void limit(float *x, float min, float max){ |
benkatz | 47:e1196a851f76 | 25 | *x = fmaxf(fminf(*x, max), min); |
benkatz | 47:e1196a851f76 | 26 | } |
benkatz | 26:2b865c00d7e9 | 27 | |
benkatz | 26:2b865c00d7e9 | 28 | int float_to_uint(float x, float x_min, float x_max, int bits){ |
benkatz | 47:e1196a851f76 | 29 | /// Converts a float to an unsigned int, given range and number of bits /// |
benkatz | 26:2b865c00d7e9 | 30 | float span = x_max - x_min; |
benkatz | 26:2b865c00d7e9 | 31 | float offset = x_min; |
benkatz | 47:e1196a851f76 | 32 | return (int) ((x-offset)*((float)((1<<bits)-1))/span); |
benkatz | 26:2b865c00d7e9 | 33 | } |
benkatz | 26:2b865c00d7e9 | 34 | |
benkatz | 47:e1196a851f76 | 35 | |
benkatz | 26:2b865c00d7e9 | 36 | float uint_to_float(int x_int, float x_min, float x_max, int bits){ |
benkatz | 47:e1196a851f76 | 37 | /// converts unsigned int to float, given range and number of bits /// |
benkatz | 26:2b865c00d7e9 | 38 | float span = x_max - x_min; |
benkatz | 26:2b865c00d7e9 | 39 | float offset = x_min; |
benkatz | 26:2b865c00d7e9 | 40 | return ((float)x_int)*span/((float)((1<<bits)-1)) + offset; |
benkatz | 26:2b865c00d7e9 | 41 | } |