foxBMS  1.4.1
The foxBMS Battery Management System API Documentation
can_cbs_tx_string-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_string-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 
76  uint8_t *pCanData,
77  uint8_t *pMuxId,
78  const CAN_SHIM_s *const kpkCanShim) {
81  FAS_ASSERT(pCanData != NULL_PTR);
82  FAS_ASSERT(pMuxId != NULL_PTR);
83  FAS_ASSERT(*pMuxId < BS_NR_OF_STRINGS);
84  FAS_ASSERT(kpkCanShim != NULL_PTR);
85  uint64_t messageData = 0u;
86 
87  /** Database entry with state estimation values does not need to be read
88  * within this callback as it is already read by function
89  * #CANTX_PackStateEstimation */
90  const uint8_t stringNumber = *pMuxId;
91 
92  /* set multiplexer in CAN frame */
93  /* AXIVION Disable Style Generic-NoMagicNumbers: Signal data defined in .dbc file. */
94  uint64_t data = (uint64_t)stringNumber;
95  CAN_TxSetMessageDataWithSignalData(&messageData, 7u, 4u, data, message.endianness);
96 
97  /* Minimum SOC */
98  float signalData = kpkCanShim->pTableSox->minimumSoc_perc[stringNumber];
99  float offset = 0.0f;
100  float factor = 4.0f; /* convert from perc to 0.25perc */
101  signalData = (signalData + offset) * factor;
102  data = (int64_t)signalData;
103  /* set data in CAN frame */
104  CAN_TxSetMessageDataWithSignalData(&messageData, 3u, 9u, data, message.endianness);
105 
106  /* Average SOC */
107  signalData = kpkCanShim->pTableSox->averageSoc_perc[stringNumber];
108  offset = 0.0f;
109  factor = 4.0f; /* convert from perc to 0.25perc */
110  signalData = (signalData + offset) * factor;
111  data = (int64_t)signalData;
112  /* set data in CAN frame */
113  CAN_TxSetMessageDataWithSignalData(&messageData, 10u, 9u, data, message.endianness);
114 
115  /* Maximum SOC */
116  signalData = kpkCanShim->pTableSox->maximumSoc_perc[stringNumber];
117  offset = 0.0f;
118  factor = 4.0f; /* convert from perc to 0.25perc */
119  signalData = (signalData + offset) * factor;
120  data = (int64_t)signalData;
121  /* set data in CAN frame */
122  CAN_TxSetMessageDataWithSignalData(&messageData, 17u, 9u, data, message.endianness);
123 
124  /* SOE */
125  if (BMS_CHARGING == BMS_GetCurrentFlowDirection(kpkCanShim->pTablePackValues->stringCurrent_mA[stringNumber])) {
126  signalData = kpkCanShim->pTableSox->maximumSoe_perc[stringNumber];
127  } else {
128  signalData = kpkCanShim->pTableSox->minimumSoe_perc[stringNumber];
129  }
130  offset = 0.0f;
131  factor = 4.0f; /* convert from perc to 0.25perc */
132  signalData = (signalData + offset) * factor;
133  data = (int64_t)signalData;
134  /* set data in CAN frame */
135  CAN_TxSetMessageDataWithSignalData(&messageData, 24u, 9u, data, message.endianness);
136 
137  /* SOH */
138  signalData = 100.0f;
139  offset = 0.0f;
140  factor = 4.0f; /* convert from perc to 0.25perc */
141  signalData = (signalData + offset) * factor;
142  data = (int64_t)signalData;
143  /* set data in CAN frame */
144  CAN_TxSetMessageDataWithSignalData(&messageData, 47u, 9u, data, message.endianness);
145 
146  /* String energy */
147  signalData = kpkCanShim->pTableSox->minimumSoe_Wh[stringNumber];
148  offset = 0.0f;
149  factor = 0.1f; /* convert from Wh to 10Wh */
150  signalData = (signalData + offset) * factor;
151  data = (int64_t)signalData;
152  /* set data in CAN frame */
153  CAN_TxSetMessageDataWithSignalData(&messageData, 54u, 15u, data, message.endianness);
154  /* AXIVION Enable Style Generic-NoMagicNumbers: */
155 
156  /* now copy data in the buffer that will be used to send data */
157  CAN_TxSetCanDataWithMessageData(messageData, pCanData, message.endianness);
158 
159  /* Increment multiplexer for next cell */
160  (*pMuxId)++;
161 
162  /* Check mux value */
163  if (*pMuxId >= BS_NR_OF_STRINGS) {
164  *pMuxId = 0u;
165  }
166 
167  return 0;
168 }
169 
170 /*========== Externalized Static Function Implementations (Unit Test) =======*/
171 #ifdef UNITY_UNIT_TEST
172 
173 #endif
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
BMS_CURRENT_FLOW_STATE_e BMS_GetCurrentFlowDirection(int32_t current_mA)
Get current flow direction, current value as function parameter.
Definition: bms.c:1573
bms driver header
@ BMS_CHARGING
Definition: bms.h:69
CAN callbacks header.
uint32_t CANTX_StringStateEstimation(CAN_MESSAGE_PROPERTIES_s message, uint8_t *pCanData, uint8_t *pMuxId, const CAN_SHIM_s *const kpkCanShim)
can tx callback function for string state estimation
#define CAN_FOXBMS_MESSAGES_DEFAULT_DLC
Definition: can_cfg.h:96
Header for the driver for the CAN module.
#define CANTX_STRING_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 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_PACK_VALUES_s * pTablePackValues
Definition: can_cfg.h:167
DATA_BLOCK_SOX_s * pTableSox
Definition: can_cfg.h:169
int32_t stringCurrent_mA[BS_NR_OF_STRINGS]
Definition: database_cfg.h:200
float maximumSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:505
float averageSoc_perc[BS_NR_OF_STRINGS]
Definition: database_cfg.h:503
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