8#include "SampleAppsHelper.h"
11#include "SampleApps.h"
15constexpr int MAX_NUM_ARGS = 256;
23static std::map<std::string, std::shared_ptr<SampleApp>> SampleAppNamesMap =
25 {
"hardware-limits", std::make_shared<HardwareLimits>()},
26 {
"memory", std::make_shared<Memory>()},
27 {
"motion-hold-released-by-software-address", std::make_shared<MotionHoldReleasedBySoftwareAddress>()},
28 {
"multiaxis-motion", std::make_shared<MultiaxisMotion>()},
29 {
"path-motion", std::make_shared<PathMotion>()},
30 {
"pvt-motion-multiaxis", std::make_shared<PVTmotionMultiAxis>()},
31 {
"single-axis-sync-outputs", std::make_shared<SingleAxisSyncOutputs>()},
32 {
"sync-output-with-motion", std::make_shared<SyncOutputWithMotion>()},
33 {
"update-buffer-points", std::make_shared<UpdateBufferPoints>()},
34 {
"user-limit-digital-input-action", std::make_shared<UserLimitDigialInputAction>()}
38static void ListSampleApps()
40 std::cout << std::endl <<
"Available Sample Apps:" << std::endl;
43 std::shared_ptr<SampleApp> sampleApp;
44 for (
auto const& sampleAppName : SampleAppNamesMap)
46 std::cout <<
" - " << sampleAppName.first << std::endl;
51static void PrintHardwareHelp()
53 std::cout << std::endl <<
"To run a SampleApp with hardware you must complete the following steps:" << std::endl;
54 std::cout <<
" 1. In the source file for the SampleApp, set the axis configuration parameters." << std::endl;
55 std::cout <<
" 2. Still in the source file, set the 'USE_HARDWARE' flag to true in the class's run method." << std::endl;
56 std::cout <<
" 3. Rebuild this project." << std::endl;
57 std::cout <<
" 4. Ensure that you have taken all proper safety precautions appropriate to your particular hardware setup." << std::endl;
58 std::cout <<
" 5. Relaunch 'SampleAppsCPP.exe' and run the SampleApp to observe the results." << std::endl;
62static void PrintHelp()
64 std::cout << std::endl <<
"Enter the name of a Sample App to run it." << std::endl;
65 std::cout <<
" - Running on hardware requires additional configuration. Type 'hardware' for more information." << std::endl;
66 std::cout << std::endl <<
"You may also use 'runall' to run all supported Sample Apps." << std::endl;
67 std::cout << std::endl <<
"Type 'l' or 'list' to list the available Sample Apps." << std::endl;
70static void PrintWelcomeMessage()
72 std::cout <<
"Welcome to the RapidCode C++ Sample Apps!" << std::endl << std::endl;
73 std::cout <<
"This program allows you to run the sample apps that are included with the RapidCode API." << std::endl;
74 std::cout << std::endl <<
"WARNING: The sample apps are meant to be used as a reference and may not contain all of the logic and safety features that your application requires." << std::endl;
75 std::cout <<
"Additionally, in order to mitigate unexpected behavior, running a sample app will reset your RMP controller, clearing any previous configuration." << std::endl;
76 std::cout << std::endl <<
"It is recommended that you do not have any hardware connected when running the sample apps for the first time." << std::endl;
77 std::cout <<
"Before you attempt to run a sample app with hardware, please ensure that you have completed the necessary configuration steps and reviewed the source files." << std::endl;
78 std::cout <<
"Type 'hardware' for more information." << std::endl;
83static std::shared_ptr<SampleApp> GetSampleApp(std::string sampleAppName)
86 std::transform(sampleAppName.begin(), sampleAppName.end(), sampleAppName.begin(),
87 [](
unsigned char c) { return std::tolower(c); });
90 if (SampleAppNamesMap.find(sampleAppName) == SampleAppNamesMap.end())
92 throw std::invalid_argument(
"Error: Invalid Sample App name.");
96 return SampleAppNamesMap.at(sampleAppName);
100static int RunSampleApp(std::string sampleAppName)
106 std::shared_ptr<SampleApp> sampleApp;
109 sampleApp = GetSampleApp(sampleAppName);
111 catch (std::invalid_argument& e)
113 std::cout << std::endl << e.what() << std::endl;
119 std::cout << std::endl <<
"--------------------------------------------------" << std::endl;
120 std::cout <<
"Running " << sampleAppName <<
"..." << std::endl;
121 std::cout <<
"--------------------------------------------------" << std::endl << std::endl;
124 appExitCode = sampleApp->Run();
126 catch (std::exception& e)
128 std::cout << std::endl <<
"Error: " << e.what() << std::endl;
132 std::cout << std::endl <<
"--------------------------------------------------" << std::endl;
135 if (appExitCode == 0)
137 std::cout <<
"Sample App " << sampleAppName <<
" completed successfully." << std::endl;
141 std::cout <<
"Sample App " << sampleAppName <<
" completed with errors." << std::endl;
144 std::cout <<
"--------------------------------------------------" << std::endl << std::endl;
152 int runallExitCode = 0;
154 std::vector<std::string> failedSampleApps;
155 for (
auto const& sampleAppName : SampleAppNamesMap)
157 appExitCode = RunSampleApp(sampleAppName.first);
160 if (appExitCode != 0)
162 failedSampleApps.push_back(sampleAppName.first);
166 if (appExitCode < runallExitCode)
168 runallExitCode = appExitCode;
173 if (failedSampleApps.size() > 0)
175 std::cout << std::endl <<
"The following Sample Apps failed:" << std::endl;
176 for (
auto const& sampleAppName : failedSampleApps)
178 std::cout <<
" - " << sampleAppName << std::endl;
182 return runallExitCode;
186static std::vector<std::string> GetInput()
189 std::cout << std::endl <<
"Enter a Sample App name or 'runall'. Type 'h' or 'help' for more info. Type 'q' or 'quit' to end program." << std::endl;
190 std::cout << std::endl <<
">> ";
191 std::getline(std::cin, input);
193 std::istringstream streamInput(input);
195 std::vector<std::string> args;
196 while (std::getline(streamInput, token,
' '))
198 args.push_back(token);
204static int ProcessInput(std::vector<std::string> args)
207 if (args.size() < 1 ||
216 if (args[0] ==
"hardware")
223 if (args[0] ==
"l" || args[0] ==
"list")
230 if (args[0] ==
"q" || args[0] ==
"quit")
237 if (args[0] ==
"runall")
243 return RunSampleApp(args[0]);
247std::vector<std::string> ParseArgs(
int argc,
char* argv[])
249 std::vector<std::string> args;
251 for (
int i = 1; i < argc; i++)
255 if (arg ==
"--no-input")
268int main(
int argc,
char* argv[])
271 if (argc > MAX_NUM_ARGS)
273 std::cout <<
"Error: Too many arguments." << std::endl;
278 std::vector<std::string> args;
281 args = ParseArgs(argc, argv);
282 exitCode = ProcessInput(args);
292 PrintWelcomeMessage();
298 exitCode = ProcessInput(args);