Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #7 from Chaoseiro/master
Browse files Browse the repository at this point in the history
Adjusts for drifting & effect multiplier
  • Loading branch information
cosmii02 committed Sep 3, 2022
2 parents 4b5b6e4 + fdaf841 commit af40cbb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 32 deletions.
45 changes: 29 additions & 16 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace ForzaDSX
{
class Program
{
public const String VERSION = "0.3.1";
public const String VERSION = "0.4.1";
static Settings settings = new Settings();
static bool verbose = false;
static bool logToCsv = false;
Expand Down Expand Up @@ -109,8 +109,10 @@ static void SendData(DataPacket data, CsvWriter csv)

//Get average tire slippage. This value runs from 0.0 upwards with a value of 1.0 or greater meaning total loss of grip.
float combinedTireSlip = (Math.Abs(data.TireCombinedSlipFrontLeft) + Math.Abs(data.TireCombinedSlipFrontRight) + Math.Abs(data.TireCombinedSlipRearLeft) + Math.Abs(data.TireCombinedSlipRearRight)) / 4;
float combinedFrontTireSlip = (Math.Abs(data.TireCombinedSlipFrontLeft) + Math.Abs(data.TireCombinedSlipFrontRight)) / 2;
float combinedRearTireSlip = (Math.Abs(data.TireCombinedSlipRearLeft) + Math.Abs(data.TireCombinedSlipRearRight)) / 2;

uint currentClass = LastValidCarClass;
uint currentClass = LastValidCarClass;
if (data.CarClass > 0)
{
LastValidCarClass = currentClass = data.CarClass;
Expand Down Expand Up @@ -215,19 +217,25 @@ static void SendData(DataPacket data, CsvWriter csv)

avgAccel = (float)Math.Sqrt((settings.TURN_ACCEL_MOD * (data.AccelerationX * data.AccelerationX)) + (settings.FORWARD_ACCEL_MOD * (data.AccelerationZ * data.AccelerationZ)));

// Define losing grip as front tires slipping or rear tires slipping while accelerating a fair ammount
bool bLosingAccelGrip =
combinedFrontTireSlip > settings.THROTTLE_GRIP_LOSS_VAL
|| (combinedRearTireSlip > settings.THROTTLE_GRIP_LOSS_VAL && data.Accelerator > 200);

// If losing grip, start to "vibrate"
if (combinedTireSlip > settings.THROTTLE_GRIP_LOSS_VAL)
if (bLosingAccelGrip)
{
freq = (int)Math.Floor(Map(combinedTireSlip, settings.THROTTLE_GRIP_LOSS_VAL, 5, 0, settings.MAX_ACCEL_GRIPLOSS_VIBRATION));
resistance = (int)Math.Floor(Map(avgAccel, 0, settings.ACCELRATION_LIMIT, settings.MIN_ACCEL_GRIPLOSS_STIFFNESS, settings.MAX_ACCEL_GRIPLOSS_STIFFNESS));
//resistance = settings.MIN_ACCEL_GRIPLOSS_STIFFNESS - (int)Math.Floor(Map(data.Accelerator, 0, 255, settings.MIN_ACCEL_GRIPLOSS_STIFFNESS, settings.MAX_ACCEL_GRIPLOSS_STIFFNESS));
filteredResistance = EWMA(resistance, lastThrottleResistance, settings.EWMA_ALPHA_THROTTLE);
filteredFreq = EWMA(freq, lastThrottleFreq, settings.EWMA_ALPHA_THROTTLE_FREQ);
filteredResistance = (int)Math.Floor(EWMA(resistance, lastThrottleResistance, settings.EWMA_ALPHA_THROTTLE) * settings.RIGHT_TRIGGER_EFFECT_INTENSITY);
filteredFreq = (int)Math.Floor(EWMA(freq, lastThrottleFreq, settings.EWMA_ALPHA_THROTTLE_FREQ) * settings.RIGHT_TRIGGER_EFFECT_INTENSITY);

lastThrottleResistance = filteredResistance;
lastThrottleFreq = filteredFreq;

if (filteredFreq <= settings.MIN_ACCEL_GRIPLOSS_VIBRATION)
if (filteredFreq <= settings.MIN_ACCEL_GRIPLOSS_VIBRATION
|| data.Accelerator <= settings.THROTTLE_VIBRATION_MODE_START)
{
p.instructions[2].parameters = new object[] { controllerIndex, Trigger.Right, TriggerMode.Resistance, 0, filteredResistance };

Expand All @@ -247,7 +255,7 @@ static void SendData(DataPacket data, CsvWriter csv)
{
//It should probably always be uniformly stiff
resistance = (int)Math.Floor(Map(avgAccel, 0, settings.ACCELRATION_LIMIT, settings.MIN_THROTTLE_RESISTANCE, settings.MAX_THROTTLE_RESISTANCE));
filteredResistance = EWMA(resistance, lastThrottleResistance, settings.EWMA_ALPHA_THROTTLE);
filteredResistance = (int)Math.Floor(EWMA(resistance, lastThrottleResistance, settings.EWMA_ALPHA_THROTTLE) * settings.RIGHT_TRIGGER_EFFECT_INTENSITY);

lastThrottleResistance = filteredResistance;
p.instructions[2].parameters = new object[] { controllerIndex, Trigger.Right, TriggerMode.Resistance, 0, filteredResistance };
Expand All @@ -262,7 +270,7 @@ static void SendData(DataPacket data, CsvWriter csv)

if (verbose)
{
Console.WriteLine($"Average Acceleration: {avgAccel}; Throttle Resistance: {filteredResistance}");
Console.WriteLine($"Average Acceleration: {avgAccel}; Throttle Resistance: {filteredResistance}; Accelerator: {data.Accelerator}");
}
#endregion

Expand All @@ -273,8 +281,8 @@ static void SendData(DataPacket data, CsvWriter csv)
{
freq = settings.MAX_BRAKE_VIBRATION - (int)Math.Floor(Map(combinedTireSlip, settings.GRIP_LOSS_VAL, 1, 0, settings.MAX_BRAKE_VIBRATION));
resistance = settings.MIN_BRAKE_STIFFNESS - (int)Math.Floor(Map(data.Brake, 0, 255, settings.MAX_BRAKE_STIFFNESS, settings.MIN_BRAKE_STIFFNESS));
filteredResistance = EWMA(resistance, lastBrakeResistance, settings.EWMA_ALPHA_BRAKE);
filteredFreq = EWMA(freq, lastBrakeFreq, settings.EWMA_ALPHA_BRAKE_FREQ);
filteredResistance = (int)Math.Floor(EWMA(resistance, lastBrakeResistance, settings.EWMA_ALPHA_BRAKE) * settings.LEFT_TRIGGER_EFFECT_INTENSITY);
filteredFreq = (int)Math.Floor(EWMA(freq, lastBrakeFreq, settings.EWMA_ALPHA_BRAKE_FREQ) * settings.LEFT_TRIGGER_EFFECT_INTENSITY);
lastBrakeFreq = filteredFreq;
lastBrakeResistance = filteredResistance;
if (filteredFreq <= settings.MIN_BRAKE_VIBRATION)
Expand All @@ -298,7 +306,7 @@ static void SendData(DataPacket data, CsvWriter csv)
{
//By default, Increasingly resistant to force
resistance = (int)Math.Floor(Map(data.Brake, 0, 255, settings.MIN_BRAKE_RESISTANCE, settings.MAX_BRAKE_RESISTANCE));
filteredResistance = EWMA(resistance, lastBrakeResistance, settings.EWMA_ALPHA_BRAKE);
filteredResistance = (int)Math.Floor(EWMA(resistance, lastBrakeResistance, settings.EWMA_ALPHA_BRAKE) * settings.LEFT_TRIGGER_EFFECT_INTENSITY);
lastBrakeResistance = filteredResistance;
p.instructions[0].parameters = new object[] { controllerIndex, Trigger.Left, TriggerMode.Resistance, 0, filteredResistance };
}
Expand All @@ -320,8 +328,9 @@ static void SendData(DataPacket data, CsvWriter csv)
#region Light Bar
//Update the light bar
//Currently registers intensity on the green channel based on engine RPM as a percantage of the maxium. Changes to red if RPM ratio > 80% (usually red line)
float CurrentRPMRatio = currentRPM / data.EngineMaxRpm;
int GreenChannel = (int)Math.Floor(CurrentRPMRatio * 255);
float engineRange = data.EngineMaxRpm - data.EngineIdleRpm;
float CurrentRPMRatio = (currentRPM - data.EngineIdleRpm) / engineRange;
int GreenChannel = Math.Max((int)Math.Floor(CurrentRPMRatio * 255), 50);
int RedChannel = (int)Math.Floor(CurrentRPMRatio * 255);
if (CurrentRPMRatio >= settings.RPM_REDLINE_RATIO)
{
Expand All @@ -333,7 +342,7 @@ static void SendData(DataPacket data, CsvWriter csv)

if (verbose)
{
Console.WriteLine($"Engine RPM: {data.CurrentEngineRpm}");
Console.WriteLine($"Engine RPM: {data.CurrentEngineRpm}; Engine Max RPM: {data.EngineMaxRpm}; Engine Idle RPM: {data.EngineIdleRpm}");
}
#endregion
}
Expand Down Expand Up @@ -666,8 +675,12 @@ static async Task Main(string[] args)
}
Console.WriteLine("Forza and DSX are running. Let's Go!");
}
//Connect to DualSenseX
Connect();

settings.LEFT_TRIGGER_EFFECT_INTENSITY = Math.Clamp(settings.LEFT_TRIGGER_EFFECT_INTENSITY, 0.0f, 1.0f);
settings.RIGHT_TRIGGER_EFFECT_INTENSITY = Math.Clamp(settings.RIGHT_TRIGGER_EFFECT_INTENSITY, 0.0f, 1.0f);

//Connect to DualSenseX
Connect();

//Connect to Forza
ipEndPoint = new IPEndPoint(IPAddress.Loopback, settings.FORZA_PORT);
Expand Down
11 changes: 7 additions & 4 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ public class Settings
public float EWMA_ALPHA_THROTTLE { get; set; } = 0.01f; //Smoothing for Throttle Resistance output. Lower = smoother. Must be greater than 0
public float EWMA_ALPHA_BRAKE { get; set; } = 1.0f; //Smoothing for Brake Resistance output. Lower = smoother. Must be greater than 0
public float EWMA_ALPHA_BRAKE_FREQ { get; set; } = 1.0f; //Smoothing for Brake Resistance output. Lower = smoother. Must be greater than 0
public float EWMA_ALPHA_THROTTLE_FREQ { get; set; } = 1.0f; //Smoothing for Brake Resistance output. Lower = smoother. Must be greater than 0
public float EWMA_ALPHA_THROTTLE_FREQ { get; set; } = 0.5f; //Smoothing for Throttle Resistance output. Lower = smoother. Must be greater than 0
public float FORWARD_ACCEL_MOD { get; set; } = 1.0f;//How to scale Forward acceleration in determining throttle stiffness.
public int MAX_ACCEL_GRIPLOSS_VIBRATION { get; set; } = 35; //The maximum acceleration frequency in Hz (avoid over 40). COrrelates to better grip
public int MIN_ACCEL_GRIPLOSS_VIBRATION { get; set; } = 3; //The Minimum acceleration frequency in Hz (avoid over 40). Helps avoid clicking in controller
public int MIN_ACCEL_GRIPLOSS_STIFFNESS { get; set; } = 200; //On a scale of 1-200 with 1 being most stiff
public int MAX_ACCEL_GRIPLOSS_STIFFNESS { get; set; } = 1; //On a scale of 1-200 with 1 being most stiff
public int MAX_ACCEL_GRIPLOSS_STIFFNESS { get; set; } = 75; //On a scale of 1-200 with 1 being most stiff
public int MIN_BRAKE_STIFFNESS { get; set; } = 200; //On a scale of 1-200 with 1 being most stiff
public int MAX_BRAKE_STIFFNESS { get; set; } = 1; //On a scale of 1-200 with 1 being most stiff
public int BRAKE_VIBRATION_START { get; set; } = 20; //The position (0-255) at which the brake should feel engaged with low grip surfaces
public int BRAKE_VIBRATION__MODE_START { get; set; } = 10; //The depression of the brake lever at which the program should switch to vibration mode rather than smooth resistance. This helps to avoid clicking as vibration mode clicks when no force is applied.
public int MAX_THROTTLE_RESISTANCE { get; set; } = 6; //The Maximum resistance on the throttle (0-7)
public int THROTTLE_VIBRATION_MODE_START { get; set; } = 5; //The depression of the throttle lever at which the program should switch to vibration mode rather than smooth resistance. This helps to avoid clicking as vibration mode clicks when no force is applied.
public int MAX_THROTTLE_RESISTANCE { get; set; } = 6; //The Maximum resistance on the throttle (0-7)
public int MAX_BRAKE_RESISTANCE { get; set; } = 6;//The Maximum resistance on the Brake (0-7)
public int MIN_THROTTLE_RESISTANCE { get; set; } = 1;//The Minimum resistance on the throttle (0-7)
public int MIN_BRAKE_RESISTANCE { get; set; } = 1;//The Minimum resistance on the Brake (0-7)
Expand All @@ -29,5 +30,7 @@ public class Settings
public bool DISABLE_APP_CHECK { get; set; } = false; //Should we disable the check for running applications?
public int DSX_PORT { get; set; } = 6969; //Port for DSX Port Listener
public int FORZA_PORT { get; set; } = 5300; //Port for Forza UDP server
}
public float LEFT_TRIGGER_EFFECT_INTENSITY { get; set; } = 1.0f; // The percentage of the trigger effects. 1 = 100%, 0.5 = 50%, 0 = 0% (off)
public float RIGHT_TRIGGER_EFFECT_INTENSITY { get; set; } = 1.0f; // The percentage of the trigger effects. 1 = 100%, 0.5 = 50%, 0 = 0% (off)
}
}
18 changes: 12 additions & 6 deletions appsettings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ BRAKE_VIBRATION_START=20
;The Maximum resistance on the throttle (0-7)
MAX_THROTTLE_RESISTANCE=4
;The Maximum resistance on the Brake (0-7)
MAX_BRAKE_RESISTANCE=7
MAX_BRAKE_RESISTANCE=6
;The Minimum resistance on the throttle (0-7)
MIN_THROTTLE_RESISTANCE=0
;The Minimum resistance on the Brake (0-7)
Expand All @@ -35,17 +35,23 @@ EWMA_ALPHA_BRAKE = 0.9
EWMA_ALPHA_BRAKE_FREQ = 0.9
;The depression of the brake lever at which the program should switch to vibration mode rather than smooth resistance. This helps to avoid clicking as vibration mode clicks when no force is applied.
BRAKE_VIBRATION__MODE_START = 1
;The depression of the throttle lever at which the program should switch to vibration mode rather than smooth resistance. This helps to avoid clicking as vibration mode clicks when no force is applied.
THROTTLE_VIBRATION_MODE_START = 5
;The Minimum brake frequency in Hz (avoid over 40). Helps avoid clicking in controller
MIN_BRAKE_VIBRATION = 1
;Smoothing for Brake Resistance output. Lower = smoother. Must be greater than 0
EWMA_ALPHA_THROTTLE_FREQ = 1
;The maximum acceleration frequency in Hz (avoid over 40). COrrelates to better grip
MAX_ACCEL_GRIPLOSS_VIBRATION = 35
MAX_ACCEL_GRIPLOSS_VIBRATION = 55
;The Minimum acceleration frequency in Hz (avoid over 40). Helps avoid clicking in controller
MIN_ACCEL_GRIPLOSS_VIBRATION = 3
MIN_ACCEL_GRIPLOSS_VIBRATION = 5
;On a scale of 1-200 with 1 being most stiff
MIN_ACCEL_GRIPLOSS_STIFFNESS = 200
MIN_ACCEL_GRIPLOSS_STIFFNESS = 255
;On a scale of 1-200 with 1 being most stiff
MAX_ACCEL_GRIPLOSS_STIFFNESS = 1
MAX_ACCEL_GRIPLOSS_STIFFNESS = 150
; The percentage of the current RPM when we are in the "redline" of the engine
RPM_REDLINE_RATIO = 0.95
RPM_REDLINE_RATIO = 0.9
; // The percentage of the trigger effects. 1 = 100%, 0.5 = 50%, 0 = 0% (off)
LEFT_TRIGGER_EFFECT_INTENSITY = 1.0
; // The percentage of the trigger effects. 1 = 100%, 0.5 = 50%, 0 = 0% (off)
RIGHT_TRIGGER_EFFECT_INTENSITY = 1.0
Binary file modified bin/Release/net6.0/win-x64/ForzaDSX.dll
Binary file not shown.
Binary file modified bin/Release/net6.0/win-x64/ForzaDSX.pdb
Binary file not shown.
18 changes: 12 additions & 6 deletions bin/Release/net6.0/win-x64/appsettings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ BRAKE_VIBRATION_START=20
;The Maximum resistance on the throttle (0-7)
MAX_THROTTLE_RESISTANCE=4
;The Maximum resistance on the Brake (0-7)
MAX_BRAKE_RESISTANCE=7
MAX_BRAKE_RESISTANCE=6
;The Minimum resistance on the throttle (0-7)
MIN_THROTTLE_RESISTANCE=0
;The Minimum resistance on the Brake (0-7)
Expand All @@ -35,17 +35,23 @@ EWMA_ALPHA_BRAKE = 0.9
EWMA_ALPHA_BRAKE_FREQ = 0.9
;The depression of the brake lever at which the program should switch to vibration mode rather than smooth resistance. This helps to avoid clicking as vibration mode clicks when no force is applied.
BRAKE_VIBRATION__MODE_START = 1
;The depression of the throttle lever at which the program should switch to vibration mode rather than smooth resistance. This helps to avoid clicking as vibration mode clicks when no force is applied.
THROTTLE_VIBRATION_MODE_START = 5
;The Minimum brake frequency in Hz (avoid over 40). Helps avoid clicking in controller
MIN_BRAKE_VIBRATION = 1
;Smoothing for Brake Resistance output. Lower = smoother. Must be greater than 0
EWMA_ALPHA_THROTTLE_FREQ = 1
;The maximum acceleration frequency in Hz (avoid over 40). COrrelates to better grip
MAX_ACCEL_GRIPLOSS_VIBRATION = 35
MAX_ACCEL_GRIPLOSS_VIBRATION = 55
;The Minimum acceleration frequency in Hz (avoid over 40). Helps avoid clicking in controller
MIN_ACCEL_GRIPLOSS_VIBRATION = 3
MIN_ACCEL_GRIPLOSS_VIBRATION = 5
;On a scale of 1-200 with 1 being most stiff
MIN_ACCEL_GRIPLOSS_STIFFNESS = 200
MIN_ACCEL_GRIPLOSS_STIFFNESS = 255
;On a scale of 1-200 with 1 being most stiff
MAX_ACCEL_GRIPLOSS_STIFFNESS = 1
MAX_ACCEL_GRIPLOSS_STIFFNESS = 150
; The percentage of the current RPM when we are in the "redline" of the engine
RPM_REDLINE_RATIO = 0.95
RPM_REDLINE_RATIO = 0.9
; // The percentage of the trigger effects. 1 = 100%, 0.5 = 50%, 0 = 0% (off)
LEFT_TRIGGER_EFFECT_INTENSITY = 1.0
; // The percentage of the trigger effects. 1 = 100%, 0.5 = 50%, 0 = 0% (off)
RIGHT_TRIGGER_EFFECT_INTENSITY = 1.0

0 comments on commit af40cbb

Please sign in to comment.