From 434e76a747c037f09ee07c798ebc47a6b93f7303 Mon Sep 17 00:00:00 2001 From: Christopher Vo Date: Wed, 29 Mar 2023 16:11:16 -0400 Subject: [PATCH] Enable joystick automatically on the active vehicle when the active vehicle is changed When there are multiple vehicles, and you select a vehicle, the joystick should switch over to the newly active vehicle, and only the active vehicle (so that joystick inputs are not sent to a vehicle you do not intend to control). To resolve this, we connect Vehicle to the activeVehicleAvailableChanged and activeVehicleChanged signals produced by the MultiVehicleManager. ALL vehicles disable joystick when activeVehicleAvailableChanged(false), because this signals when there is no longer an active vehicle available. When activeVehicleChanged is signalled, ONLY the currently active vehicle enables joystick. See issue #10544. --- src/Vehicle/Vehicle.cc | 19 ++++++++++++++++++- src/Vehicle/Vehicle.h | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Vehicle/Vehicle.cc b/src/Vehicle/Vehicle.cc index 7b34ab4..a454197 100644 --- a/src/Vehicle/Vehicle.cc +++ b/src/Vehicle/Vehicle.cc @@ -178,7 +178,8 @@ Vehicle::Vehicle(LinkInterface* link, _linkManager = _toolbox->linkManager(); connect(_joystickManager, &JoystickManager::activeJoystickChanged, this, &Vehicle::_loadSettings); - connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::activeVehicleAvailableChanged, this, &Vehicle::_loadSettings); + connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::activeVehicleAvailableChanged, this, &Vehicle::_activeVehicleAvailableChanged); + connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::activeVehicleChanged, this, &Vehicle::_activeVehicleChanged); _mavlink = _toolbox->mavlinkProtocol(); qCDebug(VehicleLog) << "Link started with Mavlink " << (_mavlink->getCurrentVersion() >= 200 ? "V2" : "V1"); @@ -2084,6 +2085,22 @@ void Vehicle::_loadSettings() } } +void Vehicle::_activeVehicleAvailableChanged(bool isActiveVehicleAvailable) +{ + // if there is no longer an active vehicle, disconnect the joystick + if(!isActiveVehicleAvailable) { + setJoystickEnabled(false); + } +} + +void Vehicle::_activeVehicleChanged(Vehicle *newActiveVehicle) +{ + if(newActiveVehicle == this) { + // this vehicle is the newly active vehicle + setJoystickEnabled(true); + } +} + void Vehicle::_saveSettings() { QSettings settings; diff --git a/src/Vehicle/Vehicle.h b/src/Vehicle/Vehicle.h index 5ec9468..2b18eba 100644 --- a/src/Vehicle/Vehicle.h +++ b/src/Vehicle/Vehicle.h @@ -1018,6 +1018,8 @@ private slots: private: void _loadSettings (); + void _activeVehicleAvailableChanged (bool isActiveVehicleAvailable); + void _activeVehicleChanged (Vehicle* newActiveVehicle); void _saveSettings (); void _startJoystick (bool start); void _handlePing (LinkInterface* link, mavlink_message_t& message);