foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
epcos_b57861s0103f045.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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 epcos_b57861s0103f045.c
44  * @author foxBMS Team
45  * @date 2018-10-30 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup TEMPERATURE_SENSORS
49  * @prefix TS
50  *
51  * @brief Resistive divider used for measuring temperature
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "epcos_b57861s0103f045.h"
57 
58 #include "foxmath.h"
60 
61 #include <math.h>
62 #include <stdbool.h>
63 #include <stdint.h>
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /*========== Static Constant and Variable Definitions =======================*/
68 
69 /* clang-format off */
70 /** LUT filled from higher resistance to lower resistance */
72  { -550, 963000.00f },
73  { -500, 670100.00f },
74  { -450, 471700.00f },
75  { -400, 336500.00f },
76  { -350, 242600.00f },
77  { -300, 177000.00f },
78  { -250, 130400.00f },
79  { -200, 97070.00f },
80  { -150, 72930.00f },
81  { -100, 55330.00f },
82  { -50, 42320.00f },
83  { 0, 32650.00f },
84  { 50, 25390.00f },
85  { 100, 19900.00f },
86  { 150, 15710.00f },
87  { 200, 12490.00f },
88  { 250, 10000.00f },
89  { 300, 8057.00f },
90  { 350, 6531.00f },
91  { 400, 5327.00f },
92  { 450, 4369.00f },
93  { 500, 3603.00f },
94  { 550, 2986.00f },
95  { 600, 2488.00f },
96  { 650, 2083.00f },
97  { 700, 1752.00f },
98  { 750, 1481.00f },
99  { 800, 1258.00f },
100  { 850, 1072.00f },
101  { 900, 917.70f },
102  { 950, 788.50f },
103  { 1000, 680.00f },
104  { 1050, 588.60f },
105  { 1100, 511.20f },
106  { 1150, 445.40f },
107  { 1200, 389.30f },
108  { 1250, 341.70f },
109  { 1300, 300.90f },
110  { 1350, 265.40f },
111  { 1400, 234.80f },
112  { 1450, 208.30f },
113  { 1500, 185.30f },
114  { 1550, 165.30f }
115 };
116 /* clang-format on */
117 
118 /** size of the #ts_b57861s0103f045Lut LUT */
120 
121 /*========== Extern Constant and Variable Definitions =======================*/
122 /**
123  * @brief Defines for calculating the ADC voltage on the ends of the operating range.
124  * @details The ADC voltage is calculated with the following formula:
125  *
126  * V_adc = ((V_supply * R_ntc) / (R + R_ntc))
127  *
128  * Depending on the position of the NTC in the voltage resistor (R1/R2),
129  * different R_ntc values are used for the calculation.
130  */
131 /**@{*/
132 #if defined(TS_EPCOS_B57861S0103F045_POSITION_IN_RESISTOR_DIVIDER_IS_R_1) && \
133  (TS_EPCOS_B57861S0103F045_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true)
134 #define TS_EPCOS_B57861S0103F045_ADC_VOLTAGE_V_MAX_V \
135  (float_t)( \
136  (TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * \
137  ts_b57861s0103f045Lut[ts_b57861s0103f045LutSize - 1].resistance_Ohm) / \
138  (ts_b57861s0103f045Lut[ts_b57861s0103f045LutSize - 1].resistance_Ohm + \
139  TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm))
140 #define TS_EPCOS_B57861S0103F045_ADC_VOLTAGE_V_MIN_V \
141  (float_t)( \
142  (TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_b57861s0103f045Lut[0].resistance_Ohm) / \
143  (ts_b57861s0103f045Lut[0].resistance_Ohm + TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm))
144 #else /* TS_EPCOS_B57861S0103F045_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == false */
145 #define TS_EPCOS_B57861S0103F045_ADC_VOLTAGE_V_MIN_V \
146  ((float_t)((TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_b57861s0103f045Lut[ts_b57861s0103f045LutSize-1].resistance_Ohm) / (ts_b57861s0103f045Lut[ts_b57861s0103f045LutSize-1].resistance_Ohm+TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
147 #define TS_EPCOS_B57861S0103F045_ADC_VOLTAGE_V_MAX_V \
148  ((float_t)((TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_b57861s0103f045Lut[0].resistance_Ohm) / (ts_b57861s0103f045Lut[0].resistance_Ohm+TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
149 #endif
150 /**@}*/
151 
152 /*========== Static Function Prototypes =====================================*/
153 
154 /*========== Static Function Implementations ================================*/
155 
156 /*========== Extern Function Implementations ================================*/
157 
158 extern int16_t TS_Epc01GetTemperatureFromLut(uint16_t adcVoltage_mV) {
159  int16_t temperature_ddegC = 0;
160  float_t resistance_Ohm = 0.0f;
161  float_t adcVoltage_V = adcVoltage_mV / 1000.0f; /* Convert mV to V */
162 
163  /* Check for valid ADC measurements to prevent undefined behavior */
165  /* Invalid measured ADC voltage -> sensor out of operating range or disconnected/shorted */
166  temperature_ddegC = INT16_MIN;
167  } else if (adcVoltage_V < TS_EPCOS_B57861S0103F045_ADC_VOLTAGE_V_MIN_V) {
168  /* Invalid measured ADC voltage -> sensor out of operating range or shorted/disconnected */
169  temperature_ddegC = INT16_MAX;
170  } else {
171  /* Calculate NTC resistance based on measured ADC voltage */
172 #if defined(TS_EPCOS_B57861S0103F045_POSITION_IN_RESISTOR_DIVIDER_IS_R_1) && \
173  (TS_EPCOS_B57861S0103F045_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true)
174  /* R_1 = R_2 * ( ( V_supply / V_adc ) - 1 ) */
177 #else /* TS_EPCOS_B57861S0103F045_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == false */
178  /* R_2 = R_1 * ( V_2 / ( V_supply - V_adc ) ) */
180  (adcVoltage_V / (TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V - adcVoltage_V));
181 #endif /* TS_EPCOS_B57861S0103F045_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 */
182 
183  /* Variables for interpolating LUT value */
184  uint16_t between_high = 0;
185  uint16_t between_low = 0;
186  for (uint16_t i = 1; i < ts_b57861s0103f045LutSize; i++) {
187  if (resistance_Ohm < ts_b57861s0103f045Lut[i].resistance_Ohm) {
188  between_low = i + 1u;
189  between_high = i;
190  }
191  }
192 
193  /* Interpolate between LUT values, but do not extrapolate LUT! */
194  if (!(((between_high == 0u) && (between_low == 0u)) || /* measured resistance > maximum LUT resistance */
195  (between_low >= ts_b57861s0103f045LutSize))) { /* measured resistance < minimum LUT resistance */
196  temperature_ddegC = (int16_t)MATH_LinearInterpolation(
197  ts_b57861s0103f045Lut[between_low].resistance_Ohm,
198  ts_b57861s0103f045Lut[between_low].temperature_ddegC,
199  ts_b57861s0103f045Lut[between_high].resistance_Ohm,
200  ts_b57861s0103f045Lut[between_high].temperature_ddegC,
201  resistance_Ohm);
202  }
203  }
204 
205  /* Return temperature based on measured NTC resistance */
206  return temperature_ddegC;
207 }
208 
209 extern int16_t TS_Epc01GetTemperatureFromPolynomial(uint16_t adcVoltage_mV) {
210  float_t temperature_degC = 0.0f;
211  float_t vadc_V = adcVoltage_mV / 1000.0f;
212  float_t vadc2 = vadc_V * vadc_V;
213  float_t vadc3 = vadc2 * vadc_V;
214  float_t vadc4 = vadc3 * vadc_V;
215  float_t vadc5 = vadc4 * vadc_V;
216 
217  /* 5th grade polynomial for EPCOS B57861S0103F045 NTC-Thermistor, 10 kOhm, Series B57861S, Vref = 3V, R in series 10k */
218  temperature_degC = (-6.2765f * vadc5) + (49.0397f * vadc4) - (151.3602f * vadc3) + (233.2521f * vadc2) -
219  (213.4588f * vadc_V) + 130.5822f;
220 
221  return (int16_t)(temperature_degC * 10.0f); /* Convert to deci &deg;C */
222 }
223 
224 /*========== Externalized Static Function Implementations (Unit Test) =======*/
225 #ifdef UNITY_UNIT_TEST
226 #endif
int16_t TS_Epc01GetTemperatureFromPolynomial(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage
static uint16_t ts_b57861s0103f045LutSize
int16_t TS_Epc01GetTemperatureFromLut(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage
#define TS_EPCOS_B57861S0103F045_ADC_VOLTAGE_V_MAX_V
Defines for calculating the ADC voltage on the ends of the operating range.
static const TS_TEMPERATURE_SENSOR_LUT_s ts_b57861s0103f045Lut[]
#define TS_EPCOS_B57861S0103F045_ADC_VOLTAGE_V_MIN_V
Defines for calculating the ADC voltage on the ends of the operating range.
Resistive divider used for measuring temperature.
#define TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V
#define TS_EPCOS_B57861S0103F045_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm
float_t MATH_LinearInterpolation(const float_t x1, const float_t y1, const float_t x2, const float_t y2, const float_t x_interpolate)
Linear inter-/extrapolates a third point according to two given points.
Definition: foxmath.c:84
math library for often used math functions