@ -23,6 +23,7 @@ const char* Joystick::_calibratedSettingsKey = "Calibrated1"; // Increment
@@ -23,6 +23,7 @@ const char* Joystick::_calibratedSettingsKey = "Calibrated1"; // Increment
const char * Joystick : : _buttonActionSettingsKey = " ButtonActionName%1 " ;
const char * Joystick : : _throttleModeSettingsKey = " ThrottleMode " ;
const char * Joystick : : _exponentialSettingsKey = " Exponential " ;
const char * Joystick : : _accumulatorSettingsKey = " Accumulator " ;
const char * Joystick : : _rgFunctionSettingsKey [ Joystick : : maxFunction ] = {
" RollAxis " ,
@ -46,6 +47,7 @@ Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int hatC
@@ -46,6 +47,7 @@ Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int hatC
, _lastButtonBits ( 0 )
, _throttleMode ( ThrottleModeCenterZero )
, _exponential ( false )
, _accumulator ( false )
, _activeVehicle ( NULL )
, _pollingStartedForCalibration ( false )
, _multiVehicleManager ( multiVehicleManager )
@ -86,6 +88,7 @@ void Joystick::_loadSettings(void)
@@ -86,6 +88,7 @@ void Joystick::_loadSettings(void)
_calibrated = settings . value ( _calibratedSettingsKey , false ) . toBool ( ) ;
_exponential = settings . value ( _exponentialSettingsKey , false ) . toBool ( ) ;
_accumulator = settings . value ( _accumulatorSettingsKey , false ) . toBool ( ) ;
_throttleMode = ( ThrottleMode_t ) settings . value ( _throttleModeSettingsKey , ThrottleModeCenterZero ) . toInt ( & convertOk ) ;
badSettings | = ! convertOk ;
@ -96,6 +99,7 @@ void Joystick::_loadSettings(void)
@@ -96,6 +99,7 @@ void Joystick::_loadSettings(void)
QString maxTpl ( " Axis%1Max " ) ;
QString trimTpl ( " Axis%1Trim " ) ;
QString revTpl ( " Axis%1Rev " ) ;
QString deadbndTpl ( " Axis%1Deadbnd " ) ;
for ( int axis = 0 ; axis < _axisCount ; axis + + ) {
Calibration_t * calibration = & _rgCalibration [ axis ] ;
@ -109,9 +113,13 @@ void Joystick::_loadSettings(void)
@@ -109,9 +113,13 @@ void Joystick::_loadSettings(void)
calibration - > max = settings . value ( maxTpl . arg ( axis ) , 32767 ) . toInt ( & convertOk ) ;
badSettings | = ! convertOk ;
calibration - > deadband = settings . value ( deadbndTpl . arg ( axis ) , 0 ) . toInt ( & convertOk ) ;
badSettings | = ! convertOk ;
calibration - > reversed = settings . value ( revTpl . arg ( axis ) , false ) . toBool ( ) ;
qCDebug ( JoystickLog ) < < " _loadSettings axis:min:max:trim:reversed:badsettings " < < axis < < calibration - > min < < calibration - > max < < calibration - > center < < calibration - > reversed < < badSettings ;
qCDebug ( JoystickLog ) < < " _loadSettings axis:min:max:trim:reversed:badsettings " < < axis < < calibration - > min < < calibration - > max < < calibration - > center < < calibration - > reversed < < calibration - > deadband < < badSettings ;
}
for ( int function = 0 ; function < maxFunction ; function + + ) {
@ -145,6 +153,7 @@ void Joystick::_saveSettings(void)
@@ -145,6 +153,7 @@ void Joystick::_saveSettings(void)
settings . setValue ( _calibratedSettingsKey , _calibrated ) ;
settings . setValue ( _exponentialSettingsKey , _exponential ) ;
settings . setValue ( _accumulatorSettingsKey , _accumulator ) ;
settings . setValue ( _throttleModeSettingsKey , _throttleMode ) ;
qCDebug ( JoystickLog ) < < " _saveSettings calibrated:throttlemode " < < _calibrated < < _throttleMode ;
@ -153,6 +162,7 @@ void Joystick::_saveSettings(void)
@@ -153,6 +162,7 @@ void Joystick::_saveSettings(void)
QString maxTpl ( " Axis%1Max " ) ;
QString trimTpl ( " Axis%1Trim " ) ;
QString revTpl ( " Axis%1Rev " ) ;
QString deadbndTpl ( " Axis%1Deadbnd " ) ;
for ( int axis = 0 ; axis < _axisCount ; axis + + ) {
Calibration_t * calibration = & _rgCalibration [ axis ] ;
@ -161,6 +171,7 @@ void Joystick::_saveSettings(void)
@@ -161,6 +171,7 @@ void Joystick::_saveSettings(void)
settings . setValue ( minTpl . arg ( axis ) , calibration - > min ) ;
settings . setValue ( maxTpl . arg ( axis ) , calibration - > max ) ;
settings . setValue ( revTpl . arg ( axis ) , calibration - > reversed ) ;
settings . setValue ( deadbndTpl . arg ( axis ) , calibration - > deadband ) ;
qCDebug ( JoystickLog ) < < " _saveSettings name:axis:min:max:trim:reversed "
< < _name
@ -168,7 +179,8 @@ void Joystick::_saveSettings(void)
@@ -168,7 +179,8 @@ void Joystick::_saveSettings(void)
< < calibration - > min
< < calibration - > max
< < calibration - > center
< < calibration - > reversed ;
< < calibration - > reversed
< < calibration - > deadband ;
}
for ( int function = 0 ; function < maxFunction ; function + + ) {
@ -189,6 +201,8 @@ float Joystick::_adjustRange(int value, Calibration_t calibration)
@@ -189,6 +201,8 @@ float Joystick::_adjustRange(int value, Calibration_t calibration)
float axisLength ;
float axisBasis ;
if ( value > calibration . center ) {
axisBasis = 1.0f ;
valueNormalized = value - calibration . center ;
@ -199,6 +213,13 @@ float Joystick::_adjustRange(int value, Calibration_t calibration)
@@ -199,6 +213,13 @@ float Joystick::_adjustRange(int value, Calibration_t calibration)
axisLength = calibration . center - calibration . min ;
}
if ( _accumulator ) { //deadband will be applied only in accumulator mode
int deadband = calibration . deadband * 1.5 ; //we are increasing deadband to accommodate slight variations
if ( valueNormalized > deadband ) valueNormalized - = deadband ;
else if ( valueNormalized < - deadband ) valueNormalized + = deadband ;
else valueNormalized = 0.f ;
}
float axisPercent = valueNormalized / axisLength ;
float correctedValue = axisBasis * axisPercent ;
@ -214,7 +235,7 @@ float Joystick::_adjustRange(int value, Calibration_t calibration)
@@ -214,7 +235,7 @@ float Joystick::_adjustRange(int value, Calibration_t calibration)
< < calibration . min
< < calibration . max
< < calibration . center
< < calibration . center
< < calibration . deadband
< < axisBasis
< < valueNormalized
< < axisLength ;
@ -276,6 +297,15 @@ void Joystick::run(void)
@@ -276,6 +297,15 @@ void Joystick::run(void)
axis = _rgFunctionAxis [ throttleFunction ] ;
float throttle = _adjustRange ( _rgAxisValues [ axis ] , _rgCalibration [ axis ] ) ;
if ( _accumulator ) {
static float throttle_accu = 0.f ;
throttle_accu + = throttle * ( 40 / 1000.f ) ; //for throttle to change from min to max it will take 1000ms (40ms is a loop time)
throttle_accu = std : : max ( static_cast < float > ( - 1.f ) , std : : min ( throttle_accu , static_cast < float > ( 1.f ) ) ) ;
throttle = throttle_accu ;
}
float roll_limited = std : : max ( static_cast < float > ( - M_PI_4 ) , std : : min ( roll , static_cast < float > ( M_PI_4 ) ) ) ;
float pitch_limited = std : : max ( static_cast < float > ( - M_PI_4 ) , std : : min ( pitch , static_cast < float > ( M_PI_4 ) ) ) ;
float yaw_limited = std : : max ( static_cast < float > ( - M_PI_4 ) , std : : min ( yaw , static_cast < float > ( M_PI_4 ) ) ) ;
@ -529,6 +559,19 @@ void Joystick::setExponential(bool expo)
@@ -529,6 +559,19 @@ void Joystick::setExponential(bool expo)
emit exponentialChanged ( _exponential ) ;
}
bool Joystick : : accumulator ( void )
{
return _accumulator ;
}
void Joystick : : setAccumulator ( bool accu )
{
_accumulator = accu ;
_saveSettings ( ) ;
emit accumulatorChanged ( _accumulator ) ;
}
void Joystick : : startCalibrationMode ( CalibrationMode_t mode )
{
if ( mode = = CalibrationModeOff ) {