foxBMS  1.4.1
The foxBMS Battery Management System API Documentation
can_cbs_tx_pack-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_pack-state-estimation.c
44  * @author foxBMS Team
45  * @date 2021-07-21 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup DRIVER
49  * @prefix CANTX
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_tx.h"
59 #include "can_helper.h"
60 #include "foxmath.h"
61 
62 /*========== Macros and Definitions =========================================*/
63 
64 /*========== Static Constant and Variable Definitions =======================*/
65 
66 /*========== Extern Constant and Variable Definitions =======================*/
67 
68 /*========== Static Function Prototypes =====================================*/
69 
70 /*========== Static Function Implementations ================================*/
71 
72 /*========== Extern Function Implementations ================================*/
73 extern uint32_t CANTX_PackStateEstimation(
75  uint8_t *pCanData,
76  uint8_t *pMuxId,
77  const CAN_SHIM_s *const kpkCanShim) {
79  FAS_ASSERT(message.dlc <= CAN_MAX_DLC);
80  FAS_ASSERT(pCanData != NULL_PTR);
81  FAS_ASSERT(pMuxId == NULL_PTR); /* pMuxId is not used here, therefore has to be NULL_PTR */
82  FAS_ASSERT(kpkCanShim != NULL_PTR);
83  uint64_t messageData = 0u;
84 
85  float minimumStringSoc_perc = FLT_MAX;
86  float maximumStringSoc_perc = FLT_MIN;
87  float minimumStringSoe_perc = FLT_MAX;
88  float maximumStringSoe_perc = FLT_MIN;
89  uint32_t minimumStringEnergy_Wh = UINT32_MAX;
90 
91  DATA_READ_DATA(kpkCanShim->pTableSox);
92 
93  /* Check current direction */
95  /* If battery system is charging use maximum values */
96  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
97  if (BMS_IsStringClosed(s) == true) {
98  if (maximumStringSoc_perc < kpkCanShim->pTableSox->maximumSoc_perc[s]) {
99  maximumStringSoc_perc = kpkCanShim->pTableSox->maximumSoc_perc[s];
100  }
101  if (maximumStringSoe_perc < kpkCanShim->pTableSox->maximumSoe_perc[s]) {
102  maximumStringSoe_perc = kpkCanShim->pTableSox->maximumSoe_perc[s];
103  }
104  if (minimumStringEnergy_Wh > kpkCanShim->pTableSox->minimumSoe_Wh[s]) {
105  minimumStringEnergy_Wh = kpkCanShim->pTableSox->minimumSoe_Wh[s];
106  }
107  }
108  }
109  } else {
110  /* If battery system is discharging or at rest use minimum values */
111  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
112  if (BMS_IsStringClosed(s) == true) {
113  if (minimumStringSoc_perc > kpkCanShim->pTableSox->minimumSoc_perc[s]) {
114  minimumStringSoc_perc = kpkCanShim->pTableSox->minimumSoc_perc[s];
115  }
116  if (minimumStringSoe_perc > kpkCanShim->pTableSox->minimumSoe_perc[s]) {
117  minimumStringSoe_perc = kpkCanShim->pTableSox->minimumSoe_perc[s];
118  }
119  if (minimumStringEnergy_Wh > kpkCanShim->pTableSox->minimumSoe_Wh[s]) {
120  minimumStringEnergy_Wh = kpkCanShim->pTableSox->minimumSoe_Wh[s];
121  }
122  }
123  }
124  }
125 
126  float packSoc_perc = 0.0f;
127  float packSoe_perc = 0.0f;
128  uint32_t packEnergyLeft_Wh = 0u;
129 
130  /* Calculate pack value */
131  if (BMS_GetNumberOfConnectedStrings() != 0u) {
133  packSoc_perc = (BMS_GetNumberOfConnectedStrings() * maximumStringSoc_perc) / BS_NR_OF_STRINGS;
134  packSoe_perc = (BMS_GetNumberOfConnectedStrings() * maximumStringSoe_perc) / BS_NR_OF_STRINGS;
135  } else {
136  packSoc_perc = (BMS_GetNumberOfConnectedStrings() * minimumStringSoc_perc) / BS_NR_OF_STRINGS;
137  packSoe_perc = (BMS_GetNumberOfConnectedStrings() * minimumStringSoe_perc) / BS_NR_OF_STRINGS;
138  }
139  packEnergyLeft_Wh = BMS_GetNumberOfConnectedStrings() * minimumStringEnergy_Wh;
140  } else {
141  packSoc_perc = 0.0f;
142  packSoe_perc = 0.0f;
143  packEnergyLeft_Wh = 0u;
144  }
145 
146  /* SOC */
147  float signalData = packSoc_perc;
148  float offset = 0.0f;
149  float factor = 100.0f; /* convert from perc to 0.01perc */
150  signalData = (signalData + offset) * factor;
151  uint64_t data = (int64_t)signalData;
152  /* set data in CAN frame */
153  CAN_TxSetMessageDataWithSignalData(&messageData, 7u, 14u, data, message.endianness);
154 
155  /* SOE */
156  signalData = packSoe_perc;
157  offset = 0.0f;
158  factor = 100.0f; /* convert from perc to 0.01perc */
159  signalData = (signalData + offset) * factor;
160  data = (int64_t)signalData;
161  /* set data in CAN frame */
162  CAN_TxSetMessageDataWithSignalData(&messageData, 9u, 14u, data, message.endianness);
163 
164  /* Pack energy */
165  signalData = packEnergyLeft_Wh;
166  offset = 0.0f;
167  factor = 0.1f; /* convert from Wh to 10Wh */
168  signalData = (signalData + offset) * factor;
169  data = (int64_t)signalData;
170  /* set data in CAN frame */
171  CAN_TxSetMessageDataWithSignalData(&messageData, 47u, 24u, data, message.endianness);
172 
173  /* SOH */
174  signalData = 100.0f; /* TODO */
175  offset = 0.0f;
176  factor = 1.0f / 0.025f; /* convert from perc to 0.025% */
177  signalData = (signalData + offset) * factor;
178  data = (int64_t)signalData;
179  /* set data in CAN frame */
180  CAN_TxSetMessageDataWithSignalData(&messageData, 27u, 12u, data, message.endianness);
181 
182  /* now copy data in the buffer that will be used to send data */
183  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
184 
185  return 0;
186 }
187 
188 /*========== Externalized Static Function Implementations (Unit Test) =======*/
189 #ifdef UNITY_UNIT_TEST
190 
191 #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:1615
bool BMS_IsStringClosed(uint8_t stringNumber)
Returns string state (closed or open)
Definition: bms.c:1597
BMS_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState(void)
Returns current battery system state (charging/discharging, resting or in relaxation phase)
Definition: bms.c:1569
bms driver header
@ BMS_CHARGING
Definition: bms.h:69
CAN callbacks header.
uint32_t CANTX_PackStateEstimation(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for state estimation values
#define CAN_MAX_DLC
Definition: can_cfg.h:91
Header for the driver for the CAN module.
#define CANTX_PACK_STATE_ESTIMATION_ID
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:252
math library for often used math functions
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:181
DATA_BLOCK_SOX_s * pTableSox
Definition: can_cfg.h:169
float maximumSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:505
float minimumSoe_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:507
float minimumSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:504
float maximumSoe_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:508
uint32_t minimumSoe_Wh[BS_NR_OF_STRINGS]
Definition: database_cfg.h:514