Line data Source code
1 : /* 2 : This program is free software: you can redistribute it and/or modify 3 : it under the terms of the GNU General Public License as published by 4 : the Free Software Foundation, either version 3 of the License, or 5 : (at your option) any later version. 6 : 7 : This program is distributed in the hope that it will be useful, 8 : but WITHOUT ANY WARRANTY; without even the implied warranty of 9 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 : GNU General Public License for more details. 11 : 12 : You should have received a copy of the GNU General Public License 13 : along with this program. If not, see <http://www.gnu.org/licenses/>. 14 : */ 15 : 16 : #pragma once 17 : 18 : #include "AP_EFI_config.h" 19 : 20 : #if HAL_EFI_ENABLED 21 : 22 : #include <AP_Common/AP_Common.h> 23 : #include <AP_Param/AP_Param.h> 24 : #include <GCS_MAVLink/GCS_MAVLink.h> 25 : #include "AP_EFI_ThrottleLinearisation.h" 26 : #include <AP_HAL/AP_HAL_Boards.h> 27 : 28 : #ifndef AP_EFI_LOWEHEISER_ENABLED 29 : #define AP_EFI_LOWEHEISER_ENABLED 0 30 : #endif 31 : 32 : #include "AP_EFI_Backend.h" 33 : #include "AP_EFI_State.h" 34 : 35 : 36 : /* 37 : * This library aims to read data from Electronic Fuel Injection 38 : * or Engine Control units. It is focused around the generic 39 : * internal combustion engine state message provided by the 40 : * UAVCAN protocol due to its comprehensiveness, but is extensible 41 : * to use other forms of data transfer besides UAVCAN. 42 : * 43 : * 44 : * 45 : * Authors: Sriram Sami and David Ingraham 46 : * With direction from Andrew Tridgell, Robert Lefebvre, Francisco Ferreira and 47 : * Pavel Kirienko. 48 : * Thanks to Yonah, SpektreWorks Inc, and HFE International. 49 : */ 50 : 51 : class AP_EFI { 52 : public: 53 : friend class AP_EFI_Backend; 54 : 55 : // For parameter initialization 56 : AP_EFI(); 57 : 58 : // Initializes backend 59 : void init(void); 60 : 61 : // Requests backend to update the frontend. Should be called at 10Hz. 62 : void update(); 63 : 64 : // Returns the RPM 65 24570 : uint32_t get_rpm() const { return state.engine_speed_rpm; } 66 : 67 : // returns enabled state of EFI 68 : bool enabled() const { return type != Type::NONE; } 69 : 70 : bool is_healthy() const; 71 : 72 : // return timestamp of last update 73 0 : uint32_t get_last_update_ms(void) const { 74 0 : return state.last_updated_ms; 75 : } 76 : 77 : // get a copy of state structure 78 : void get_state(EFI_State &state); 79 : 80 : // Parameter info 81 : static const struct AP_Param::GroupInfo var_info[]; 82 : 83 : // Backend driver types 84 : enum class Type : uint8_t { 85 : NONE = 0, 86 : #if AP_EFI_SERIAL_MS_ENABLED 87 : MegaSquirt = 1, 88 : #endif 89 : #if AP_EFI_NWPWU_ENABLED 90 : NWPMU = 2, 91 : #endif 92 : #if AP_EFI_SERIAL_LUTAN_ENABLED 93 : Lutan = 3, 94 : #endif 95 : #if AP_EFI_LOWEHEISER_ENABLED 96 : LOWEHEISER = 4, 97 : #endif 98 : #if AP_EFI_DRONECAN_ENABLED 99 : DroneCAN = 5, 100 : #endif 101 : #if AP_EFI_CURRAWONG_ECU_ENABLED 102 : CurrawongECU = 6, 103 : #endif 104 : #if AP_EFI_SCRIPTING_ENABLED 105 : SCRIPTING = 7, 106 : #endif 107 : #if AP_EFI_SERIAL_HIRTH_ENABLED 108 : Hirth = 8, 109 : #endif 110 : MAV = 9, 111 : }; 112 : 113 1146774 : static AP_EFI *get_singleton(void) { 114 1146774 : return singleton; 115 : } 116 : 117 : // send EFI_STATUS 118 : void send_mavlink_status(mavlink_channel_t chan); 119 : 120 : #if AP_SCRIPTING_ENABLED 121 0 : AP_EFI_Backend* get_backend(uint8_t idx) { return idx==0?backend:nullptr; } 122 : #endif 123 : 124 : void handle_EFI_message(const mavlink_message_t &msg); 125 : 126 : protected: 127 : 128 : // Back end Parameters 129 : AP_Float coef1; 130 : AP_Float coef2; 131 : 132 : AP_Float ecu_fuel_density; 133 : 134 : EFI_State state; 135 : 136 : #if AP_EFI_THROTTLE_LINEARISATION_ENABLED 137 : AP_EFI_ThrLin throttle_linearisation; 138 : #endif 139 : 140 : private: 141 : // Front End Parameters 142 : AP_Enum<Type> type; 143 : 144 : // Tracking backends 145 : AP_EFI_Backend *backend; 146 : static AP_EFI *singleton; 147 : 148 : // Semaphore for access to shared frontend data 149 : HAL_Semaphore sem; 150 : 151 : // write to log 152 : void log_status(); 153 : }; 154 : 155 : namespace AP { 156 : AP_EFI *EFI(); 157 : }; 158 : 159 : #endif // HAL_EFI_ENABLED