APIs, concepts, guides, and more
HelloRTTasks.cpp
1
10#include "SampleAppsHelper.h" // Import our helper functions.
11#include "rsi.h" // Import our RapidCode Library.
12
13#include "RTTasksHelpers.h" // Import our helper functions for RTTasks
14#include "rttask.h" // Import the RTTask library
15
16#include <iostream>
17#include <memory>
18#include <thread>
19#include <chrono>
20
21using namespace RSI::RapidCode; // Import the RapidCode namespace
22using namespace RSI::RapidCode::RealTimeTasks; // Import the RealTimeTasks namespace
23
24int main()
25{
26 const std::string SAMPLE_APP_NAME = "Real-Time Tasks: Hello RTTasks";
27
28 // Print a start message to indicate that the sample app has started
29 SampleAppsHelper::PrintHeader(SAMPLE_APP_NAME);
30
32 // Create the controller
34 MotionController *controller = MotionController::Create(&params);
35
36 RTTaskManagerCreationParameters parameters = RTTaskHelper::GetTaskManagerCreationParameters();
37 std::cout << "Creating task manager..." << std::endl;
38 std::unique_ptr<RTTaskManager> manager(RTTaskManager::Create(parameters));
39
40 int exitCode = -1; // Set the exit code to an error value.
41 try
42 {
43 // Prepare the controller as defined in SampleAppsHelper.h depending on the configuration
46
47 // Tell the task manager the name of the function to run as a task, the name
48 // of the library that contains the function, and the directory where the
49 // library is located. The Increment function is defined in the RTTaskFunctions library.
50 // It increments a global counter variable.
51 std::cout << "Submitting task..." << std::endl;
52 RTTaskCreationParameters params("Increment");
54 std::unique_ptr<RTTask> task(manager->TaskSubmit(params));
55
56 // Wait for the task to run for a bit
57 task->ExecutionCountAbsoluteWait(50, 500);
58
59 // Get the counter global tag to see if the task ran correctly
60 std::cout << "Getting counter global tag..." << std::endl;
61 RSI::RapidCode::FirmwareValue counter = manager->GlobalValueGet("counter");
62
63 if (counter.Int64 <= 0)
64 {
65 // The task did not run correctly
66 exitCode = -1;
67 std::cout << "Counter is not greater than 0. The task did not run correctly." << std::endl;
68 }
69 else
70 {
71 // The task ran correctly
72 exitCode = 0;
73 std::cout << "Counter: " << counter.Int64 << std::endl;
74 }
75 }
76 catch (const std::exception& e)
77 {
78 std::cerr << e.what() << std::endl;
79 }
80 // Shutdown the task manager firmware process
81 if (manager != nullptr)
82 {
83 manager->Shutdown();
84 }
85
86 // Clean up the controller and any other objects as needed
87 SampleAppsHelper::Cleanup(controller);
88
89 // Delete the controller as the program exits to ensure memory is deallocated in the correct order
90 controller->Delete();
92
93 // Print a message to indicate the sample app has finished and if it was successful or not
94 SampleAppsHelper::PrintFooter(SAMPLE_APP_NAME, exitCode);
95
96 return exitCode;
97}
static MotionController * Create()
Initialize and start the RMP EtherCAT controller.
void Delete(void)
Delete the MotionController and all its objects.
Represents the RMP soft motion controller. This class provides an interface to general controller con...
Definition rsi.h:800
static RTTaskManager * Create(const RTTaskManagerCreationParameters &parameters)
Create a new RTTaskManager instance.
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 void Cleanup(MotionController *controller)
Cleanup the controller and restore the object counts to the original values.
static MotionController::CreationParameters GetCreationParameters()
Returns a MotionController::CreationParameters object with user-defined parameters.
static void SetupController(MotionController *controller, int numAxes=0)
Setup the controller with user defined axis counts and configuration.
CreationParameters for MotionController::Create.
Definition rsi.h:861
RTTaskCreationParameters specifies all the information required to create and configure a real-time t...
Definition rttask.h:124
static constexpr int32_t RepeatForever
Special value to indicate the task should repeat forever.
Definition rttask.h:138
RTTaskManagerCreationParameters specifies all the information required to create and configure an RTT...
Definition rttask.h:318
Union representing a generic RMP firmware value with multiple data types, stored in 64-bits.
Definition rsi.h:468