Motion Master
|
Manages communication and control for an external autotuning process. More...
#include <standalone_autotuning.h>
Public Member Functions | |
StandaloneAutotuning (const std::string &programPath, const std::string &pipeAddress) | |
Constructs the StandaloneAutotuning instance. More... | |
~StandaloneAutotuning () | |
Destroys the StandaloneAutotuning instance. More... | |
bool | start () |
Starts the external autotuning program by creating a new process. More... | |
void | stop () |
Stops the external autotuning process. More... | |
bool | connect () |
Establishes a connection to the named pipe (Windows) or Unix domain socket (Linux) for communication with the external autotuning program. More... | |
void | disconnect () |
Disconnects from the named pipe and terminates the communication with the external autotuning program. More... | |
bool | send (const nlohmann::json &jsonObj) |
Sends a JSON object to the external autotuning program through the named pipe or Unix domain socket. More... | |
nlohmann::json | receive (unsigned int timeoutMilliseconds=5000) |
Receives a JSON response from the external autotuning program through the named pipe. More... | |
ControllerGains | auto_tune_velocity_controller (const nlohmann::json &data) |
Performs velocity controller autotuning using external autotuning program. More... | |
ControllerGains | auto_tune_position_controller (const nlohmann::json &data) |
Performs position controller autotuning using external autotuning program. More... | |
FullVelocityAutoTuningResult | full_auto_tune_velocity_controller (const nlohmann::json &data) |
Performs a full velocity controller autotuning sequence using external autotuning program. More... | |
FullPositionAutoTuningResult | full_auto_tune_position_controller (const nlohmann::json &data) |
Performs a full position controller autotuning sequence using external autotuning program. More... | |
ControllerGains | compute_position_controller_gains (const nlohmann::json &data) |
Computes the position controller gains without performing full autotuning. More... | |
PlantModelResult | identify_plant_model (const nlohmann::json &data) |
Identifies the plant model based on the input data. More... | |
Static Public Attributes | |
static const int | BUFFER_SIZE = 4096 |
Size of the communication buffer for pipe transfers. More... | |
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.
StandaloneAutotuning::StandaloneAutotuning | ( | const std::string & | programPath, |
const std::string & | pipeAddress | ||
) |
Constructs the StandaloneAutotuning instance.
programPath | Path to the external autotuning program executable. |
pipeAddress | Specifies the address of the named pipe (on Windows) used for IPC, or the socket file path (on Linux) for communication. |
This constructor initializes the programPath
and pipeAddress
members with the provided values, and sets the connected
flag to false
, indicating that the initial connection state is not established.
StandaloneAutotuning::~StandaloneAutotuning | ( | ) |
Destroys the StandaloneAutotuning instance.
This destructor stops the autotuning process by calling the stop()
method. It ensures proper cleanup of resources when the object is destroyed.
ControllerGains StandaloneAutotuning::auto_tune_position_controller | ( | const nlohmann::json & | data | ) |
Performs position controller autotuning using external autotuning program.
data | Input data for the autotuning process, typically containing the necessary parameters for tuning. |
ControllerGains
for the position controller.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.
data | Input data for the autotuning process, typically containing the necessary parameters for tuning. |
ControllerGains
for the velocity controller.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.
data | Input data for computing the position controller gains, typically containing parameters related to the system. |
ControllerGains
.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.
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
.
void StandaloneAutotuning::disconnect | ( | ) |
Disconnects from the named pipe and terminates the communication with the external autotuning program.
This method closes the named pipe handle and sets the connected
flag to false
, indicating that the connection to the autotuning program is no longer active.
FullPositionAutoTuningResult StandaloneAutotuning::full_auto_tune_position_controller | ( | const nlohmann::json & | data | ) |
Performs a full position controller autotuning sequence using external autotuning program.
data | Input data for the autotuning process, typically containing the necessary parameters for tuning. |
FullPositionAutoTuningResult
.std::runtime_error | If the response is empty, null, or cannot be parsed into the expected result. |
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 can be parsed into a FullPositionAutoTuningResult
, it returns the result. If the response is invalid or empty, an error is logged and an exception is thrown.
FullVelocityAutoTuningResult StandaloneAutotuning::full_auto_tune_velocity_controller | ( | const nlohmann::json & | data | ) |
Performs a full velocity controller autotuning sequence using external autotuning program.
data | Input data for the autotuning process, typically containing the necessary parameters for tuning. |
FullVelocityAutoTuningResult
.std::runtime_error | If the response is empty, null, or cannot be parsed into the expected result. |
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 can be parsed into a FullVelocityAutoTuningResult
, it returns the result. If the response is invalid or empty, an error is logged and an exception is thrown.
PlantModelResult StandaloneAutotuning::identify_plant_model | ( | const nlohmann::json & | data | ) |
Identifies the plant model based on the input data.
data | Input data containing parameters for plant model identification, typically related to the system's dynamics. |
PlantModelResult
object containing the identified plant model's numerators and denominators as vectors of doubles.std::runtime_error | If the response is empty, null, or does not contain the expected 'numerators' or 'denominators' fields. |
This method sends a request to the external autotuning program to identify the plant model using the provided input data. The response is expected to contain the plant model's numerators and denominators, which are extracted and returned as a PlantModelResult
object. If the response is invalid or missing the required fields, an error is logged, and an exception is thrown.
nlohmann::json StandaloneAutotuning::receive | ( | unsigned int | timeoutMilliseconds = 5000 | ) |
Receives a JSON response from the external autotuning program through the named pipe.
timeoutMilliseconds | Timeout in milliseconds for receiving data. |
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.
jsonObj | The JSON object to send. |
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.
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.
|
static |
Size of the communication buffer for pipe transfers.