foxBMS  1.1.2
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-08-06 (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 /* AXIVION Disable Style Generic-LocalInclude: foxmath is intended as a library and therefore includes all useful libraries */
66 #include <float.h>
67 #include <math.h>
68 #include <stdlib.h>
69 /* AXIVION Enable Style Generic-LocalInclude: */
70 
71 /*========== Macros and Definitions =========================================*/
72 
73 /** Add defines for unit conversions */
74 #define UNIT_CONVERSION_FACTOR_1000_INTEGER (1000)
75 #define UNIT_CONVERSION_FACTOR_10_FLOAT (10.0f)
76 #define UNIT_CONVERSION_FACTOR_100_FLOAT (100.0f)
77 #define UNIT_CONVERSION_FACTOR_1000_FLOAT (1000.0f)
78 
79 /*========== Extern Constant and Variable Declarations ======================*/
80 
81 /*========== Extern Function Prototypes =====================================*/
82 /**
83  * @brief: self test for math functions that can be called at startup
84  *
85  *@details: This self test is intended to be called at startup. It will assert
86  * if one of the assumptions is violated. This function can be used
87  * to make sure that features of foxmath that are currently not used
88  * by the code are working nevertheless as expected.
89  */
90 extern void MATH_StartupSelfTest(void);
91 
92 /**
93  * @brief Linear inter-/extrapolates a third point according to two given points
94  *
95  * @param x1: x-value of point 1
96  * @param y1: y-value of point 1
97  * @param x2: x-value of point 2
98  * @param y2: y-value of point 2
99  * @param x_interpolate: x value of interpolation point
100  *
101  * @return interpolated value (float)
102  */
103 extern float MATH_LinearInterpolation(
104  const float x1,
105  const float y1,
106  const float x2,
107  const float y2,
108  const float x_interpolate);
109 
110 /**
111  * @brief Swap bytes of uint16_t value
112  * @param val: value to swap bytes: 0x1234 -> 0x3412
113  */
114 extern uint16_t MATH_SwapBytesUint16_t(const uint16_t val);
115 
116 /**
117  * @brief Swap bytes of uint32_t value
118  * @param val: value to swap bytes: 0x12345678 -> 0x78563412
119  */
120 extern uint32_t MATH_SwapBytesUint32_t(const uint32_t val);
121 
122 /**
123  * @brief Swap bytes of uint64_t value
124  * @param val: value to swap bytes: 0x1122334455667788 -> 0x8877665544332211
125  */
126 extern uint64_t MATH_SwapBytesUint64_t(const uint64_t val);
127 
128 /**
129  * @brief Returns the minimum of the passed float values
130  * @param[in] value1 value 1
131  * @param[in] value2 value 2
132  * @return minimum value
133  */
134 extern float MATH_MinimumOfTwoFloats(const float value1, const float value2);
135 
136 /**
137  * @brief Returns the minimum of the passed uint8_t values
138  * @param[in] value1 value 1
139  * @param[in] value2 value 2
140  * @return minimum value
141  */
142 extern uint8_t MATH_MinimumOfTwoUint8_t(const uint8_t value1, const uint8_t value2);
143 
144 /**
145  * @brief Returns the minimum of the passed uint16_t values
146  * @param[in] value1 value 1
147  * @param[in] value2 value 2
148  * @return minimum value
149  */
150 extern uint16_t MATH_MinimumOfTwoUint16_t(const uint16_t value1, const uint16_t value2);
151 
152 /**
153  * @brief Returns the absolute value of passed int32_t value
154  * @param[in] value integer value
155  * @return absolute value or INT32_MAX if INT32_MIN is passed
156  */
157 extern int32_t MATH_AbsInt32_t(const int32_t value);
158 
159 /**
160  * @brief Returns the absolute value of passed int64_t value
161  * @param[in] value integer value
162  * @return absolute value or INT64_MAX if INT64_MIN is passed
163  */
164 extern int64_t MATH_AbsInt64_t(const int64_t value);
165 
166 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
167 
168 #endif /* FOXBMS__FOXMATH_H_ */
int32_t MATH_AbsInt32_t(const int32_t value)
Returns the absolute value of passed int32_t value.
Definition: foxmath.c:152
float MATH_LinearInterpolation(const float x1, const float y1, const float x2, const float y2, const float x_interpolate)
Linear inter-/extrapolates a third point according to two given points.
Definition: foxmath.c:84
uint16_t MATH_SwapBytesUint16_t(const uint16_t val)
Swap bytes of uint16_t value.
Definition: foxmath.c:107
int64_t MATH_AbsInt64_t(const int64_t value)
Returns the absolute value of passed int64_t value.
Definition: foxmath.c:160
uint16_t MATH_MinimumOfTwoUint16_t(const uint16_t value1, const uint16_t value2)
Returns the minimum of the passed uint16_t values.
Definition: foxmath.c:144
uint32_t MATH_SwapBytesUint32_t(const uint32_t val)
Swap bytes of uint32_t value.
Definition: foxmath.c:111
void MATH_StartupSelfTest(void)
: self test for math functions that can be called at startup
Definition: foxmath.c:79
uint8_t MATH_MinimumOfTwoUint8_t(const uint8_t value1, const uint8_t value2)
Returns the minimum of the passed uint8_t values.
Definition: foxmath.c:136
float MATH_MinimumOfTwoFloats(const float value1, const float value2)
Returns the minimum of the passed float values.
Definition: foxmath.c:132
uint64_t MATH_SwapBytesUint64_t(const uint64_t val)
Swap bytes of uint64_t value.
Definition: foxmath.c:119
General macros and definitions for the whole platform.