Open FFBoard
Open source force feedback firmware
ErrorHandler.h
Go to the documentation of this file.
1/*
2 * ErrorHandler.h
3 *
4 * Created on: 09.02.2021
5 * Author: Yannick
6 */
7
8#ifndef SRC_ERRORHANDLER_H_
9#define SRC_ERRORHANDLER_H_
10#include <array>
11#include <string>
12#include "thread.hpp"
13#include "CommandHandler.h"
14#include <span>
15
16#ifndef ERRORHANDLER_MAXERRORS
17#define ERRORHANDLER_MAXERRORS 16
18#endif
19
20/*
21 * Error code definitions
22 */
23
24enum class ErrorCode : uint32_t{
25 none = 0,
26 shutdown = 1,
27 emergencyStop = 2,
29 systemError = 5,
30
31 cmdNotFound = 5,
33
34 undervoltage = 10,
35 overvoltage = 11,
37 tmcPLLunlocked = 13,
38
39 overtemp = 15,
40
43 tmcCalibFail = 22,
45 encoderReversed = 24,
46
47 axisOutOfRange = 31,
48
50};
51/*
52 * Error type for severity
53 * temporary errors are cleared when read back
54 * critical errors cause a shutdown or have to be cleared externally
55 */
56enum class ErrorType : uint8_t{
58};
59
60class Error{
61public:
62 //Error(const Error &e);
63 Error(){}; // No error
67 std::string info;
68
69 std::string toString();
70
71 /*
72 * Errors are equivalent if their code and type match
73 */
74 bool operator ==(const Error &b) const {return (this->code == b.code && this->type == b.type);}
75 bool isError() const {return this->code != ErrorCode::none && this->type != ErrorType::none;}
76};
77
78
79
80
82public:
83 static std::vector<ErrorHandler*> errorHandlers;
84
86 virtual ~ErrorHandler();
87 virtual void errorCallback(const Error &error, bool cleared); // Called when a new error happened. Warning: Could be called from ISR!
88
89 static void addError(const Error &error);
90 static void clearError(const Error &error);
91 static void clearError(ErrorCode errorcode);
92 static void clearTemp();
93 static void clearAll();
94
95 static std::span<Error> getErrors(); // Returns a vector of active errors
96
97protected:
98 static std::array<Error,ERRORHANDLER_MAXERRORS> errors;
99 static void sortErrors();
100};
101
102/*
103 * Can handle errors occuring in the main class
104 */
106public:
107 ErrorPrinter();
108 void errorCallback(const Error &error, bool cleared);
109 void setEnabled(bool enable);
110 bool getEnabled();
111 void Run();
112
113// static ClassIdentifier info;
114// ClassIdentifier getInfo();
115//
116// const ClassType getClassType(){return ClassType::Internal;};
117private:
118 bool enabled = true;
119 //std::vector<Error> errorsToPrint;
120};
121
122#endif /* SRC_ERRORHANDLER_H_ */
ErrorType
Definition: ErrorHandler.h:56
ErrorCode
Definition: ErrorHandler.h:24
@ brakeResistorFailure
@ externalConfigurationError
@ cmdExecutionError
@ adcCalibrationError
@ encoderAlignmentFailed
@ encoderIndexMissed
@ tmcCommunicationError
static std::span< Error > getErrors()
static void sortErrors()
static std::array< Error, ERRORHANDLER_MAXERRORS > errors
Definition: ErrorHandler.h:98
static void clearTemp()
virtual ~ErrorHandler()
virtual void errorCallback(const Error &error, bool cleared)
static std::vector< ErrorHandler * > errorHandlers
Definition: ErrorHandler.h:83
static void clearError(const Error &error)
static void addError(const Error &error)
static void clearAll()
std::string info
Definition: ErrorHandler.h:67
ErrorCode code
Definition: ErrorHandler.h:65
ErrorType type
Definition: ErrorHandler.h:66
bool isError() const
Definition: ErrorHandler.h:75
Error(ErrorCode code, ErrorType type, std::string info)
Definition: ErrorHandler.h:64
bool operator==(const Error &b) const
Definition: ErrorHandler.h:74
std::string toString()
void errorCallback(const Error &error, bool cleared)
void setEnabled(bool enable)