foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
can_cbs_tx_minmax.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 can_cbs_tx_minmax.c
44  * @author foxBMS Team
45  * @date 2021-04-20 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.0
48  * @ingroup DRIVER
49  * @prefix CAN
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.h"
58 #include "can_helper.h"
59 
60 /*========== Macros and Definitions =========================================*/
61 
62 /*========== Static Constant and Variable Definitions =======================*/
63 
64 /*========== Extern Constant and Variable Definitions =======================*/
65 
66 /*========== Static Function Prototypes =====================================*/
67 
68 /*========== Static Function Implementations ================================*/
69 
70 /*========== Extern Function Implementations ================================*/
72  uint32_t id,
73  uint8_t dlc,
74  CAN_ENDIANNESS_e endianness,
75  uint8_t *pCanData,
76  uint8_t *pMuxId,
77  const CAN_SHIM_s *const kpkCanShim) {
78  /* pMuxId is not used here, therefore has to be NULL_PTR */
79  FAS_ASSERT(pMuxId == NULL_PTR);
80 
81  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
82  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
83  FAS_ASSERT(pCanData != NULL_PTR);
84  FAS_ASSERT(kpkCanShim != NULL_PTR);
85  uint64_t message = 0;
86 
87  DATA_READ_DATA(kpkCanShim->pTableMinMax);
88 
89  int16_t packMaximumVoltage_mV = INT16_MIN;
90  int16_t packMinimumVoltage_mV = INT16_MAX;
91  int16_t packMaximumTemperature_ddegC = INT16_MIN;
92  int16_t packMinimumTemperature_ddegC = INT16_MAX;
93 
94  if (0u == BMS_GetNumberOfConnectedStrings()) {
95  /* Calculate min/max values of complete pack if all slice switches are open */
96  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
97  if (kpkCanShim->pTableMinMax->maximumCellVoltage_mV[s] >= packMaximumVoltage_mV) {
98  packMaximumVoltage_mV = kpkCanShim->pTableMinMax->maximumCellVoltage_mV[s];
99  }
100  if (kpkCanShim->pTableMinMax->minimumCellVoltage_mV[s] <= packMinimumVoltage_mV) {
101  packMinimumVoltage_mV = kpkCanShim->pTableMinMax->minimumCellVoltage_mV[s];
102  }
103  if (kpkCanShim->pTableMinMax->maximumTemperature_ddegC[s] >= packMaximumTemperature_ddegC) {
104  packMaximumTemperature_ddegC = kpkCanShim->pTableMinMax->maximumTemperature_ddegC[s];
105  }
106  if (kpkCanShim->pTableMinMax->minimumTemperature_ddegC[s] <= packMinimumTemperature_ddegC) {
107  packMinimumTemperature_ddegC = kpkCanShim->pTableMinMax->minimumTemperature_ddegC[s];
108  }
109  }
110  } else {
111  /* Calculate min/max values of connected slices */
112  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
113  if (BMS_IsStringClosed(s) == true) {
114  if (kpkCanShim->pTableMinMax->maximumCellVoltage_mV[s] >= packMaximumVoltage_mV) {
115  packMaximumVoltage_mV = kpkCanShim->pTableMinMax->maximumCellVoltage_mV[s];
116  }
117  if (kpkCanShim->pTableMinMax->minimumCellVoltage_mV[s] <= packMinimumVoltage_mV) {
118  packMinimumVoltage_mV = kpkCanShim->pTableMinMax->minimumCellVoltage_mV[s];
119  }
120  if (kpkCanShim->pTableMinMax->maximumTemperature_ddegC[s] >= packMaximumTemperature_ddegC) {
121  packMaximumTemperature_ddegC = kpkCanShim->pTableMinMax->maximumTemperature_ddegC[s];
122  }
123  if (kpkCanShim->pTableMinMax->minimumTemperature_ddegC[s] <= packMinimumTemperature_ddegC) {
124  packMinimumTemperature_ddegC = kpkCanShim->pTableMinMax->minimumTemperature_ddegC[s];
125  }
126  }
127  }
128  }
129 
130  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
131  /* Minimum cell voltage */
132  float signalData = (float)packMinimumVoltage_mV;
133  float offset = 0.0f;
134  float factor = 1.0f;
135  signalData = (signalData + offset) * factor;
136  uint64_t data = (int64_t)signalData;
137  /* set data in CAN frame */
138  CAN_TxSetMessageDataWithSignalData(&message, 10u, 13u, data, endianness);
139 
140  /* Maximum cell voltage */
141  signalData = (float)packMaximumVoltage_mV;
142  offset = 0.0f;
143  factor = 1.0f;
144  signalData = (signalData + offset) * factor;
145  data = (int64_t)signalData;
146  /* set data in CAN frame */
147  CAN_TxSetMessageDataWithSignalData(&message, 7u, 13u, data, endianness);
148 
149  /* Minimum cell temperature */
150  signalData = (float)packMinimumTemperature_ddegC;
151  offset = 0.0f;
152  factor = 0.1f; /* convert ddegC to degC */
153  signalData = (signalData + offset) * factor;
154  data = (int64_t)signalData;
155  /* set data in CAN frame */
156  CAN_TxSetMessageDataWithSignalData(&message, 63u, 8u, data, endianness);
157 
158  /* Maximum cell temperature */
159  signalData = (float)packMaximumTemperature_ddegC;
160  offset = 0.0f;
161  factor = 0.1f; /* convert ddegC to degC */
162  signalData = (signalData + offset) * factor;
163  data = (int64_t)signalData;
164  /* set data in CAN frame */
165  CAN_TxSetMessageDataWithSignalData(&message, 55u, 8u, data, endianness);
166  /* AXIVION Enable Style Generic-NoMagicNumbers: */
167 
168  /* now copy data in the buffer that will be used to send data */
169  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
170 
171  return 0;
172 }
173 
175  uint32_t id,
176  uint8_t dlc,
177  CAN_ENDIANNESS_e endianness,
178  uint8_t *pCanData,
179  uint8_t *pMuxId,
180  const CAN_SHIM_s *const kpkCanShim) {
181  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
182  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
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 message = 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  * #CAN_TxMinimumMaximumValues */
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 signalData = (float)kpkCanShim->pTableMinMax->minimumCellVoltage_mV[stringNumber];
197  float offset = 0.0f;
198  float factor = 1.0f;
199  signalData = (signalData + offset) * factor;
200  uint64_t data = (int64_t)signalData;
201  /* set data in CAN frame */
202  CAN_TxSetMessageDataWithSignalData(&message, 18u, 13u, data, endianness);
203 
204  /* Maximum cell voltage */
205  signalData = (float)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(&message, 15u, 13u, data, endianness);
212 
213  /* Minimum cell temperature */
214  signalData = (float)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(&message, 44u, 9u, data, endianness);
221 
222  /* Maximum cell temperature */
223  signalData = (float)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(&message, 37, 9u, data, 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(message, pCanData, 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 
249 #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:1320
bool BMS_IsStringClosed(uint8_t stringNumber)
Returns string state (closed or open)
Definition: bms.c:1302
bms driver header
CAN callbacks header.
uint32_t CAN_TxMinimumMaximumValues(uint32_t id, uint8_t dlc, CAN_ENDIANNESS_e endianness, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for min/max values
uint32_t CAN_TxStringMinimumMaximumValues(uint32_t id, uint8_t dlc, CAN_ENDIANNESS_e endianness, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for string minimum and maximum values
CAN_ENDIANNESS_e
Definition: can_cfg.h:298
#define CAN_MAX_11BIT_ID
Definition: can_cfg.h:86
#define CAN_MAX_DLC
Definition: can_cfg.h:88
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:167
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:206
Headers for the helper functions for the CAN module.
#define DATA_READ_DATA(...)
Definition: database.h:83
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:237
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
DATA_BLOCK_MIN_MAX_s * pTableMinMax
Definition: can_cfg.h:315
int16_t minimumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:172
int16_t maximumTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:175
int16_t maximumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:164
int16_t minimumCellVoltage_mV[BS_NR_OF_STRINGS]
Definition: database_cfg.h:162