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 <optional>
19#include <thread>
20#include <chrono>
21
22using namespace RSI::RapidCode; // Import the RapidCode namespace
23using namespace RSI::RapidCode::RealTimeTasks; // Import the RealTimeTasks namespace
24
25int main()
26{
27 const std::string SAMPLE_APP_NAME = "Real-Time Tasks: Random Walk";
28
29 // Print a start message to indicate that the sample app has started
30 SampleAppsHelper::PrintHeader(SAMPLE_APP_NAME);
31
32 /* CONSTANTS */
33 // *NOTICE* The following constants must be configured before attempting to run with hardware.
34 const int NUM_AXES = 1; // The number of axes to configure, default is 0 for this template
35 const int TIMEOUT_MS = 5000; // Timeout for waiting for the task to execute once
36
37 /* RAPIDCODE INITIALIZATION */
38
39 // Create the controller
41 MotionController *controller = MotionController::Create(&params);
42
43 std::optional<RTTaskManager> manager;
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 = RTTaskManager::Create(parameters);
67 SampleAppsHelper::CheckErrors(&manager.value());
68
69 // Call the initialization task to initialize the global variables and get pointers to RapidCode objects
70 // in the RTTaskFunctions library. This needs to be called before any task that uses RapidCode objects.
71 RTTaskHelper::InitializeRTTaskObjects(manager.value());
72
73 // Tell the task manager the name of the function to run as a task, the name
74 // of the library that contains the function, and the directory where the
75 // library is located. The RandomWalk function is defined in the RTTaskFunctions library.
76 // It moves the axis back and forth based on std::rand().
77 std::cout << "Submitting task..." << std::endl;
78 RTTaskCreationParameters params("RandomWalk");
80 params.Period = 5;
81 RTTask task = manager->TaskSubmit(params);
82
83 // Wait for the task to run for a bit
84 task.ExecutionCountAbsoluteWait(50, 500);
85
86 // Get the counter global tag to see if the task ran correctly
87 std::cout << "Getting counter global tag..." << std::endl;
88 FirmwareValue counter = manager->GlobalValueGet("counter");
89 if (counter.Int64 <= 0)
90 {
91 // The task did not run correctly
92 exitCode = -1;
93 std::cout << "Counter is not greater than 0. The task did not run correctly." << std::endl;
94 }
95 else
96 {
97 // The task ran correctly
98 exitCode = 0;
99 std::cout << "Counter: " << counter.Int64 << std::endl;
100 }
101
102 // The average global tag is the position the axis should be at if the RandomWalk task is working correctly
103 FirmwareValue average = manager->GlobalValueGet("average");
104 std::cout << "Average: " << average.Double << std::endl;
105
106 // Print the final axis position
107 std::cout << "Axis position: " << axis->CommandPositionGet() << std::endl;
108
109 task.Stop();
110 axis->MotionDoneWait(TIMEOUT_MS); // The axis might still be moving from the task, so wait for it to finish
111 axis->AmpEnableSet(false);
113 }
114 catch (const std::exception& e)
115 {
116 std::cerr << e.what() << std::endl;
117 }
118
119 // Shutdown the task manager firmware process
120 if (manager.has_value())
121 {
122 manager->Shutdown();
123 }
124
125 // Clean up the controller and any other objects as needed
126 SampleAppsHelper::Cleanup(controller);
127
128 // Delete the controller as the program exits to ensure memory is deallocated in the correct order
129 controller->Delete();
130
131 // Print a message to indicate the sample app has finished and if it was successful or not
132 SampleAppsHelper::PrintFooter(SAMPLE_APP_NAME, exitCode);
133
134 return exitCode;
135}
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:5706
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.
int64_t ExecutionCountAbsoluteWait(int64_t count=ExecutionCountDefault, int32_t timeoutMs=ExecutionCountWaitTimeoutMillisecondsDefault)
Wait for the task to reach a specific execution count.
Interface for controlling and monitoring a single real-time task. See RTTaskManager::TaskSubmit and R...
Definition rttask.h:414
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:866
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