Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement motion sensor support #416

Merged
merged 31 commits into from
Jun 26, 2022
Merged

Implement motion sensor support #416

merged 31 commits into from
Jun 26, 2022

Conversation

mmmaisel
Copy link
Contributor

Begin step-by-step implementation of Playstation 4/5 and Switch Pro controllers motion sensor support in AntimicroX. For example, gyro can be mapped as mouse like a stick for precise aiming and the accelerometer can be used to lean in game by tilting the controller.

Closes #375

@pktiuk

This comment was marked as outdated.

@mmmaisel mmmaisel force-pushed the master branch 5 times, most recently from be7185e to 7437814 Compare April 15, 2022 12:08
src/inputdaemon.cpp Outdated Show resolved Hide resolved
@mmmaisel mmmaisel force-pushed the master branch 5 times, most recently from 6d6cef5 to cc49b8f Compare April 17, 2022 12:56
@pktiuk
Copy link
Member

pktiuk commented Apr 23, 2022

@mmmaisel
This change is pretty big.
I think it would be good to split it to some smaller, independent changes like:

  • preparations - fixes and refactors in code linked with this change (like 37407c4)
  • showing sensors - add showing sensor status/values
  • utilizing sensors - calibrating and mapping sensors to things

It would make this change easier to review and manage.

What do you think?

@mmmaisel
Copy link
Contributor Author

@mmmaisel This change is pretty big. I think it would be good to split it to some smaller, independent changes like:

