Skip to content

Commit

Permalink
#98 Refactoring. Moved functions to num.c
Browse files Browse the repository at this point in the history
  • Loading branch information
krichardsson committed Mar 10, 2016
1 parent 3158387 commit 3fb2649
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 46 deletions.
43 changes: 2 additions & 41 deletions src/modules/src/stabilizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,9 @@
#else
#include "lps25h.h"
#endif
#include "num.h"


#undef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#undef min
#define min(a,b) ((a) < (b) ? (a) : (b))

/**
* Defines in what divided update rate should the attitude
* control loop run relative the rate control loop.
Expand Down Expand Up @@ -155,8 +151,6 @@ static void distributePower(const uint16_t thrust, const int16_t roll,
const int16_t pitch, const int16_t yaw);
static uint16_t limitThrust(int32_t value);
static void stabilizerTask(void* param);
static float constrain(float value, const float minVal, const float maxVal);
static float deadband(float value, const float threshold);

void stabilizerInit(void)
{
Expand Down Expand Up @@ -594,40 +588,7 @@ static void distributePower(const uint16_t thrust, const int16_t roll,

static uint16_t limitThrust(int32_t value)
{
if(value > UINT16_MAX)
{
value = UINT16_MAX;
}
else if(value < 0)
{
value = 0;
}

return (uint16_t)value;
}

// Constrain value between min and max
static float constrain(float value, const float minVal, const float maxVal)
{
return min(maxVal, max(minVal,value));
}

// Deadzone
static float deadband(float value, const float threshold)
{
if (fabs(value) < threshold)
{
value = 0;
}
else if (value > 0)
{
value -= threshold;
}
else if (value < 0)
{
value += threshold;
}
return value;
return limitUint16(value);
}

LOG_GROUP_START(ctrltarget)
Expand Down
10 changes: 10 additions & 0 deletions src/utils/interface/num.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@

#include <stdint.h>

#undef max
#define max(a,b) ((a) > (b) ? (a) : (b))
#undef min
#define min(a,b) ((a) < (b) ? (a) : (b))


uint16_t single2half(float number);
float half2single(uint16_t number);

uint16_t limitUint16(int32_t value);
float constrain(float value, const float minVal, const float maxVal);
float deadband(float value, const float threshold);

#endif
52 changes: 47 additions & 5 deletions src/utils/src/num.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@
* num.c - 16bit floating point handling functions
*/

/* To not use the GCC implementation, uint16_t is used to carry fp16 values
#include <math.h>
#include <stdint.h>

#include "num.h"

/* Half precision floating point **********************************************
*
* To not use the GCC implementation, uint16_t is used to carry fp16 values
*
* FP16 or Half precision floating points is specified by IEEE 754 as binary 16.
* (float is specified as binary 32). This implementation is NOT GUARANTEED to
Expand All @@ -38,10 +45,6 @@
* * Faster and smaller than the GCC implementation
*/

#include "num.h"

#include <stdint.h>

uint16_t single2half(float number)
{
uint32_t num = *((uint32_t*)&number);
Expand Down Expand Up @@ -79,3 +82,42 @@ float half2single(uint16_t number)

return *(float*)&fp32;
}


/*****************************************************************************/

uint16_t limitUint16(int32_t value)
{
if(value > UINT16_MAX)
{
value = UINT16_MAX;
}
else if(value < 0)
{
value = 0;
}

return (uint16_t)value;
}

float constrain(float value, const float minVal, const float maxVal)
{
return min(maxVal, max(minVal,value));
}

float deadband(float value, const float threshold)
{
if (fabs(value) < threshold)
{
value = 0;
}
else if (value > 0)
{
value -= threshold;
}
else if (value < 0)
{
value += threshold;
}
return value;
}

0 comments on commit 3fb2649

Please sign in to comment.