#include "helpers.h"
#include "config.h"
#include "rsi.h"
#include "rttasks-helpers.h"
#include "rttask.h"
#include <iostream>
#include <iomanip>
#include <memory>
#include <optional>
#include <thread>
#include <chrono>
{
std::cout
<< " | cmd: " << std::setw(8) << std::fixed << std::setprecision(3)
<< " | sens: " << std::setw(8) << std::fixed << std::setprecision(3)
<< '\n';
}
void PrintStatusWhileRunning(
RTTaskManager& manager,
Axis* axis,
int duration=3,
int interval=50)
{
std::cout << "Begin logging" << std::endl;
using clock = std::chrono::steady_clock;
auto start = clock::now();
auto chronoDuration = std::chrono::seconds(duration);
auto chronoInterval = std::chrono::milliseconds(interval);
while (clock::now() - start < chronoDuration) {
LogStatus(manager, axis);
std::this_thread::sleep_for(chronoInterval);
}
std::cout << "End logging\n" << std::endl;
}
{
std::cout << "-----GLOBALS-----\n";
for (const auto& name : globals) {
std::cout << " " << name << '\n';
}
std::cout << "---END GLOBALS---\n" << std::endl;
}
{
std::cout
<< "--Tasks------------------------------\n";
const auto& params = task.InfoGet().CreationParameters;
const auto& status = task.StatusGet();
std::cout
<< "Task " << std::right << std::setw(4) << task.IdGet() << " | "
<< std::left << std::setw(14) << params.FunctionName << " | "
<< RTTaskHelper::StateToString(status.State)
<< '\n' << std::right;
}
std::cout
<< "-------------------------------------\n"
<< std::endl;
}
int main()
{
const std::string SAMPLE_APP_NAME = "📜 Real-Time Tasks: Follow Sensor";
const int NUM_AXES = 1;
const int TIMEOUT_MS = 5000;
std::optional<RTTaskManager> manager;
int exitCode = -1;
try
{
Axis *axis = controller->AxisGet(0);
std::cout << "Creating task manager..." << std::endl;
PrintGlobals(manager.value());
RTTaskHelper::SubmitInitializationTask(manager.value());
std::cout << "Submitting task..." << std::endl;
params.Period = 10;
RTTask task = manager->TaskSubmit(params);
PrintTasks(manager.value());
PrintStatusWhileRunning(manager.value(), axis);
if (executionCount <= 0)
{
exitCode = -1;
std::cout << "Execution count is not greater than 0. The task did not run correctly." << std::endl;
}
else
{
exitCode = 0;
std::cout << "Execution count: " << executionCount << " (>= 300 expected)" << std::endl;
}
std::cout << "State: " << RTTaskHelper::StateToString(taskState) << " (DISABLED expected)" << std::endl;
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
if (manager.has_value())
{
manager->Shutdown();
}
controller->Delete();
return exitCode;
}
double CommandPositionGet()
Get the current command position.
void PositionSet(double position)
Set the Command and Actual positions.
Represents a single axis of motion control. This class provides an interface for commanding motion,...
static MotionController * Create(CreationParameters *creationParameters)
Initialize and start the RMP EtherCAT controller.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
int32_t MotionDoneWait(int32_t waitTimeoutMilliseconds=WaitForever)
Waits for a move to complete.
int32_t AmpEnableSet(bool enable, int32_t ampActiveTimeoutMilliseconds=AmpEnableTimeoutMillisecondsDefault, bool overrideRestrictedState=false)
Enable all amplifiers.
void Stop()
Stop the task from executing.
int64_t ExecutionCountAbsoluteWait(int64_t count=ExecutionCountDefault, int32_t timeoutMs=ExecutionCountWaitTimeoutMillisecondsDefault)
Wait for the task to reach a specific execution count.
RTTaskStatus StatusGet()
Get the current status of the task.
Interface for controlling and monitoring a single real-time task. See RTTaskManager::TaskSubmit and R...
RSI::RapidCode::FirmwareValue GlobalValueGet(int32_t offset)
Read a GlobalTag by its offset. (internal use).
RapidVector< RTTask > TasksGet()
Get all tasks managed by this RTTaskManager.
RapidVector< const char * > GlobalNamesGet(const char *const libraryName=nullptr, const char *const libraryDirectory=nullptr)
Get names of all global variables.
RTTaskManagerStatus StatusGet()
Get the current status of the manager.
static RTTaskManager Create(const RTTaskManagerCreationParameters ¶meters)
Create a new RTTaskManager instance.
Interface for managing real-time tasks firmware. See Real-Time Tasks for more information.
RTTaskState
Enum representing the possible states of a real-time task.
void SetupController(MotionController *controller, int numAxes=0)
Setup the controller and check if the network is in the correct state for the configuration.
MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
void Cleanup(MotionController *controller)
[SetupController]
void CheckErrors(RapidCodeObject *rsiObject, const std::source_location &location=std::source_location::current())
Checks for errors in the given RapidCodeObject and throws an exception if any non-warning errors are ...
void PrintHeader(std::string sampleAppName)
[NetworkShutdown]
void PrintFooter(std::string sampleAppName, int exitCode)
[PrintHeader]
The RealTimeTasks namespace.
CreationParameters for MotionController::Create.
RTTaskCreationParameters specifies all the information required to create and configure a real-time t...
static constexpr int32_t RepeatForever
Special value to indicate the task should repeat forever.
RTTaskManagerCreationParameters specifies all the information required to create and configure an RTT...
int64_t CycleCount
Number of cycles executed by the manager.
RTTaskStatus provides status information for a real-time task, including its current state,...
RTTaskState State
Current state of the task.
int64_t ExecutionCount
Number of times the task has executed.
double Double
Double precision (64-bit) floating-point.