Skip to content

Commit

Permalink
Use queue for passing config from serial task to pedal update
Browse files Browse the repository at this point in the history
This is just a tidy-up as it allows simpler handling of the config
struct.

No intentional functional changes.
  • Loading branch information
MichaelJFr committed Aug 1, 2023
1 parent 19dd94c commit e993de7
Showing 1 changed file with 12 additions and 64 deletions.
76 changes: 12 additions & 64 deletions Arduino/Esp32/Main/Main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ ForceCurve_Interpolated forceCurve;
TaskHandle_t Task1;
TaskHandle_t Task2;

static SemaphoreHandle_t semaphore_updateConfig=NULL;
bool configUpdateAvailable = false; // semaphore protected data
DAP_config_st dap_config_st_local;

static QueueHandle_t queue_updateJoystick=NULL;
static QueueHandle_t queue_updateConfig=NULL;

bool resetPedalPosition = false;

Expand Down Expand Up @@ -238,31 +235,23 @@ void setup()
// interprete config values
dap_calculationVariables_st.updateFromConfig(dap_config_st);

// activate parameter update in first cycle
configUpdateAvailable = true;
// equalize pedal config for both tasks
dap_config_st_local = dap_config_st;





// setup multi tasking
queue_updateJoystick = xQueueCreate(1, sizeof(int32_t));
semaphore_updateConfig = xSemaphoreCreateMutex();
queue_updateConfig = xQueueCreate(1, sizeof(DAP_config_st));
delay(10);


if(queue_updateJoystick==NULL)
if(queue_updateJoystick==NULL || queue_updateConfig==NULL)
{
Serial.println("Could not create queue");
ESP.restart();
}
if(semaphore_updateConfig==NULL)
{
Serial.println("Could not create semaphore");
ESP.restart();
}

// activate parameter update in first cycle
xQueueSend(queue_updateConfig, &dap_config_st, /*xTicksToWait=*/10);

disableCore0WDT();

Expand Down Expand Up @@ -316,15 +305,6 @@ void updatePedalCalcParameters()

// tune the PID settings
tunePidValues(dap_config_st);

// equalize pedal config for both tasks
dap_config_st_local = dap_config_st;






}


Expand Down Expand Up @@ -394,36 +374,15 @@ void pedalUpdateTask( void * pvParameters )


// if a config update was received over serial, update the variables required for further computation
if (configUpdateAvailable == true)
{
if(semaphore_updateConfig!=NULL)
{

bool configWasUpdated_b = false;
// Take the semaphore and just update the config file, then release the semaphore
if(xSemaphoreTake(semaphore_updateConfig, 1)==pdTRUE)
DAP_config_st dap_config_st_local;
if (pdTRUE == xQueueReceive(queue_updateConfig, &dap_config_st_local, /*xTicksToWait=*/0))
{
Serial.println("Update pedal config!");
configUpdateAvailable = false;
dap_config_st = dap_config_st_local;
configWasUpdated_b = true;
xSemaphoreGive(semaphore_updateConfig);
}

// update the calc params
if (true == configWasUpdated_b)
{
Serial.println("Updating the calc params!");
configWasUpdated_b = false;
dap_config_st.storeConfigToEprom(dap_config_st); // store config to EEPROM
updatePedalCalcParameters(); // update the calc parameters
}

}
else
{
Serial.println("semaphore_updateConfig == 0");
}
}


Expand Down Expand Up @@ -616,16 +575,8 @@ void serialCommunicationTask( void * pvParameters )
// likely config structure
if ( n == sizeof(DAP_config_st) )
{

if(semaphore_updateConfig!=NULL)
{
if(xSemaphoreTake(semaphore_updateConfig, 1)==pdTRUE)
{
DAP_config_st * dap_config_st_local_ptr;
dap_config_st_local_ptr = &dap_config_st_local;
Serial.readBytes((char*)dap_config_st_local_ptr, sizeof(DAP_config_st));


DAP_config_st dap_config_st_local;
Serial.readBytes((char*)&dap_config_st_local, sizeof(DAP_config_st));

// check if data is plausible
bool structChecker = true;
Expand All @@ -644,7 +595,7 @@ void serialCommunicationTask( void * pvParameters )
Serial.println(dap_config_st_local.payLoadHeader_.version);
}
// checksum validation
uint16_t crc = checksumCalculator((uint8_t*)(&(dap_config_st_local_ptr->payLoadPedalConfig_)), sizeof(dap_config_st_local.payLoadPedalConfig_) );
uint16_t crc = checksumCalculator((uint8_t*)(&(dap_config_st_local.payLoadPedalConfig_)), sizeof(dap_config_st_local.payLoadPedalConfig_));
if (crc != dap_config_st_local.payLoadHeader_.checkSum){
structChecker = false;
Serial.print("CRC expected: ");
Expand All @@ -658,11 +609,8 @@ void serialCommunicationTask( void * pvParameters )
if (structChecker == true)
{
Serial.println("Update pedal config!");
configUpdateAvailable = true;
xQueueSend(queue_updateConfig, &dap_config_st_local, /*xTicksToWait=*/10);
}
xSemaphoreGive(semaphore_updateConfig);
}
}
}
else
{
Expand Down

0 comments on commit e993de7

Please sign in to comment.