Line data Source code
1 : /* 2 : * This file is free software: you can redistribute it and/or modify it 3 : * under the terms of the GNU General Public License as published by the 4 : * Free Software Foundation, either version 3 of the License, or 5 : * (at your option) any later version. 6 : * 7 : * This file is distributed in the hope that it will be useful, but 8 : * WITHOUT ANY WARRANTY; without even the implied warranty of 9 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 10 : * See the GNU General Public License for more details. 11 : * 12 : * You should have received a copy of the GNU General Public License along 13 : * with this program. If not, see <http://www.gnu.org/licenses/>. 14 : * 15 : * Author: Oliver Walters / Currawong Engineering Pty Ltd 16 : */ 17 : 18 : #pragma once 19 : 20 : #include "AP_Generator_config.h" 21 : 22 : #if AP_GENERATOR_CORTEX_ENABLED 23 : 24 : #include "AP_Generator_Backend.h" 25 : #include <AP_Common/AP_Common.h> 26 : #include <AP_PiccoloCAN/AP_PiccoloCAN.h> 27 : #include <AP_PiccoloCAN/AP_PiccoloCAN_Device.h> 28 : #include <AP_PiccoloCAN/piccolo_protocol/CortexPackets.h> 29 : 30 : 31 : /** 32 : * Class defining the Cortex generator from Currawong Engineering 33 : */ 34 : class AP_Generator_Cortex : public AP_Generator_Backend 35 : { 36 : public: 37 : using AP_Generator_Backend::AP_Generator_Backend; 38 : 39 : // init should be called at vehicle startup to get the generator library ready 40 : __INITFUNC__ void init(void) override; 41 : // update should be called regularly to update the generator state 42 : virtual void update(void) override; 43 : 44 : // methods to control the generator state: 45 : virtual bool stop(void) override; 46 : virtual bool idle(void) override; 47 : virtual bool run(void) override; 48 : 49 : // method to send a GENERATOR_STATUS mavlink message 50 : virtual void send_generator_status(const GCS_MAVLINK &channel) override; 51 : 52 : // prearm checks to ensure generator is good for arming. 53 : virtual bool pre_arm_check(char *failmsg, uint8_t failmsg_len) const override; 54 : 55 : // Update front end with voltage, current, and rpm values 56 : void update_frontend_readings(void); 57 : 58 : bool is_connected(void) const; 59 : 60 0 : bool is_inhibited(void) const { 61 0 : return telemetry.status.status.inhibited; 62 : } 63 : 64 0 : bool is_ready(void) const { 65 0 : return is_connected() && telemetry.status.status.readyToRun; 66 : } 67 : 68 : // healthy returns true if the generator is not present, or it is 69 : // present, providing telemetry and not indicating an errors. 70 0 : bool healthy() const override { 71 0 : return is_connected(); 72 : } 73 : 74 0 : static AP_Generator_Cortex* get_instance(void) 75 : { 76 0 : return _singleton; 77 : } 78 : 79 : bool handle_message(AP_HAL::CANFrame &frame, AP_PiccoloCAN &can_iface); 80 : 81 : private: 82 : static AP_Generator_Cortex* _singleton; 83 : 84 : // Internal data structures for received telemetry 85 : struct CortexTelemetry_t { 86 : Cortex_TelemetryStatus_t status; 87 : Cortex_TelemetryGenerator_t generator; 88 : Cortex_TelemetryBattery_t battery; 89 : Cortex_TelemetryOutputRail_t rails; 90 : Cortex_TelemetryController_t controller; 91 : } telemetry; 92 : 93 0 : float batteryCurrent(void) const { return telemetry.battery.current; } 94 : float batteryVoltage(void) const { return telemetry.battery.voltage; } 95 : 96 0 : float generatorCurrent(void) const { return telemetry.generator.current; } 97 0 : float generatorVoltage(void) const { return telemetry.generator.voltage; } 98 0 : float generatorPower(void) const { return generatorCurrent() * generatorVoltage(); } 99 : 100 0 : float loadCurrent(void) const { return batteryCurrent() - generatorCurrent(); } 101 : 102 : int16_t rectifierTemperature(void) const; 103 : 104 : // Pointer to the CAN interface used to communicate with the generator 105 : AP_PiccoloCAN* _can_iface; 106 : 107 : // CAN device type 108 : uint16_t _can_device_type; 109 : uint8_t _can_device_id = 0xFF; 110 : 111 : // Last telemetry reading from the generator 112 : uint32_t last_reading_ms; 113 : 114 : // Connection state, used to detect changes 115 : bool connected; 116 : 117 : bool send_message(AP_HAL::CANFrame &frame); 118 : }; 119 : 120 : #endif // AP_GENERATOR_CORTEX_ENABLED