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