foxBMS  1.5.0
The foxBMS Battery Management System API Documentation
vishay_ntcle317e4103sba.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 vishay_ntcle317e4103sba.c
44  * @author foxBMS Team
45  * @date 2021-11-03 (date of creation)
46  * @updated 2023-02-03 (date of last update)
47  * @version v1.5.0
48  * @ingroup TEMPERATURE_SENSORS
49  * @prefix TS
50  *
51  * @brief Resistive divider used for measuring temperature
52  *
53  */
54 
55 /*========== Includes =======================================================*/
57 
58 #include "fassert.h"
59 #include "foxmath.h"
61 
62 #include <math.h>
63 #include <stdbool.h>
64 #include <stdint.h>
65 
66 /*========== Macros and Definitions =========================================*/
67 
68 /*========== Static Constant and Variable Definitions =======================*/
69 
70 /** LUT filled from higher resistance to lower resistance */
72  {-550, 908973.8f}, {-540, 845010.0f}, {-530, 785955.0f}, {-520, 731403.2f}, {-510, 680984.5f}, {-500, 634361.3f},
73  {-490, 591225.5f}, {-480, 551295.7f}, {-470, 514314.8f}, {-460, 480047.7f}, {-450, 448279.4f}, {-440, 418812.9f},
74  {-430, 391468.1f}, {-420, 366079.9f}, {-410, 342496.8f}, {-400, 320580.1f}, {-390, 300202.2f}, {-380, 281246.4f},
75  {-370, 263605.0f}, {-360, 247179.4f}, {-350, 231878.9f}, {-340, 217619.8f}, {-330, 204325.3f}, {-320, 191924.7f},
76  {-310, 180352.7f}, {-300, 169549.3f}, {-290, 159459.0f}, {-280, 150030.8f}, {-270, 141217.3f}, {-260, 132975.1f},
77  {-250, 125263.9f}, {-240, 118046.5f}, {-230, 111288.5f}, {-220, 104958.2f}, {-210, 99025.9f}, {-200, 93464.6f},
78  {-190, 88248.8f}, {-180, 83355.3f}, {-170, 78762.3f}, {-160, 74449.7f}, {-150, 70398.9f}, {-140, 66592.4f},
79  {-130, 63014.3f}, {-120, 59649.6f}, {-110, 56484.3f}, {-100, 53505.6f}, {-90, 50701.4f}, {-80, 48060.6f},
80  {-70, 45572.8f}, {-60, 43228.3f}, {-50, 41017.9f}, {-40, 38933.5f}, {-30, 36967.0f}, {-20, 35111.2f},
81  {-10, 33359.3f}, {0, 31704.8f}, {10, 30141.9f}, {20, 28665.0f}, {30, 27268.9f}, {40, 25948.8f},
82  {50, 24700.1f}, {60, 23518.5f}, {70, 22400.2f}, {80, 21341.4f}, {90, 20338.6f}, {100, 19388.5f},
83  {110, 18488.2f}, {120, 17634.7f}, {130, 16825.4f}, {140, 16057.8f}, {150, 15329.4f}, {160, 14638.2f},
84  {170, 13981.9f}, {180, 13358.7f}, {190, 12766.7f}, {200, 12204.2f}, {210, 11669.6f}, {220, 11161.3f},
85  {230, 10677.9f}, {240, 10218.2f}, {250, 9780.5f}, {260, 9363.4f}, {270, 8965.9f}, {280, 8587.8f},
86  {290, 8227.4f}, {300, 7884.4f}, {310, 7557.1f}, {320, 7245.6f}, {330, 6948.3f}, {340, 6665.1f},
87  {350, 6394.6f}, {360, 6136.5f}, {370, 5890.5f}, {380, 5655.4f}, {390, 5431.0f}, {400, 5216.6f},
88  {410, 5012.1f}, {420, 4816.4f}, {430, 4629.4f}, {440, 4450.6f}, {450, 4279.7f}, {460, 4116.5f},
89  {470, 3960.1f}, {480, 3810.4f}, {490, 3667.2f}, {500, 3530.2f}, {510, 3398.9f}, {520, 3273.2f},
90  {530, 3152.8f}, {540, 3037.5f}, {550, 2926.9f}, {560, 2821.0f}, {570, 2719.4f}, {580, 2622.0f},
91  {590, 2528.6f}, {600, 2439.0f}, {610, 2353.0f}, {620, 2270.5f}, {630, 2191.3f}, {640, 2115.2f},
92  {650, 2042.2f}, {660, 1971.9f}, {670, 1904.5f}, {680, 1839.8f}, {690, 1777.6f}, {700, 1717.8f},
93  {710, 1660.2f}, {720, 1604.9f}, {730, 1551.7f}, {740, 1500.6f}, {750, 1451.3f}, {760, 1403.9f},
94  {770, 1358.3f}, {780, 1314.4f}, {790, 1272.1f}, {800, 1231.5f}, {810, 1192.2f}, {820, 1154.5f},
95  {830, 1118.1f}, {840, 1083.0f}, {850, 1049.2f}, {860, 1016.4f}, {870, 984.7f}, {880, 954.2f},
96  {890, 924.8f}, {900, 896.4f}, {910, 869.1f}, {920, 842.7f}, {930, 817.2f}, {940, 792.6f},
97  {950, 768.9f}, {960, 745.9f}, {970, 723.8f}, {980, 702.5f}, {990, 681.8f}, {1000, 661.9f},
98  {1010, 642.6f}, {1020, 624.0f}, {1030, 606.0f}, {1040, 588.7f}, {1050, 571.9f}, {1060, 555.6f},
99  {1070, 539.9f}, {1080, 524.7f}, {1090, 510.0f}, {1100, 495.8f}, {1110, 482.0f}, {1120, 468.7f},
100  {1130, 455.8f}, {1140, 443.4f}, {1150, 431.3f}, {1160, 419.6f}, {1170, 408.3f}, {1180, 397.3f},
101  {1190, 386.7f}, {1200, 376.4f}, {1210, 366.4f}, {1220, 356.7f}, {1230, 347.4f}, {1240, 338.3f},
102  {1250, 329.5f}, {1260, 321.0f}, {1270, 312.7f}, {1280, 304.7f}, {1290, 296.9f}, {1300, 289.3f},
103  {1310, 282.0f}, {1320, 274.9f}, {1330, 268.0f}, {1340, 261.3f}, {1350, 254.8f}, {1360, 248.5f},
104  {1370, 242.4f}, {1380, 236.4f}, {1390, 230.6f}, {1400, 225.0f}, {1410, 219.6f}, {1420, 214.3f},
105  {1430, 209.1f}, {1440, 204.1f}, {1450, 199.3f}, {1460, 194.5f}, {1470, 189.9f}, {1480, 185.5f},
106  {1490, 181.1f}, {1500, 176.9f},
107 };
108 
109 /** size of the #ts_ntcle317e4103sbaLut LUT */
111 
112 /*========== Extern Constant and Variable Definitions =======================*/
113 /**
114  * Defines for calculating the ADC voltage on the ends of the operating range.
115  * The ADC voltage is calculated with the following formula:
116  *
117  * V_adc = ( ( V_supply * R_ntc ) / ( R + R_ntc ) )
118  *
119  * Depending on the position of the NTC in the voltage resistor (R_1/R_2),
120  * different R_ntc values are used for the calculation.
121  * @{
122  */
123 #if defined(TS_VISHAY_NTCLE317E4103SBA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1) && \
124  (TS_VISHAY_NTCLE317E4103SBA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true)
125 #define TS_VISHAY_NTCLE317E4103SBA_ADC_VOLTAGE_V_MAX_V \
126  ((float_t)((TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ntcle317e4103sbaLut[ts_ntcle317e4103sbaLutSize-1u].resistance_Ohm) / (ts_ntcle317e4103sbaLut[ts_ntcle317e4103sbaLutSize-1u].resistance_Ohm+TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
127 #define TS_VISHAY_NTCLE317E4103SBA_ADC_VOLTAGE_V_MIN_V \
128  ((float_t)((TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ntcle317e4103sbaLut[0u].resistance_Ohm) / (ts_ntcle317e4103sbaLut[0u].resistance_Ohm+TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
129 #else /* TS_VISHAY_NTCLE317E4103SBA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 is false */
130 #define TS_VISHAY_NTCLE317E4103SBA_ADC_VOLTAGE_V_MIN_V \
131  ((float_t)((TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ntcle317e4103sbaLut[ts_ntcle317e4103sbaLutSize-1u].resistance_Ohm) / (ts_ntcle317e4103sbaLut[ts_ntcle317e4103sbaLutSize-1u].resistance_Ohm+TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
132 #define TS_VISHAY_NTCLE317E4103SBA_ADC_VOLTAGE_V_MAX_V \
133  ((float_t)((TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ntcle317e4103sbaLut[0u].resistance_Ohm) / (ts_ntcle317e4103sbaLut[0u].resistance_Ohm+TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
134 #endif
135 /**@}*/
136 
137 /*========== Static Function Prototypes =====================================*/
138 
139 /*========== Static Function Implementations ================================*/
140 
141 /*========== Extern Function Implementations ================================*/
142 
143 extern int16_t TS_Vis01GetTemperatureFromLut(uint16_t adcVoltage_mV) {
144  int16_t temperature_ddegC = INT16_MIN;
145  float_t adcVoltage_V = (float_t)adcVoltage_mV / TS_SCALING_FACTOR_1V_IN_MV_FLOAT; /* Convert mV to V */
146 
147  /* Check for valid ADC measurements to prevent undefined behavior */
149  /* Invalid measured ADC voltage -> sensor out of operating range or disconnected/shorted */
150  temperature_ddegC = INT16_MIN;
151  } else if (adcVoltage_V < TS_VISHAY_NTCLE317E4103SBA_ADC_VOLTAGE_V_MIN_V) {
152  /* Invalid measured ADC voltage -> sensor out of operating range or shorted/disconnected */
153  temperature_ddegC = INT16_MAX;
154  } else {
155  /* Calculate NTC resistance based on measured ADC voltage */
156 #if defined(TS_VISHAY_NTCLE317E4103SBA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1) && \
157  (TS_VISHAY_NTCLE317E4103SBA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true)
158  /* R_1 = R_2 * ( ( V_supply / V_adc ) - 1 ) */
159  const float_t resistance_Ohm =
162 #else /* TS_VISHAY_NTCLE317E4103SBA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 is false */
163  /* formula: R_2 = R_1 * ( V_2 / ( V_supply - V_adc ) ) */
164  const float_t resistance_Ohm =
166  (adcVoltage_V / (TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V - adcVoltage_V));
167 #endif /* TS_VISHAY_NTCLE317E4103SBA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 */
168 
169  /* Variables for interpolating LUT value */
170  uint16_t between_high = 0;
171  uint16_t between_low = 0;
172  for (uint16_t i = 0u; i < ts_ntcle317e4103sbaLutSize; i++) {
173  if (resistance_Ohm < ts_ntcle317e4103sbaLut[i].resistance_Ohm) {
174  between_low = i + 1u;
175  between_high = i;
176  }
177  }
178 
179  /* Interpolate between LUT values, but do not extrapolate LUT! */
180  if (!(((between_high == 0u) && (between_low == 0u)) || /* measured resistance > maximum LUT resistance */
181  (between_low >= ts_ntcle317e4103sbaLutSize))) { /* measured resistance < minimum LUT resistance */
182  temperature_ddegC = (int16_t)MATH_LinearInterpolation(
183  ts_ntcle317e4103sbaLut[between_low].resistance_Ohm,
184  (float_t)ts_ntcle317e4103sbaLut[between_low].temperature_ddegC,
186  (float_t)ts_ntcle317e4103sbaLut[between_high].temperature_ddegC,
187  resistance_Ohm);
188  }
189  }
190 
191  /* Return temperature based on measured NTC resistance */
192  return temperature_ddegC;
193 }
194 
195 extern int16_t TS_Vis01GetTemperatureFromPolynomial(uint16_t adcVoltage_mV) {
196  (void)adcVoltage_mV;
198  int16_t temperature_ddegC = 0;
199  /* TODO this is not implemented */
200  return temperature_ddegC;
201 }
202 
203 /*========== Externalized Static Function Implementations (Unit Test) =======*/
204 #ifdef UNITY_UNIT_TEST
205 #endif
Assert macro implementation.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:254
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:129
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
#define TS_SCALING_FACTOR_1V_IN_MV_FLOAT
int16_t TS_Vis01GetTemperatureFromPolynomial(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage
static const TS_TEMPERATURE_SENSOR_LUT_s ts_ntcle317e4103sbaLut[]
#define TS_VISHAY_NTCLE317E4103SBA_ADC_VOLTAGE_V_MAX_V
#define TS_VISHAY_NTCLE317E4103SBA_ADC_VOLTAGE_V_MIN_V
static const uint16_t ts_ntcle317e4103sbaLutSize
int16_t TS_Vis01GetTemperatureFromLut(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage.
Resistive divider used for measuring temperature.
#define TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V
#define TS_VISHAY_NTCLE317E4103SBA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm