foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
can_cbs_tx_pack-minimum-maximum-values.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 can_cbs_tx_pack-minimum-maximum-values.c
44  * @author foxBMS Team
45  * @date 2021-04-20 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVER
49  * @prefix CANTX
50  *
51  * @brief CAN driver Tx callback implementation
52  * @details CAN Tx callback for min/max values
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "bms.h"
57 #include "can_cbs_tx.h"
59 #include "can_helper.h"
60 
61 #include <math.h>
62 #include <stdint.h>
63 
64 /*========== Macros and Definitions =========================================*/
65 
66 /*========== Static Constant and Variable Definitions =======================*/
67 
68 /*========== Extern Constant and Variable Definitions =======================*/
69 
70 /*========== Static Function Prototypes =====================================*/
71 
72 /*========== Static Function Implementations ================================*/
73 
74 /*========== Extern Function Implementations ================================*/
77  uint8_t *pCanData,
78  uint8_t *pMuxId,
79  const CAN_SHIM_s *const kpkCanShim) {
83  FAS_ASSERT(pCanData != NULL_PTR);
84  FAS_ASSERT(pMuxId == NULL_PTR); /* pMuxId is not used here, therefore has to be NULL_PTR */
85  FAS_ASSERT(kpkCanShim != NULL_PTR);
86  uint64_t messageData = 0u;
87 
88  DATA_READ_DATA(kpkCanShim->pTableMinMax);
89 
90  int16_t packMaximumVoltage_mV = INT16_MIN;
91  int16_t packMinimumVoltage_mV = INT16_MAX;
92  int16_t packMaximumTemperature_ddegC = INT16_MIN;
93  int16_t packMinimumTemperature_ddegC = INT16_MAX;
94 
95  if (0u == BMS_GetNumberOfConnectedStrings()) {
96  /* Calculate min/max values of complete pack if all slice switches are open */
97  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
98  if (kpkCanShim->pTableMinMax->maximumCellVoltage_mV[s] >= packMaximumVoltage_mV) {
99  packMaximumVoltage_mV = kpkCanShim->pTableMinMax->maximumCellVoltage_mV[s];
100  }
101  if (kpkCanShim->pTableMinMax->minimumCellVoltage_mV[s] <= packMinimumVoltage_mV) {
102  packMinimumVoltage_mV = kpkCanShim->pTableMinMax->minimumCellVoltage_mV[s];
103  }
104  if (kpkCanShim->pTableMinMax->maximumTemperature_ddegC[s] >= packMaximumTemperature_ddegC) {
105  packMaximumTemperature_ddegC = kpkCanShim->pTableMinMax->maximumTemperature_ddegC[s];
106  }
107  if (kpkCanShim->pTableMinMax->minimumTemperature_ddegC[s] <= packMinimumTemperature_ddegC) {
108  packMinimumTemperature_ddegC = kpkCanShim->pTableMinMax->minimumTemperature_ddegC[s];
109  }
110  }
111  } else {
112  /* Calculate min/max values of connected slices */
113  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
114  if (BMS_IsStringClosed(s) == true) {
115  if (kpkCanShim->pTableMinMax->maximumCellVoltage_mV[s] >= packMaximumVoltage_mV) {
116  packMaximumVoltage_mV = kpkCanShim->pTableMinMax->maximumCellVoltage_mV[s];
117  }
118  if (kpkCanShim->pTableMinMax->minimumCellVoltage_mV[s] <= packMinimumVoltage_mV) {
119  packMinimumVoltage_mV = kpkCanShim->pTableMinMax->minimumCellVoltage_mV[s];
120  }
121  if (kpkCanShim->pTableMinMax->maximumTemperature_ddegC[s] >= packMaximumTemperature_ddegC) {
122  packMaximumTemperature_ddegC = kpkCanShim->pTableMinMax->maximumTemperature_ddegC[s];
123  }
124  if (kpkCanShim->pTableMinMax->minimumTemperature_ddegC[s] <= packMinimumTemperature_ddegC) {
125  packMinimumTemperature_ddegC = kpkCanShim->pTableMinMax->minimumTemperature_ddegC[s];
126  }
127  }
128  }
129  }
130 
131  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
132  /* Minimum cell voltage */
133  float_t signalData = (float_t)packMinimumVoltage_mV;
134  float_t offset = 0.0f;
135  float_t factor = 1.0f;
136  signalData = (signalData + offset) * factor;
137  uint64_t data = (int64_t)signalData;
138  /* set data in CAN frame */
139  CAN_TxSetMessageDataWithSignalData(&messageData, 10u, 13u, data, message.endianness);
140 
141  /* Maximum cell voltage */
142  signalData = (float_t)packMaximumVoltage_mV;
143  offset = 0.0f;
144  factor = 1.0f;
145  signalData = (signalData + offset) * factor;
146  data = (int64_t)signalData;
147  /* set data in CAN frame */
148  CAN_TxSetMessageDataWithSignalData(&messageData, 7u, 13u, data, message.endianness);
149 
150  /* Minimum cell temperature */
151  signalData = (float_t)packMinimumTemperature_ddegC;
152  offset = 0.0f;
153  factor = 0.1f; /* convert ddegC to degC */
154  signalData = (signalData + offset) * factor;
155  data = (int64_t)signalData;
156  /* set data in CAN frame */
157  CAN_TxSetMessageDataWithSignalData(&messageData, 63u, 8u, data, message.endianness);
158 
159  /* Maximum cell temperature */
160  signalData = (float_t)packMaximumTemperature_ddegC;
161  offset = 0.0f;
162  factor = 0.1f; /* convert ddegC to degC */
163  signalData = (signalData + offset) * factor;
164  data = (int64_t)signalData;
165  /* set data in CAN frame */
166  CAN_TxSetMessageDataWithSignalData(&messageData, 55u, 8u, data, message.endianness);
167  /* AXIVION Enable Style Generic-NoMagicNumbers: */
168 
169  /* now copy data in the buffer that will be used to send data */
170  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
171 
172  return 0;
173 }
174 
176  CAN_MESSAGE_PROPERTIES_s message,
177  uint8_t *pCanData,
178  uint8_t *pMuxId,
179  const CAN_SHIM_s *const kpkCanShim) {
183  FAS_ASSERT(pCanData != NULL_PTR);
184  FAS_ASSERT(pMuxId != NULL_PTR);
185  FAS_ASSERT(*pMuxId < BS_NR_OF_STRINGS);
186  FAS_ASSERT(kpkCanShim != NULL_PTR);
187  uint64_t messageData = 0u;
188 
189  /** Database entry with minimum and maximum values does not need to be read
190  * within this callback as it is already read by function
191  * #CANTX_MinimumMaximumValues */
192  const uint8_t stringNumber = *pMuxId;
193 
194  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
195  /* Minimum cell voltage */
196  float_t signalData = (float_t)kpkCanShim->pTableMinMax->minimumCellVoltage_mV[stringNumber];
197  float_t offset = 0.0f;
198  float_t factor = 1.0f;
199  signalData = (signalData + offset) * factor;
200  uint64_t data = (int64_t)signalData;
201  /* set data in CAN frame */
202  CAN_TxSetMessageDataWithSignalData(&messageData, 18u, 13u, data, message.endianness);
203 
204  /* Maximum cell voltage */
205  signalData = (float_t)kpkCanShim->pTableMinMax->maximumCellVoltage_mV[stringNumber];
206  offset = 0.0f;
207  factor = 1.0f;
208  signalData = (signalData + offset) * factor;
209  data = (int64_t)signalData;
210  /* set data in CAN frame */
211  CAN_TxSetMessageDataWithSignalData(&messageData, 15u, 13u, data, message.endianness);
212 
213  /* Minimum cell temperature */
214  signalData = (float_t)kpkCanShim->pTableMinMax->minimumTemperature_ddegC[stringNumber];
215  offset = 0.0f;
216  factor = 0.2f; /* convert ddegC to 0.5degC */
217  signalData = (signalData + offset) * factor;
218  data = (int64_t)signalData;
219  /* set data in CAN frame */
220  CAN_TxSetMessageDataWithSignalData(&messageData, 44u, 9u, data, message.endianness);
221 
222  /* Maximum cell temperature */
223  signalData = (float_t)kpkCanShim->pTableMinMax->maximumTemperature_ddegC[stringNumber];
224  offset = 0.0f;
225  factor = 0.2f; /* convert ddegC to 0.5degC */
226  signalData = (signalData + offset) * factor;
227  data = (int64_t)signalData;
228  /* set data in CAN frame */
229  CAN_TxSetMessageDataWithSignalData(&messageData, 37, 9u, data, message.endianness);
230  /* AXIVION Enable Style Generic-NoMagicNumbers: */
231 
232  /* now copy data in the buffer that will be use to send data */
233  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
234 
235  /* Increment multiplexer for next cell */
236  (*pMuxId)++;
237 
238  /* Check mux value */
239  if (*pMuxId >= BS_NR_OF_STRINGS) {
240  *pMuxId = 0u;
241  }
242 
243  return 0;
244 }
245 
246 /*========== Externalized Static Function Implementations (Unit Test) =======*/
247 #ifdef UNITY_UNIT_TEST
248 #endif
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
uint8_t BMS_GetNumberOfConnectedStrings(void)
Returns number of connected strings.
Definition: bms.c:1632
bool BMS_IsStringClosed(uint8_t stringNumber)
Returns string state (closed or open)
Definition: bms.c:1614
bms driver header
CAN callbacks header.
uint32_t CANTX_MinimumMaximumValues(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for min/max values
uint32_t CANTX_StringMinimumMaximumValues(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for string minimum and maximum values
#define CAN_FOXBMS_MESSAGES_DEFAULT_DLC
Definition: can_cfg.h:106
Header for the driver for the CAN module.
#define CANTX_STRING_MIN_MAX_VALUES_ID_TYPE
#define CANTX_STRING_MIN_MAX_VALUES_ID
#define CANTX_MINIMUM_MAXIMUM_VALUES_ID
#define CANTX_MINIMUM_MAXIMUM_VALUES_ID_TYPE
void CAN_TxSetMessageDataWithSignalData(uint64_t *pMessage, uint64_t bitStart, uint8_t bitLength, uint64_t canSignal, CAN_ENDIANNESS_e endianness)
Puts CAN signal data in a 64-bit variable. This function is used to compose a 64-bit CAN message....
Definition: can_helper.c:173
void CAN_TxSetCanDataWithMessageData(uint64_t message, uint8_t *pCanData, CAN_ENDIANNESS_e endianness)
Copy CAN data from a 64-bit variable to 8 bytes. This function is used to copy a 64-bit CAN message t...
Definition: can_helper.c:212
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:86
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
CAN_IDENTIFIER_TYPE_e idType
Definition: can_cfg.h:201
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:203
DATA_BLOCK_MIN_MAX_s * pTableMinMax
Definition: can_cfg.h:184
int16_t minimumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:180
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:183
int16_t maximumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:172
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:170