foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
beta.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 beta.c
44  * @author foxBMS Team
45  * @date 2020-01-17 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup TEMPERATURE_SENSORS
49  * @prefix BETA
50  *
51  * @brief Resistive divider used for measuring temperature
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "beta.h"
57 
58 #include "foxmath.h"
59 
60 #include <math.h>
61 #include <stdbool.h>
62 #include <stdint.h>
63 
64 /*========== Macros and Definitions =========================================*/
65 
66 /** inverse temperature coefficient for ideal gas */
67 #define BETA_KELVIN (273.15f)
68 
69 /*========== Static Constant and Variable Definitions =======================*/
70 
71 /*========== Extern Constant and Variable Definitions =======================*/
72 /** Defines for calculating the ADC voltage on the ends of the operating range.
73  * The ADC voltage is calculated with the following formula:
74  *
75  * V_adc = ( ( V_supply * R_ntc ) / ( R + R_ntc ) )
76  *
77  * Depending on the position of the NTC in the voltage resistor (R_1/R_2),
78  * different R_ntc values are used for the calculation.
79  */
80 /**@{*/
81 #if defined(BETA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1) && (BETA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true)
82 #define BETA_ADC_VOLTAGE_V_MAX_V \
83  (float_t)( \
84  (BETA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * BETA_ResistanceFromTemperature(1400)) / \
85  (BETA_ResistanceFromTemperature(1400) + BETA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm))
86 #define BETA_ADC_VOLTAGE_V_MIN_V \
87  (float_t)( \
88  (BETA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * BETA_ResistanceFromTemperature(-400)) / \
89  (BETA_ResistanceFromTemperature(-400) + BETA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm))
90 #else /* BETA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == false */
91 #define BETA_ADC_VOLTAGE_V_MIN_V \
92  ((float_t)((BETA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * BETA_ResistanceFromTemperature(1400)) / (BETA_ResistanceFromTemperature(1400) + BETA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
93 #define BETA_ADC_VOLTAGE_V_MAX_V \
94  ((float_t)((BETA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V * BETA_ResistanceFromTemperature(-400)) / (BETA_ResistanceFromTemperature(-400) + BETA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm)))
95 #endif
96 /**@}*/
97 
98 /*========== Static Function Prototypes =====================================*/
99 
100 /*========== Static Function Implementations ================================*/
101 
102 /*========== Extern Function Implementations ================================*/
103 
104 extern int16_t BETA_GetTemperatureFromBeta(uint16_t adcVoltage_mV) {
105  int16_t temperature_ddegC = 0;
106  float_t resistance_Ohm = 0.0f;
107  float_t adcVoltage_V = (float_t)adcVoltage_mV / 1000.0f; /* Convert mV to V */
108 
109  /* Check for valid ADC measurements to prevent undefined behavior */
110  if (adcVoltage_V > BETA_ADC_VOLTAGE_V_MAX_V) {
111  /* Invalid measured ADC voltage -> sensor out of operating range or disconnected/shorted */
112  temperature_ddegC = INT16_MIN;
113  } else if (adcVoltage_V < BETA_ADC_VOLTAGE_V_MIN_V) {
114  /* Invalid measured ADC voltage -> sensor out of operating range or shorted/disconnected */
115  temperature_ddegC = INT16_MAX;
116  } else {
117  /* Calculate NTC resistance based on measured ADC voltage */
118 #if defined(BETA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1) && (BETA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == true)
119  /* R_1 = R_2 * ( ( V_supply / V_adc ) - 1 ) */
121  ((BETA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V / adcVoltage_V) - 1);
122 #else /* BETA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 == false */
123  /* R_2 = R_1 * ( V_2 / (V_supply - V_adc ) ) */
125  (adcVoltage_V / (BETA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V - adcVoltage_V));
126 #endif /* BETA_POSITION_IN_RESISTOR_DIVIDER_IS_R_1 */
127  /* Use BETA formula to compute temperature with resistance*/
128  temperature_ddegC = BETA_TemperatureFromResistance(resistance_Ohm);
129  }
130 
131  /* Return temperature based on measured NTC resistance */
132  return temperature_ddegC;
133 }
134 
135 extern int16_t BETA_TemperatureFromResistance(float_t resistance_Ohm) {
136  int16_t temperature_ddegC = 0;
137  if (resistance_Ohm > 0.0f) {
138  float_t temperature_degC = (1.0f / ((log(resistance_Ohm / BETA_R_REF_Ohm) / BETA_BETACOEFFICIENT) +
139  (1.0f / (BETA_T_REF_C + BETA_KELVIN)))) -
140  BETA_KELVIN;
141  temperature_ddegC = (int16_t)(10.0f * temperature_degC); /* Convert to deci &deg;C */
142  } else {
143  /* Invalid value if as resistance can not be negative */
144  temperature_ddegC = INT16_MIN;
145  }
146  return temperature_ddegC;
147 }
148 
149 extern float_t BETA_ResistanceFromTemperature(int16_t temperature_ddegC) {
150  float_t resistance_Ohm = 0.0f;
151  resistance_Ohm = BETA_R_REF_Ohm *
152  exp(BETA_BETACOEFFICIENT * ((1.0f / (((float_t)temperature_ddegC / 10.0f) + BETA_KELVIN)) -
153  (1.0f / (BETA_T_REF_C + BETA_KELVIN))));
154  return resistance_Ohm;
155 }
156 
157 /*========== Externalized Static Function Implementations (Unit Test) =======*/
158 #ifdef UNITY_UNIT_TEST
159 #endif
float_t BETA_ResistanceFromTemperature(int16_t temperature_ddegC)
returns NTC resistance corresponding to temperature, used to compute Vmin and Vmax of the divider
Definition: beta.c:149
#define BETA_KELVIN
Definition: beta.c:67
int16_t BETA_TemperatureFromResistance(float_t resistance_Ohm)
returns temperature corresponding to NTC resistance
Definition: beta.c:135
#define BETA_ADC_VOLTAGE_V_MAX_V
Definition: beta.c:82
int16_t BETA_GetTemperatureFromBeta(uint16_t adcVoltage_mV)
returns temperature based on measured ADC voltage
Definition: beta.c:104
#define BETA_ADC_VOLTAGE_V_MIN_V
Definition: beta.c:86
Resistive divider used for measuring temperature.
#define BETA_RESISTOR_DIVIDER_RESISTANCE_R_1_R_2_Ohm
Definition: beta.h:101
#define BETA_T_REF_C
Definition: beta.h:104
#define BETA_R_REF_Ohm
Definition: beta.h:107
#define BETA_BETACOEFFICIENT
Definition: beta.h:110
#define BETA_RESISTOR_DIVIDER_SUPPLY_VOLTAGE_V
Definition: beta.h:95
math library for often used math functions