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