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