Ben Katz / Hobbyking_Cheetah_Compact

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

Dependencies:   FastPWM3 mbed

Fork of Hobbyking_Cheetah_V1 by Ben Katz

Committer:
benkatz
Date:
Mon May 01 15:22:58 2017 +0000
Revision:
26:2b865c00d7e9
Parent:
25:f5741040c4bb
- 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?

UserRevisionLine numberNew contents of line
benkatz20:bf9ea5125d52 1
benkatz20:bf9ea5125d52 2#include "math_ops.h"
benkatz20:bf9ea5125d52 3
benkatz20:bf9ea5125d52 4
benkatz20:bf9ea5125d52 5float fmaxf(float x, float y){
benkatz25:f5741040c4bb 6 /// Returns maximum of x, y ///
benkatz20:bf9ea5125d52 7 return (((x)>(y))?(x):(y));
benkatz20:bf9ea5125d52 8 }
benkatz20:bf9ea5125d52 9
benkatz20:bf9ea5125d52 10float fminf(float x, float y){
benkatz25:f5741040c4bb 11 /// Returns minimum of x, y ///
benkatz20:bf9ea5125d52 12 return (((x)<(y))?(x):(y));
benkatz20:bf9ea5125d52 13 }
benkatz20:bf9ea5125d52 14
benkatz20:bf9ea5125d52 15float fmaxf3(float x, float y, float z){
benkatz25:f5741040c4bb 16 /// Returns maximum of x, y, z ///
benkatz20:bf9ea5125d52 17 return (x > y ? (x > z ? x : z) : (y > z ? y : z));
benkatz20:bf9ea5125d52 18 }
benkatz20:bf9ea5125d52 19
benkatz20:bf9ea5125d52 20float fminf3(float x, float y, float z){
benkatz25:f5741040c4bb 21 /// Returns minimum of x, y, z ///
benkatz20:bf9ea5125d52 22 return (x < y ? (x < z ? x : z) : (y < z ? y : z));
benkatz20:bf9ea5125d52 23 }
benkatz20:bf9ea5125d52 24
benkatz20:bf9ea5125d52 25void limit_norm(float *x, float *y, float limit){
benkatz25:f5741040c4bb 26 /// Scales the lenght of vector (x, y) to be <= limit ///
benkatz20:bf9ea5125d52 27 float norm = sqrt(*x * *x + *y * *y);
benkatz20:bf9ea5125d52 28 if(norm > limit){
benkatz20:bf9ea5125d52 29 *x = *x * limit/norm;
benkatz20:bf9ea5125d52 30 *y = *y * limit/norm;
benkatz20:bf9ea5125d52 31 }
benkatz20:bf9ea5125d52 32 }
benkatz26:2b865c00d7e9 33
benkatz26:2b865c00d7e9 34int float_to_uint(float x, float x_min, float x_max, int bits){
benkatz26:2b865c00d7e9 35 float span = x_max - x_min;
benkatz26:2b865c00d7e9 36 float offset = x_min;
benkatz26:2b865c00d7e9 37 return (int) ((x+offset)*((float)((1<<bits)-1))/span);
benkatz26:2b865c00d7e9 38 }
benkatz26:2b865c00d7e9 39
benkatz26:2b865c00d7e9 40float uint_to_float(int x_int, float x_min, float x_max, int bits){
benkatz26:2b865c00d7e9 41 float span = x_max - x_min;
benkatz26:2b865c00d7e9 42 float offset = x_min;
benkatz26:2b865c00d7e9 43 return ((float)x_int)*span/((float)((1<<bits)-1)) + offset;
benkatz26:2b865c00d7e9 44 }