Line data Source code
1 : #pragma once 2 : 3 : #include "AP_BattMonitor.h" 4 : #include "AP_BattMonitor_Backend.h" 5 : #if HAL_ENABLE_DRONECAN_DRIVERS 6 : #include <AP_DroneCAN/AP_DroneCAN.h> 7 : 8 : #define AP_BATTMONITOR_UAVCAN_TIMEOUT_MICROS 5000000 // sensor becomes unhealthy if no successful readings for 5 seconds 9 : 10 : #ifndef AP_BATTMONITOR_UAVCAN_MPPT_DEBUG 11 : #define AP_BATTMONITOR_UAVCAN_MPPT_DEBUG 0 12 : #endif 13 : 14 : class AP_BattMonitor_DroneCAN : public AP_BattMonitor_Backend 15 : { 16 : public: 17 : enum BattMonitor_DroneCAN_Type { 18 : UAVCAN_BATTERY_INFO = 0 19 : }; 20 : 21 : /// Constructor 22 : AP_BattMonitor_DroneCAN(AP_BattMonitor &mon, AP_BattMonitor::BattMonitor_State &mon_state, BattMonitor_DroneCAN_Type type, AP_BattMonitor_Params ¶ms); 23 : 24 : static const struct AP_Param::GroupInfo var_info[]; 25 : 26 0 : void init() override {} 27 : 28 : /// Read the battery voltage and current. Should be called at 10hz 29 : void read() override; 30 : 31 : /// capacity_remaining_pct - returns true if the percentage is valid and writes to percentage argument 32 : bool capacity_remaining_pct(uint8_t &percentage) const override; 33 : 34 0 : bool has_temperature() const override { return _has_temperature; } 35 : 36 0 : bool has_current() const override { return true; } 37 : 38 : // Always have consumed energy, either directly from BatteryInfoAux msg or by cumulative current draw 39 0 : bool has_consumed_energy() const override { return true; } 40 : 41 0 : bool has_time_remaining() const override { return _has_time_remaining; } 42 : 43 0 : bool has_cell_voltages() const override { return _has_cell_voltages; } 44 : 45 : bool get_cycle_count(uint16_t &cycles) const override; 46 : 47 : // return mavlink fault bitmask (see MAV_BATTERY_FAULT enum) 48 : uint32_t get_mavlink_fault_bitmask() const override; 49 : 50 : static bool subscribe_msgs(AP_DroneCAN* ap_dronecan); 51 : static AP_BattMonitor_DroneCAN* get_dronecan_backend(AP_DroneCAN* ap_dronecan, uint8_t node_id, uint8_t battery_id); 52 : static void handle_battery_info_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const uavcan_equipment_power_BatteryInfo &msg); 53 : static void handle_battery_info_aux_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const ardupilot_equipment_power_BatteryInfoAux &msg); 54 : static void handle_mppt_stream_trampoline(AP_DroneCAN *ap_dronecan, const CanardRxTransfer& transfer, const mppt_Stream &msg); 55 : 56 : void set_powered_state(bool power_on) override; 57 : 58 : // reset remaining percentage to given value 59 : bool reset_remaining(float percentage) override; 60 : 61 : private: 62 : void handle_battery_info(const uavcan_equipment_power_BatteryInfo &msg); 63 : void handle_battery_info_aux(const ardupilot_equipment_power_BatteryInfoAux &msg); 64 : void update_interim_state(const float voltage, const float current, const float temperature_K, const uint8_t soc, uint8_t soh_pct); 65 : 66 : static bool match_battery_id(uint8_t instance, uint8_t battery_id); 67 : 68 : // MPPT related enums and methods 69 : enum class MPPT_FaultFlags : uint8_t { 70 : OVER_VOLTAGE = (1U<<0), 71 : UNDER_VOLTAGE = (1U<<1), 72 : OVER_CURRENT = (1U<<2), 73 : OVER_TEMPERATURE = (1U<<3), 74 : }; 75 : void handle_mppt_stream(const mppt_Stream &msg); 76 : void mppt_check_powered_state(); 77 : 78 : #if AP_BATTMONITOR_UAVCAN_MPPT_DEBUG 79 : static void mppt_report_faults(const uint8_t instance, const uint8_t fault_flags); 80 : static const char* mppt_fault_string(const MPPT_FaultFlags fault); 81 : #endif 82 : 83 : // Return true if the DroneCAN state of charge should be used. 84 : // Return false if state of charge should be calculated locally by counting mah. 85 : bool use_CAN_SoC() const; 86 : 87 : AP_BattMonitor::BattMonitor_State _interim_state; 88 : 89 : HAL_Semaphore _sem_battmon; 90 : 91 : AP_DroneCAN* _ap_dronecan; 92 : uint8_t _soc; 93 : uint8_t _node_id; 94 : uint16_t _cycle_count; 95 : float _remaining_capacity_wh; 96 : float _full_charge_capacity_wh; 97 : bool _has_temperature; 98 : bool _has_cell_voltages; 99 : bool _has_time_remaining; 100 : bool _has_battery_info_aux; 101 : uint8_t _instance; // instance of this battery monitor 102 : 103 : AP_Float _curr_mult; // scaling multiplier applied to current reports for adjustment 104 : // MPPT variables 105 : struct { 106 : bool is_detected; // true if this UAVCAN device is a Packet Digital MPPT 107 : bool powered_state; // true if the mppt is powered on, false if powered off 108 : bool vehicle_armed_last; // latest vehicle armed state. used to detect changes and power on/off MPPT board 109 : uint8_t fault_flags; // bits holding fault flags 110 : uint32_t powered_state_remote_ms; // timestamp of when request was sent, zeroed on response. Used to retry 111 : } _mppt; 112 : 113 : void handle_outputEnable_response(const CanardRxTransfer&, const mppt_OutputEnableResponse&); 114 : 115 : Canard::ObjCallback<AP_BattMonitor_DroneCAN, mppt_OutputEnableResponse> mppt_outputenable_res_cb{this, &AP_BattMonitor_DroneCAN::handle_outputEnable_response}; 116 : Canard::Client<mppt_OutputEnableResponse> *mppt_outputenable_client; 117 : }; 118 : #endif