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