* preparations - fixes and refactors in code linked with this change (like [37407c4](https://github.com/AntiMicroX/antimicrox/commit/37407c400a548cdf42391fa9bd87879c233fd879))

* showing sensors - add showing sensor status/values

* utilizing sensors - calibrating and mapping sensors to things

It would make this change easier to review and manage.

What do you think?

My plan was to take the finished code and split it again into different commits, so that there are no things to be reviewed that are changed again (like the mentioned 37407c4 which only refactors code from an earlier commit in this pull request) or reverted a few commits later. This should be very similar than your suggestion.

The planned commit outline is:

  • Show number of sensors in status dialog
  • Basic JoySensor class
  • InputDaemon related changes
  • Show raw sensor values in status dialog
  • Add sensors buttons to main window
  • Add sensor edit dialog
  • Add mouse sensor edit dialog
  • Sensor preview widgets
  • Sensor calibration (I plan a separate pull request for some preparations and improvements to this, maybe related to Joystick calibration is broken #407)
  • Generate input events from sensors
  • Context menus
  • Presets
  • XML load/store

@mmmaisel mmmaisel force-pushed the master branch 2 times, most recently from 813086c to aceb319 Compare April 30, 2022 07:15
@mmmaisel mmmaisel mentioned this pull request Apr 30, 2022
1 task
@mmmaisel mmmaisel force-pushed the master branch 4 times, most recently from 1b64a55 to 33a9b54 Compare May 3, 2022 17:16
@pktiuk
Copy link
Member

pktiuk commented May 3, 2022

Could you also add some doxygen descriptions for some of the classes you have worked with? (the ones which you got familiar with and are too big to understand at first glance)
It would make understanding code much easier for future developers. (and it would improve online docs )

Anyway. You don't have to do it if, you don't want to or don't have time.

@mmmaisel mmmaisel force-pushed the master branch 6 times, most recently from 7c79206 to 83feffe Compare May 5, 2022 16:58
mmmaisel added 23 commits June 26, 2022 14:34
If a controller has sensors, add configuration buttons similar to a
control stick to the main view. The layout is based on a isometric 3D view
with the regular XY axes and a diagonal Z axis.

Add a JoySensor button for every direction to the JoySensor object
and connect them to the "flashing" events. Add dummy methods for required
JoySensor interfaces.

Make all the QT connections in JoyTabWidgets as for a JoyControlStick.
But I'm not sure for what checkSensorDisplay and checkSensorEmptyDisplay are
really used.

The new buttons have no function yet. Even though it is possible to map
buttons to the sensor directions, but no events will be generated yet.
Draw sensor position like an artifical horizon instrument.
This should be very intuitive to understand for accelerometer and
not much more difficult for gyroscope. Only draw the diagonal zones for
the XY plane to avoid cluttering.

Add the required interfaces to JoySensor. Use dummies for non-trivial
interfaces.

This widget will be used in the coming sensor edit dialog and
calibration dialog.
They were implemented as dummies in previous commits.
Add missing setters for dead zone, max zone and diagonal range as well
as change events for them. These are required for the coming sensor
settings edit dialog. Add missing sensor delay property.

Initialize sensor settings with default values from global constants
during initialization.
Show this dialog based on JoyControlStickEditDialog when
clicking on the central sensor button in the GUI. It allows editing sensor
parameters with sliders and spin boxes.

This is the first time in the patcheset where sensors can be watch in
action in the GUI. Show raw and processed sensor values with correct units
in indicators.

Presets and sensor preview widget will be implemented in a later commit.
Add group boxes to the common MouseSettingsDialog to hide the parts
that are not needed for sensors. This avoids duplicating the dialog for
sensors.
Use the existing mouse settings dialog as base and hide the parts that
are not needed for sensors. For example, an absolute mouse does not make
any sense for gyroscopes because they are relative sensors. Thus, only
show mouse sensitivity widgets.
Add presets for the existing mouse variants to gyroscope.
Add presets for existing keyboard variants to accelerometer.
Do not add all presets to both because a accelerometer mouse or a
gyroscope keyboard does not make too much sense.
Define the preset values in the caonstructor and not in the .ui file
to realize dynamic entries based on the sensor type.
With this commit, preset selection from the sensor edit dialog works.

Factor out a UI thread helper similar to
joycontrolstickcontextmenuhelper or joycontrolstickeditdialoghelper
which will be used used for both types of UI.
ControlSticks have context menues which allow quick preset selection so
implement them for sensors as well.
Gyroscope calibration is mandatory to get useful information from it.
Apply calibration to all sets similar to control sticks.
Calibration is applied when queueing pending events so that all
following calculations receive corrected values.

Define abstract calibration methods on JoySensor class and implement them for
JoyGyroscopeSensor. Add some stubs for JoyAccelerometerSensor to make it
compile.

Calibration workflow as well as load/store of calibration values follow
in another commit.
Add gyroscope as input device to the existing calibration dialog and
implement a calibration workflow. Only implement offset calibration
because calibrating gyroscope gain requires to apply a defined angular
velocity to all axes, e.g. with an accurate turntable, which is not
feasible for users.
InputDevice and SetJoystick need sensor name interfaces for setting
sensor names for loading XML.
Read/write sensor properties similar to a JoyControlStick.
For smooth mouse movement, JoyButton needs information about the current
desired movement speed.

Do not normalize sensor distances by max zone because this would cause a
change in sensitivity when adjusting max zone. Instead, limit the
values at max zone.
Implement the required delay timer similar to JoyControlStick.
The timer will be used in a following commit.
Add methods to calculate accelerometer and gyroscope directions for
measurement values. These methods take the configured diagonal zone
into account. The used algorithm is described in the code.

The new methods will be used in a following commit.
Perform shock detection through envelope detection with a PT1 filter.
The filter needs the sensor event rate to work correctly.
If no data rate is available assume a default of 5 ms to avoid division by zero.
Using some type of timer does not work because none of SDL ticks, QElapsedTimer
and std::chrono::high_resolution_timer had enough resolution to be non-zero
between two events.

The algorithm used for shock detection is described in the code.
These events and connections are necessary for set change slots.
Propagate events that affect all sets from SetJoystick to InputDevice.
I'm not 100% sure how all of this works, so implement all events from
JoyControlStick for JoySensor as well.
This allows copying sensor assignments from one set to another.
Using multiple similar sets is the way to go to configure a "gyro off"
button.
Process sensor joyEvents into desktopEvents. The emitted desktop events
trigger flashes on the configuration buttons in the GUI and generate
the configured key presses.
This allows users to define a custom neutral controller orientation for
the accelerometer. Implement the stubs in JoyAccelerometerSensor.
Add accelerometer as input device to the existing calibration dialog and
implement a calibration workflow. The calibration simply gets the
desired neutral orientation because offset and gain calibration would
require a fixture which exactly aligns the controller to gravity on all
axes which is not feasible for users.
@pktiuk pktiuk merged commit 8fbe91b into AntiMicroX:master Jun 26, 2022
@pktiuk
Copy link
Member

pktiuk commented Jun 26, 2022

Finally merged 🎉

I will prepare test release soon.

@pktiuk
Copy link
Member

pktiuk commented Jul 5, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request]: Add support for motion sensors
3 participants