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