Skip to content

Commit 5c6479b

Browse files
committed
Relay
1 parent 61d041c commit 5c6479b

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

libraries/AP_Relay/AP_Relay.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ void AP_Relay::init()
169169
set(i, (bool)default_state);
170170
}
171171
}
172+
173+
for (uint8_t instance = 0; instance < AP_RELAY_NUM_RELAYS; instance++) {
174+
const int8_t pin = _params[instance].pin;
175+
if (pin == -1) {
176+
// no valid pin to set it on, skip it
177+
continue;
178+
}
179+
180+
const AP_Relay_Params::Function function = _params[instance].function;
181+
if (function <= AP_Relay_Params::Function::relay || function >= AP_Relay_Params::Function::num_functions) {
182+
// invalid function, skip it
183+
continue;
184+
}
185+
186+
// all functions are interpreted to be off at power on, so if we tag our pins to the wrong
187+
// state that will trigger the first call to output to update the state, and will get the
188+
// pins into the correct state.
189+
//
190+
// This will need to be made aware of pin inversion when that support is added
191+
_pin_states = _pin_states | (1U<<instance);
192+
}
193+
172194
}
173195

174196
void AP_Relay::set(const AP_Relay_Params::Function function, const bool value) {
@@ -186,6 +208,13 @@ void AP_Relay::set(const uint8_t instance, const bool value)
186208
return;
187209
}
188210

211+
if (!_relay_by_instance_has_been_asserted.get(instance)) {
212+
// this is the first time this pin has been assigned make sure the tagged previous state was wrong
213+
// so that update is guranteed to cause it to output
214+
_pin_states = !value ? _pin_states | (1U<<instance) : _pin_states & ~(1U<<instance);
215+
_relay_by_instance_has_been_asserted.set(instance);
216+
}
217+
189218
_relay_by_instance.setonoff(instance, value);
190219
}
191220

@@ -267,8 +296,16 @@ void AP_Relay::update()
267296
continue;
268297
}
269298

270-
const bool instance_numbered_relay = (function == AP_Relay_Params::Function::relay);
271-
const bool value = instance_numbered_relay ? _relay_by_instance.get (instance) : _desired_state.get((uint16_t)function);
299+
bool value;
300+
if (function == AP_Relay_Params::Function::relay) {
301+
if (_relay_by_instance_has_been_asserted.get(instance)) {
302+
value = _relay_by_instance.get (instance);
303+
} else {
304+
continue; // still in the default no change state
305+
}
306+
} else {
307+
value = _desired_state.get((uint16_t)function);
308+
}
272309
const uint8_t old_pin_states = _pin_states;
273310
_pin_states = value ? _pin_states | (1U<<instance) : _pin_states & ~(1U<<instance);
274311
if (old_pin_states != _pin_states) {

libraries/AP_Relay/AP_Relay.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class AP_Relay {
7474
void convert_params();
7575

7676
Bitmask<AP_RELAY_NUM_RELAYS> _relay_by_instance;
77+
Bitmask<AP_RELAY_NUM_RELAYS> _relay_by_instance_has_been_asserted;
7778
Bitmask<(uint16_t)AP_Relay_Params::Function::num_functions> _desired_state;
7879
};
7980

0 commit comments

Comments
 (0)