Skip to content

Commit db5a2d0

Browse files
committed
Adding function to interact with the robot through the dashboard
Adding short function to make it easier to: - Power off - Power on - Brake release - Load a program - Play - Pause - Stop - Close popup - Close safety popup - Restart safety - Unlock protective stop - Shutdown
1 parent f43c618 commit db5a2d0

File tree

5 files changed

+379
-5
lines changed

5 files changed

+379
-5
lines changed

examples/dashboard_example.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2+
3+
// -- BEGIN LICENSE BLOCK ----------------------------------------------
4+
// Copyright 2022 Universal Robots A/S
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// -- END LICENSE BLOCK ------------------------------------------------
19+
20+
#include <ur_client_library/log.h>
21+
#include <ur_client_library/ur/dashboard_client.h>
22+
23+
#include <iostream>
24+
#include <memory>
25+
26+
using namespace urcl;
27+
28+
// In a real-world example it would be better to get those values from command line parameters / a
29+
// better configuration system such as Boost.Program_options
30+
const std::string DEFAULT_ROBOT_IP = "127.0.0.1";
31+
32+
std::unique_ptr<DashboardClient> my_dashboard;
33+
34+
// We need a callback function to register. See UrDriver's parameters for details.
35+
void handleRobotProgramState(bool program_running)
36+
{
37+
// Print the text in green so we see it better
38+
std::cout << "\033[1;32mProgram running: " << std::boolalpha << program_running << "\033[0m\n" << std::endl;
39+
}
40+
41+
int main(int argc, char* argv[])
42+
{
43+
urcl::setLogLevel(urcl::LogLevel::DEBUG);
44+
45+
// Parse the ip arguments if given
46+
std::string robot_ip = DEFAULT_ROBOT_IP;
47+
if (argc > 1)
48+
{
49+
robot_ip = std::string(argv[1]);
50+
}
51+
52+
// Making the robot ready for the program by:
53+
// Connect the the robot Dashboard
54+
my_dashboard.reset(new DashboardClient(robot_ip));
55+
if (!my_dashboard->connect())
56+
{
57+
URCL_LOG_ERROR("Could not connect to dashboard");
58+
return 1;
59+
}
60+
61+
if (!my_dashboard->commandPowerOff())
62+
{
63+
URCL_LOG_ERROR("Could not send power off");
64+
return 1;
65+
}
66+
67+
// Power it on
68+
if (!my_dashboard->commandPowerOn())
69+
{
70+
URCL_LOG_ERROR("Could not send Power on command");
71+
return 1;
72+
}
73+
74+
// Release the brakes
75+
if (!my_dashboard->commandBreakeRelease())
76+
{
77+
URCL_LOG_ERROR("Could not send BreakeRelease command");
78+
return 1;
79+
}
80+
81+
// Load existing program
82+
const std::string program_file_name_to_be_loaded("wait_program.urp");
83+
if (!my_dashboard->commandLoadProgram(program_file_name_to_be_loaded))
84+
{
85+
URCL_LOG_ERROR("Could not load %s program", program_file_name_to_be_loaded.c_str());
86+
return 1;
87+
}
88+
89+
// Play loaded program
90+
if (!my_dashboard->commandPlay())
91+
{
92+
URCL_LOG_ERROR("Could not play program");
93+
return 1;
94+
}
95+
96+
// Pause running program
97+
if (!my_dashboard->commandPause())
98+
{
99+
URCL_LOG_ERROR("Could not pause program");
100+
return 1;
101+
}
102+
103+
// Play loaded program
104+
if (!my_dashboard->commandPlay())
105+
{
106+
URCL_LOG_ERROR("Could not play program");
107+
return 1;
108+
}
109+
110+
// Stop program
111+
if (!my_dashboard->commandStop())
112+
{
113+
URCL_LOG_ERROR("Could not stop program");
114+
return 1;
115+
}
116+
117+
// Power it off
118+
if (!my_dashboard->commandPowerOff())
119+
{
120+
URCL_LOG_ERROR("Could not send Power off command");
121+
return 1;
122+
}
123+
124+
// Now the robot is ready to receive a program
125+
126+
return 0;
127+
}

include/ur_client_library/ur/dashboard_client.h

