foxBMS  1.1.1
The foxBMS Battery Management System API Documentation
foxmath.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file foxmath.h
44  * @author foxBMS Team
45  * @date 2018-01-18 (date of creation)
46  * @updated 2021-07-15 (date of last update)
47  * @ingroup DRIVERS
48  * @prefix MATH
49  *
50  * @brief math library for often used math functions
51  *
52  * @details This header file contains the different math function declarations.
53  * Currently the following functions are supported:
54  * - Slope
55  * - Linear interpolation
56  *
57  */
58 
59 #ifndef FOXBMS__FOXMATH_H_
60 #define FOXBMS__FOXMATH_H_
61 
62 /*========== Includes =======================================================*/
63 #include "general.h"
64 
65 #include <float.h>
66 #include <math.h>
67 #include <stdlib.h>
68 
69 /*========== Macros and Definitions =========================================*/
70 
71 /** Add defines for unit conversions */
72 #define UNIT_CONVERSION_FACTOR_1000_INTEGER (1000)
73 #define UNIT_CONVERSION_FACTOR_10_FLOAT (10.0f)
74 #define UNIT_CONVERSION_FACTOR_100_FLOAT (100.0f)
75 #define UNIT_CONVERSION_FACTOR_1000_FLOAT (1000.0f)
76 
77 /*========== Extern Constant and Variable Declarations ======================*/
78 
79 /*========== Extern Function Prototypes =====================================*/
80 
81 /**
82  * @brief Linear inter-/extrapolates a third point according to two given points
83  *
84  * @param x1: x-value of point 1
85  * @param y1: y-value of point 1
86  * @param x2: x-value of point 2
87  * @param y2: y-value of point 2
88  * @param x_interpolate: x value of interpolation point
89  *
90  * @return interpolated value (float)
91  */
92 extern float MATH_linearInterpolation(float x1, float y1, float x2, float y2, float x_interpolate);
93 
94 /**
95  * @brief Swap bytes of uint16_t value
96  * @param val: value to swap bytes: 0x1234 -> 0x3412
97  */
98 extern uint16_t MATH_swapBytes_uint16_t(uint16_t val);
99 
100 /**
101  * @brief Swap bytes of uint32_t value
102  * @param val: value to swap bytes: 0x12345678 -> 0x78563412
103  */
104 extern uint32_t MATH_swapBytes_uint32_t(uint32_t val);
105 
106 /**
107  * @brief Swap bytes of uint64_t value
108  * @param val: value to swap bytes: 0x1122334455667788 -> 0x8877665544332211
109  */
110 extern uint64_t MATH_swapBytes_uint64_t(uint64_t val);
111 
112 /**
113  * @brief Returns the minimum of the passed float values
114  * @param[in] value1 value 1
115  * @param[in] value2 value 2
116  * @return minimum value
117  */
118 extern float MATH_MinimumOfTwoFloats(float value1, float value2);
119 
120 /**
121  * @brief Returns the minimum of the passed uint8_t values
122  * @param[in] value1 value 1
123  * @param[in] value2 value 2
124  * @return minimum value
125  */
126 extern uint8_t MATH_MinimumOfTwoUint8_t(uint8_t value1, uint8_t value2);
127 
128 /**
129  * @brief Returns the minimum of the passed uint16_t values
130  * @param[in] value1 value 1
131  * @param[in] value2 value 2
132  * @return minimum value
133  */
134 extern uint16_t MATH_MinimumOfTwoUint16_t(uint16_t value1, uint16_t value2);
135 
136 /**
137  * @brief Returns the absolute value of passed int32_t value
138  * @param[in] value integer value
139  * @return absolute value or INT32_MAX if INT32_MIN is passed
140  */
141 extern int32_t MATH_AbsInt32(int32_t value);
142 
143 /**
144  * @brief Returns the absolute value of passed int64_t value
145  * @param[in] value integer value
146  * @return absolute value or INT64_MAX if INT64_MIN is passed
147  */
148 extern int64_t MATH_AbsInt64(int64_t value);
149 
150 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
151 
152 #endif /* FOXBMS__FOXMATH_H_ */
uint64_t MATH_swapBytes_uint64_t(uint64_t val)
Swap bytes of uint64_t value.
Definition: foxmath.c:95
float MATH_MinimumOfTwoFloats(float value1, float value2)
Returns the minimum of the passed float values.
Definition: foxmath.c:101
uint16_t MATH_swapBytes_uint16_t(uint16_t val)
Swap bytes of uint16_t value.
Definition: foxmath.c:86
uint8_t MATH_MinimumOfTwoUint8_t(uint8_t value1, uint8_t value2)
Returns the minimum of the passed uint8_t values.
Definition: foxmath.c:105
int32_t MATH_AbsInt32(int32_t value)
Returns the absolute value of passed int32_t value.
Definition: foxmath.c:117
uint32_t MATH_swapBytes_uint32_t(uint32_t val)
Swap bytes of uint32_t value.
Definition: foxmath.c:90
int64_t MATH_AbsInt64(int64_t value)
Returns the absolute value of passed int64_t value.
Definition: foxmath.c:125
float MATH_linearInterpolation(float x1, float y1, float x2, float y2, float x_interpolate)
Linear inter-/extrapolates a third point according to two given points.
Definition: foxmath.c:69
uint16_t MATH_MinimumOfTwoUint16_t(uint16_t value1, uint16_t value2)
Returns the minimum of the passed uint16_t values.
Definition: foxmath.c:109
General macros and definitions for the whole platform.