foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
vishay_ntcle413e2103f102l.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_ntcle413e2103f102l.c
44  * @author foxBMS Team
45  * @date 2021-11-03 (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 =======================================================*/
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  {-40, 190953.0f}, {-35, 145953.0f}, {-30, 112440.0f}, {-25, 87285.0f}, {-20, 68260.0f}, {-15, 53762.0f},
73  {-10, 42636.0f}, {-5, 34038.0f}, {0, 27348.0f}, {5, 22108.0f}, {10, 17979.0f}, {15, 17706.0f},
74  {20, 12904.0f}, {25, 10000.0f}, {30, 8310.8f}, {35, 6941.1f}, {40, 5824.9f}, {45, 4910.6f},
75  {50, 4158.3f}, {55, 3536.2f}, {60, 3019.7f}, {65, 2588.8f}, {70, 2228.0f}, {75, 1924.6f},
76  {80, 1668.4f}, {85, 1451.3f}, {90, 1266.7f}, {95, 1109.2f}, {100, 974.26f}, {105, 858.33f}};
77 
78 /** size of the #ts_ntcle413e2103f102lLut LUT */
79 static const uint16_t ts_ntcle413e2103f102lLutSize = sizeof(ts_ntcle413e2103f102lLut) /
81 
82 /*========== Extern Constant and Variable Definitions =======================*/
83 /**
84  * Defines for calculating the ADC voltage on the ends of the operating range.
85  * The ADC voltage is calculated with the following formula:
86  *
87  * V_adc = ( ( V_supply * R_ntc ) / ( R + R_ntc ) )
88  *
89  * Depending on the position of the NTC in the voltage resistor (R_1/R_2),
90  * different R_ntc values are used for the calculation.
91  * @{
92  */
93 #if defined(TS_VISHAY_NTCLE413E2103F102L_POSITION_IN_RESISTOR_DIVIDER_IS_R_1) && \
94  (TS_VISHAY_NTCLE413E2103F102L_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true)
95 #define TS_VISHAY_NTCLE413E2103F102L_ADC_VOLTAGE_V_MAX_V \
96  ((float_t)((TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ntcle413e2103f102lLut[ts_ntcle413e2103f102lLutSize-1u].resistance_Ohm) / (ts_ntcle413e2103f102lLut[ts_ntcle413e2103f102lLutSize-1u].resistance_Ohm+TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
97 #define TS_VISHAY_NTCLE413E2103F102L_ADC_VOLTAGE_V_MIN_V \
98  ((float_t)((TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ntcle413e2103f102lLut[0u].resistance_Ohm) / (ts_ntcle413e2103f102lLut[0u].resistance_Ohm+TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
99 #else /* TS_VISHAY_NTCLE413E2103F102L_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 is false */
100 #define TS_VISHAY_NTCLE413E2103F102L_ADC_VOLTAGE_V_MIN_V \
101  ((float_t)((TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ntcle413e2103f102lLut[ts_ntcle413e2103f102lLutSize-1u].resistance_Ohm) / (ts_ntcle413e2103f102lLut[ts_ntcle413e2103f102lLutSize-1u].resistance_Ohm+TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
102 #define TS_VISHAY_NTCLE413E2103F102L_ADC_VOLTAGE_V_MAX_V \
103  ((float_t)((TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ntcle413e2103f102lLut[0u].resistance_Ohm) / (ts_ntcle413e2103f102lLut[0u].resistance_Ohm+TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
104 #endif
105 /**@}*/
106 
107 /*========== Static Function Prototypes =====================================*/
108 
109 /*========== Static Function Implementations ================================*/
110 
111 /*========== Extern Function Implementations ================================*/
112 
113 extern int16_t TS_Vis02GetTemperatureFromLut(uint16_t adcVoltage_mV) {
114  int16_t temperature_ddegC = INT16_MIN;
115  float_t adcVoltage_V = (float_t)adcVoltage_mV / TS_SCALING_FACTOR_1V_IN_MV_FLOAT; /* Convert mV to V */
116 
117  /* Check for valid ADC measurements to prevent undefined behavior */
119  /* Invalid measured ADC voltage -> sensor out of operating range or disconnected/shorted */
120  temperature_ddegC = INT16_MIN;
121  } else if (adcVoltage_V < TS_VISHAY_NTCLE413E2103F102L_ADC_VOLTAGE_V_MIN_V) {
122  /* Invalid measured ADC voltage -> sensor out of operating range or shorted/disconnected */
123  temperature_ddegC = INT16_MAX;
124  } else {
125  /* Calculate NTC resistance based on measured ADC voltage */
126 #if defined(TS_VISHAY_NTCLE413E2103F102L_POSITION_IN_RESISTOR_DIVIDER_IS_R_1) && \
127  (TS_VISHAY_NTCLE413E2103F102L_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true)
128  /* formula: R_1 = R_2 * ( ( V_supply / V_adc ) - 1 ) */
129  const float_t resistance_Ohm =
132 #else /* TS_VISHAY_NTCLE413E2103F102L_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 is false */
133  /* formula: R_2 = R_1 * ( V_2 / ( V_supply - V_adc ) ) */
134  const float_t resistance_Ohm =
136  (adcVoltage_V / (TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V - adcVoltage_V));
137 #endif /* TS_VISHAY_NTCLE413E2103F102L_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 */
138 
139  /* Variables for interpolating LUT value */
140  uint16_t between_high = 0;
141  uint16_t between_low = 0;
142  for (uint16_t i = 0u; i < ts_ntcle413e2103f102lLutSize; i++) {
143  if (resistance_Ohm < ts_ntcle413e2103f102lLut[i].resistance_Ohm) {
144  between_low = i + 1u;
145  between_high = i;
146  }
147  }
148 
149  /* Interpolate between LUT values, but do not extrapolate LUT! */
150  if (!(((between_high == 0u) && (between_low == 0u)) || /* measured resistance > maximum LUT resistance */
151  (between_low >= ts_ntcle413e2103f102lLutSize))) { /* measured resistance < minimum LUT resistance */
152  temperature_ddegC = (int16_t)MATH_LinearInterpolation(
153  ts_ntcle413e2103f102lLut[between_low].resistance_Ohm,
154  (float_t)ts_ntcle413e2103f102lLut[between_low].temperature_ddegC,
156  (float_t)ts_ntcle413e2103f102lLut[between_high].temperature_ddegC,
157  resistance_Ohm);
158  }
159  }
160 
161  /* Return temperature based on measured NTC resistance */
162  return temperature_ddegC;
163 }
164 
165 extern int16_t TS_Vis02GetTemperatureFromPolynomial(uint16_t adcVoltage_mV) {
166  (void)adcVoltage_mV;
168  int16_t temperature_ddegC = 0;
169  /* TODO this is not implemented */
170  return temperature_ddegC;
171 }
172 
173 /*========== Externalized Static Function Implementations (Unit Test) =======*/
174 #ifdef UNITY_UNIT_TEST
175 #endif
Assert macro implementation.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:251
#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
#define TS_VISHAY_NTCLE413E2103F102L_ADC_VOLTAGE_V_MIN_V
static const uint16_t ts_ntcle413e2103f102lLutSize
static const TS_TEMPERATURE_SENSOR_LUT_s ts_ntcle413e2103f102lLut[]
int16_t TS_Vis02GetTemperatureFromLut(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage.
#define TS_VISHAY_NTCLE413E2103F102L_ADC_VOLTAGE_V_MAX_V
int16_t TS_Vis02GetTemperatureFromPolynomial(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage
Resistive divider used for measuring temperature.
#define TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V
#define TS_VISHAY_NTCLE413E2103F102L_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm