Line data Source code
1 : #pragma once 2 : 3 : #include "AP_Generator_Backend.h" 4 : 5 : #if AP_GENERATOR_IE_ENABLED 6 : 7 : #include <AP_Logger/AP_Logger_config.h> 8 : #include <GCS_MAVLink/GCS.h> 9 : 10 : class AP_Generator_IE_FuelCell : public AP_Generator_Backend 11 : { 12 : 13 : public: 14 : // Constructor 15 : using AP_Generator_Backend::AP_Generator_Backend; 16 : 17 : // Initialize the fuel cell driver 18 : __INITFUNC__ void init(void) override; 19 : 20 : // Check if readings are healthy 21 10390 : bool healthy(void) const override { return _healthy; } 22 : 23 : // Check for arming 24 : bool pre_arm_check(char *failmsg, uint8_t failmsg_len) const override; 25 : 26 : // Update fuel cell, expected to be called at 20hz 27 : void update(void) override; 28 : 29 : protected: 30 : 31 : // Pointer to serial uart 32 : AP_HAL::UARTDriver *_uart = nullptr; 33 : 34 : // IE fuel cell running states 35 : enum class State { 36 : STARTING = 0, 37 : READY = 1, 38 : RUNNING = 2, 39 : FAULT = 3, 40 : BATTERY_ONLY = 8, 41 : }; 42 : 43 : // State enum to string lookup 44 : struct Lookup_State { 45 : State option; 46 : const char *msg_txt; 47 : }; 48 : 49 : static const Lookup_State lookup_state[]; 50 : 51 : uint32_t _err_code; // The error code from fuel cell 52 : uint32_t _last_err_code; // The previous error code from fuel cell 53 : uint32_t _sub_err_code; // The sub error code from fuel cell 54 : uint32_t _last_sub_err_code; // The previous sub error code from fuel cell 55 : State _state; // The PSU state 56 : State _last_state; // The previous PSU state 57 : uint32_t _last_time_ms; // Time we got a reading 58 : bool _healthy; // Is the driver working 59 : uint32_t _health_warn_last_ms; // Time to persist warning message without spamming 60 : 61 : // Temporary state params 62 : struct ParsedValue { 63 : float tank_pct; 64 : uint16_t tank_bar; 65 : float battery_pct; 66 : float battery_volt; 67 : int16_t pwr_out; 68 : uint16_t spm_pwr; 69 : int16_t battery_pwr; 70 : uint8_t state; 71 : uint32_t err_code; 72 : uint32_t sub_err_code; 73 : } _parsed; 74 : 75 : // Constants 76 : static const uint8_t TERM_BUFFER = 33; // Max length of term we expect 77 : static const uint16_t HEALTHY_TIMEOUT_MS = 5000; // Time for driver to be marked un-healthy 78 : 79 : // Decoding vars 80 : char _start_char; // inital sentence character giving sentence type 81 : char _term[TERM_BUFFER]; // Term buffer 82 : bool _sentence_valid; // Is current sentence valid 83 : bool _data_valid; // Is data within expected limits 84 : uint8_t _term_number; // Term index within the current sentence 85 : uint8_t _term_offset; // Offset within the _term buffer where the next character should be placed 86 : bool _in_string; // True if we should be decoding 87 : uint8_t _checksum; // Basic checksum used by V2 protocol 88 : 89 : // Assigns the unit specific measurements once a valid sentence is obtained 90 : virtual void assign_measurements(const uint32_t now) = 0; 91 : 92 : #if HAL_LOGGING_ENABLED 93 0 : virtual void log_write(void) {} 94 : #endif 95 : 96 : // Add a single character to the buffer and attempt to decode. 97 : // Returns true if a complete sentence was successfully decoded or if the buffer is full. 98 : bool decode(char c); 99 : 100 : // Unit specific decoding to process characters received and build sentence 101 : virtual void decode_latest_term(void) = 0; 102 : 103 : // Check if we should notify on any change of fuel cell state 104 : virtual void check_status(const uint32_t now); 105 : 106 : // Check error codes and populate message with error code 107 : virtual bool check_for_err_code(char* msg_txt, uint8_t msg_len) const = 0; 108 : 109 : // Check if we have received an warning code and populate message with warning code 110 0 : virtual bool check_for_warning_code(char* msg_txt, uint8_t msg_len) const { return false; } 111 : 112 : // Only check the error code if it has changed since we last checked 113 : void check_for_err_code_if_changed(); 114 : 115 : // Return true is fuel cell is in running state suitable for arming 116 : virtual bool is_running() const; 117 : 118 : // Print msg to user updating on state change 119 : virtual void update_state_msg(); 120 : 121 : #if HAL_GCS_ENABLED 122 : // Get the MAV_SEVERITY level of a given error code 123 0 : virtual MAV_SEVERITY get_mav_severity(uint32_t err_code) const { return MAV_SEVERITY_ALERT; } 124 : #endif 125 : 126 : }; 127 : #endif // AP_GENERATOR_IE_ENABLED