foxBMS  1.4.1
The foxBMS Battery Management System API Documentation
can_cbs_rx_current-sensor.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_rx_current-sensor.c
44  * @author foxBMS Team
45  * @date 2021-04-20 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup DRIVER
49  * @prefix CANRX
50  *
51  * @brief CAN driver Rx callback implementation
52  * @details CAN Rx callback for current sensor measurements
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "can_cbs_rx.h"
58 #include "can_helper.h"
59 
60 /*========== Macros and Definitions =========================================*/
61 
62 /** Overcurrent flag */
63 #define CANRX_CURRENT_SENSOR_DIAGNOSIS_OVERCURRENT_BITMASK (0x1u)
64 /** Actual measurement error flag */
65 #define CANRX_CURRENT_SENSOR_DIAGNOSIS_ACTUAL_MEASUREMENT_ERROR_BITMASK (0x2u)
66 /** Any measurement error flag */
67 #define CANRX_CURRENT_SENSOR_DIAGNOSIS_ANY_MEASUREMENT_ERROR_BITMASK (0x4u)
68 /** System error flag */
69 #define CANRX_CURRENT_SENSOR_DIAGNOSIS_SYSTEM_ERROR_BITMASK (0x8u)
70 
71 /*========== Static Constant and Variable Definitions =======================*/
72 
73 /*========== Extern Constant and Variable Definitions =======================*/
74 
75 /*========== Static Function Prototypes =====================================*/
76 
77 /*========== Static Function Implementations ================================*/
78 
79 /*========== Extern Function Implementations ================================*/
80 extern uint32_t CANRX_CurrentSensor(
82  const uint8_t *const kpkCanData,
83  const CAN_SHIM_s *const kpkCanShim) {
86  FAS_ASSERT(kpkCanData != NULL_PTR);
87  FAS_ASSERT(kpkCanShim != NULL_PTR);
88 
89  /**
90  * CAN signals used in this message
91  * Parameters:
92  * bit start, bit length, factor, offset, minimum value, maximum value
93  */
94  const CAN_SIGNAL_TYPE_s currentSensorStatus = {7u, 8u, 1.0f, 0.0f, 0.0f, 255.0f};
95  const CAN_SIGNAL_TYPE_s currentSensorData = {23u, 32u, 1.0f, 0.0f, -2147483648.0f, 2147483648.0f};
96 
97  uint64_t messageData = 0u;
98  uint64_t canSignal = 0u;
99 
100  int32_t sensorSignalValue = 0;
101  uint8_t diagInfo = 0u;
102  uint8_t stringNumber = 0u;
103 
104  if (message.id <= CANRX_STRING0_ENERGY_COUNTER_ID) {
105  stringNumber = 0u;
106  }
107 
108  CAN_RxGetMessageDataFromCanData(&messageData, kpkCanData, message.endianness);
109 
110  /* Get status*/
112  messageData, currentSensorStatus.bitStart, currentSensorStatus.bitLength, &canSignal, message.endianness);
113 
114  /* only high nibble contains diag info */
115  diagInfo = canSignal & 0xF0u;
116  diagInfo >>= 4u;
117 
119  /* Overcurrent detected. This feature is currently not supported. */
120  }
122  switch (message.id) {
123  case CANRX_STRING0_CURRENT_ID: /* Current status */
124  kpkCanShim->pTableCurrentSensor->invalidCurrentMeasurement[stringNumber] = 1;
125  break;
126  case CANRX_STRING0_VOLTAGE1_ID: /* Voltage status */
127  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][0] = 1;
128  break;
130  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][1] = 1;
131  break;
133  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][2] = 1;
134  break;
135  case CANRX_STRING0_TEMPERATURE_ID: /* Temperature status */
136  kpkCanShim->pTableCurrentSensor->invalidSensorTemperatureMeasurement[stringNumber] = 1;
137  break;
138  case CANRX_STRING0_POWER_ID: /* Power status */
139  kpkCanShim->pTableCurrentSensor->invalidPowerMeasurement[stringNumber] = 1;
140  break;
141  case CANRX_STRING0_CURRENT_COUNTER_ID: /* CC status */
142  kpkCanShim->pTableCurrentSensor->invalidCurrentCountingMeasurement[stringNumber] = 1;
143  break;
144  case CANRX_STRING0_ENERGY_COUNTER_ID: /* EC status */
145  kpkCanShim->pTableCurrentSensor->invalidEnergyCountingMeasurement[stringNumber] = 1;
146  break;
147  default:
148  /* No error detected */
149  break;
150  }
151  } else {
152  kpkCanShim->pTableCurrentSensor->invalidCurrentMeasurement[stringNumber] = 0;
153  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][0] = 0;
154  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][1] = 0;
155  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][2] = 0;
156  kpkCanShim->pTableCurrentSensor->invalidSensorTemperatureMeasurement[stringNumber] = 0;
157  kpkCanShim->pTableCurrentSensor->invalidPowerMeasurement[stringNumber] = 0;
158  kpkCanShim->pTableCurrentSensor->invalidCurrentCountingMeasurement[stringNumber] = 0;
159  kpkCanShim->pTableCurrentSensor->invalidEnergyCountingMeasurement[stringNumber] = 0;
160  }
161 
164  kpkCanShim->pTableCurrentSensor->invalidCurrentMeasurement[stringNumber] = 1;
165  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][0] = 1;
166  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][1] = 1;
167  kpkCanShim->pTableCurrentSensor->invalidHighVoltageMeasurement[stringNumber][2] = 1;
168  kpkCanShim->pTableCurrentSensor->invalidSensorTemperatureMeasurement[stringNumber] = 1;
169  kpkCanShim->pTableCurrentSensor->invalidPowerMeasurement[stringNumber] = 1;
170  kpkCanShim->pTableCurrentSensor->invalidCurrentCountingMeasurement[stringNumber] = 1;
171  kpkCanShim->pTableCurrentSensor->invalidEnergyCountingMeasurement[stringNumber] = 1;
172  }
173 
174  /* Get measurement */
176  messageData, currentSensorData.bitStart, currentSensorData.bitLength, &canSignal, message.endianness);
177  switch (message.id) {
178  /* Current measurement */
180  sensorSignalValue = (int32_t)canSignal;
181  kpkCanShim->pTableCurrentSensor->current_mA[stringNumber] = sensorSignalValue;
182  kpkCanShim->pTableCurrentSensor->newCurrent++;
183  kpkCanShim->pTableCurrentSensor->previousTimestampCurrent[stringNumber] =
184  kpkCanShim->pTableCurrentSensor->timestampCurrent[stringNumber];
185  kpkCanShim->pTableCurrentSensor->timestampCurrent[stringNumber] = OS_GetTickCount();
186  break;
187  /* Voltage measurement U1 */
189  sensorSignalValue = (int32_t)canSignal;
190  kpkCanShim->pTableCurrentSensor->highVoltage_mV[stringNumber][0] = sensorSignalValue;
191  kpkCanShim->pTableCurrentSensor->previousTimestampHighVoltage[stringNumber][0] =
192  kpkCanShim->pTableCurrentSensor->timestampHighVoltage[stringNumber][0];
193  kpkCanShim->pTableCurrentSensor->timestampHighVoltage[stringNumber][0] = OS_GetTickCount();
194  break;
195  /* Voltage measurement U2 */
197  sensorSignalValue = (int32_t)canSignal;
198  kpkCanShim->pTableCurrentSensor->highVoltage_mV[stringNumber][1] = sensorSignalValue;
199  kpkCanShim->pTableCurrentSensor->previousTimestampHighVoltage[stringNumber][1] =
200  kpkCanShim->pTableCurrentSensor->timestampHighVoltage[stringNumber][1];
201  kpkCanShim->pTableCurrentSensor->timestampHighVoltage[stringNumber][1] = OS_GetTickCount();
202  break;
203  /* Voltage measurement U3 */
205  sensorSignalValue = (int32_t)canSignal;
206  kpkCanShim->pTableCurrentSensor->highVoltage_mV[stringNumber][2] = sensorSignalValue;
207  kpkCanShim->pTableCurrentSensor->previousTimestampHighVoltage[stringNumber][2] =
208  kpkCanShim->pTableCurrentSensor->timestampHighVoltage[stringNumber][2];
209  kpkCanShim->pTableCurrentSensor->timestampHighVoltage[stringNumber][2] = OS_GetTickCount();
210  break;
211  /* Temperature measurement */
213  sensorSignalValue = (int32_t)canSignal;
214  kpkCanShim->pTableCurrentSensor->sensorTemperature_ddegC[stringNumber] = sensorSignalValue;
215  break;
216  /* Power measurement */
218  sensorSignalValue = (int32_t)canSignal;
219  kpkCanShim->pTableCurrentSensor->power_W[stringNumber] = sensorSignalValue;
220  kpkCanShim->pTableCurrentSensor->newPower++;
221  kpkCanShim->pTableCurrentSensor->previousTimestampPower[stringNumber] =
222  kpkCanShim->pTableCurrentSensor->timestampPower[stringNumber];
223  kpkCanShim->pTableCurrentSensor->timestampPower[stringNumber] = OS_GetTickCount();
224  break;
225  /* CC measurement */
227  sensorSignalValue = (int32_t)canSignal;
228  kpkCanShim->pTableCurrentSensor->previousTimestampCurrentCounting[stringNumber] =
229  kpkCanShim->pTableCurrentSensor->timestampCurrentCounting[stringNumber];
230  kpkCanShim->pTableCurrentSensor->timestampCurrentCounting[stringNumber] = OS_GetTickCount();
231  kpkCanShim->pTableCurrentSensor->currentCounter_As[stringNumber] = sensorSignalValue;
232  break;
233  /* EC measurement */
235  sensorSignalValue = (int32_t)canSignal;
236  kpkCanShim->pTableCurrentSensor->energyCounter_Wh[stringNumber] = sensorSignalValue;
237  kpkCanShim->pTableCurrentSensor->previousTimestampEnergyCounting[stringNumber] =
238  kpkCanShim->pTableCurrentSensor->timestampEnergyCounting[stringNumber];
239  kpkCanShim->pTableCurrentSensor->timestampEnergyCounting[stringNumber] = OS_GetTickCount();
240  break;
241 
242  default:
244  break;
245  }
246 
248  return 0;
249 }
250 
251 /*========== Externalized Static Function Implementations (Unit Test) =======*/
252 #ifdef UNITY_UNIT_TEST
253 
254 #endif
CAN callbacks header.
#define CANRX_CURRENT_SENSOR_DIAGNOSIS_SYSTEM_ERROR_BITMASK
#define CANRX_CURRENT_SENSOR_DIAGNOSIS_ANY_MEASUREMENT_ERROR_BITMASK
uint32_t CANRX_CurrentSensor(CAN_MESSAGE_PROPERTIES_s message, const uint8_t *const kpkCanData, const CAN_SHIM_s *const kpkCanShim)
can rx callback function for current sensor measurements
#define CANRX_CURRENT_SENSOR_DIAGNOSIS_ACTUAL_MEASUREMENT_ERROR_BITMASK
#define CANRX_CURRENT_SENSOR_DIAGNOSIS_OVERCURRENT_BITMASK
Header for the driver for the CAN module.
#define CANRX_STRING0_POWER_ID
#define CANRX_STRING0_VOLTAGE2_ID
#define CANRX_STRING0_ENERGY_COUNTER_ID
#define CANRX_STRING0_CURRENT_COUNTER_ID
#define CANRX_STRING0_CURRENT_ID
#define CANRX_STRING0_VOLTAGE1_ID
#define CANRX_CURRENT_SENSOR_MESSAGES_DLC
#define CANRX_STRING0_VOLTAGE3_ID
#define CANRX_STRING0_TEMPERATURE_ID
void CAN_RxGetMessageDataFromCanData(uint64_t *pMessage, const uint8_t *const kpkCanData, CAN_ENDIANNESS_e endianness)
Copy CAN data from 8 bytes to a 64-bit variable.
Definition: can_helper.c:289
void CAN_RxGetSignalDataFromMessageData(uint64_t message, uint64_t bitStart, uint8_t bitLength, uint64_t *pCanSignal, CAN_ENDIANNESS_e endianness)
Gets CAN signal data from a 64-bit variable. This function is used to get signal data from a 64-bit C...
Definition: can_helper.c:249
Headers for the helper functions for the CAN module.
#define DATA_WRITE_DATA(...)
Definition: database.h:93
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:252
#define FAS_TRAP
Define that evaluates to essential boolean false thus tripping an assert.
Definition: fassert.h:126
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:139
CAN_ENDIANNESS_e endianness
Definition: can_cfg.h:181
DATA_BLOCK_CURRENT_SENSOR_s * pTableCurrentSensor
Definition: can_cfg.h:164
uint8_t bitLength
Definition: can_helper.h:82
uint8_t bitStart
Definition: can_helper.h:81
uint32_t previousTimestampCurrent[BS_NR_OF_STRINGS]
Definition: database_cfg.h:215
uint32_t timestampCurrent[BS_NR_OF_STRINGS]
Definition: database_cfg.h:216
uint32_t previousTimestampHighVoltage[BS_NR_OF_STRINGS][BS_NR_OF_VOLTAGES_FROM_CURRENT_SENSOR]
Definition: database_cfg.h:237
int32_t current_mA[BS_NR_OF_STRINGS]
Definition: database_cfg.h:212
uint32_t timestampHighVoltage[BS_NR_OF_STRINGS][BS_NR_OF_VOLTAGES_FROM_CURRENT_SENSOR]
Definition: database_cfg.h:239
uint32_t previousTimestampPower[BS_NR_OF_STRINGS]
Definition: database_cfg.h:222
uint8_t invalidSensorTemperatureMeasurement[BS_NR_OF_STRINGS]
Definition: database_cfg.h:218
int32_t energyCounter_Wh[BS_NR_OF_STRINGS]
Definition: database_cfg.h:228
uint32_t timestampPower[BS_NR_OF_STRINGS]
Definition: database_cfg.h:223
uint32_t previousTimestampEnergyCounting[BS_NR_OF_STRINGS]
Definition: database_cfg.h:230
int32_t highVoltage_mV[BS_NR_OF_STRINGS][BS_NR_OF_VOLTAGES_FROM_CURRENT_SENSOR]
Definition: database_cfg.h:234
uint8_t invalidCurrentMeasurement[BS_NR_OF_STRINGS]
Definition: database_cfg.h:213
uint8_t invalidHighVoltageMeasurement[BS_NR_OF_STRINGS][BS_NR_OF_VOLTAGES_FROM_CURRENT_SENSOR]
Definition: database_cfg.h:233
uint8_t invalidCurrentCountingMeasurement[BS_NR_OF_STRINGS]
Definition: database_cfg.h:225
uint32_t timestampEnergyCounting[BS_NR_OF_STRINGS]
Definition: database_cfg.h:231
int32_t power_W[BS_NR_OF_STRINGS]
Definition: database_cfg.h:219
int32_t currentCounter_As[BS_NR_OF_STRINGS]
Definition: database_cfg.h:224
uint32_t timestampCurrentCounting[BS_NR_OF_STRINGS]
Definition: database_cfg.h:227
int32_t sensorTemperature_ddegC[BS_NR_OF_STRINGS]
Definition: database_cfg.h:217
uint32_t previousTimestampCurrentCounting[BS_NR_OF_STRINGS]
Definition: database_cfg.h:226
uint8_t invalidPowerMeasurement[BS_NR_OF_STRINGS]
Definition: database_cfg.h:220
uint8_t invalidEnergyCountingMeasurement[BS_NR_OF_STRINGS]
Definition: database_cfg.h:229