foxBMS - Unit Tests  1.4.1
The foxBMS Unit Tests API Documentation
murata_ncu15xh103f6sxx.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 murata_ncu15xh103f6sxx.c
44  * @author foxBMS Team
45  * @date 2022-10-12 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup TEMPERATURE_SENSORS
49  * @prefix TS
50  *
51  * @brief Resistive divider used for measuring temperature
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "murata_ncu15xh103f6sxx.h"
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 - datasheet: RO3E - Aug. 3, 2018 */
67  {-400, 195652.0f}, {-350, 148171.0f}, {-300, 113347.0f}, {-250, 87559.0f}, {-200, 68237.0f}, {-150, 53650.0f},
68  {-100, 42506.0f}, {-50, 33892.0f}, {0, 27219.0f}, {50, 22021.0f}, {100, 17926.0f}, {150, 14674.0f},
69  {200, 12081.0f}, {250, 10000.0f}, {300, 8315.0f}, {350, 6948.0f}, {400, 5834.0f}, {450, 4917.0f},
70  {500, 4161.0f}, {550, 3535.0f}, {600, 3014.0f}, {650, 2586.0f}, {700, 2228.0f}, {750, 1925.0f},
71  {800, 1669.0f}, {850, 1452.0f}, {900, 1268.0f}, {950, 1110.0f}, {1000, 974.0f}, {1050, 858.0f},
72  {1100, 758.0f}, {1150, 672.0f}, {1200, 596.0f}, {1250, 531.0f}, {1300, 474.0f}, {1350, 424.0f},
73  {1400, 381.0f}, {1450, 342.0f}, {1500, 309.0f},
74 };
75 
76 /** size of the #ts_ntcle317e4103sbaLut LUT */
78 
79 /*========== Extern Constant and Variable Definitions =======================*/
80 /**
81  * Defines for calculating the ADC voltage on the ends of the operating range.
82  * The ADC voltage is calculated with the following formula:
83  *
84  * V_adc = ( ( V_supply * R_ntc ) / ( R + R_ntc ) )
85  *
86  * Depending on the position of the NTC in the voltage resistor (R_1/R_2),
87  * different R_ntc values are used for the calculation.
88  * @{
89  */
90 #if TS_MURATA_NCU15XH103F6SXX_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true
91 #define TS_MURATA_NCU15XH103F6SXX_ADC_VOLTAGE_V_MAX_V \
92  ((float)((TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ncu15xh103f6sxxLut[ts_ncu15xh103f6sxxLutSize-1u].resistance_Ohm) / (ts_ncu15xh103f6sxxLut[ts_ncu15xh103f6sxxLutSize-1u].resistance_Ohm+TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
93 #define TS_MURATA_NCU15XH103F6SXX_ADC_VOLTAGE_V_MIN_V \
94  ((float)((TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ncu15xh103f6sxxLut[0u].resistance_Ohm) / (ts_ncu15xh103f6sxxLut[0u].resistance_Ohm+TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
95 #else /* TS_MURATA_NCU15XH103F6SXX_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 is false */
96 #define TS_MURATA_NCU15XH103F6SXX_ADC_VOLTAGE_V_MIN_V \
97  ((float)((TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ncu15xh103f6sxxLut[ts_ncu15xh103f6sxxLutSize-1u].resistance_Ohm) / (ts_ncu15xh103f6sxxLut[ts_ncu15xh103f6sxxLutSize-1u].resistance_Ohm+TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
98 #define TS_MURATA_NCU15XH103F6SXX_ADC_VOLTAGE_V_MAX_V \
99  ((float)((TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * ts_ncu15xh103f6sxxLut[0u].resistance_Ohm) / (ts_ncu15xh103f6sxxLut[0u].resistance_Ohm+TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
100 #endif
101 /**@}*/
102 
103 /*========== Static Function Prototypes =====================================*/
104 
105 /*========== Static Function Implementations ================================*/
106 
107 /*========== Extern Function Implementations ================================*/
108 
109 extern int16_t TS_Mur00GetTemperatureFromLut(uint16_t adcVoltage_mV) {
110  int16_t temperature_ddegC = INT16_MIN;
111  float adcVoltage_V = (float)adcVoltage_mV / TS_SCALING_FACTOR_1V_IN_MV_FLOAT; /* Convert mV to V */
112 
113  /* Check for valid ADC measurements to prevent undefined behavior */
115  /* Invalid measured ADC voltage -> sensor out of operating range or disconnected/shorted */
116  temperature_ddegC = INT16_MIN;
117  } else if (adcVoltage_V < TS_MURATA_NCU15XH103F6SXX_ADC_VOLTAGE_V_MIN_V) {
118  /* Invalid measured ADC voltage -> sensor out of operating range or shorted/disconnected */
119  temperature_ddegC = INT16_MAX;
120  } else {
121  /* Calculate NTC resistance based on measured ADC voltage */
122 #if TS_MURATA_NCU15XH103F6SXX_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true
123  /* R_1 = R_2 * ( ( V_supply / V_adc ) - 1 ) */
126 #else /* TS_MURATA_NCU15XH103F6SXX_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 is false */
127  /* formula: R_2 = R_1 * ( V_2 / ( V_supply - V_adc ) ) */
128  const float resistance_Ohm =
130  (adcVoltage_V / (TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V - adcVoltage_V));
131 #endif /* TS_MURATA_NCU15XH103F6SXX_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 */
132 
133  /* Variables for interpolating LUT value */
134  uint16_t between_high = 0;
135  uint16_t between_low = 0;
136  for (uint16_t i = 0u; i < ts_ncu15xh103f6sxxLutSize; i++) {
137  if (resistance_Ohm < ts_ncu15xh103f6sxxLut[i].resistance_Ohm) {
138  between_low = i + 1u;
139  between_high = i;
140  }
141  }
142 
143  /* Interpolate between LUT values, but do not extrapolate LUT! */
144  if (!(((between_high == 0u) && (between_low == 0u)) || /* measured resistance > maximum LUT resistance */
145  (between_low >= ts_ncu15xh103f6sxxLutSize))) { /* measured resistance < minimum LUT resistance */
146  temperature_ddegC = (int16_t)MATH_LinearInterpolation(
147  ts_ncu15xh103f6sxxLut[between_low].resistance_Ohm,
148  (float)ts_ncu15xh103f6sxxLut[between_low].temperature_ddegC,
150  (float)ts_ncu15xh103f6sxxLut[between_high].temperature_ddegC,
151  resistance_Ohm);
152  }
153  }
154 
155  /* Return temperature based on measured NTC resistance */
156  return temperature_ddegC;
157 }
158 
159 extern int16_t TS_Mur00GetTemperatureFromPolynomial(uint16_t adcVoltage_mV) {
160  (void)adcVoltage_mV;
162  int16_t temperature_ddegC = 0;
163  /* TODO this is not implemented */
164  return temperature_ddegC;
165 }
166 
167 /*========== 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
int16_t TS_Mur00GetTemperatureFromLut(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage.
int16_t TS_Mur00GetTemperatureFromPolynomial(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage
static const TS_TEMPERATURE_SENSOR_LUT_s ts_ncu15xh103f6sxxLut[]
#define TS_MURATA_NCU15XH103F6SXX_ADC_VOLTAGE_V_MAX_V
#define TS_MURATA_NCU15XH103F6SXX_ADC_VOLTAGE_V_MIN_V
static const uint16_t ts_ncu15xh103f6sxxLutSize
Resistive divider used for measuring temperature.
#define TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm
#define TS_MURATA_NCU15XH103F6SXX_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V
#define TS_SCALING_FACTOR_1V_IN_MV_FLOAT