Skip to content

Commit

Permalink
AP_Relay: added relay output invert function
Browse files Browse the repository at this point in the history
  • Loading branch information
LaneaLucy authored and tridge committed Mar 21, 2024
1 parent aa8b48b commit b060267
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
8 changes: 8 additions & 0 deletions libraries/AP_Relay/AP_Relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ void AP_Relay::set_pin_by_instance(uint8_t instance, bool value)

const bool initial_value = get_pin(pin);

if (_params[instance].reverse > 0) {
value = !value;
}

if (initial_value != value) {
set_pin(pin, value);
#if HAL_LOGGING_ENABLED
Expand Down Expand Up @@ -496,6 +500,10 @@ bool AP_Relay::get(uint8_t instance) const
return false;
}

if (_params[instance].reverse > 0) {
return !get_pin(_params[instance].pin.get());
}

return get_pin(_params[instance].pin.get());
}

Expand Down
9 changes: 8 additions & 1 deletion libraries/AP_Relay/AP_Relay_Params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ const AP_Param::GroupInfo AP_Relay_Params::var_info[] = {

// @Param: DEFAULT
// @DisplayName: Relay default state
// @Description: Should the relay default to on or off, this only applies to RELAYx_FUNC "Relay" (1). All other uses will pick the appropriate default output state from within the controlling function's parameters.
// @Description: Should the relay default to on or off, this only applies to RELAYx_FUNC "Relay" (1). All other uses will pick the appropriate default output state from within the controlling function's parameters. Note that if REVERSED is set then the default is reversed.
// @Values: 0: Off,1:On,2:NoChange
// @User: Standard
AP_GROUPINFO("DEFAULT", 3, AP_Relay_Params, default_state, (float)DefaultState::OFF),

// @Param: REVERSED
// @DisplayName: Relay reverse output signal
// @Description: Should the relay output signal be reversed. If enabled, relay on would be pin low and relay off would be pin high. NOTE: this impact's DEFAULT.
// @Values: 0: Off (normal),1:On (reversed)
// @User: Standard
AP_GROUPINFO("REVERSED", 4, AP_Relay_Params, reverse, false),

AP_GROUPEND

};
Expand Down
1 change: 1 addition & 0 deletions libraries/AP_Relay/AP_Relay_Params.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ class AP_Relay_Params {
AP_Enum<FUNCTION> function; // relay function
AP_Int16 pin; // gpio pin number
AP_Enum<DefaultState> default_state; // default state
AP_Int8 reverse; // reverse state
};

0 comments on commit b060267

Please sign in to comment.