10
10
11
11
#include " Joystick.h"
12
12
#include " QGC.h"
13
- #include " AutoPilotPlugin.h"
14
13
#include " UAS.h"
15
14
#include " QGCApplication.h"
16
15
#include " VideoManager.h"
@@ -88,6 +87,7 @@ const float Joystick::_minAxisFrequencyHz = 0.25f;
88
87
const float Joystick::_maxAxisFrequencyHz = 200 .0f ;
89
88
const float Joystick::_minButtonFrequencyHz = 0 .25f ;
90
89
const float Joystick::_maxButtonFrequencyHz = 50 .0f ;
90
+ const float Joystick::_rcOverrideFrequencyHz = 0 .5f ;
91
91
92
92
AssignedButtonAction::AssignedButtonAction (QObject* parent, const QString action)
93
93
: QObject(parent)
@@ -109,28 +109,28 @@ AssignedButtonAction::AssignedButtonAction(
109
109
{
110
110
}
111
111
112
- void AssignedButtonAction::sendPwm (Vehicle *vehicle, bool buttonDown)
112
+ int16_t AssignedButtonAction::calculatePwm (Vehicle *vehicle, bool buttonDown)
113
113
{
114
114
if (!_isPwmOverrideAction) {
115
115
qCWarning (JoystickLog) << " send called on non-pwm action" ;
116
- return ;
116
+ return - 1 ;
117
117
}
118
118
119
119
uint16_t pwmValue = buttonDown ? _hiPwmValue : _loPwmValue;
120
120
if (_pwmLatchMode) {
121
121
qCDebug (JoystickLog) << " Latch mode, current saved button state " << (_pwmLatchButtonDown ? " down" : " up" );
122
122
123
123
if (buttonDown) {
124
+ // altering latch state
124
125
_pwmLatchButtonDown = !_pwmLatchButtonDown;
125
- pwmValue = _pwmLatchButtonDown ? _hiPwmValue : _loPwmValue;
126
- } else {
127
- qCDebug (JoystickLog) << " since button is up - exiting" ;
128
- return ;
129
126
}
127
+ pwmValue = _pwmLatchButtonDown ? _hiPwmValue : _loPwmValue;
130
128
}
131
129
132
- qCDebug (JoystickLog) << " Sending PWM Value " << pwmValue;
133
- vehicle->rcChannelOverride (_pwmRcChannel, pwmValue);
130
+ qCDebug (JoystickLog) << " Calculated PWM Value " << pwmValue << " for action " << _action;
131
+ // vehicle->rcChannelOverride(_pwmRcChannel, pwmValue);
132
+
133
+ return pwmValue;
134
134
}
135
135
136
136
uint8_t AssignedButtonAction::getRcChannelFromAction (const QString action)
@@ -177,6 +177,7 @@ Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int hatC
177
177
_rgButtonValues[i] = BUTTON_UP;
178
178
_buttonActionArray.append (nullptr );
179
179
}
180
+
180
181
_buildActionList (_multiVehicleManager->activeVehicle ());
181
182
_updateTXModeSettingsKey (_multiVehicleManager->activeVehicle ());
182
183
_loadSettings ();
@@ -277,6 +278,7 @@ void Joystick::_activeVehicleChanged(Vehicle* activeVehicle)
277
278
int mode = settings.value (_txModeSettingsKey, activeVehicle->firmwarePlugin ()->defaultJoystickTXMode ()).toInt ();
278
279
setTXMode (mode);
279
280
}
281
+ _clearRcOverrideButtonActions ();
280
282
}
281
283
282
284
void Joystick::_loadSettings ()
@@ -384,7 +386,7 @@ void Joystick::_loadSettings()
384
386
_buttonActionArray[button]->buttonTime ().start ();
385
387
386
388
bool savedRepeatState = settings.value (QString (_buttonActionRepeatKey).arg (button), false ).toBool ();
387
- setButtonRepeatIfAvailable (button, savedRepeatState);
389
+ _setButtonRepeatIfAvailable (button, savedRepeatState);
388
390
389
391
qCDebug (JoystickLog) << " _loadSettings button:action" << button << _buttonActionArray[button]->action ()
390
392
<< _buttonActionArray[button]->repeat ();
@@ -411,7 +413,6 @@ void Joystick::_saveButtonSettings()
411
413
settings.setValue (QString (_buttonActionLowPwmValueKey).arg (button), _buttonActionArray[button]->lowPwm ());
412
414
settings.setValue (QString (_buttonActionHighPwmValueKey).arg (button), _buttonActionArray[button]->highPwm ());
413
415
settings.setValue (QString (_buttonActionLatchPwmValueKey).arg (button), _buttonActionArray[button]->pwmLatchMode ());
414
-
415
416
}
416
417
}
417
418
}
@@ -567,6 +568,7 @@ void Joystick::run()
567
568
_open ();
568
569
// -- Reset timers
569
570
_axisTime.start ();
571
+ _rcOverrideTimer.start ();
570
572
for (int buttonIndex = 0 ; buttonIndex < _totalButtonCount; buttonIndex++) {
571
573
if (_buttonActionArray[buttonIndex]) {
572
574
_buttonActionArray[buttonIndex]->buttonTime ().start ();
@@ -576,6 +578,7 @@ void Joystick::run()
576
578
_update ();
577
579
_handleButtons ();
578
580
_handleAxis ();
581
+ _handleRcOverride ();
579
582
QGC::SLEEP::msleep (qMin (static_cast <int >(1000 .0f / _maxAxisFrequencyHz), static_cast <int >(1000 .0f / _maxButtonFrequencyHz)) / 2 );
580
583
}
581
584
_close ();
@@ -773,6 +776,26 @@ void Joystick::_handleAxis()
773
776
}
774
777
}
775
778
779
+ void Joystick::_handleRcOverride ()
780
+ {
781
+ if (!_activeVehicle || !_activeVehicle->supportsRcChannelOverride ()) {
782
+ return ;
783
+ }
784
+ const int rcOverrideInterval = static_cast <int >(1000 .0f / _rcOverrideFrequencyHz);
785
+
786
+ if (_rcOverrideActive || _rcOverrideTimer.elapsed () > rcOverrideInterval) {
787
+ _rcOverrideActive = false ;
788
+ _rcOverrideTimer.start ();
789
+ uint16_t overrideData[18 ] = {UINT16_MAX};
790
+ overrideData[16 ] = _mapRcOverrideToRelease (17 , 0 );
791
+ overrideData[17 ] = _mapRcOverrideToRelease (18 , 0 );
792
+ for (int i = 0 ; i < MAX_RC_CHANNELS; i++) {
793
+ overrideData[i] = _rcOverride[i] == -1 ? _mapRcOverrideToRelease (i + 1 , 0 ) : _rcOverride[i];
794
+ }
795
+ _activeVehicle->rcChannelsOverride (overrideData);
796
+ }
797
+ }
798
+
776
799
void Joystick::startPolling (Vehicle* vehicle)
777
800
{
778
801
if (vehicle) {
@@ -905,7 +928,7 @@ void Joystick::setButtonAction(int button, const QString& action)
905
928
if (action.isEmpty () || action == _buttonActionNone) {
906
929
_pwmSettingsVisibilities[button] = false ;
907
930
if (_buttonActionArray[button]) {
908
- removeButtonSettings (button);
931
+ _removeButtonSettings (button);
909
932
_buttonActionArray[button]->deleteLater ();
910
933
_buttonActionArray[button] = nullptr ;
911
934
}
@@ -915,7 +938,7 @@ void Joystick::setButtonAction(int button, const QString& action)
915
938
qCDebug (JoystickLog) << " setButtonAction: isPwmAction " << isPwmAction;
916
939
917
940
if (!_buttonActionArray[button]) {
918
- _buttonActionArray[button] = isPwmAction ? new AssignedButtonAction (this , action, 1000 , 2000 , false )
941
+ _buttonActionArray[button] = isPwmAction ? new AssignedButtonAction (this , action, 1500 , 1500 , false )
919
942
: new AssignedButtonAction (this , action);
920
943
} else {
921
944
if (isPwmAction) {
@@ -933,14 +956,14 @@ void Joystick::setButtonAction(int button, const QString& action)
933
956
}
934
957
935
958
// -- Make sure repeat is off if this action doesn't support repeats
936
- setButtonRepeatIfAvailable (button, false );
937
- saveButtonSettings (button);
959
+ _setButtonRepeatIfAvailable (button, false );
960
+ _saveButtonSettings (button);
938
961
}
939
962
emit pwmVisibilitiesChanged ();
940
963
emit buttonActionsChanged ();
941
964
}
942
965
943
- void Joystick::setButtonRepeatIfAvailable (int button, bool repeat)
966
+ void Joystick::_setButtonRepeatIfAvailable (int button, bool repeat)
944
967
{
945
968
int idx = _findAssignableButtonAction (_buttonActionArray[button]->action ());
946
969
if (idx >= 0 ) {
@@ -953,7 +976,7 @@ void Joystick::setButtonRepeatIfAvailable(int button, bool repeat)
953
976
_buttonActionArray[button]->repeat (false );
954
977
}
955
978
956
- void Joystick::saveButtonSettings (int button)
979
+ void Joystick::_saveButtonSettings (int button)
957
980
{
958
981
QSettings settings;
959
982
settings.beginGroup (_settingsGroup);
@@ -968,7 +991,7 @@ void Joystick::saveButtonSettings(int button)
968
991
}
969
992
}
970
993
971
- void Joystick::removeButtonSettings (int button)
994
+ void Joystick::_removeButtonSettings (int button)
972
995
{
973
996
QSettings settings;
974
997
settings.beginGroup (_settingsGroup);
@@ -1347,10 +1370,10 @@ void Joystick::_buildActionList(Vehicle* activeVehicle)
1347
1370
_assignableButtonActions.append (new AssignableButtonAction (this , _buttonActionGripperGrab));
1348
1371
_assignableButtonActions.append (new AssignableButtonAction (this , _buttonActionGripperRelease));
1349
1372
1350
- // TODO(bzd) take channel nos from config, especially max
1351
- for (int ch = 8 ; ch <= 16 ;ch++) {
1352
- _assignableButtonActions.append (new AssignableButtonAction (this , QString (" Channel %1 direct PWM" ).arg (ch)));
1373
+ for (int ch = 8 ; ch <= MAX_RC_CHANNELS; ch++) {
1374
+ _assignableButtonActions.append (new AssignableButtonAction (this , QString (" Channel %1 direct PWM" ).arg (ch)));
1353
1375
}
1376
+ _clearRcOverrideButtonActions ();
1354
1377
1355
1378
_assignableButtonActions.append (new AssignableButtonAction (this , _buttonActionEmergencyStop));
1356
1379
for (auto & item : _customMavCommands) {
@@ -1367,8 +1390,27 @@ void Joystick::_buildActionList(Vehicle* activeVehicle)
1367
1390
bool Joystick::_executeRcOverrideButtonAction (int buttonIndex, bool buttonDown)
1368
1391
{
1369
1392
if (_buttonActionArray[buttonIndex]) {
1370
- _buttonActionArray[buttonIndex]->sendPwm (_activeVehicle, buttonDown);
1393
+ uint8_t channel = _buttonActionArray[buttonIndex]->rcChannel ();
1394
+ auto pwm = _buttonActionArray[buttonIndex]->calculatePwm (_activeVehicle, buttonDown);
1395
+ _rcOverride[channel-1 ] = pwm;
1396
+ _rcOverrideActive = true ;
1371
1397
return true ;
1372
1398
}
1373
1399
return false ;
1374
1400
}
1401
+
1402
+ void Joystick::_clearRcOverrideButtonActions () {
1403
+ qCDebug (JoystickLog) << " Clearing RC override button actions" ;
1404
+ for (int i = 0 ; i < MAX_RC_CHANNELS; i++) {
1405
+ _rcOverride[i] = -1 ;
1406
+ }
1407
+ }
1408
+
1409
+ uint16_t Joystick::_mapRcOverrideToRelease (uint8_t rcChannel, uint16_t value) {
1410
+ // UINT16_MAX - ignore
1411
+ // 0 Release
1412
+ if (value == 0 ) {
1413
+ return rcChannel < 9 ? 0 : UINT16_MAX - 1 ;
1414
+ }
1415
+ return value;
1416
+ }
0 commit comments