Motion Master
Loading...
Searching...
No Matches
Public Member Functions | Static Public Attributes | List of all members
StandaloneAutotuning Class Reference

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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ StandaloneAutotuning()

StandaloneAutotuning::StandaloneAutotuning ( const std::string &  programPath,
const std::string &  pipeAddress 
)

Constructs the StandaloneAutotuning instance.

Parameters
programPathPath to the external autotuning program executable.
pipeAddressSpecifies 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::~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.

Member Function Documentation

◆ auto_tune_position_controller()

ControllerGains StandaloneAutotuning::auto_tune_position_controller ( const nlohmann::json &  data)

Performs position controller autotuning using external autotuning program.

Parameters
dataInput data for the autotuning process, typically containing the necessary parameters for tuning.
Returns
The computed ControllerGains for the position controller.
Exceptions
std::runtime_errorIf 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.

◆ auto_tune_velocity_controller()

ControllerGains StandaloneAutotuning::auto_tune_velocity_controller ( const nlohmann::json &  data)

Performs velocity controller autotuning using external autotuning program.

Parameters
dataInput data for the autotuning process, typically containing the necessary parameters for tuning.
Returns
The computed ControllerGains for the velocity controller.
Exceptions
std::runtime_errorIf 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.

◆ compute_position_controller_gains()

ControllerGains StandaloneAutotuning::compute_position_controller_gains ( const nlohmann::json &  data)

Computes the position controller gains without performing full autotuning.

Parameters
dataInput 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_errorIf 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.

◆ connect()

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.

◆ disconnect()

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.

◆ full_auto_tune_position_controller()

FullPositionAutoTuningResult StandaloneAutotuning::full_auto_tune_position_controller ( const nlohmann::json &  data)

Performs a full position controller autotuning sequence using external autotuning program.

Parameters
dataInput data for the autotuning process, typically containing the necessary parameters for tuning.
Returns
The detailed results of the full position autotuning, encapsulated in FullPositionAutoTuningResult.
Exceptions
std::runtime_errorIf 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.

◆ full_auto_tune_velocity_controller()

FullVelocityAutoTuningResult StandaloneAutotuning::full_auto_tune_velocity_controller ( const nlohmann::json &  data)

Performs a full velocity controller autotuning sequence using external autotuning program.

Parameters
dataInput data for the autotuning process, typically containing the necessary parameters for tuning.
Returns
The detailed results of the full velocity autotuning, encapsulated in FullVelocityAutoTuningResult.
Exceptions
std::runtime_errorIf 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.

◆ identify_plant_model()

PlantModelResult StandaloneAutotuning::identify_plant_model ( const nlohmann::json &  data)

Identifies the plant model based on the input data.

Parameters
dataInput data containing parameters for plant model identification, typically related to the system's dynamics.
Returns
A PlantModelResult object containing the identified plant model's numerators and denominators as vectors of doubles.
Exceptions
std::runtime_errorIf 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.

◆ receive()

nlohmann::json StandaloneAutotuning::receive ( unsigned int  timeoutMilliseconds = 5000)

Receives a JSON response from the external autotuning program through the named pipe.

Parameters
timeoutMillisecondsTimeout 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.

◆ send()

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
jsonObjThe 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.

◆ start()

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.

◆ stop()

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.

Member Data Documentation

◆ BUFFER_SIZE

const int StandaloneAutotuning::BUFFER_SIZE = 4096
static

Size of the communication buffer for pipe transfers.


The documentation for this class was generated from the following files: