6 #ifndef SM_ERRORHANDLE_H
7 #define SM_ERRORHANDLE_H
9 #include "sg_monitor.h"
10 #include "sm_errorhandlebase.h"
11 #include "sm_errorpub.h"
13 #include "sm_statemachine.h"
15 namespace mcx::state_machine {
17 template <
typename SUPER_STATE>
21 template <
typename... STATES>
22 void action_variadic(
const ErrorGroup& error_group, signal_monitor::MonitorAction action,
25 for (
auto&
id : id_list) {
26 state_machine_->getStatePtr(
id)->addErrors(error_group, action);
31 void action_variadic(
const ErrorGroup& error_group, signal_monitor::MonitorAction action) {}
47 error_publisher_.addParameters(param_name, parent);
50 void setName(
const std::string& name) { error_publisher_.setName(name); }
52 const std::string& getName()
const {
return error_publisher_.getName(); }
56 addParameters(error_name_prefix, parent);
59 const std::map<Error, MotorcortexError>& getActiveErrors()
const {
return error_publisher_.getActiveErrors(); }
61 MotorcortexError getActiveError()
const {
return error_publisher_.getActiveError(); }
63 bool empty()
override {
return error_publisher_.empty(); }
65 bool hasError(
const Error& error)
override {
return error_publisher_.hasError(error); }
67 bool hasErrorLevel(MotorcortexErrorLevel error_level)
override {
return error_publisher_.hasErrorLevel(error_level); }
69 bool acknowledgeAll() {
return error_publisher_.clear(
false); }
71 bool acknowledgeWarnings() {
return error_publisher_.clear(
true); }
73 bool acknowledge(
bool warnings_only =
false)
override {
return error_publisher_.clear(warnings_only); }
75 bool warning(
const Error& error)
override {
76 return error_publisher_.addError(error, MotorcortexErrorLevel::motorcortex_ErrorLevel_WARNING);
79 bool forcedDisengaged(
const Error& error)
override {
80 return error_publisher_.addError(error, MotorcortexErrorLevel::motorcortex_ErrorLevel_FORCED_DISENGAGE);
83 bool shutdown(
const Error& error)
override {
84 return error_publisher_.addError(error, MotorcortexErrorLevel::motorcortex_ErrorLevel_SHUTDOWN);
87 bool emergencyStop(
const Error& error)
override {
88 return error_publisher_.addError(error, MotorcortexErrorLevel::motorcortex_ErrorLevel_EMERGENCY_STOP);
91 void iterate(
double dt_sec) {
92 error_publisher_.publish();
93 signal_monitor_.iterate(dt_sec);
96 const ErrorPublisher& getErrorPublisher()
const {
return error_publisher_; }
102 void enableErrors(
const ErrorGroup& error_group) {
103 using namespace signal_monitor;
104 uint32_t counter = 0;
105 while (
auto state_ptr = state_machine_->getStatePtr(counter++)) {
106 state_ptr->addErrors(error_group, MonitorAction::ENABLE);
110 template <
class STATE,
typename... STATES>
111 void enableErrors(
const ErrorGroup& error_group) {
112 using namespace signal_monitor;
114 state_machine_->getStatePtr(
id)->addErrors(error_group, MonitorAction::ENABLE);
118 void disableErrors(
const ErrorGroup& error_group) {
119 using namespace signal_monitor;
120 uint32_t counter = 0;
121 while (
auto state_ptr = state_machine_->getStatePtr(counter++)) {
122 state_ptr->addErrors(error_group, MonitorAction::DISABLE);
126 template <
class STATE,
typename... STATES>
127 void disableErrors(
const ErrorGroup& error_group) {
128 using namespace signal_monitor;
130 state_machine_->getStatePtr(
id)->addErrors(error_group, MonitorAction::DISABLE);
134 void resetErrors(
const ErrorGroup& error_group) {
135 using namespace signal_monitor;
136 uint32_t counter = 0;
137 while (
auto state_ptr = state_machine_->getStatePtr(counter++)) {
138 state_ptr->addErrors(error_group, MonitorAction::RESET);
142 template <
class STATE,
typename... STATES>
143 void resetErrors(
const ErrorGroup& error_group) {
144 using namespace signal_monitor;
146 state_machine_->getStatePtr(
id)->addErrors(error_group, MonitorAction::RESET);
150 void addEmergencyStop(
const Error& error_id,
bool* input_ptr,
bool objective_val,
151 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
152 double stop_time_s = std::numeric_limits<double>::max()) {
156 trigger_type, start_time_s, stop_time_s);
161 void addEmergencyStop(
const Error& error_id,
const bool&& input_val,
bool objective_val,
162 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
163 double stop_time_s = std::numeric_limits<double>::max()) {
167 trigger_type, start_time_s, stop_time_s);
172 void addShutdown(
const Error& error_id,
bool* input_ptr,
bool objective_val,
173 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
174 double stop_time_s = std::numeric_limits<double>::max()) {
178 start_time_s, stop_time_s);
183 void addShutdown(
const Error& error_id,
const bool&& input_val,
bool objective_val,
184 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
185 double stop_time_s = std::numeric_limits<double>::max()) {
189 start_time_s, stop_time_s);
194 void addForceDisengage(
const Error& error_id,
bool* input_ptr,
bool objective_val,
195 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
196 double stop_time_s = std::numeric_limits<double>::max()) {
200 trigger_type, start_time_s, stop_time_s);
205 void addForceDisengage(
const Error& error_id,
const bool&& input_val,
bool objective_val,
206 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
207 double stop_time_s = std::numeric_limits<double>::max()) {
211 trigger_type, start_time_s, stop_time_s);
216 void addWarning(
const Error& error_id,
bool* input_ptr,
bool objective_val,
217 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
218 double stop_time_s = std::numeric_limits<double>::max()) {
222 start_time_s, stop_time_s);
227 void addWarning(
const Error& error_id,
const bool&& input_val,
bool objective_val,
228 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
229 double stop_time_s = std::numeric_limits<double>::max()) {
233 start_time_s, stop_time_s);
238 void addSignal(
const Error& error_id,
bool* input_val,
bool objective_val,
239 const std::function<
bool(
const Error& signal_id)>& callback,
240 TriggerType trigger_type = TriggerType::TRIGGER_ALWAYS,
double start_time_s = 0,
241 double stop_time_s = std::numeric_limits<double>::max()) {
243 signal_monitor_.add(error_id, *input_val, objective_val, callback, trigger_type, start_time_s, stop_time_s);
249 void addError(
const Error& error);
260 using namespace signal_monitor;
262 Error errorToAdd(error.getCode(), error.hasSubsystem() ? error.getSubsystem() : 0, error.getInfoPtr());
263 uint32_t counter = 0;
264 while (
auto state_ptr = state_machine_->getStatePtr(counter++)) {
265 state_ptr->addErrors(errorToAdd, MonitorAction::ADD);
266 state_ptr->addErrors(errorToAdd, MonitorAction::ENABLE);