1
Dependencies: mbed-dev-f303 FastPWM3
math_ops.cpp@26:2b865c00d7e9, 2017-05-01 (annotated)
- Committer:
- benkatz
- Date:
- Mon May 01 15:22:58 2017 +0000
- Revision:
- 26:2b865c00d7e9
- Parent:
- 25:f5741040c4bb
- Child:
- 34:51647c6c500d
- Added CAN Send/Receive; - Updated to most recent MBED (Broke SPI 16-bit read/write. Hacked to work with 2 8-bit writes now); - Removed most sine/cos calculations; - Fixed sign error in autocalibration routine
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 20:bf9ea5125d52 | 1 | |
benkatz | 20:bf9ea5125d52 | 2 | #include "math_ops.h" |
benkatz | 20:bf9ea5125d52 | 3 | |
benkatz | 20:bf9ea5125d52 | 4 | |
benkatz | 20:bf9ea5125d52 | 5 | float fmaxf(float x, float y){ |
benkatz | 25:f5741040c4bb | 6 | /// Returns maximum of x, y /// |
benkatz | 20:bf9ea5125d52 | 7 | return (((x)>(y))?(x):(y)); |
benkatz | 20:bf9ea5125d52 | 8 | } |
benkatz | 20:bf9ea5125d52 | 9 | |
benkatz | 20:bf9ea5125d52 | 10 | float fminf(float x, float y){ |
benkatz | 25:f5741040c4bb | 11 | /// Returns minimum of x, y /// |
benkatz | 20:bf9ea5125d52 | 12 | return (((x)<(y))?(x):(y)); |
benkatz | 20:bf9ea5125d52 | 13 | } |
benkatz | 20:bf9ea5125d52 | 14 | |
benkatz | 20:bf9ea5125d52 | 15 | float fmaxf3(float x, float y, float z){ |
benkatz | 25:f5741040c4bb | 16 | /// Returns maximum of x, y, z /// |
benkatz | 20:bf9ea5125d52 | 17 | return (x > y ? (x > z ? x : z) : (y > z ? y : z)); |
benkatz | 20:bf9ea5125d52 | 18 | } |
benkatz | 20:bf9ea5125d52 | 19 | |
benkatz | 20:bf9ea5125d52 | 20 | float fminf3(float x, float y, float z){ |
benkatz | 25:f5741040c4bb | 21 | /// Returns minimum of x, y, z /// |
benkatz | 20:bf9ea5125d52 | 22 | return (x < y ? (x < z ? x : z) : (y < z ? y : z)); |
benkatz | 20:bf9ea5125d52 | 23 | } |
benkatz | 20:bf9ea5125d52 | 24 | |
benkatz | 20:bf9ea5125d52 | 25 | void limit_norm(float *x, float *y, float limit){ |
benkatz | 25:f5741040c4bb | 26 | /// Scales the lenght of vector (x, y) to be <= limit /// |
benkatz | 20:bf9ea5125d52 | 27 | float norm = sqrt(*x * *x + *y * *y); |
benkatz | 20:bf9ea5125d52 | 28 | if(norm > limit){ |
benkatz | 20:bf9ea5125d52 | 29 | *x = *x * limit/norm; |
benkatz | 20:bf9ea5125d52 | 30 | *y = *y * limit/norm; |
benkatz | 20:bf9ea5125d52 | 31 | } |
benkatz | 20:bf9ea5125d52 | 32 | } |
benkatz | 26:2b865c00d7e9 | 33 | |
benkatz | 26:2b865c00d7e9 | 34 | int float_to_uint(float x, float x_min, float x_max, int bits){ |
benkatz | 26:2b865c00d7e9 | 35 | float span = x_max - x_min; |
benkatz | 26:2b865c00d7e9 | 36 | float offset = x_min; |
benkatz | 26:2b865c00d7e9 | 37 | return (int) ((x+offset)*((float)((1<<bits)-1))/span); |
benkatz | 26:2b865c00d7e9 | 38 | } |
benkatz | 26:2b865c00d7e9 | 39 | |
benkatz | 26:2b865c00d7e9 | 40 | float uint_to_float(int x_int, float x_min, float x_max, int bits){ |
benkatz | 26:2b865c00d7e9 | 41 | float span = x_max - x_min; |
benkatz | 26:2b865c00d7e9 | 42 | float offset = x_min; |
benkatz | 26:2b865c00d7e9 | 43 | return ((float)x_int)*span/((float)((1<<bits)-1)) + offset; |
benkatz | 26:2b865c00d7e9 | 44 | } |