Manages communication and control for an external autotuning process.
This class handles starting, stopping, and communicating with an external autotuning program via named pipes. It supports sending JSON commands and receiving JSON responses, and provides functions for various autotuning tasks.
ControllerGains StandaloneAutotuning::auto_tune_position_controller |
( |
const nlohmann::json & |
data | ) |
|
Performs position controller autotuning using external autotuning program.
- Parameters
-
data | Input data for the autotuning process, typically containing the necessary parameters for tuning. |
- Returns
- The computed
ControllerGains
for the position controller.
- Exceptions
-
std::runtime_error | If the response is empty, null, or does not contain the expected 'gains' field. |
This method constructs a JSON object with the autotuning command and input data, sends it to the external autotuning program, and waits for the response. If the response is valid and contains the 'gains' field, it parses and returns the ControllerGains
. If the response is invalid or the 'gains' field is missing, an error is logged and an exception is thrown.
ControllerGains StandaloneAutotuning::auto_tune_velocity_controller |
( |
const nlohmann::json & |
data | ) |
|
Performs velocity controller autotuning using external autotuning program.
- Parameters
-
data | Input data for the autotuning process, typically containing the necessary parameters for tuning. |
- Returns
- The computed
ControllerGains
for the velocity controller.
- Exceptions
-
std::runtime_error | If the response is empty, null, or does not contain the expected 'gains' field. |
This method constructs a JSON object with the autotuning command and input data, sends it to the external autotuning program, and waits for the response. If the response is valid and contains the 'gains' field, it parses and returns the ControllerGains
. If the response is invalid or the 'gains' field is missing, an error is logged and an exception is thrown.
ControllerGains StandaloneAutotuning::compute_position_controller_gains |
( |
const nlohmann::json & |
data | ) |
|
Computes the position controller gains without performing full autotuning.
- Parameters
-
data | Input data for computing the position controller gains, typically containing parameters related to the system. |
- Returns
- The computed position controller gains, encapsulated in
ControllerGains
.
- Exceptions
-
std::runtime_error | If the response is empty, null, or does not contain the expected 'gains' field. |
This method constructs a JSON object with the command to compute position controller gains and the input data, sends it to the external autotuning program, and waits for the response. If the response contains the computed gains, it returns them in the form of a ControllerGains
object. If the response is invalid or does not contain the expected 'gains', an error is logged and an exception is thrown.
bool StandaloneAutotuning::connect |
( |
| ) |
|
Establishes a connection to the named pipe (Windows) or Unix domain socket (Linux) for communication with the external autotuning program.
- Returns
- True if the connection is successfully established, false otherwise.
This method checks if the object is already connected. If not, it attempts to create a connection to the named pipe using CreateFileA
. If the connection is successful, it sets the connected
flag to true
. If an error occurs while creating the pipe connection, an error message is displayed, and the method returns false
.
nlohmann::json StandaloneAutotuning::exchange |
( |
const nlohmann::json & |
jsonObj, |
|
|
unsigned int |
timeoutMilliseconds = 5000 |
|
) |
| |
Sends a JSON request and receives a JSON response.
This function sends a JSON object via the send
method, waits for a response, and processes the received response. If the response is empty, null, or contains an error, an exception is thrown. The function logs the request and response for debugging.
- Parameters
-
jsonObj | The JSON object to be sent in the request. |
timeoutMilliseconds | The timeout in milliseconds for the request. Default is 5000 ms. |
- Returns
- The JSON response received from the process.
- Exceptions
-
std::runtime_error | if the response is empty, null, or contains an error. |
nlohmann::json StandaloneAutotuning::receive |
( |
unsigned int |
timeoutMilliseconds = 5000 | ) |
|
Receives a JSON response from the external autotuning program through the named pipe.
- Parameters
-
timeoutMilliseconds | Timeout in milliseconds for receiving data. |
- Returns
- The received JSON object, or an empty JSON object if an error occurs or the timeout is reached.
This method checks if the object is connected to the named pipe. If connected, it attempts to read data from the pipe using an asynchronous read operation. It waits for the operation to complete within the specified timeoutMilliseconds
. If the data is successfully received, it is parsed into a JSON object and returned. If an error occurs, a timeout is reached, or the object is not connected, it logs the error and returns an empty JSON object.
bool StandaloneAutotuning::send |
( |
const nlohmann::json & |
jsonObj | ) |
|
Sends a JSON object to the external autotuning program through the named pipe or Unix domain socket.
- Parameters
-
jsonObj | The JSON object to send. |
- Returns
- True if the data is sent successfully, false otherwise.
This method checks if the object is connected to the named pipe. If connected, it converts the provided jsonObj
to a string using dump()
and writes it to the pipe using the WriteFile
function. If the data is successfully written, it returns true
. If the object is not connected, it returns false
.
bool StandaloneAutotuning::start |
( |
| ) |
|
Starts the external autotuning program by creating a new process.
- Returns
- True if the process starts successfully, false otherwise.
This method creates a new process for the external autotuning program using the CreateProcess
API. It sets up the necessary STARTUPINFO
structure and uses the provided programPath
to start the program. If the process is successfully created, it logs the process ID. If the process creation fails, an error message is displayed. On Windows, a job object is created to manage the child process and ensure it is terminated if the parent process is forcibly terminated. This is necessary because when the Electron main process sends a kill signal on Windows, it forcefully terminates the parent process without notifying the child process or allowing it to shut down properly. Without the job object, the child process would remain running in the background.
void StandaloneAutotuning::stop |
( |
| ) |
|
Stops the external autotuning process.
This method checks whether the autotuning process is running by verifying if pi.hProcess
is not NULL
. If the process is active, it terminates it using TerminateProcess
, then closes the associated process and thread handles to ensure proper cleanup.
On Unix-based systems, it performs a similar check by verifying if the process ID (PID) exists and then terminates it accordingly.