foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
can_cbs_tx_state_estimation.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_state_estimation.c
44  * @author foxBMS Team
45  * @date 2021-07-21 (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 state estimation messages
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "bms.h"
57 #include "can_cbs.h"
58 #include "can_helper.h"
59 #include "foxmath.h"
60 
61 /*========== Macros and Definitions =========================================*/
62 
63 /*========== Static Constant and Variable Definitions =======================*/
64 
65 /*========== Extern Constant and Variable Definitions =======================*/
66 
67 /*========== Static Function Prototypes =====================================*/
68 
69 /*========== Static Function Implementations ================================*/
70 
71 /*========== Extern Function Implementations ================================*/
72 extern uint32_t CAN_TxStateEstimation(
73  uint32_t id,
74  uint8_t dlc,
75  CAN_ENDIANNESS_e endianness,
76  uint8_t *pCanData,
77  uint8_t *pMuxId,
78  const CAN_SHIM_s *const kpkCanShim) {
79  /* pMuxId is not used here, therefore has to be NULL_PTR */
80  FAS_ASSERT(pMuxId == NULL_PTR);
81 
82  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
83  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
84  FAS_ASSERT(pCanData != NULL_PTR);
85  FAS_ASSERT(kpkCanShim != NULL_PTR);
86  uint64_t message = 0;
87 
88  float minimumStringSoc_perc = FLT_MAX;
89  float maximumStringSoc_perc = FLT_MIN;
90  float minimumStringSoe_perc = FLT_MAX;
91  float maximumStringSoe_perc = FLT_MIN;
92  uint32_t minimumStringEnergy_Wh = UINT32_MAX;
93 
94  DATA_READ_DATA(kpkCanShim->pTableSox);
95 
96  /* Check current direction */
98  /* If battery system is charging use maximum values */
99  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
100  if (BMS_IsStringClosed(s) == true) {
101  if (maximumStringSoc_perc < kpkCanShim->pTableSox->maximumSoc_perc[s]) {
102  maximumStringSoc_perc = kpkCanShim->pTableSox->maximumSoc_perc[s];
103  }
104  if (maximumStringSoe_perc < kpkCanShim->pTableSox->maximumSoe_perc[s]) {
105  maximumStringSoe_perc = kpkCanShim->pTableSox->maximumSoe_perc[s];
106  }
107  if (minimumStringEnergy_Wh > kpkCanShim->pTableSox->minimumSoe_Wh[s]) {
108  minimumStringEnergy_Wh = kpkCanShim->pTableSox->minimumSoe_Wh[s];
109  }
110  }
111  }
112  } else {
113  /* If battery system is discharging or at rest use minimum values */
114  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
115  if (BMS_IsStringClosed(s) == true) {
116  if (minimumStringSoc_perc > kpkCanShim->pTableSox->minimumSoc_perc[s]) {
117  minimumStringSoc_perc = kpkCanShim->pTableSox->minimumSoc_perc[s];
118  }
119  if (minimumStringSoe_perc > kpkCanShim->pTableSox->minimumSoe_perc[s]) {
120  minimumStringSoe_perc = kpkCanShim->pTableSox->minimumSoe_perc[s];
121  }
122  if (minimumStringEnergy_Wh > kpkCanShim->pTableSox->minimumSoe_Wh[s]) {
123  minimumStringEnergy_Wh = kpkCanShim->pTableSox->minimumSoe_Wh[s];
124  }
125  }
126  }
127  }
128 
129  float packSoc_perc = 0.0f;
130  float packSoe_perc = 0.0f;
131  uint32_t packEnergyLeft_Wh = 0u;
132 
133  /* Calculate pack value */
134  if (BMS_GetNumberOfConnectedStrings() != 0u) {
136  packSoc_perc = (BMS_GetNumberOfConnectedStrings() * maximumStringSoc_perc) / BS_NR_OF_STRINGS;
137  packSoe_perc = (BMS_GetNumberOfConnectedStrings() * maximumStringSoe_perc) / BS_NR_OF_STRINGS;
138  } else {
139  packSoc_perc = (BMS_GetNumberOfConnectedStrings() * minimumStringSoc_perc) / BS_NR_OF_STRINGS;
140  packSoe_perc = (BMS_GetNumberOfConnectedStrings() * minimumStringSoe_perc) / BS_NR_OF_STRINGS;
141  }
142  packEnergyLeft_Wh = BMS_GetNumberOfConnectedStrings() * minimumStringEnergy_Wh;
143  } else {
144  packSoc_perc = 0.0f;
145  packSoe_perc = 0.0f;
146  packEnergyLeft_Wh = 0u;
147  }
148 
149  /* SOC */
150  float signalData = packSoc_perc;
151  float offset = 0.0f;
152  float factor = 100.0f; /* convert from perc to 0.01perc */
153  signalData = (signalData + offset) * factor;
154  uint64_t data = (int64_t)signalData;
155  /* set data in CAN frame */
156  CAN_TxSetMessageDataWithSignalData(&message, 7u, 14u, data, endianness);
157 
158  /* SOE */
159  signalData = packSoe_perc;
160  offset = 0.0f;
161  factor = 100.0f; /* convert from perc to 0.01perc */
162  signalData = (signalData + offset) * factor;
163  data = (int64_t)signalData;
164  /* set data in CAN frame */
165  CAN_TxSetMessageDataWithSignalData(&message, 9u, 14u, data, endianness);
166 
167  /* Pack energy */
168  signalData = packEnergyLeft_Wh;
169  offset = 0.0f;
170  factor = 0.1f; /* convert from Wh to 10Wh */
171  signalData = (signalData + offset) * factor;
172  data = (int64_t)signalData;
173  /* set data in CAN frame */
174  CAN_TxSetMessageDataWithSignalData(&message, 47u, 24u, data, endianness);
175 
176  /* SOH */
177  signalData = 100.0f; /* TODO */
178  offset = 0.0f;
179  factor = 1.0f / 0.025f; /* convert from perc to 0.025% */
180  signalData = (signalData + offset) * factor;
181  data = (int64_t)signalData;
182  /* set data in CAN frame */
183  CAN_TxSetMessageDataWithSignalData(&message, 27u, 12u, data, endianness);
184 
185  /* now copy data in the buffer that will be used to send data */
186  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
187 
188  return 0;
189 }
190 
192  uint32_t id,
193  uint8_t dlc,
194  CAN_ENDIANNESS_e endianness,
195  uint8_t *pCanData,
196  uint8_t *pMuxId,
197  const CAN_SHIM_s *const kpkCanShim) {
198  FAS_ASSERT(id < CAN_MAX_11BIT_ID); /* Currently standard ID, 11 bit */
199  FAS_ASSERT(dlc <= CAN_MAX_DLC); /* Currently max 8 bytes in a CAN frame */
200  FAS_ASSERT(pCanData != NULL_PTR);
201  FAS_ASSERT(pMuxId != NULL_PTR);
202  FAS_ASSERT(*pMuxId < BS_NR_OF_STRINGS);
203  FAS_ASSERT(kpkCanShim != NULL_PTR);
204  uint64_t message = 0;
205 
206  /** Database entry with state estimation values does not need to be read
207  * within this callback as it is already read by function
208  * #CAN_TxStateEstimation */
209  const uint8_t stringNumber = *pMuxId;
210 
211  /* set multiplexer in CAN frame */
212  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
213  uint64_t data = (uint64_t)stringNumber;
214  CAN_TxSetMessageDataWithSignalData(&message, 7u, 4u, data, endianness);
215 
216  /* Minimum SOC */
217  float signalData = kpkCanShim->pTableSox->minimumSoc_perc[stringNumber];
218  float offset = 0.0f;
219  float factor = 4.0f; /* convert from perc to 0.25perc */
220  signalData = (signalData + offset) * factor;
221  data = (int64_t)signalData;
222  /* set data in CAN frame */
223  CAN_TxSetMessageDataWithSignalData(&message, 3u, 9u, data, endianness);
224 
225  /* Average SOC */
226  signalData = kpkCanShim->pTableSox->averageSoc_perc[stringNumber];
227  offset = 0.0f;
228  factor = 4.0f; /* convert from perc to 0.25perc */
229  signalData = (signalData + offset) * factor;
230  data = (int64_t)signalData;
231  /* set data in CAN frame */
232  CAN_TxSetMessageDataWithSignalData(&message, 10u, 9u, data, endianness);
233 
234  /* Maximum SOC */
235  signalData = kpkCanShim->pTableSox->maximumSoc_perc[stringNumber];
236  offset = 0.0f;
237  factor = 4.0f; /* convert from perc to 0.25perc */
238  signalData = (signalData + offset) * factor;
239  data = (int64_t)signalData;
240  /* set data in CAN frame */
241  CAN_TxSetMessageDataWithSignalData(&message, 17u, 9u, data, endianness);
242 
243  /* SOE */
244  if (BMS_CHARGING == BMS_GetCurrentFlowDirection(kpkCanShim->pTablePackValues->stringCurrent_mA[stringNumber])) {
245  signalData = kpkCanShim->pTableSox->maximumSoe_perc[stringNumber];
246  } else {
247  signalData = kpkCanShim->pTableSox->minimumSoe_perc[stringNumber];
248  }
249  offset = 0.0f;
250  factor = 4.0f; /* convert from perc to 0.25perc */
251  signalData = (signalData + offset) * factor;
252  data = (int64_t)signalData;
253  /* set data in CAN frame */
254  CAN_TxSetMessageDataWithSignalData(&message, 24u, 9u, data, endianness);
255 
256  /* SOH */
257  signalData = 100.0f;
258  offset = 0.0f;
259  factor = 4.0f; /* convert from perc to 0.25perc */
260  signalData = (signalData + offset) * factor;
261  data = (int64_t)signalData;
262  /* set data in CAN frame */
263  CAN_TxSetMessageDataWithSignalData(&message, 47u, 9u, data, endianness);
264 
265  /* String energy */
266  signalData = kpkCanShim->pTableSox->minimumSoe_Wh[stringNumber];
267  offset = 0.0f;
268  factor = 0.1f; /* convert from Wh to 10Wh */
269  signalData = (signalData + offset) * factor;
270  data = (int64_t)signalData;
271  /* set data in CAN frame */
272  CAN_TxSetMessageDataWithSignalData(&message, 54u, 15u, data, endianness);
273  /* AXIVION Enable Style Generic-NoMagicNumbers: */
274 
275  /* now copy data in the buffer that will be used to send data */
276  CAN_TxSetCanDataWithMessageData(message, pCanData, endianness);
277 
278  /* Increment multiplexer for next cell */
279  (*pMuxId)++;
280 
281  /* Check mux value */
282  if (*pMuxId >= BS_NR_OF_STRINGS) {
283  *pMuxId = 0u;
284  }
285 
286  return 0;
287 }
288 
289 /*========== Externalized Static Function Implementations (Unit Test) =======*/
290 #ifdef UNITY_UNIT_TEST
291 
292 #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_CURRENT_FLOW_STATE_e BMS_GetCurrentFlowDirection(int32_t current_mA)
Get current flow direction, current value as function parameter.
Definition: bms.c:1278
BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState(void)
Returns current battery system state (charging/discharging, resting or in relaxation phase)
Definition: bms.c:1274
bms driver header
@ BMS_CHARGING
Definition: bms.h:67
CAN callbacks header.
uint32_t CAN_TxStateEstimation(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 state estimation values
uint32_t CAN_TxStringStateEstimation(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 state estimation
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
math library for often used math functions
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
DATA_BLOCK_PACK_VALUES_s * pTablePackValues
Definition: can_cfg.h:319
DATA_BLOCK_SOX_s * pTableSox
Definition: can_cfg.h:321
int32_t stringCurrent_mA[BS_NR_OF_STRINGS]
Definition: database_cfg.h:200
float maximumSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:515
float averageSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:513
float minimumSoe_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:517
float minimumSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:514
float maximumSoe_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:518
uint32_t minimumSoe_Wh[BS_NR_OF_STRINGS]
Definition: database_cfg.h:524