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