demo-epb  v1.0
Elektrische Parkbremse - slohmaier Dev Process Demo
apply_controller.c
gehe zur Dokumentation dieser Datei
1 /**
2  * @file apply_controller.c
3  * @brief Apply/Hold/Release State Machine.
4  *
5  * @arch SWA-002
6  * @reqs SWE-001 SWE-002 SWE-003 SWE-004
7  *
8  * ASIL: D. This is the safety-critical core logic.
9  * Changes require a technical review with 2 approvals.
10  */
11 #include <stddef.h>
12 
13 #include "apply_controller.h"
14 #include "actuator_driver.h"
15 
16 typedef struct {
18  uint8_t step_in_state; /* 50ms ticks in the current state */
20  uint32_t step_count; /* Watchdog alive counter */
21 } ApplyCtx;
22 
23 static ApplyCtx s_ctx;
24 
25 static void enter_state(EpbState new_state)
26 {
27  s_ctx.state = new_state;
28  s_ctx.step_in_state = 0U;
29 }
30 
31 static bool release_preconditions_ok(const ApplyInputs* in)
32 {
33  /* @reqs SWE-005 (Release preconditions) — consumed here */
34  return in->engine_running
35  && in->brake_pedal_pressed
36  && in->gear_engaged;
37 }
38 
39 static bool apply_request_present(const ApplyInputs* in)
40 {
41  return (in->sw_state == SWITCH_APPLY) || in->safety_apply_request;
42 }
43 
44 static bool release_request_present(const ApplyInputs* in)
45 {
46  return in->sw_state == SWITCH_RELEASE;
47 }
48 
49 static uint16_t min_force(const ApplyInputs* in)
50 {
51  return (in->left_force_n < in->right_force_n)
52  ? in->left_force_n : in->right_force_n;
53 }
54 
56 {
58  s_ctx.step_in_state = 0U;
60  s_ctx.step_count = 0U;
61  return EPB_OK;
62 }
63 
65 {
66  if (in == NULL) {
68  return;
69  }
70 
71  /* SWE-002: Watchdog alive counter erhoehen */
72  ++s_ctx.step_count;
73 
74  if (s_ctx.step_in_state < UINT8_MAX) {
76  }
77 
78  switch (s_ctx.state) {
79  case EPB_STATE_RELEASED:
80  if (apply_request_present(in) && in->standstill) {
81  (void)actuator_apply(ACTUATOR_LEFT, 80U);
82  (void)actuator_apply(ACTUATOR_RIGHT, 80U);
84  }
85  break;
86 
87  case EPB_STATE_APPLYING:
88  /* SWE-004: Check target clamping force reached */
89  if (min_force(in) >= APPLY_TARGET_FORCE_N) {
93  } else if (s_ctx.step_in_state >= APPLY_TIMEOUT_50MS) {
98  }
99  break;
100 
101  case EPB_STATE_APPLIED:
102  /* SWE-001: Hold clamping force — re-apply on drop */
104  (void)actuator_apply(ACTUATOR_LEFT, 60U);
105  (void)actuator_apply(ACTUATOR_RIGHT, 60U);
107  break;
108  }
110  (void)actuator_release(ACTUATOR_LEFT, 80U);
111  (void)actuator_release(ACTUATOR_RIGHT, 80U);
113  }
114  break;
115 
116  case EPB_STATE_RELEASING:
117  if (min_force(in) < HOLD_TOLERANCE_N) {
121  } else if (s_ctx.step_in_state >= (APPLY_TIMEOUT_50MS - 6U)) {
126  }
127  break;
128 
129  case EPB_STATE_ERROR:
130  default:
131  /* Wait for reset; safe state is Apply, hence no release */
135  }
136  break;
137  }
138 }
139 
141 {
142  return s_ctx.state;
143 }
144 
146 {
147  return s_ctx.last_error;
148 }
149 
151 {
152  return s_ctx.step_count;
153 }
EpbStatus actuator_release(ActuatorId id, uint8_t pwm_percent)
EpbStatus actuator_apply(ActuatorId id, uint8_t pwm_percent)
EpbStatus actuator_stop(ActuatorId id)
Low-level control of the EPB actuators.
static uint16_t min_force(const ApplyInputs *in)
EpbStatus apply_ctrl_init(void)
static void enter_state(EpbState new_state)
static bool apply_request_present(const ApplyInputs *in)
static ApplyCtx s_ctx
static bool release_preconditions_ok(const ApplyInputs *in)
EpbState apply_ctrl_get_state(void)
static bool release_request_present(const ApplyInputs *in)
uint32_t apply_ctrl_get_step_count(void)
EpbStatus apply_ctrl_last_error(void)
void apply_ctrl_step_50ms(const ApplyInputs *in)
50 ms step function.
Apply/Hold/Release control of the EPB.
#define HOLD_TOLERANCE_N
#define APPLY_TIMEOUT_50MS
#define APPLY_TARGET_FORCE_N
EpbState
Definition: epb_types.h:22
@ EPB_STATE_ERROR
Definition: epb_types.h:27
@ EPB_STATE_APPLIED
Definition: epb_types.h:25
@ EPB_STATE_APPLYING
Definition: epb_types.h:24
@ EPB_STATE_RELEASED
Definition: epb_types.h:23
@ EPB_STATE_RELEASING
Definition: epb_types.h:26
EpbStatus
Definition: epb_types.h:13
@ EPB_EINVAL
Definition: epb_types.h:15
@ EPB_ETIMEOUT
Definition: epb_types.h:16
@ EPB_OK
Definition: epb_types.h:14
@ SWITCH_APPLY
Definition: epb_types.h:38
@ SWITCH_RELEASE
Definition: epb_types.h:39
@ ACTUATOR_RIGHT
Definition: epb_types.h:32
@ ACTUATOR_LEFT
Definition: epb_types.h:31
uint32_t step_count
EpbStatus last_error
uint8_t step_in_state
EpbState state
bool safety_apply_request
bool brake_pedal_pressed
SwitchState sw_state
uint16_t right_force_n
uint16_t left_force_n