foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
algorithm.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 algorithm.c
44  * @author foxBMS Team
45  * @date 2017-12-18 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup ALGORITHMS
49  * @prefix ALGO
50  *
51  * @brief Main module to handle the execution of algorithms
52  *
53  *
54  */
55 
56 /*========== Includes =======================================================*/
57 #include "algorithm.h"
58 
59 #include "fassert.h"
60 #include "os.h"
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /*========== Static Constant and Variable Definitions =======================*/
68 /**
69  * This is a signal that skips initialization of #ALGO_Initialization()
70  * until it has been requested.
71  */
72 static bool algo_initializationRequested = false;
73 
74 /*========== Extern Constant and Variable Definitions =======================*/
75 
76 /*========== Static Function Prototypes =====================================*/
77 /**
78  * @brief initializes local variables and module internals needed to use the
79  * algorithm module
80  */
81 static void ALGO_Initialization(void);
82 
83 /*========== Static Function Implementations ================================*/
84 static void ALGO_Initialization(void) {
85  /* iterate over all algorithms */
86  for (uint16_t i = 0u; i < algo_length; i++) {
87  /* check if the cycle time is valid */
88  FAS_ASSERT((algo_algorithms[i].cycleTime_ms % ALGO_TICK_ms) == 0u);
89 
90  /* check only uninitialized algorithms */
91  if (algo_algorithms[i].state == ALGO_UNINITIALIZED) {
92  /* directly make ready when init function is a null pointer otherwise run init */
93  if (algo_algorithms[i].fpInitialization == NULL_PTR) {
95  } else {
97  FAS_ASSERT((STD_OK == result) || (STD_NOT_OK == result));
98  if (STD_OK == result) {
100  } else {
102  }
103  }
104  }
105  }
106 }
107 
108 /*========== Extern Function Implementations ================================*/
109 
110 extern void ALGO_UnlockInitialization(void) {
114 }
115 
116 extern void ALGO_MainFunction(void) {
118  const bool initializationRequested = algo_initializationRequested;
120  if (initializationRequested == true) {
125  }
126 
127  static uint32_t counter_ticks = 0u;
128 
129  for (uint16_t i = 0u; i < algo_length; i++) {
130  const bool runAlgorithmAsap = (algo_algorithms[i].cycleTime_ms == 0u);
131  const bool runAlgorithmCycleElapsed =
132  ((algo_algorithms[i].cycleTime_ms != 0u) && ((counter_ticks % algo_algorithms[i].cycleTime_ms) == 0u));
133  if ((runAlgorithmAsap != false) || (runAlgorithmCycleElapsed != false)) {
134  /* Cycle time elapsed -> call function */
135  if (algo_algorithms[i].state == ALGO_READY) {
136  /* Set state to running -> reset to READY before leaving algorithm function */
140  ALGO_MarkAsDone(i);
141  }
142  /* check if we need to reinitialize */
143  if (algo_algorithms[i].state == ALGO_REINIT_REQUESTED) {
144  /* set to uninitialized so that the algorithm can be reinitialized */
146 
148  }
149  }
150  }
151 
152  counter_ticks += ALGO_TICK_ms;
153 }
154 
155 extern void ALGO_MonitorExecutionTime(void) {
156  const uint32_t timestamp = OS_GetTickCount();
157 
158  for (uint16_t i = 0u; i < algo_length; i++) {
159  if ((algo_algorithms[i].startTime != 0u) && (algo_algorithms[i].state == ALGO_RUNNING) &&
160  ((algo_algorithms[i].startTime + algo_algorithms[i].maxCalculationDuration_ms) < timestamp)) {
161  /* Block task from further execution because of runtime violation, but task will finish its execution */
163 
164  /* TODO: Add diag call to notify error in algorithm module */
165  }
166  }
167 }
168 
169 /*========== Externalized Static Function Implementations (Unit Test) =======*/
170 #ifdef UNITY_UNIT_TEST
171 extern void TEST_ALGO_ResetInitializationRequest() {
173 }
174 #endif
void ALGO_UnlockInitialization(void)
Calling this function sets a signal that lets ALGO_Initialization() know that the initialization has ...
Definition: algorithm.c:110
static void ALGO_Initialization(void)
initializes local variables and module internals needed to use the algorithm module
Definition: algorithm.c:84
void ALGO_MainFunction(void)
handles the call of different algorithm functions when cycle time has expired
Definition: algorithm.c:116
void ALGO_MonitorExecutionTime(void)
monitors the calculation duration of the different algorithms
Definition: algorithm.c:155
static bool algo_initializationRequested
Definition: algorithm.c:72
Headers for the driver for the storage in the EEPROM memory.
const uint16_t algo_length
Definition: algorithm_cfg.c:74
ALGO_TASKS_s algo_algorithms[]
Definition: algorithm_cfg.c:70
void ALGO_MarkAsDone(uint32_t algorithmIndex)
Mark the current algorithm as done (will reset to ALGO_READY if possible)
Definition: algorithm_cfg.c:82
@ ALGO_UNINITIALIZED
Definition: algorithm_cfg.h:84
@ ALGO_REINIT_REQUESTED
Definition: algorithm_cfg.h:90
@ ALGO_RUNNING
Definition: algorithm_cfg.h:86
@ ALGO_FAILED_INIT
Definition: algorithm_cfg.h:89
@ ALGO_READY
Definition: algorithm_cfg.h:85
@ ALGO_BLOCKED
Definition: algorithm_cfg.h:88
Assert macro implementation.
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
#define ALGO_TICK_ms
Definition: algorithm_cfg.h:69
Declaration of the OS wrapper interface.
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:154
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:150
uint32_t OS_GetTickCount(void)
Returns OS based system tick value.
Definition: os_freertos.c:158
uint32_t cycleTime_ms
Definition: algorithm_cfg.h:96
uint32_t startTime
Definition: algorithm_cfg.h:98
ALGO_COMPUTATION_FUNCTION_f * fpAlgorithm
ALGO_INITIALIZATION_FUNCTION_f * fpInitialization
Definition: algorithm_cfg.h:99
ALGO_STATE_e state
Definition: algorithm_cfg.h:95