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
[Patch] Add accelerometer and gyroscope APIs #3411
Comments
(can I comment here?) @meyraud705 additional information if you don't know it: on Switch pro controller and joycons, the report rate is 66Hz, but every report contains 3 IMU reports, for a final 200Hz report rate for gyro and accelerometer. In it's current form, GetSensorData returns all three measures concatenated from newest to oldest, as 9 floats, but you need to know about this behaviour to exploit all values instead of just one like with PS4 controller. Native support in SDL should handle this case. |
@Yamakaky They're currently averaged for the single report. Does it make more sense to split them into 3 separate sensor events? |
Sending 3 sensor events lets the user integrate the velocity on every sample. It should give more accurate orientation than integrating the average because rotation are not linear. I would also like to repeat that having an accurate timestamp or the sensor report rate is needed to integrate accurately:
|
How does a186a50 look? |
@slouken I spotted some mistakes:
SendSensorUpdate(joystick, ctx, SDL_SENSOR_GYRO, &packet->imuState[2].sGyroX);
SendSensorUpdate(joystick, ctx, SDL_SENSOR_GYRO, &packet->imuState[1].sGyroX);
SendSensorUpdate(joystick, ctx, SDL_SENSOR_GYRO, &packet->imuState[0].sGyroX); I don't know the correct order, I would have guessed the opposite. But if you know this is the correct order then it's OK.
I do not have Joycon to test, but everything else looks good. Now, we only need support for the Steam controller ;) |
I don't think the axes are accurate since they vary per device : https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/imu_sensor_notes.md. Iirc the pro controller use the same axis as the left joycon. |
@Yamakaky Axes for right Joycon are switched here: |
OK, nice! |
Nice catch, I fixed that in b3a0174
Yeah, the old code used the axes in Y,Z,X order for both gyro and accelerometer and I checked that orientation when I originally wrote it. @Yamakaky mentioned that the data in the report is newest to oldest, and I'm not really sure how to test that, but the new code should deliver the axes as before, in oldest to newest order. However, please feel free to double check my work. :)
Nice catch, I fixed that in 65ff00e
Are we actually getting 250 reports per second from the DS4? I know the USB polling rate should be set to 4 ms, but I haven't confirmed that. |
In my own library at github.com/Yamakaky/joy, I just printed the samples in both orders, and newest first was smoother^^ I can retest it just to be sure. I also think it is 250Hz,i can test that too. |
To test this you could log all values from gyro and do an accelerating movement. Because you are accelerating, the largest value from a packet is the newest. I don't have Joycon, so if someone else could test that. diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index aef17c8de..71f956c4c 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -44,6 +44,9 @@
/* Define this to get log output for rumble logic */
/*#define DEBUG_RUMBLE*/
+/* Define this to get log output for sensor values */
+#define DEBUG_SWITCH_SENSORS
+
/* The initialization sequence doesn't appear to work correctly on Windows unless
the reads and writes are on the same thread.
@@ -1446,6 +1449,12 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
SendSensorUpdate(joystick, ctx, SDL_SENSOR_GYRO, &packet->imuState[2].sGyroX);
SendSensorUpdate(joystick, ctx, SDL_SENSOR_GYRO, &packet->imuState[1].sGyroX);
SendSensorUpdate(joystick, ctx, SDL_SENSOR_GYRO, &packet->imuState[0].sGyroX);
+#ifdef DEBUG_SWITCH_SENSORS
+ SDL_Log("%f %f %f | %f %f %f | %f %f %f\n",
+ HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[2].sGyroX), HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[2].sGyroY), HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[2].sGyroZ),
+ HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[1].sGyroX), HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[1].sGyroY), HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[1].sGyroZ),
+ HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[0].sGyroX), HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[0].sGyroY), HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[0].sGyroZ));
+#endif
SendSensorUpdate(joystick, ctx, SDL_SENSOR_ACCEL, &packet->imuState[2].sAccelX);
SendSensorUpdate(joystick, ctx, SDL_SENSOR_ACCEL, &packet->imuState[1].sAccelX);
Yes, 250 reports per second with USB and Bluetooth, tested on Windows and Linux. |
testgamecontroller prints out sensor values, and the current implementation looks correct. This is a smooth curve as I move the controller: |
Thanks everyone, I think we're good! |
This bug report was migrated from our old Bugzilla tracker.
These attachments are available in the static archive:
Add accelerometer and gyroscope APIs (13105_add_joystick_imu_apis.patch, text/plain, 2019-10-04 23:49:20 +0000, 18762 bytes)Add accelerometer and gyroscope APIs (with correct dynapi entries) (13105_add_joystick_imu_apis_2.patch, text/plain, 2019-10-05 00:15:13 +0000, 20021 bytes)PS4 hidapi implementation (gyro.patch, text/plain, 2020-03-11 18:02:15 +0000, 26347 bytes)PS4 hidapi implementation update1 (gyro.patch, text/plain, 2020-05-04 17:13:34 +0000, 25939 bytes)Reported in version: HG 2.0
Reported for operating system, platform: All, All
Comments on the original bug report:
On 2019-10-04 23:49:20 +0000, Jonathan Newman wrote:
On 2019-10-05 00:15:13 +0000, Jonathan Newman wrote:
On 2019-10-06 12:16:33 +0000, Alex Szpakowski wrote:
On 2019-10-06 12:43:07 +0000, Jonathan Newman wrote:
On 2019-10-15 01:59:09 +0000, Alex Szpakowski wrote:
On 2020-03-11 18:02:15 +0000, Mathieu Eyraud wrote:
On 2020-04-30 19:33:26 +0000, Jonathan Newman wrote:
On 2020-05-04 17:13:34 +0000, Mathieu Eyraud wrote:
On 2020-07-12 15:26:49 +0000, Jonathan Newman wrote:
On 2020-11-12 12:46:03 +0000, Mathieu Eyraud wrote:
On 2020-11-24 05:36:22 +0000, Sam Lantinga wrote:
On 2020-11-24 09:30:00 +0000, Mathieu Eyraud wrote:
The text was updated successfully, but these errors were encountered: