@@ -169,6 +169,28 @@ void AP_Relay::init()
169
169
set (i, (bool )default_state);
170
170
}
171
171
}
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
+
172
194
}
173
195
174
196
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)
186
208
return ;
187
209
}
188
210
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
+
189
218
_relay_by_instance.setonoff (instance, value);
190
219
}
191
220
@@ -267,8 +296,16 @@ void AP_Relay::update()
267
296
continue ;
268
297
}
269
298
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
+ }
272
309
const uint8_t old_pin_states = _pin_states;
273
310
_pin_states = value ? _pin_states | (1U <<instance) : _pin_states & ~(1U <<instance);
274
311
if (old_pin_states != _pin_states) {
0 commit comments