Skip to content

Commit 837fd41

Browse files
committed
Updated examples to reflect changes to the primary client.
1 parent 3f00c2c commit 837fd41

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

examples/full_driver.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const std::string SCRIPT_FILE = "resources/external_control.urscript";
4444
const std::string OUTPUT_RECIPE = "examples/resources/rtde_output_recipe.txt";
4545
const std::string INPUT_RECIPE = "examples/resources/rtde_input_recipe.txt";
4646
const std::string CALIBRATION_CHECKSUM = "calib_12788084448423163542";
47+
const bool SIMULATED_ROBOT = true;
48+
bool g_program_running = false;
4749

4850
std::unique_ptr<UrDriver> g_my_driver;
4951
std::unique_ptr<DashboardClient> g_my_dashboard;
@@ -54,6 +56,7 @@ void handleRobotProgramState(bool program_running)
5456
{
5557
// Print the text in green so we see it better
5658
std::cout << "\033[1;32mProgram running: " << std::boolalpha << program_running << "\033[0m\n" << std::endl;
59+
g_program_running = program_running;
5760
}
5861

5962
int main(int argc, char* argv[])
@@ -109,12 +112,18 @@ int main(int argc, char* argv[])
109112
std::unique_ptr<ToolCommSetup> tool_comm_setup;
110113
const bool HEADLESS = true;
111114
g_my_driver.reset(new UrDriver(robot_ip, SCRIPT_FILE, OUTPUT_RECIPE, INPUT_RECIPE, &handleRobotProgramState, HEADLESS,
112-
std::move(tool_comm_setup), CALIBRATION_CHECKSUM));
115+
std::move(tool_comm_setup), CALIBRATION_CHECKSUM, SIMULATED_ROBOT));
113116

114117
// Once RTDE communication is started, we have to make sure to read from the interface buffer, as
115-
// otherwise we will get pipeline overflows. Therefor, do this directly before starting your main
118+
// otherwise we will get pipeline overflows. Therefore, do this directly before starting your main
116119
// loop.
117120

121+
// Wait for the program to run on the robot
122+
while (!g_program_running)
123+
{
124+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
125+
}
126+
118127
g_my_driver->startRTDECommunication();
119128

120129
double increment = 0.01;
@@ -157,7 +166,7 @@ int main(int argc, char* argv[])
157166
URCL_LOG_ERROR("Could not send joint command. Is the robot in remote control?");
158167
return 1;
159168
}
160-
URCL_LOG_DEBUG("data_pkg:\n%s", data_pkg->toString());
169+
URCL_LOG_DEBUG("data_pkg:\n%s", data_pkg->toString().c_str());
161170
}
162171
else
163172
{

examples/primary_pipeline.cpp

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727

2828
#include <ur_client_library/comm/pipeline.h>
2929
#include <ur_client_library/comm/producer.h>
30-
#include <ur_client_library/comm/shell_consumer.h>
3130
#include <ur_client_library/primary/primary_parser.h>
31+
#include <ur_client_library/primary/primary_client.h>
3232

3333
using namespace urcl;
3434

3535
// In a real-world example it would be better to get those values from command line parameters / a better configuration
3636
// system such as Boost.Program_options
3737
const std::string DEFAULT_ROBOT_IP = "192.168.56.101";
38+
const std::string CALIBRATION_CHECKSUM = "calib_12788084448423163542";
3839

3940
int main(int argc, char* argv[])
4041
{
41-
// Set the loglevel to info get print out the DH parameters
4242
urcl::setLogLevel(urcl::LogLevel::INFO);
4343

4444
// Parse the ip arguments if given
@@ -48,40 +48,42 @@ int main(int argc, char* argv[])
4848
robot_ip = std::string(argv[1]);
4949
}
5050

51-
// Parse how many seconds to run
52-
int second_to_run = -1;
53-
if (argc > 2)
54-
{
55-
second_to_run = std::stoi(argv[2]);
56-
}
57-
58-
// First of all, we need a stream that connects to the robot
59-
comm::URStream<primary_interface::PrimaryPackage> primary_stream(robot_ip, urcl::primary_interface::UR_PRIMARY_PORT);
60-
61-
// This will parse the primary packages
62-
primary_interface::PrimaryParser parser;
63-
64-
// The producer needs both, the stream and the parser to fully work
65-
comm::URProducer<primary_interface::PrimaryPackage> prod(primary_stream, parser);
66-
prod.setupProducer();
51+
// First of all, we need to create a primary client that connects to the robot
52+
primary_interface::PrimaryClient primary_client(robot_ip, CALIBRATION_CHECKSUM);
6753

68-
// The shell consumer will print the package contents to the shell
69-
std::unique_ptr<comm::IConsumer<primary_interface::PrimaryPackage>> consumer;
70-
consumer.reset(new comm::ShellConsumer<primary_interface::PrimaryPackage>());
54+
// Give time to get the client to connect
55+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
7156

72-
// The notifer will be called at some points during connection setup / loss. This isn't fully
73-
// implemented atm.
74-
comm::INotifier notifier;
75-
76-
// Now that we have all components, we can create and start the pipeline to run it all.
77-
comm::Pipeline<primary_interface::PrimaryPackage> pipeline(prod, consumer.get(), "Pipeline", notifier);
78-
pipeline.run();
79-
80-
// Package contents will be printed while not being interrupted
81-
// Note: Packages for which the parsing isn't implemented, will only get their raw bytes printed.
82-
do
57+
for (int i = 0; i < 10; ++i)
8358
{
84-
std::this_thread::sleep_for(std::chrono::seconds(second_to_run));
85-
} while (second_to_run < 0);
59+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
60+
// Create script program to send through client
61+
std::stringstream cmd;
62+
cmd.imbue(std::locale::classic()); // Make sure, decimal divider is actually '.'
63+
cmd << "sec setup():" << std::endl
64+
<< " textmsg(\"Command through primary interface complete " << i++ << "\")" << std::endl
65+
<< "end";
66+
std::string script_code = cmd.str();
67+
auto program_with_newline = script_code + '\n';
68+
// Send script
69+
primary_client.sendScript(program_with_newline);
70+
71+
try
72+
{
73+
URCL_LOG_INFO("Cartesian Information:\n%s", primary_client.getCartesianInfo()->toString().c_str());
74+
URCL_LOG_INFO("Calibration Hash:\n%s", primary_client.getCalibrationChecker()->getData()->toHash().c_str());
75+
URCL_LOG_INFO("Build Date:\n%s", primary_client.getVersionMessage()->build_date_.c_str());
76+
std::cout << primary_client.getJointData()->toString() << std::endl;
77+
std::stringstream os;
78+
os << primary_client.getJointData()->q_actual_;
79+
URCL_LOG_INFO("Joint Angles:\n%s", os.str().c_str());
80+
// getGlobalVariablesSetupMessage() will throw an exception if a program on the robot has not been started
81+
URCL_LOG_INFO("Global Variables:\n%s", primary_client.getGlobalVariablesSetupMessage()->variable_names_.c_str());
82+
}
83+
catch (const UrException& e)
84+
{
85+
URCL_LOG_WARN(e.what());
86+
}
87+
}
8688
return 0;
8789
}

0 commit comments

Comments
 (0)