Skip to content

Commit

Permalink
Imporments: motion timestamp and sensitivity scaling by @the-maazu
Browse files Browse the repository at this point in the history
Motion Timestamp and Sensitivity Scaling
  • Loading branch information
r57zone authored Aug 31, 2022
2 parents ed3db0d + d144207 commit f9fdf1f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ You can use swipes for the keyboard, the button codes are described below.
## Gyroscope
1. Check Windows Firewall to see if incoming connections are allowed on your network type (private) and allow if disabled.
2. Install FreePieIMU on your Android phone by taking the latest version in the [OpenTrack archive](https://github.com/opentrack/opentrack) or in the [releases](https://github.com/r57zone/DualShock4-emulator/releases), enter the IP address of your computer, select "Send raw data", if not selected, select the data rate "Fastest" or "Fast".
3. Reduce the sensitivity if necessary (the `Sens` parameter, in the `Motion` section, where `100` is 100% sensitivity) in configuration file.
4. Invert the axes if necessary (the parameters `InverseX`, `InverseY` and `InverseZ`, in the `Motion` section, where `1` is turning on the inversion, and `0` is turning off).
5. Change phone orientation (the parameter `Orientation`, in the `Motion` section. where `1` is landscape and `0` is portrait).
3. Reduce the general sensitivity if necessary (the `Sens` parameter, in the `Motion` section, where `100` is 100% sensitivity) in configuration file.
4. Reduce individual sensor sensitivity if necessary (the `AccelSens` and `GyroSense`, in the `Motion` section, where `100` is 100% sensitivity) in configuration file.
5. Invert the axes if necessary (the parameters `InverseX`, `InverseY` and `InverseZ`, in the `Motion` section, where `1` is turning on the inversion, and `0` is turning off).
6. Change phone orientation (the parameter `Orientation`, in the `Motion` section. where `1` is landscape and `0` is portrait).

If you just need to shake (gyro) the gamepad in the game, then there is no need to install Android applications, just press the "shake" button of the gamepad.

Expand Down
24 changes: 15 additions & 9 deletions Source/DS4Emulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool SocketActivated = false;
std::thread *pSocketThread = NULL;
unsigned char freePieIMU[50];
float AccelX = 0, AccelY = 0, AccelZ = 0, GyroX = 0, GyroY = 0, GyroZ = 0;
int curTimeStamp;
USHORT curTimeStamp = 0;

float bytesToFloat(unsigned char b3, unsigned char b2, unsigned char b1, unsigned char b0)
{
Expand Down Expand Up @@ -172,7 +172,11 @@ int main(int argc, char **argv)
SocketActivated = false;
}
}
float MotionSens = IniFile.ReadFloat("Motion", "Sens", 100) * 0.01;
int MotionSens = IniFile.ReadInteger("Motion", "Sens", 100); // sampling(timestamp) speed of motion sensor
// min & max programmable ds4 gyro +/-125 degrees/s = 2 rad/s to 2000 degrees/s = 35 rad/s
float GyroSens = 35 - IniFile.ReadInteger("Motion", "GyroSens", 100)/100 * 33; // 33 = max - min
// min & max programmable ds4 accel +/-2g = 20 m/s^2 to 16g = 160 m/s^2
float AccelSens = 160 - IniFile.ReadInteger("Motion", "AccelSens", 100)/100 * 140; // 140 = max - min
int InverseXStatus = IniFile.ReadBoolean("Motion", "InverseX", false) == false ? 1 : -1 ;
int InverseYStatus = IniFile.ReadBoolean("Motion", "InverseY", false) == false ? 1 : -1 ;
int InverseZStatus = IniFile.ReadBoolean("Motion", "InverseZ", false) == false ? 1 : -1 ;
Expand Down Expand Up @@ -699,12 +703,14 @@ int main(int argc, char **argv)

report.sCurrentTouch.bPacketCounter = TouchIndex;

report.wAccelX = trunc( Clamp(AccelX * 1638.35 * 1.0479 / 2, -32767, 32767) ) * InverseXStatus * MotionSens; // freepie accel max 19.61, min -20, short -32,768 to 32,767
report.wAccelY = trunc( Clamp(AccelY * 1638.35 * 1.0479 / 2, -32767, 32767) ) * InverseYStatus * MotionSens;
report.wAccelZ = trunc( Clamp(AccelZ * 1638.35 * 1.0479 / 2, -32767, 32767) ) * InverseZStatus * MotionSens;
report.wGyroX = trunc( Clamp(GyroX * 2376.7 * 0.8 / 2, -32767, 32767) ) * InverseXStatus * MotionSens; // freepie max gyro 10, min -10.09
report.wGyroY = trunc( Clamp(GyroY * 2376.7 * 0.8 / 2, -32767, 32767) ) * InverseYStatus * MotionSens;
report.wGyroZ = trunc( Clamp(GyroZ * 2376.7 * 0.8 / 2, -32767, 32767) ) * InverseZStatus * MotionSens; // if ((GetAsyncKeyState(VK_NUMPAD1) & 0x8000) != 0) printf("%d\t%d\t%d\t%d\t%d\t%d\t\n", report.wAccelX, report.wAccelY, report.wAccelZ, report.wGyroX, report.wGyroY, report.wGyroZ);
// freePIE gyro and accel in rad/s and m/s^2, need to scale with accel and gyro configs
report.wAccelX = trunc(Clamp(AccelX / AccelSens * 32767, -32767, 32767)) * InverseXStatus;
report.wAccelY = trunc(Clamp(AccelY / AccelSens * 32767, -32767, 32767)) * InverseYStatus;
report.wAccelZ = trunc(Clamp(AccelZ / AccelSens * 32767, -32767, 32767)) * InverseZStatus;
report.wGyroX = trunc(Clamp(GyroX / GyroSens * 32767, -32767, 32767)) * InverseXStatus;
report.wGyroY = trunc(Clamp(GyroY / GyroSens * 32767, -32767, 32767)) * InverseYStatus;
report.wGyroZ = trunc(Clamp(GyroZ / GyroSens * 32767, -32767, 32767)) * InverseZStatus;
// if ((GetAsyncKeyState(VK_NUMPAD1) & 0x8000) != 0) printf("%d\t%d\t%d\t%d\t%d\t%d\t\n", report.wAccelX, report.wAccelY, report.wAccelZ, report.wGyroX, report.wGyroY, report.wGyroZ);

// Motion shaking
if (MotionShaking) {
Expand All @@ -724,7 +730,7 @@ int main(int argc, char **argv)

// if ((GetAsyncKeyState(VK_NUMPAD0) & 0x8000) != 0) system("cls");

curTimeStamp++; if (curTimeStamp > 65535) curTimeStamp = 0; // Is it right? / Правильно ли это?
curTimeStamp = curTimeStamp + MotionSens * 100;
report.wTimestamp = curTimeStamp;

ret = vigem_target_ds4_update_ex(client, ds4, report);
Expand Down

0 comments on commit f9fdf1f

Please sign in to comment.