The function that is exploited as we make use of C++ programming.

The function that is exploited as we make use of C++ programming.

Map Function ()

Syntax

map(au32_IN, au32_INmin, au32_INmax, au32_OUTmin, au32_OUTmax);

uint32_t MAP(uint32_t au32_IN, uint32_t au32_INmin, uint32_t au32_INmax, uint32_t au32_OUTmin, uint32_t au32_OUTmax)

{

return ((((au32_IN - au32_INmin)*(au32_OUTmax - au32_OUTmin))/(au32_INmax - au32_INmin)) + au32_OUTmin);

}

Re-maps a number from one range to another. That is, a value of au32_INmin would get mapped to au32_OUTmin, a value of au32_INmax to au32_OUTmax, values in-between to values in-between, etc.

Does not constrain values to within the range, because out-of-range values are sometimes intended and valuable. The deny () function may be used either before or after this function if limits to the ranges are desired.

Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the map() function may be used to reverse a range of numbers, for example

angle = map(x, 1, 50, 50, 1);

The function also handles negative numbers well, so this example

angle = map(x, 1, 50, 50, -100);

is also valid and works well.

The map() function uses integer math so it will not generate fractions when the math might indicate that it should do so. Fractional remainders are truncated and are not rounded or averaged.

We can use a potentiometer with an ADC to read the analog voltage and if the ADC is 12-Bit in resolution then its digital output is going to be in this range (domain) [0 up to 4095]. If we’re willing to use this value as a control signal to position a servo motor whose angle has a range (domain) of [0° up to 180°], then we need to do a mapping from the first domain to the second one. As you can see in the example below.

Angle = map(adcReadValue, 0, 4095, 0, 180);

Constrain Function

The constrain function is a very simple logical operation. We typically perform this function on sensors’ readings when want to hard-limit or cap the readings of a specific sensor and also define a minimum output value to be accepted by the system.

Using this function guarantees that any digital controller or calculations performing mathematical operations on a specific sensor’s reading would not go wrong. As it’s guaranteed to fall in the specified range by using this function.

Here is a very simple implementation for this function in C language.

uint32_t Constrain(uint32_t au32_IN, uint32_t au32_MIN, uint32_t au32_MAX)

{

????if(au32_IN < au32_MIN)

????{

????return au32_MIN;

????}

????else if (au32_IN > au32_MAX)

????{

????return au32_MAX;

????}

????else

????{

????return au32_IN;

????}

}

Min & Max Functions

The min & max functions are commonly used in different applications. And the task required by each of them is very basic and simple. It’s a searching routine that goes through an array of values to find the maximum or minimum and return it back.

Sometimes you may have a system that records some variables or sensors’ readings and would like to find the maximum or minimum peaks or drops. Or maybe use this information to know if your data needs some sort of digital filtering or not. In case of high spikes in the readings recorded.

Here is an implementation in C for both functions.

MATH_DataType MIN(MATH_DataType* IN_Arr, uint32_t au32_LEN)

{

????uint32_t i = 0;

????MATH_DataType MIN = 0;

?

????for(i=0; i<au32_LEN; i++)

????{

????if(IN_Arr[i] < MIN)

????{

????????MIN = IN_Arr[i];

????}

????}

????return MIN;

}

?

MATH_DataType MAX(MATH_DataType* IN_Arr, uint32_t au32_LEN)

{

????uint32_t i = 0;

????MATH_DataType MAX = 0;

?

????for(i=0; i<au32_LEN; i++)

????{

????if(IN_Arr[i] > MAX)

????{

????????MAX = IN_Arr[i];

????}

????}

????return MAX;

}

Note that the data type being used here is defined so that the user (the programmer) can adjust it if the variable being monitored is a floating-point number of signed values that can go negative. In case of having negative numbers, you have to be careful in the MIN function as defining the min value at the beginning with 0 may not converge to the right result.

typedef uint32_t MATH_DataType;

要查看或添加评论,请登录

?smail E.的更多文章

  • Stm32G030F6P6 cheap microcontrollers series

    Stm32G030F6P6 cheap microcontrollers series

    #ArmCortex #st #stm32 #embeddedsystems #c

  • ADS1115 stm32f407

    ADS1115 stm32f407

    #stm32 #stmicroelectronics #texasinstruments #cubeide #embeddedsystems

  • Stm32 Timers

    Stm32 Timers

    Timers are one of the essential features of modern microcontrollers. They allow us to measure how long something takes…

  • STM32 ADC Read Methods

    STM32 ADC Read Methods

    We can read actually configure the ADC module to take samples (conversions) in 3 different ways. Depending on the…

社区洞察

其他会员也浏览了