Skip to content

Commit

Permalink
Add custom effect 1/2
Browse files Browse the repository at this point in the history
bump DAP version to 137
  • Loading branch information
tcfshcrw committed Jul 18, 2024
1 parent ca0147d commit 7c9d894
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 25 deletions.
49 changes: 49 additions & 0 deletions ESP32/include/ABSOscillation.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static const long ABS_ACTIVE_TIME_PER_TRIGGER_MILLIS = 100;
static const long RPM_ACTIVE_TIME_PER_TRIGGER_MILLIS = 100;
static const long BP_ACTIVE_TIME_PER_TRIGGER_MILLIS = 100;
static const long WS_ACTIVE_TIME_PER_TRIGGER_MILLIS = 100;
static const long CV_ACTIVE_TIME_PER_TRIGGER_MILLIS = 100;
static int RPM_VALUE_LAST = 0;

class ABSOscillation {
Expand Down Expand Up @@ -383,3 +384,51 @@ class Road_impact_effect

}
};
//Wheel slip
class Custom_vibration {
private:
long _timeLastTriggerMillis;
long _CVTimeMillis;
long _lastCallTimeMillis = 0;


public:
Custom_vibration()
: _timeLastTriggerMillis(0)
{}
//float RPM_value =0;
float CV_Force_offset = 0;
public:
void trigger() {
_timeLastTriggerMillis = millis();
}

void forceOffset(float CV_freq, float CV_amp) {


long timeNowMillis = millis();
float timeSinceTrigger = (timeNowMillis - _timeLastTriggerMillis);
float CVForceOffset = 0;


if (timeSinceTrigger > CV_ACTIVE_TIME_PER_TRIGGER_MILLIS)
{
_CVTimeMillis = 0;
CVForceOffset = 0;
}
else
{
_CVTimeMillis += timeNowMillis - _lastCallTimeMillis;
float CVTimeSeconds = _CVTimeMillis / 1000.0f;

CVForceOffset = CV_amp/20.0f * sin( 2*PI* CV_freq* CVTimeSeconds);



}
CV_Force_offset=CVForceOffset;
_lastCallTimeMillis = timeNowMillis;


}
};
10 changes: 9 additions & 1 deletion ESP32/include/DiyActivePedal_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdint.h>

// define the payload revision
#define DAP_VERSION_CONFIG 136
#define DAP_VERSION_CONFIG 137

// define the payload types
#define DAP_PAYLOAD_TYPE_CONFIG 100
Expand Down Expand Up @@ -33,6 +33,8 @@ struct payloadPedalAction {
uint8_t G_value;
uint8_t WS_u8;
uint8_t impact_value_u8;
uint8_t Trigger_CV_1;
uint8_t Trigger_CV_2;
};


Expand Down Expand Up @@ -118,6 +120,12 @@ struct payloadPedalConfig {
//Road impact effect
uint8_t Road_multi;
uint8_t Road_window;
//Custom Vibration 1
uint8_t CV_amp_1;
uint8_t CV_freq_1;
//Custom Vibration 2
uint8_t CV_amp_2;
uint8_t CV_freq_2;
// cubic spline parameters
float cubic_spline_param_a_array[5];
float cubic_spline_param_b_array[5];
Expand Down
17 changes: 15 additions & 2 deletions ESP32/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ BitePointOscillation _BitePointOscillation;
G_force_effect _G_force_effect;
WSOscillation _WSOscillation;
Road_impact_effect _Road_impact_effect;
Custom_vibration CV1;
Custom_vibration CV2;
#define ABS_OSCILLATION


Expand Down Expand Up @@ -770,6 +772,8 @@ void pedalUpdateTask( void * pvParameters )
_G_force_effect.forceOffset(&dap_calculationVariables_st, dap_config_st.payLoadPedalConfig_.G_multi);
_WSOscillation.forceOffset(&dap_calculationVariables_st);
_Road_impact_effect.forceOffset(&dap_calculationVariables_st, dap_config_st.payLoadPedalConfig_.Road_multi);
CV1.forceOffset(dap_config_st.payLoadPedalConfig_.CV_freq_1,dap_config_st.payLoadPedalConfig_.CV_amp_1);
CV2.forceOffset(dap_config_st.payLoadPedalConfig_.CV_freq_2,dap_config_st.payLoadPedalConfig_.CV_amp_2);
#endif

//update max force with G force effect
Expand Down Expand Up @@ -930,7 +934,7 @@ void pedalUpdateTask( void * pvParameters )


//Add effect by force
float effect_force=absForceOffset+ _BitePointOscillation.BitePoint_Force_offset+_WSOscillation.WS_Force_offset;
float effect_force=absForceOffset+ _BitePointOscillation.BitePoint_Force_offset+_WSOscillation.WS_Force_offset+CV1.CV_Force_offset+CV2.CV_Force_offset;

// use interpolation to determine local linearized spring stiffness
double stepperPosFraction = stepper->getCurrentPositionFraction();
Expand Down Expand Up @@ -1322,7 +1326,16 @@ void serialCommunicationTask( void * pvParameters )
{
systemIdentificationMode_b = true;
}

// trigger Custom effect effect 1
if (dap_actions_st.payloadPedalAction_.Trigger_CV_1)
{
CV1.trigger();
}
// trigger Custom effect effect 2
if (dap_actions_st.payloadPedalAction_.Trigger_CV_2)
{
CV2.trigger();
}
// trigger return pedal position
if (dap_actions_st.payloadPedalAction_.returnPedalConfig_u8)
{
Expand Down
54 changes: 47 additions & 7 deletions SimHubPlugin/DataPluginDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
static class Constants
{
// payload revisiom
public const uint pedalConfigPayload_version = 136;
public const uint pedalConfigPayload_version = 137;


// pyload types
Expand Down Expand Up @@ -57,6 +57,8 @@ public struct payloadPedalAction
public byte G_value;
public byte WS_u8;
public byte impact_value;
public byte Trigger_CV_1;
public byte Trigger_CV_2;
};

public struct payloadPedalState_Basic
Expand Down Expand Up @@ -136,7 +138,13 @@ public struct payloadPedalConfig
public byte WS_amp;
public byte WS_freq;
public byte Impact_multi;
public byte Impact_window;
public byte Impact_window;
//Custom Vibration 1
public byte CV_amp_1;
public byte CV_freq_1;
//Custom Vibration 2
public byte CV_amp_2;
public byte CV_freq_2;
// cubic spline params
public float cubic_spline_param_a_0;
public float cubic_spline_param_a_1;
Expand Down Expand Up @@ -400,6 +408,8 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
double _G_force = 128;
byte WS_value = 0;
byte Road_impact_value = 0;
byte CV1_value = 0;
byte CV2_value = 0;
//bool WS_flag = false;

if (data.GamePaused | (!data.GameRunning))
Expand Down Expand Up @@ -473,6 +483,7 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
}

game_running_index = 1;


}
else
Expand Down Expand Up @@ -531,6 +542,8 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)

