AntiMicroX
calibration.h
Go to the documentation of this file.
1 /* antimicrox Gamepad to KB+M event mapper
2  * Copyright (C) 2020 Jagoda Górska <juliagoda.pl@protonmail.com>
3  * Copyright (C) 2022 Max Maisel <max.maisel@posteo.de>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14 
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 #pragma once
19 
20 #include "pt1filter.h"
21 #include "statisticsestimator.h"
22 
23 #include <QDateTime>
24 #include <QDialog>
25 #include <QElapsedTimer>
26 
27 class JoyControlStick;
28 class JoySensor;
29 class InputDevice;
30 
31 namespace Ui {
32 class Calibration;
33 }
34 
40 class Calibration : public QDialog
41 {
42  Q_OBJECT
43 
44  public:
46  {
51 
52  CAL_TYPE_MASK = 0x0000FFFF,
53  CAL_INDEX_MASK = 0xFFFF0000,
55  };
56 
57  explicit Calibration(InputDevice *joystick, QDialog *parent = 0);
58  ~Calibration();
59 
60  protected:
62  bool askConfirmation(QString message, bool confirmed);
63  void showSensorCalibrationValues(bool offsetXvalid, double offsetX, bool offsetYvalid, double offsetY, bool offsetZvalid,
64  double offsetZ);
65  void showStickCalibrationValues(bool offsetXvalid, double offsetX, bool gainXvalid, double gainX, bool offsetYvalid,
66  double offsetY, bool gainYvalid, double gainY);
67  void hideCalibrationData();
68  void selectTypeIndex(unsigned int type_index);
69  static void stickRegression(double *offset, double *gain, double xoffset, double xmin, double xmax);
70 
71  private:
72  Ui::Calibration *m_ui;
74  unsigned int m_index;
76  bool m_changed;
80 
85  double m_last_slope[2];
86  QDateTime m_end_time;
87  QElapsedTimer m_rate_timer;
89  int m_phase;
90 
91  static const int CAL_MIN_SAMPLES;
92  static const double CAL_ACCURACY_SQ;
93  static const double STICK_CAL_TAU;
94  static const int STICK_RATE_SAMPLES;
95  static const int CAL_TIMEOUT;
96 
97  public slots:
98  void saveSettings();
105 
106  protected slots:
107  void closeEvent(QCloseEvent *event) override;
108  void resetSettings();
109  void deviceSelectionChanged(int index);
110  void onSensorOffsetData(float x, float y, float z);
111  void onStickOffsetData(int x, int y);
112  void onStickGainData(int x, int y);
113 
114  signals:
115  void propertyUpdated();
116 };
StatisticsEstimator::process
void process(double x)
Processes a new sample of the current data stream and updates internal intermediate values.
Definition: statisticsestimator.cpp:36
Calibration::CAL_STICK
@ CAL_STICK
Definition: calibration.h:48
pt1filter.h
JoySensor::moved
void moved(float xaxis, float yaxis, float zaxis)
Calibration::CAL_INDEX_POS
@ CAL_INDEX_POS
Definition: calibration.h:54
Calibration::startStickOffsetCalibration
void startStickOffsetCalibration()
Shows user instructions for stick offset calibration, initializes estimators and connects stick data ...
Definition: calibration.cpp:717
joycontrolstick.h
SetJoystick::getSensor
JoySensor * getSensor(JoySensorType type) const
Definition: setjoystick.cpp:71
Calibration::~Calibration
~Calibration()
Definition: calibration.cpp:98
StatisticsEstimator::getCount
size_t getCount() const
Gets the amount of processed samples.
Definition: statisticsestimator.h:37
Calibration::CAL_GYROSCOPE
@ CAL_GYROSCOPE
Definition: calibration.h:50
JoyControlStick::getCalibration
void getCalibration(double *offsetX, double *gainX, double *offsetY, double *gainY) const
Reads the calibration values of both stick axes.
Definition: joycontrolstick.cpp:946
Calibration
Opens calibration window used for calibration.
Definition: calibration.h:40
JoyControlStick::getDeadZone
int getDeadZone()
Get the assigned dead zone value.
Definition: joycontrolstick.cpp:192
JoySensor::isCalibrated
bool isCalibrated() const
Check if the sensor is calibrated.
Definition: joysensor.cpp:530
Calibration::Calibration
Calibration(InputDevice *joystick, QDialog *parent=0)
Definition: calibration.cpp:38
Calibration::m_type
CalibrationType m_type
Definition: calibration.h:73
Calibration::showSensorCalibrationValues
void showSensorCalibrationValues(bool offsetXvalid, double offsetX, bool offsetYvalid, double offsetY, bool offsetZvalid, double offsetZ)
Shows the sensor offset calibration values to the user.
Definition: calibration.cpp:146
globalvariables.h
Calibration::m_rate_timer
QElapsedTimer m_rate_timer
Definition: calibration.h:87
joysensor.h
Calibration::stickRegression
static void stickRegression(double *offset, double *gain, double xoffset, double xmin, double xmax)
Performs linear regression on the measurement values of one axis to determine offset and gain.
Definition: calibration.cpp:334
Calibration::onSensorOffsetData
void onSensorOffsetData(float x, float y, float z)
Sensor data event handler. Performs gyroscope offset and accelerometer neutral orientation estimation...
Definition: calibration.cpp:432
Calibration::m_stick
JoyControlStick * m_stick
Definition: calibration.h:77
Calibration::m_phase
int m_phase
Definition: calibration.h:89
InputDevice
Abstract class representing a hardware input device, e.g a joystick or controller.
Definition: inputdevice.h:50
Calibration::CAL_ACCELEROMETER
@ CAL_ACCELEROMETER
Definition: calibration.h:49
statisticsestimator.h
Calibration::startGyroscopeOffsetCalibration
void startGyroscopeOffsetCalibration()
Show calibration message to the user and enable gyroscope data event handler.
Definition: calibration.cpp:699
Calibration::CAL_TIMEOUT
static const int CAL_TIMEOUT
Definition: calibration.h:95
Calibration::CalibrationType
CalibrationType
Definition: calibration.h:45
Calibration::askConfirmation
bool askConfirmation(QString message, bool confirmed)
Asks the user for confirmation with a given message if the given condition is false.
Definition: calibration.cpp:377
Calibration::selectTypeIndex
void selectTypeIndex(unsigned int type_index)
Prepares calibration for the selected device type. Show all used values and labels and connect button...
Definition: calibration.cpp:221
StatisticsEstimator
Estimates mean of a data stream using Welford's algorithm and calculates statistic properties.
Definition: statisticsestimator.h:25
PT1Filter::process
double process(double value)
Processes a new sample.
Definition: pt1filter.cpp:36
JoyControlStick::moved
void moved(int xaxis, int yaxis)
Calibration::startAccelerometerAngleCalibration
void startAccelerometerAngleCalibration()
Show calibration message to the user and enable accelerometer data event handler.
Definition: calibration.cpp:650
Calibration::startAccelerometerCalibration
void startAccelerometerCalibration()
Shows user instructions for accelerometer calibration and initializes estimators.
Definition: calibration.cpp:622
Calibration::resetSettings
void resetSettings()
Asks for confirmation and resets calibration values of the selected device afterwards.
Definition: calibration.cpp:122
Calibration::startStickGainCalibration
void startStickGainCalibration()
Shows user instructions for stick gain calibration, initializes estimators and connects stick data ev...
Definition: calibration.cpp:754
Calibration::STICK_CAL_TAU
static const double STICK_CAL_TAU
Definition: calibration.h:93
Calibration::CAL_INDEX_MASK
@ CAL_INDEX_MASK
Definition: calibration.h:53
Calibration::deviceSelectionChanged
void deviceSelectionChanged(int index)
Device change event handler. Asks for confirmation if there are unsaved changes.
Definition: calibration.cpp:403
SetJoystick::hasSensor
bool hasSensor(JoySensorType type) const
Checks if this set has a sensor.
Definition: setjoystick.cpp:253
PT1Filter::reset
void reset()
Resets the filter state to default.
Definition: pt1filter.cpp:45
JoySensor::radToDeg
static double radToDeg(double value)
Utility function which converts a given value from radians to degree.
Definition: joysensor.cpp:519
Ui
Definition: aboutdialog.h:26
Calibration::m_filter
PT1Filter m_filter[2]
Definition: calibration.h:84
Calibration::m_index
unsigned int m_index
Definition: calibration.h:74
Calibration::m_last_slope
double m_last_slope[2]
Definition: calibration.h:85
JoyControlStick::isCalibrated
bool isCalibrated() const
Check if the stick is calibrated.
Definition: joycontrolstick.cpp:928
Calibration::m_changed
bool m_changed
Definition: calibration.h:76
Calibration::resetCalibrationValues
void resetCalibrationValues()
Resets calibration values of the currently selected device and updates UI.
Definition: calibration.cpp:351
Calibration::m_end_time
QDateTime m_end_time
Definition: calibration.h:86
Calibration::saveSettings
void saveSettings()
Save calibration values into the device object.
Definition: calibration.cpp:596
Calibration::m_joystick
InputDevice * m_joystick
Definition: calibration.h:79
JoySensor::resetCalibration
void resetCalibration()
Resets the calibration of the sensor back to uncalibrated state.
Definition: joysensor.cpp:535
Calibration::CAL_ACCURACY_SQ
static const double CAL_ACCURACY_SQ
Definition: calibration.h:92
JoySensor::getCalibration
virtual void getCalibration(double *offsetX, double *offsetY, double *offsetZ) const =0
inputdevice.h
InputDevice::updateAccelerometerCalibration
void updateAccelerometerCalibration(double offsetX, double offsetY, double offsetZ)
Applies calibration to the specified accelerometer in all sets See JoySensor::setCalibration.
Definition: inputdevice.cpp:1782
GlobalVariables::JoyAxis::AXISMIN
static const int AXISMIN
Definition: globalvariables.h:117
GlobalVariables::JoyAxis::AXISMAX
static const int AXISMAX
Definition: globalvariables.h:118
Calibration::startGyroscopeCalibration
void startGyroscopeCalibration()
Shows user instructions for gyroscope calibration and initializes estimators.
Definition: calibration.cpp:669
JoyControlStick
Represents stick of a joystick.
Definition: joycontrolstick.h:37
Calibration::CAL_MIN_SAMPLES
static const int CAL_MIN_SAMPLES
Definition: calibration.h:91
Calibration::hideCalibrationData
void hideCalibrationData()
hides all calibration values and their labels.
Definition: calibration.cpp:196
Calibration::showStickCalibrationValues
void showStickCalibrationValues(bool offsetXvalid, double offsetX, bool gainXvalid, double gainX, bool offsetYvalid, double offsetY, bool gainYvalid, double gainY)
Shows the stick offset and gain calibration values to the user.
Definition: calibration.cpp:174
Calibration::onStickOffsetData
void onStickOffsetData(int x, int y)
Stick data event handler. Performs stick offset estimation and stops itself if the value was found or...
Definition: calibration.cpp:474
Calibration::m_ui
Ui::Calibration * m_ui
Definition: calibration.h:72
Calibration::m_offset
StatisticsEstimator m_offset[3]
Definition: calibration.h:81
JoySensor
Represents one sensor in a SetJoystick and its connections to other parts of the application....
Definition: joysensor.h:39
StatisticsEstimator::calculateRelativeErrorSq
double calculateRelativeErrorSq() const
Calculates the squared relative three sigma error range, i.e. the squared estimation accuracy in perc...
Definition: statisticsestimator.cpp:58
Calibration::CAL_TYPE_MASK
@ CAL_TYPE_MASK
Definition: calibration.h:52
PT1Filter
Implementation of a first order lag or PT1 filter. Can be used for example to smooth noisy values a b...
Definition: pt1filter.h:25
InputDevice::updateGyroscopeCalibration
void updateGyroscopeCalibration(double offsetX, double offsetY, double offsetZ)
Updates stored calibration for this controller and applies calibration to the specified gyroscope in ...
Definition: inputdevice.cpp:1813
PT1Filter::getValue
double getValue() const
Get the current filter output value.
Definition: pt1filter.h:35
Calibration::m_max
StatisticsEstimator m_max[2]
Definition: calibration.h:83
StatisticsEstimator::reset
void reset()
Resets the StatisticsEstimator so that it is ready to process a new data stream.
Definition: statisticsestimator.cpp:25
GYROSCOPE
@ GYROSCOPE
Definition: joysensortype.h:26
SetJoystick::getSticks
const QHash< int, JoyControlStick * > & getSticks() const
Definition: setjoystick.cpp:1107
Calibration::m_sample_count
int m_sample_count
Definition: calibration.h:88
Calibration::closeEvent
void closeEvent(QCloseEvent *event) override
Ask for confirmation when the dialog is closed with unsafed changed.
Definition: calibration.cpp:103
JoyControlStick::resetCalibration
void resetCalibration()
Resets the calibration of both stick axes back to uncalibrated state.
Definition: joycontrolstick.cpp:933
Calibration::m_min
StatisticsEstimator m_min[2]
Definition: calibration.h:82
number
it is up to the author donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License If the distribution and or use of the Library is restricted in certain countries either by patents or by copyrighted the original copyright holder who places the Library under this License may add an geographical distribution limitation excluding those so that distribution is permitted only in or among countries not thus excluded In such this License incorporates the limitation as if written in the body of this License The Free Software Foundation may publish revised and or new versions of the Lesser General Public License from time to time Such new versions will be similar in spirit to the present but may differ in detail to address new problems or concerns Each version is given a distinguishing version number If the Library specifies a version number of this License which applies to it and any later you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation If the Library does not specify a license version number
Definition: lgpl-2.1.txt:423
calibration.h
Calibration::m_calibrated
bool m_calibrated
Definition: calibration.h:75
Calibration::STICK_RATE_SAMPLES
static const int STICK_RATE_SAMPLES
Definition: calibration.h:94
Calibration::m_sensor
JoySensor * m_sensor
Definition: calibration.h:78
Calibration::CAL_NONE
@ CAL_NONE
Definition: calibration.h:47
Calibration::propertyUpdated
void propertyUpdated()
Calibration::onStickGainData
void onStickGainData(int x, int y)
Stick data event handler. Performs stick gain estimation and stops itself if the value was found or t...
Definition: calibration.cpp:539
ACCELEROMETER
@ ACCELEROMETER
Definition: joysensortype.h:25
InputDevice::getActiveSetJoystick
SetJoystick * getActiveSetJoystick()
Definition: inputdevice.cpp:563
InputDevice::updateStickCalibration
void updateStickCalibration(int index, double offsetX, double gainX, double offsetY, double gainY)
Updates stored calibration for this controller and applies calibration to the specified stick in all ...
Definition: inputdevice.cpp:1750