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