tmp.payloadPedalAction_.WS_u8 = 0;
tmp.payloadPedalAction_.impact_value = 0;
tmp.payloadPedalAction_.Trigger_CV_1 = 0;
tmp.payloadPedalAction_.Trigger_CV_2 = 0;
if (Settings.G_force_enable_flag[pedalIdx] == 1)
{
tmp.payloadPedalAction_.G_value = (Byte)g_force_last_value;
Expand Down Expand Up @@ -604,9 +617,6 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
update_flag = true;
}
}



}
//Road impact
if (Settings.Road_impact_enable_flag[pedalIdx] == 1)
Expand Down Expand Up @@ -641,8 +651,34 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
}
}
}


if (Settings.CV1_enable_flag[pedalIdx] == true)
{
if (pluginManager.GetPropertyValue(Settings.CV1_bindings[pedalIdx]) != null)
{

CV1_value = Convert.ToByte(pluginManager.GetPropertyValue(Settings.CV1_bindings[pedalIdx]));
if (CV1_value > (Settings.CV1_trigger[pedalIdx]))
{
tmp.payloadPedalAction_.Trigger_CV_1 = 1;
update_flag = true;
}
}
}
if (Settings.CV2_enable_flag[pedalIdx] == true)
{
if (pluginManager.GetPropertyValue(Settings.CV2_bindings[pedalIdx]) != null)
{

CV2_value = Convert.ToByte(pluginManager.GetPropertyValue(Settings.CV2_bindings[pedalIdx]));
if (CV2_value > (Settings.CV2_trigger[pedalIdx]))
{
tmp.payloadPedalAction_.Trigger_CV_2 = 1;
update_flag = true;
}
}
}




if (pedalIdx == 1)
Expand Down Expand Up @@ -706,6 +742,8 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
tmp.payloadPedalAction_.G_value = 128;
tmp.payloadPedalAction_.WS_u8 = 0;
tmp.payloadPedalAction_.impact_value = 0;
tmp.payloadPedalAction_.Trigger_CV_1 = 0;
tmp.payloadPedalAction_.Trigger_CV_2 = 0;
rpm_last_value = 0;
Road_impact_last = 0;
debug_value = 0;
Expand Down Expand Up @@ -742,6 +780,8 @@ unsafe public void DataUpdate(PluginManager pluginManager, ref GameData data)
tmp.payloadPedalAction_.G_value = 128;
tmp.payloadPedalAction_.WS_u8 = 0;
tmp.payloadPedalAction_.impact_value = 0;
tmp.payloadPedalAction_.Trigger_CV_1 = 0;
tmp.payloadPedalAction_.Trigger_CV_2 = 0;
DAP_action_st* v = &tmp;
byte* p = (byte*)v;
tmp.payloadFooter_.checkSum = checksumCalc(p, sizeof(payloadHeader) + sizeof(payloadPedalAction));
Expand Down
11 changes: 9 additions & 2 deletions SimHubPlugin/DataPluginDemoSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace User.PluginSdkDemo
using System.Linq.Expressions;

namespace User.PluginSdkDemo
{
/// <summary>
/// Settings class, make sure it can be correctly serialized using JSON.net
Expand Down Expand Up @@ -36,7 +38,12 @@ public class DataPluginDemoSettings
public double kinematicDiagram_zeroPos_OY = 20;
public double kinematicDiagram_zeroPos_scale = 1.5;
public bool[] RTSDTR_False = new bool[3] { true,true,true};

public bool[] CV1_enable_flag = new bool[3] { false,false, false };
public int[] CV1_trigger = new int[3] { 0, 0, 0 };
public string[] CV1_bindings = new string[3] { "","",""};
public bool[] CV2_enable_flag = new bool[3] { false, false, false };
public int[] CV2_trigger = new int[3] { 0, 0, 0 };
public string[] CV2_bindings = new string[3] { "", "", "" };

}

Expand Down
Loading

0 comments on commit 7c9d894

Please sign in to comment.