foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
epcos_b57251v5103j060.c
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 epcos_b57251v5103j060.c
44  * @author foxBMS Team
45  * @date 2018-10-30 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.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_b57251v5103j060.h"
57 
58 #include "foxmath.h"
60 
61 /*========== Macros and Definitions =========================================*/
62 
63 /*========== Static Constant and Variable Definitions =======================*/
64 
65 /* clang-format off */
66 /** LUT filled from higher resistance to lower resistance */
68  { -550, 961580.00f },
69  { -500, 668920.00f },
70  { -450, 471270.00f },
71  { -400, 336060.00f },
72  { -350, 242430.00f },
73  { -300, 176810.00f },
74  { -250, 130320.00f },
75  { -200, 97020.00f },
76  { -150, 72923.00f },
77  { -100, 55314.00f },
78  { -50, 42325.00f },
79  { 0, 32657.00f },
80  { 50, 25400.00f },
81  { 100, 19907.00f },
82  { 150, 15716.00f },
83  { 200, 12494.00f },
84  { 250, 10000.00f },
85  { 300, 8055.20f },
86  { 350, 6528.80f },
87  { 400, 5322.90f },
88  { 450, 4364.50f },
89  { 500, 3598.10f },
90  { 550, 2981.90f },
91  { 600, 2483.70f },
92  { 650, 2078.70f },
93  { 700, 1747.90f },
94  { 750, 1476.30f },
95  { 800, 1252.30f },
96  { 850, 1066.70f },
97  { 900, 912.27f },
98  { 950, 783.19f },
99  { 1000, 674.88f },
100  { 1050, 583.63f },
101  { 1100, 506.47f },
102  { 1150, 440.98f },
103  { 1200, 385.20f },
104  { 1250, 337.52f },
105  { 1300, 296.63f },
106  { 1350, 261.46f },
107  { 1400, 231.11f },
108  { 1450, 204.84f },
109  { 1500, 182.03f }
110 };
111 /* clang-format on */
112 
113 /** size of the #ts_b57251v5103j060Lut LUT */
115 
116 /*========== Extern Constant and Variable Definitions =======================*/
117 /**
118  * @brief Defines for calculating the ADC voltage on the ends of the operating range.
119  * @details The ADC voltage is calculated with the following formula:
120  *
121  * V_adc = ((V_supply * R_ntc) / (R + R_ntc))
122  *
123  * Depending on the position of the NTC in the voltage resistor (R1/R2),
124  * different R_ntc values are used for the calculation.
125  */
126 /**@{*/
127 #if TS_EPCOS_B57251V5103J060_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true
128 #define TS_EPCOS_B57251V5103J060_ADC_VOLTAGE_V_MAX_V \
129  ((float)((TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_b57251v5103j060Lut[b57251v5103j060LutSize-1].resistance_Ohm) / (ts_b57251v5103j060Lut[b57251v5103j060LutSize-1].resistance_Ohm+TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
130 #define TS_EPCOS_B57251V5103J060_ADC_VOLTAGE_V_MIN_V \
131  ((float)((TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_b57251v5103j060Lut[0].resistance_Ohm) / (ts_b57251v5103j060Lut[0].resistance_Ohm+TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
132 #else /* TS_EPCOS_B57251V5103J060_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == false */
133 #define TS_EPCOS_B57251V5103J060_ADC_VOLTAGE_V_MIN_V \
134  ((float)((TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_b57251v5103j060Lut[b57251v5103j060LutSize-1].resistance_Ohm) / (ts_b57251v5103j060Lut[b57251v5103j060LutSize-1].resistance_Ohm+TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
135 #define TS_EPCOS_B57251V5103J060_ADC_VOLTAGE_V_MAX_V \
136  ((float)((TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_b57251v5103j060Lut[0].resistance_Ohm) / (ts_b57251v5103j060Lut[0].resistance_Ohm+TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
137 #endif
138 /**@}*/
139 
140 /*========== Static Function Prototypes =====================================*/
141 
142 /*========== Static Function Implementations ================================*/
143 
144 /*========== Extern Function Implementations ================================*/
145 
146 extern int16_t TS_Epc00GetTemperatureFromLut(uint16_t adcVoltage_mV) {
147  /* AXIVION Routine Generic-MissingParameterAssert: adcVoltage_mV: parameter accepts whole range */
148 
149  int16_t temperature_ddegC = 0;
150  float resistance_Ohm = 0.0f;
151  float adcVoltage_V = adcVoltage_mV / 1000.0f; /* Convert mV to V */
152 
153  /* Check for valid ADC measurements to prevent undefined behavior */
155  /* Invalid measured ADC voltage -> sensor out of operating range or disconnected/shorted */
156  temperature_ddegC = INT16_MIN;
157  } else if (adcVoltage_V < TS_EPCOS_B57251V5103J060_ADC_VOLTAGE_V_MIN_V) {
158  /* Invalid measured ADC voltage -> sensor out of operating range or shorted/disconnected */
159  temperature_ddegC = INT16_MAX;
160  } else {
161  /* Calculate NTC resistance based on measured ADC voltage */
162 #if TS_EPCOS_B57251V5103J060_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true
163  /* R_1 = R_2 * ( ( V_supply / V_adc ) - 1 ) */
166 #else /* TS_EPCOS_B57251V5103J060_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == false */
167  /* R_2 = R_1 * ( V_2 / ( V_supply - V_adc ) ) */
169  (adcVoltage_V / (TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V - adcVoltage_V));
170 #endif /* TS_EPCOS_B57251V5103J060_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 */
171 
172  /* Variables for interpolating LUT value */
173  uint16_t between_high = 0;
174  uint16_t between_low = 0;
175  for (uint16_t i = 1; i < b57251v5103j060LutSize; i++) {
176  if (resistance_Ohm < ts_b57251v5103j060Lut[i].resistance_Ohm) {
177  between_low = i + 1u;
178  between_high = i;
179  }
180  }
181 
182  /* Interpolate between LUT values, but do not extrapolate LUT! */
183  if (!(((between_high == 0u) && (between_low == 0u)) || /* measured resistance > maximum LUT resistance */
184  (between_low > b57251v5103j060LutSize))) { /* measured resistance < minimum LUT resistance */
185  temperature_ddegC = (int16_t)MATH_LinearInterpolation(
186  ts_b57251v5103j060Lut[between_low].resistance_Ohm,
187  ts_b57251v5103j060Lut[between_low].temperature_ddegC,
188  ts_b57251v5103j060Lut[between_high].resistance_Ohm,
189  ts_b57251v5103j060Lut[between_high].temperature_ddegC,
190  resistance_Ohm);
191  }
192  }
193 
194  /* Return temperature based on measured NTC resistance */
195  return temperature_ddegC;
196 }
197 
198 extern int16_t TS_Epc00GetTemperatureFromPolynomial(uint16_t adcVoltage_mV) {
199  /* AXIVION Routine Generic-MissingParameterAssert: adcVoltage_mV: parameter accepts whole range */
200 
201  float temperature_degC = 0.0;
202  float vadc_V = adcVoltage_mV / 1000.0;
203  float vadc2 = vadc_V * vadc_V;
204  float vadc3 = vadc2 * vadc_V;
205  float vadc4 = vadc3 * vadc_V;
206  float vadc5 = vadc4 * vadc_V;
207  float vadc6 = vadc5 * vadc_V;
208 
209  temperature_degC = (6.8405f * vadc6) - (74.815f * vadc5) + (317.48f * vadc4) - (669.16f * vadc3) +
210  (740.82f * vadc2) - (444.97f * vadc_V) + 166.48f;
211 
212  return (int16_t)(temperature_degC * 10.0f); /* Convert deg into deci &deg;C */
213 }
214 
215 /*========== Externalized Static Function Implementations (Unit Test) =======*/
int16_t TS_Epc00GetTemperatureFromPolynomial(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage
static uint16_t b57251v5103j060LutSize
#define TS_EPCOS_B57251V5103J060_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_b57251v5103j060Lut[]
int16_t TS_Epc00GetTemperatureFromLut(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage
#define TS_EPCOS_B57251V5103J060_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_B57251V5103J060_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm
#define TS_EPCOS_B57251V5103J060_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V
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
math library for often used math functions