#include <iomanip>
#include "SampleAppsHelper.h"
#include "rsi.h"
static std::vector<int> MinMaxAvg(std::vector<int> data)
{
int min = data[0];
int max = data[0];
int sum = 0;
for (int i = 0; i < data.size(); i++)
{
if (data[i] < min)
{
min = data[i];
}
if (data[i] > max)
{
max = data[i];
}
sum += data[i];
}
int avg = sum / data.size();
return {min, max, avg};
}
int main()
{
const std::string SAMPLE_APP_NAME = "Utilities: Record Performance";
const int RECORD_PERIOD_SAMPLES = 1;
const int RECORD_TIME = 1000;
int exitCode = -1;
try
{
int valuesPerRecord;
bool networkTimingEnabled = false;
{
std::cout << "Network is not operational. Only Firmware Timing Deltas will be recorded." << std::endl;
valuesPerRecord = 1;
}
else
{
controller->NetworkTimingEnableSet(true);
networkTimingEnabled = true;
valuesPerRecord = 3;
}
int recorderIndex = controller->RecorderCountGet();
controller->RecorderCountSet(recorderIndex + 1);
if (controller->RecorderEnabledGet(recorderIndex))
{
controller->RecorderStop(recorderIndex);
controller->RecorderReset(recorderIndex);
}
controller->RecorderPeriodSet(RECORD_PERIOD_SAMPLES);
controller->RecorderCircularBufferSet(false);
controller->RecorderDataCountSet(valuesPerRecord);
if (networkTimingEnabled)
{
}
controller->RecorderStart(recorderIndex);
controller->OS->Sleep(RECORD_TIME);
controller->RecorderStop(recorderIndex);
int recordCount = controller->RecorderRecordCountGet(recorderIndex);
std::cout << "There are " << recordCount << " records available." << std::endl;
std::vector<int> firmawareTimingDeltas(recordCount);
std::vector<int> networkTimingDeltas(recordCount);
std::vector<int> networkTimingReceiveDeltas(recordCount);
for (int i = 0; i < recordCount; i++)
{
controller->RecorderRecordDataRetrieve(recorderIndex);
firmawareTimingDeltas[i] = controller->RecorderRecordDataValueGet(recorderIndex, 0);
if (networkTimingEnabled)
{
networkTimingDeltas[i] = controller->RecorderRecordDataValueGet(recorderIndex, 1);
networkTimingReceiveDeltas[i] = controller->RecorderRecordDataValueGet(recorderIndex, 2);
}
}
std::vector<int> firmwareTimingStats = MinMaxAvg(firmawareTimingDeltas);
std::cout << "Firmware Timing Deltas (us): ";
std::cout << " Min = " << std::setw(4) << firmwareTimingStats[0];
std::cout << " Max = " << std::setw(4) << firmwareTimingStats[1];
std::cout << " Avg = " << std::setw(4) << firmwareTimingStats[2] << std::endl;
if (networkTimingEnabled)
{
std::vector<int> networkTimingStats = MinMaxAvg(networkTimingDeltas);
std::cout << "Network Timing Deltas (us): ";
std::cout << " Min = " << std::setw(4) << networkTimingStats[0];
std::cout << " Max = " << std::setw(4) << networkTimingStats[1];
std::cout << " Avg = " << std::setw(4) << networkTimingStats[2] << std::endl;
std::vector<int> networkTimingReceiveStats = MinMaxAvg(networkTimingReceiveDeltas);
std::cout << "Network Timing Receive Deltas (us):";
std::cout << " Min = " << std::setw(4) << networkTimingReceiveStats[0];
std::cout << " Max = " << std::setw(4) << networkTimingReceiveStats[1];
std::cout << " Avg = " << std::setw(4) << networkTimingReceiveStats[2] << std::endl;
}
controller->RecorderCountSet(recorderIndex);
exitCode = 0;
}
catch (const std::exception &ex)
{
std::cerr << ex.what() << std::endl;
exitCode = -1;
}
controller->Delete();
return exitCode;
}
static MotionController * Create(CreationParameters *creationParameters)
Initialize and start the RMP EtherCAT controller.
@ RSINetworkStateOPERATIONAL
EtherCAT operational, good state.
@ RSIControllerAddressTypeNETWORK_TIMING_DELTA
the latest time delta between the current network packet send time and the previous (microseconds)....
@ RSIControllerAddressTypeNETWORK_TIMING_RECEIVE_DELTA
the latest time delta between the current network packet receive time and the previous (microseconds)...
@ RSIControllerAddressTypeFIRMWARE_TIMING_DELTA
the latest time delta between the current RMP sample and the previous (microseconds)
static void PrintFooter(std::string sampleAppName, int exitCode)
Print a message to indicate the sample app has finished and if it was successful or not.
static void CheckErrors(RapidCodeObject *rsiObject)
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
static void PrintHeader(std::string sampleAppName)
Print a start message to indicate that the sample app has started.
static MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
CreationParameters for MotionController::Create.