Line data Source code
1 : /* 2 : * AP_Relay.h 3 : * 4 : * Created on: Oct 2, 2011 5 : * Author: Amilcar Lucas 6 : */ 7 : 8 : /// @file AP_Relay.h 9 : /// @brief APM relay control class 10 : #pragma once 11 : 12 : #include "AP_Relay_config.h" 13 : 14 : #if AP_RELAY_ENABLED 15 : 16 : #include <AP_Param/AP_Param.h> 17 : #include <AP_Relay/AP_Relay_Params.h> 18 : 19 : #ifndef AP_RELAY_NUM_RELAYS 20 : #define AP_RELAY_NUM_RELAYS 6 21 : #endif 22 : 23 : #if AP_RELAY_NUM_RELAYS < 1 24 : #error There must be at least one relay instance if using AP_Relay 25 : #endif 26 : 27 : #if AP_RELAY_DRONECAN_ENABLED 28 : #include <AP_DroneCAN/AP_DroneCAN.h> 29 : #endif 30 : 31 : /// @class AP_Relay 32 : /// @brief Class to manage the ArduPilot relay 33 : class AP_Relay { 34 : #if AP_RELAY_DRONECAN_ENABLED 35 : // Allow DroneCAN to directly access private DroneCAN state 36 : friend class AP_DroneCAN; 37 : #endif 38 : public: 39 : AP_Relay(); 40 : 41 : /* Do not allow copies */ 42 : CLASS_NO_COPY(AP_Relay); 43 : 44 : // setup the relay pin 45 : void init(); 46 : 47 : // activate the relay 48 23 : void on(uint8_t instance) { set(instance, true); } 49 : 50 : // de-activate the relay 51 26 : void off(uint8_t instance) { set(instance, false); } 52 : 53 : // get state of relay 54 : bool get(uint8_t instance) const; 55 : 56 : // see if the relay is enabled 57 : bool enabled(uint8_t instance) const; 58 : 59 : // toggle the relay status 60 : void toggle(uint8_t instance); 61 : 62 : // check settings are valid 63 : bool arming_checks(size_t buflen, char *buffer) const; 64 : 65 476164 : static AP_Relay *get_singleton(void) {return singleton; } 66 : 67 : static const struct AP_Param::GroupInfo var_info[]; 68 : 69 : bool send_relay_status(const class GCS_MAVLINK &link) const; 70 : 71 : // Set the state of all relays that are configured as the specified function type 72 : void set(AP_Relay_Params::FUNCTION function, bool value); 73 : 74 : // see if the relay is enabled 75 : bool enabled(AP_Relay_Params::FUNCTION function) const; 76 : 77 : // Get the pin number of the instance (if assigned to a pin) 78 : bool get_pin_by_instance(uint8_t instance, uint8_t &pin) const; 79 : 80 : private: 81 : static AP_Relay *singleton; 82 : 83 : AP_Relay_Params _params[AP_RELAY_NUM_RELAYS]; 84 : 85 : // Return true if function is valid 86 : bool function_valid(AP_Relay_Params::FUNCTION function) const; 87 : 88 : // Set the state of the specified instance, if it is a valid relay 89 : void set(uint8_t instance, bool value); 90 : 91 : void set_defaults(); 92 : void convert_params(); 93 : 94 : // Internal helper: set the instance state, accounting for configured inversion 95 : void set_instance_state(uint8_t instance, bool value); 96 : 97 : // Internal helper: directly set the state of the specified pin 98 : void set_pin_state(const int16_t pin, const bool value); 99 : 100 : // Get the state of the specified pin 101 : bool get_pin_state(const int16_t pin) const; 102 : 103 : #if AP_RELAY_DRONECAN_ENABLED 104 : // Virtual DroneCAN pins 105 : class DroneCAN { 106 : public: 107 : // Return true if pin number is a virtual DroneCAN pin 108 : bool valid_pin(int16_t pin) const; 109 : 110 : // Enable streaming of pin number 111 : void enable_pin(int16_t pin); 112 : 113 : // Populate message and update index with the sent command 114 : bool populate_next_command(uint8_t &index, uavcan_equipment_hardpoint_Command &msg) const; 115 : 116 : // Set DroneCAN relay state from pin number 117 : void set_pin_state(const int16_t pin, const bool value); 118 : 119 : // Get relay state from pin number 120 : bool get_pin_state(const int16_t pin) const; 121 : 122 : private: 123 : 124 : // Get the hardpoint index of given pin number 125 : uint8_t hardpoint_index(const int16_t pin) const; 126 : 127 : // Send DroneCAN hardpoint message for given index on all interfaces 128 : void send_index(const uint8_t index); 129 : 130 : static constexpr uint8_t num_pins = (int16_t)AP_Relay_Params::VIRTUAL_PINS::DroneCAN_15 - (int16_t)AP_Relay_Params::VIRTUAL_PINS::DroneCAN_0; 131 : 132 : struct { 133 : bool value; 134 : bool enabled; 135 : } state[num_pins]; 136 : 137 : } dronecan; 138 : #endif // AP_RELAY_DRONECAN_ENABLED 139 : 140 : }; 141 : 142 : namespace AP { 143 : AP_Relay *relay(); 144 : }; 145 : 146 : #endif // AP_RELAY_ENABLED