APIs, concepts, guides, and more
RandomWalk.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: Random Walk";
27
28 // Print a start message to indicate that the sample app has started
29 SampleAppsHelper::PrintHeader(SAMPLE_APP_NAME);
30
31 /* CONSTANTS */
32 // *NOTICE* The following constants must be configured before attempting to run with hardware.
33 const int NUM_AXES = 1; // The number of axes to configure, default is 0 for this template
34 const int TIMEOUT_MS = 5000; // Timeout for waiting for the task to execute once
35
36 /* RAPIDCODE INITIALIZATION */
37
38 // Create the controller
40 MotionController *controller = MotionController::Create(&params);
41
42 // Create the task manager
43 std::unique_ptr<RTTaskManager> manager = nullptr;
44
45 int exitCode = -1; // Set the exit code to an error value.
46 try
47 {
48 // Prepare the controller as defined in SampleAppsHelper.h depending on the configuration
50 SampleAppsHelper::SetupController(controller, NUM_AXES);
51
52 /* SET CONTROLLER OBJECT COUNTS HERE*/
53 // Normally you would set the number of axes here, but for samples that is handled in the SampleAppsHelper::SetupController function
54 // controller->AxisCountSet(NUM_AXES);
55
57 // Configure the axis
58 Axis *axis = controller->AxisGet(0);
60 axis->PositionSet(0);
61 axis->AmpEnableSet(true);
62
63 // Create the task manager
64 RTTaskManagerCreationParameters parameters = RTTaskHelper::GetTaskManagerCreationParameters();
65 std::cout << "Creating task manager..." << std::endl;
66 manager.reset(RTTaskManager::Create(parameters));
67
68 // Call the initialization task to initialize the global variables and get pointers to RapidCode objects
69 // in the RTTaskFunctions library. This needs to be called before any task that uses RapidCode objects.
70 RTTaskHelper::InitializeRTTaskObjects(manager);
71
72 // Tell the task manager the name of the function to run as a task, the name
73 // of the library that contains the function, and the directory where the
74 // library is located. The RandomWalk function is defined in the RTTaskFunctions library.
75 // It moves the axis back and forth based on std::rand().
76 std::cout << "Submitting task..." << std::endl;
77 RTTaskCreationParameters params("RandomWalk");
79 params.Period = 5;
80 std::unique_ptr<RTTask> task(manager->TaskSubmit(params));
81
82 // Wait for the task to run for a bit
83 task->ExecutionCountAbsoluteWait(50, 500);
84
85 // Get the counter global tag to see if the task ran correctly
86 std::cout << "Getting counter global tag..." << std::endl;
87 FirmwareValue counter = manager->GlobalValueGet("counter");
88 if (counter.Int64 <= 0)
89 {
90 // The task did not run correctly
91 exitCode = -1;
92 std::cout << "Counter is not greater than 0. The task did not run correctly." << std::endl;
93 }
94 else
95 {
96 // The task ran correctly
97 exitCode = 0;
98 std::cout << "Counter: " << counter.Int64 << std::endl;
99 }
100
101 // The average global tag is the position the axis should be at if the RandomWalk task is working correctly
102 FirmwareValue average = manager->GlobalValueGet("average");
103 std::cout << "Average: " << average.Double << std::endl;
104
105 // Print the final axis position
106 std::cout << "Axis position: " << axis->CommandPositionGet() << std::endl;
107
108 task->Stop();
109 axis->MotionDoneWait(TIMEOUT_MS); // The axis might still be moving from the task, so wait for it to finish
110 axis->AmpEnableSet(false);
112 }
113 catch (const std::exception& e)
114 {
115 std::cerr << e.what() << std::endl;
116 }
117 // Shutdown the task manager firmware process
118 if (manager != nullptr)
119 {
120 manager->Shutdown();
121 }
122
123 // Clean up the controller and any other objects as needed
124 SampleAppsHelper::Cleanup(controller);
125
126 // Delete the controller as the program exits to ensure memory is deallocated in the correct order
127 controller->Delete();
128
129 // Print a message to indicate the sample app has finished and if it was successful or not
130 SampleAppsHelper::PrintFooter(SAMPLE_APP_NAME, exitCode);
131
132 return exitCode;
133}
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,...
Definition rsi.h:5680
Axis * AxisGet(int32_t axisNumber)
AxisGet returns a pointer to an Axis object and initializes its internals.
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
void AmpEnableSet(bool enable)
Enable all amplifiers.
int32_t MotionDoneWait()
Waits for a move to complete.
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
double Double
Double precision (64-bit) floating-point.
Definition rsi.h:477