foxBMS  1.1.0
The foxBMS Battery Management System API Documentation
foxmath.c
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.c
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 mathlib function implementations
51  *
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "foxmath.h"
56 
57 /*========== Macros and Definitions =========================================*/
58 
59 /*========== Static Constant and Variable Definitions =======================*/
60 
61 /*========== Extern Constant and Variable Definitions =======================*/
62 
63 /*========== Static Function Prototypes =====================================*/
64 
65 /*========== Static Function Implementations ================================*/
66 
67 /*========== Extern Function Implementations ================================*/
68 
69 extern float MATH_linearInterpolation(float x1, float y1, float x2, float y2, float x_interpolate) {
70  float y_interpolate = 0.0f;
71  float slope = 0.0f;
72 
73  if (x1 != x2) {
74  /* Calculate slope */
75  slope = (y2 - y1) / (x2 - x1);
76  } else {
77  /* x values are identical -> no interpolation possible: return y1 value */
78  slope = 0;
79  }
80  /* Interpolate starting from x1/y1 */
81  y_interpolate = y1 + (slope * (x_interpolate - x1));
82 
83  return y_interpolate;
84 }
85 
86 extern uint16_t MATH_swapBytes_uint16_t(uint16_t val) {
87  return (val << 8) | (val >> 8);
88 }
89 
90 extern uint32_t MATH_swapBytes_uint32_t(uint32_t val) {
91  val = ((val << 8) & 0xFF00FF00u) | ((val >> 8) & 0xFF00FFu);
92  return (val << 16) | (val >> 16);
93 }
94 
95 extern uint64_t MATH_swapBytes_uint64_t(uint64_t val) {
96  val = ((val << 8) & 0xFF00FF00FF00FF00ull) | ((val >> 8) & 0x00FF00FF00FF00FFull);
97  val = ((val << 16) & 0xFFFF0000FFFF0000ull) | ((val >> 16) & 0x0000FFFF0000FFFFull);
98  return (val << 32) | (val >> 32);
99 }
100 
101 extern float MATH_MinimumOfTwoFloats(float value1, float value2) {
102  return fminf(value1, value2);
103 }
104 
105 extern uint8_t MATH_MinimumOfTwoUint8_t(uint8_t value1, uint8_t value2) {
106  return ((value1 < value2) ? value1 : value2);
107 }
108 
109 extern uint16_t MATH_MinimumOfTwoUint16_t(uint16_t value1, uint16_t value2) {
110  uint16_t returnvalue = value1;
111  if (returnvalue > value2) {
112  returnvalue = value2;
113  }
114  return returnvalue;
115 }
116 
117 extern int32_t MATH_AbsInt32(int32_t value) {
118  int32_t absValue = INT32_MAX;
119  if (value != INT32_MIN) {
120  absValue = labs(value);
121  }
122  return absValue;
123 }
124 
125 extern int64_t MATH_AbsInt64(int64_t value) {
126  int64_t absValue = INT64_MAX;
127  if (value != INT64_MIN) {
128  absValue = llabs(value);
129  }
130  return absValue;
131 }
132 
133 /*========== Externalized Static Function Implementations (Unit Test) =======*/
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
math library for often used math functions