Lines changed: 126 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,131 @@ class DashboardClient : public comm::TCPSocket
7878
*/
7979
std::string sendAndReceive(const std::string& command);
8080

81+
/*!
82+
* \brief Sends command and compare it with the expected answer
83+
*
84+
* \param command Command that will be sent to the server. It is important, that the command sent is finished with a
85+
* '\n' (newline) so it will be processed by the server.
86+
* \param expected Expected replay
87+
*
88+
* \return True if the reply to the command is as expected
89+
*/
90+
bool sendRequest(const std::string& command, const std::string& expected);
91+
92+
/*!
93+
* \brief brief Sends a command and wait until it returns the expected answer
94+
*
95+
* \param command Command that will be sent to the server
96+
* \param expected Expected replay
97+
* \param timeout Timeout time in seconds
98+
*
99+
* \return True if the reply was as expected within the timeout time
100+
*/
101+
bool waitForReply(const std::string& command, const std::string& expected, double timeout = 30.0);
102+
103+
/*!
104+
* \brief Keep Sending the requesting Command and wait until it returns the expected answer
105+
*
106+
* \param requestCommand Request command that will be sent to the server
107+
* \param requestExpectedResponse The expected reply to the request
108+
* \param waitRequest The status request
109+
* \param waitExpectedResponse The expected reply on the status
110+
* \param timeout Timeout time in seconds
111+
*
112+
* \return True when both the requested command was receive with the expected reply as well as the resulting status
113+
* also is as expected within the timeout time
114+
*/
115+
bool retryCommand(const std::string& requestCommand, const std::string& requestExpectedResponse,
116+
const std::string& waitRequest, const std::string& waitExpectedResponse, unsigned int timeout);
117+
118+
/*!
119+
* \brief Send Power off command
120+
*
121+
* \return True succeeded
122+
*/
123+
bool commandPowerOff();
124+
125+
/*!
126+
* \brief Send Power on command
127+
*
128+
* \param timeout Timeout in seconds
129+
*
130+
* \return True succeeded
131+
*/
132+
bool commandPowerOn(unsigned int timeout = 1200);
133+
134+
/*!
135+
* \brief Send Brake release command
136+
*
137+
* \return True succeeded
138+
*/
139+
bool commandBreakeRelease();
140+
141+
/*!
142+
* \brief Send Load program command
143+
*
144+
* \param program_file_name The urp program file name with the urp extension
145+
*
146+
* \return True succeeded
147+
*/
148+
bool commandLoadProgram(const std::string& program_file_name);
149+
150+
/*!
151+
* \brief Send Play program command
152+
*
153+
* \return True succeeded
154+
*/
155+
bool commandPlay();
156+
157+
/*!
158+
* \brief Send Pause program command
159+
*
160+
* \return True succeeded
161+
*/
162+
bool commandPause();
163+
164+
/*!
165+
* \brief Send Stop program command
166+
*
167+
* \return True succeeded
168+
*/
169+
bool commandStop();
170+
171+
/*!
172+
* \brief Send Close popup command
173+
*
174+
* \return True succeeded
175+
*/
176+
bool commandClosePopup();
177+
178+
/*!
179+
* \brief Send Close safety popup command
180+
*
181+
* \return True succeeded
182+
*/
183+
bool commandCloseSafetyPopup();
184+
185+
/*!
186+
* \brief Send Restart Safety command
187+
*
188+
* \return True succeeded
189+
*/
190+
bool commandRestartSafety();
191+
192+
/*!
193+
* \brief Send Unlock Protective stop popup command
194+
*
195+
* \return True succeeded
196+
*/
197+
bool commandUnlockProtectiveStop();
198+
199+
/*!
200+
* \brief Send Shutdown command
201+
*
202+
* \return True succeeded
203+
*/
204+
bool commandShutdown();
205+
81206
protected:
82207
virtual bool open(int socket_fd, struct sockaddr* address, size_t address_len)
83208
{
@@ -87,11 +212,7 @@ class DashboardClient : public comm::TCPSocket
87212
private:
88213
bool send(const std::string& text);
89214
std::string read();
90-
91-
void rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
92-
{
93-
str.erase(str.find_last_not_of(chars) + 1);
94-
}
215+
void rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
95216

96217
std::string host_;
97218
int port_;

0 commit comments

Comments
 (0)