foxBMS  1.6.0
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 - 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-state-estimation.c
44  * @author foxBMS Team
45  * @date 2021-07-21 (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 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 #include <math.h>
63 #include <stdint.h>
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /*========== Static Constant and Variable Definitions =======================*/
68 
69 /*========== Extern Constant and Variable Definitions =======================*/
70 
71 /*========== Static Function Prototypes =====================================*/
72 
73 /*========== Static Function Implementations ================================*/
74 
75 /*========== Extern Function Implementations ================================*/
76 extern uint32_t CANTX_PackStateEstimation(
78  uint8_t *pCanData,
79  uint8_t *pMuxId,
80  const CAN_SHIM_s *const kpkCanShim) {
83  FAS_ASSERT(message.dlc <= CAN_MAX_DLC);
84  FAS_ASSERT(pCanData != NULL_PTR);
85  FAS_ASSERT(pMuxId == NULL_PTR); /* pMuxId is not used here, therefore has to be NULL_PTR */
86  FAS_ASSERT(kpkCanShim != NULL_PTR);
87  uint64_t messageData = 0u;
88 
89  float_t minimumStringSoc_perc = FLT_MAX;
90  float_t maximumStringSoc_perc = FLT_MIN;
91  float_t minimumStringSoe_perc = FLT_MAX;
92  float_t maximumStringSoe_perc = FLT_MIN;
93  uint32_t minimumStringEnergy_Wh = UINT32_MAX;
94 
95  DATA_READ_DATA(kpkCanShim->pTableSoc, kpkCanShim->pTableSoe);
96 
97  /* Check current direction */
99  /* If battery system is charging use maximum values */
100  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
101  if (BMS_IsStringClosed(s) == true) {
102  if (maximumStringSoc_perc < kpkCanShim->pTableSoc->maximumSoc_perc[s]) {
103  maximumStringSoc_perc = kpkCanShim->pTableSoc->maximumSoc_perc[s];
104  }
105  if (maximumStringSoe_perc < kpkCanShim->pTableSoe->maximumSoe_perc[s]) {
106  maximumStringSoe_perc = kpkCanShim->pTableSoe->maximumSoe_perc[s];
107  }
108  if (minimumStringEnergy_Wh > kpkCanShim->pTableSoe->minimumSoe_Wh[s]) {
109  minimumStringEnergy_Wh = kpkCanShim->pTableSoe->minimumSoe_Wh[s];
110  }
111  }
112  }
113  } else {
114  /* If battery system is discharging or at rest use minimum values */
115  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
116  if (BMS_IsStringClosed(s) == true) {
117  if (minimumStringSoc_perc > kpkCanShim->pTableSoc->minimumSoc_perc[s]) {
118  minimumStringSoc_perc = kpkCanShim->pTableSoc->minimumSoc_perc[s];
119  }
120  if (minimumStringSoe_perc > kpkCanShim->pTableSoe->minimumSoe_perc[s]) {
121  minimumStringSoe_perc = kpkCanShim->pTableSoe->minimumSoe_perc[s];
122  }
123  if (minimumStringEnergy_Wh > kpkCanShim->pTableSoe->minimumSoe_Wh[s]) {
124  minimumStringEnergy_Wh = kpkCanShim->pTableSoe->minimumSoe_Wh[s];
125  }
126  }
127  }
128  }
129 
130  float_t packSoc_perc = 0.0f;
131  float_t packSoe_perc = 0.0f;
132  uint32_t packEnergyLeft_Wh = 0u;
133 
134  /* Calculate pack value */
135  if (BMS_GetNumberOfConnectedStrings() != 0u) {
137  packSoc_perc = (BMS_GetNumberOfConnectedStrings() * maximumStringSoc_perc) / BS_NR_OF_STRINGS;
138  packSoe_perc = (BMS_GetNumberOfConnectedStrings() * maximumStringSoe_perc) / BS_NR_OF_STRINGS;
139  } else {
140  packSoc_perc = (BMS_GetNumberOfConnectedStrings() * minimumStringSoc_perc) / BS_NR_OF_STRINGS;
141  packSoe_perc = (BMS_GetNumberOfConnectedStrings() * minimumStringSoe_perc) / BS_NR_OF_STRINGS;
142  }
143  packEnergyLeft_Wh = BMS_GetNumberOfConnectedStrings() * minimumStringEnergy_Wh;
144  } else {
145  packSoc_perc = 0.0f;
146  packSoe_perc = 0.0f;
147  packEnergyLeft_Wh = 0u;
148  }
149 
150  /* SOC */
151  float_t signalData = packSoc_perc;
152  float_t offset = 0.0f;
153  float_t factor = 100.0f; /* convert from perc to 0.01perc */
154  signalData = (signalData + offset) * factor;
155  uint64_t data = (int64_t)signalData;
156  /* set data in CAN frame */
157  CAN_TxSetMessageDataWithSignalData(&messageData, 7u, 14u, data, message.endianness);
158 
159  /* SOE */
160  signalData = packSoe_perc;
161  offset = 0.0f;
162  factor = 100.0f; /* convert from perc to 0.01perc */
163  signalData = (signalData + offset) * factor;
164  data = (int64_t)signalData;
165  /* set data in CAN frame */
166  CAN_TxSetMessageDataWithSignalData(&messageData, 9u, 14u, data, message.endianness);
167 
168  /* Pack energy */
169  signalData = packEnergyLeft_Wh;
170  offset = 0.0f;
171  factor = 0.1f; /* convert from Wh to 10Wh */
172  signalData = (signalData + offset) * factor;
173  data = (int64_t)signalData;
174  /* set data in CAN frame */
175  CAN_TxSetMessageDataWithSignalData(&messageData, 47u, 24u, data, message.endianness);
176 
177  /* SOH */
178  signalData = 100.0f; /* TODO */
179  offset = 0.0f;
180  factor = 1.0f / 0.025f; /* convert from perc to 0.025% */
181  signalData = (signalData + offset) * factor;
182  data = (int64_t)signalData;
183  /* set data in CAN frame */
184  CAN_TxSetMessageDataWithSignalData(&messageData, 27u, 12u, data, message.endianness);
185 
186  /* now copy data in the buffer that will be used to send data */
187  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
188 
189  return 0;
190 }
191 
192 /*========== Externalized Static Function Implementations (Unit Test) =======*/
193 #ifdef UNITY_UNIT_TEST
194 #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_CURRENT_FLOW_STATE_e BMS_GetBatterySystemState(void)
Returns current battery system state (charging/discharging, resting or in relaxation phase)
Definition: bms.c:1586
bms driver header
@ BMS_CHARGING
Definition: bms.h:73
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:101
Header for the driver for the CAN module.
#define CANTX_PACK_STATE_ESTIMATION_ID_TYPE
#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: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
math library for often used math functions
#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_SOE_s * pTableSoe
Definition: can_cfg.h:191
DATA_BLOCK_SOC_s * pTableSoc
Definition: can_cfg.h:190
float_t minimumSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:519
float_t maximumSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:520
uint32_t minimumSoe_Wh[BS_NR_OF_STRINGS]
Definition: database_cfg.h:545
float_t maximumSoe_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:542
float_t minimumSoe_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:541