Visual Servoing Platform  version 3.6.1 under development (2024-04-24)
testUniversalRobotsGetData.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test Universal Robots behavior
33  *
34 *****************************************************************************/
35 
42 #include <iostream>
43 
44 #include <visp3/core/vpConfig.h>
45 
46 #if defined(VISP_HAVE_UR_RTDE)
47 
48 #include <visp3/robot/vpRobotUniversalRobots.h>
49 
50 int main(int argc, char **argv)
51 {
52  std::string robot_ip = "192.168.0.100";
53 
54  for (int i = 1; i < argc; i++) {
55  if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
56  robot_ip = std::string(argv[i + 1]);
57  }
58  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
59  std::cout << argv[0] << " [--ip " << robot_ip << "] [--help] [-h]"
60  << "\n";
61  return EXIT_SUCCESS;
62  }
63  }
64 
65  try {
66  std::cout << "-- Start test 1/3" << std::endl;
68  robot.connect(robot_ip);
69 
70  std::shared_ptr<ur_rtde::RTDEReceiveInterface> rtde_receive_interface = robot.getRTDEReceiveInterfaceHandler();
71  std::shared_ptr<ur_rtde::DashboardClient> db_client = robot.getDashboardClientHandler();
72 
73  std::cout << "Robot connected : " << (rtde_receive_interface->isConnected() ? "yes" : "no") << std::endl;
74  std::cout << "Robot mode : " << rtde_receive_interface->getRobotMode() << std::endl;
75  std::cout << "Robot model : " << db_client->getRobotModel() << std::endl;
76  std::cout << "PolyScope version: " << db_client->polyscopeVersion() << std::endl;
77  robot.disconnect();
78  }
79  catch (const vpException &e) {
80  std::cout << "ViSP exception: " << e.what() << std::endl;
81  return EXIT_FAILURE;
82  }
83  catch (const std::exception &e) {
84  std::cout << "ur_rtde exception: " << e.what() << std::endl;
85  return EXIT_FAILURE;
86  }
87 
88  try {
89  std::cout << "-- Start test 2/3" << std::endl;
91  robot.connect(robot_ip);
92 
93  // Next test is only done when robot powered on
94  // See robot mode doc:
95  // https://sdurobotics.gitlab.io/ur_rtde/api/api.html#_CPPv4N7ur_rtde20RTDEReceiveInterface12getRobotModeEv
96  if (robot.getRobotMode() >= 4) {
97  vpColVector q, q_init;
98 
99  for (unsigned i = 0; i < 10; i++) {
100  robot.getPosition(vpRobot::JOINT_STATE, q_init);
101  q = q_init;
102  std::cout << "Joint position [deg]: " << q.rad2deg().t() << std::endl;
103  vpTime::wait(10);
104  }
105 
106  vpPoseVector fPe;
107  robot.getPosition(vpRobot::END_EFFECTOR_FRAME, fPe);
108  std::cout << "fMe pose vector: " << fPe.t() << std::endl;
109  vpHomogeneousMatrix fMe_1(fPe);
110  std::cout << "fMe pose matrix: \n" << fMe_1 << std::endl;
111  vpColVector position;
112  robot.getPosition(vpRobot::END_EFFECTOR_FRAME, position);
113  for (size_t i = 0; i < fPe.size(); i++) {
114  if (!vpMath::equal(fPe[i], position[i])) {
115  std::cout << "Wrong end-effector pose returned by getPosition(). Test failed" << std::endl;
116  return EXIT_FAILURE;
117  }
118  }
119  vpHomogeneousMatrix fMe_2 = robot.get_fMe();
120  std::cout << "fMe pose matrix: \n" << fMe_2 << std::endl;
121  for (size_t i = 0; i < fMe_2.size(); i++) {
122  if (!vpMath::equal(fMe_1.data[i], fMe_2.data[i])) {
123  std::cout << "Wrong end-effector pose returned by get_fMe(). Test failed" << std::endl;
124  return EXIT_FAILURE;
125  }
126  }
127 
128  // Next test is only done when brakes are released
129  // See robot mode doc:
130  // https://sdurobotics.gitlab.io/ur_rtde/api/api.html#_CPPv4N7ur_rtde20RTDEReceiveInterface12getRobotModeEv
131  if (robot.getRobotMode() == 7) {
132  vpHomogeneousMatrix fMe_3 = robot.get_fMe(q_init);
133  std::cout << "fMe pose matrix: \n" << fMe_3 << std::endl;
134  for (size_t i = 0; i < fMe_3.size(); i++) {
135  if (!vpMath::equal(fMe_2.data[i], fMe_3.data[i])) {
136  std::cout << "Wrong end-effector forward kinematics . Test failed" << std::endl;
137  return EXIT_FAILURE;
138  }
139  }
140  }
141 
142  vpPoseVector fPc;
143  robot.getPosition(vpRobot::TOOL_FRAME, fPc);
144  std::cout << "fMc pose vector: " << fPc.t() << std::endl;
145  std::cout << "fMc pose matrix: \n" << vpHomogeneousMatrix(fPc) << std::endl;
146  robot.getPosition(vpRobot::TOOL_FRAME, position);
147  for (size_t i = 0; i < fPc.size(); i++) {
148  if (!vpMath::equal(fPc[i], position[i])) {
149  std::cout << "Wrong tool pose. Test failed" << std::endl;
150  return EXIT_FAILURE;
151  }
152  }
153  }
154  else {
155  std::cout << "To proceed with this test you need to power on the robot" << std::endl;
156  }
157  }
158  catch (const vpException &e) {
159  std::cout << "ViSP exception: " << e.what() << std::endl;
160  return EXIT_FAILURE;
161  }
162  catch (const std::exception &e) {
163  std::cout << "ur_rtde exception: " << e.what() << std::endl;
164  return EXIT_FAILURE;
165  }
166 
167  try {
168  std::cout << "-- Start test 3/3" << std::endl;
169  vpRobotUniversalRobots robot(robot_ip);
170  robot.disconnect();
171  robot.connect(robot_ip);
172 
173  vpColVector eFe;
174  for (unsigned i = 0; i < 10; i++) {
175  robot.getForceTorque(vpRobot::END_EFFECTOR_FRAME, eFe);
176  std::cout << "End-effector force/torque: " << eFe.t() << std::endl;
177  vpTime::wait(10);
178  }
179 
180  vpColVector cFc;
181  for (unsigned i = 0; i < 10; i++) {
182  robot.getForceTorque(vpRobot::TOOL_FRAME, cFc);
183  std::cout << "Camera or tool frame force/torque: " << cFc.t() << std::endl;
184  vpTime::wait(10);
185  }
186  }
187  catch (const vpException &e) {
188  std::cout << "ViSP exception: " << e.what() << std::endl;
189  return EXIT_FAILURE;
190  }
191  catch (const std::exception &e) {
192  std::cout << "ur_rtde exception: " << e.what() << std::endl;
193  return EXIT_FAILURE;
194  }
195 
196  std::cout << "The end" << std::endl;
197  return EXIT_SUCCESS;
198 }
199 
200 #else
201 int main()
202 {
203  std::cout << "ViSP is not build with libur_rtde 3rd party used to control a robot from Universal Robots..."
204  << std::endl;
205 
206 }
207 #endif
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:139
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:339
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
vpColVector & rad2deg()
Definition: vpColVector.h:970
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const char * what() const
Definition: vpException.cpp:70
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:449
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:189
vpRowVector t() const
@ JOINT_STATE
Definition: vpRobot.h:80
@ TOOL_FRAME
Definition: vpRobot.h:84
@ END_EFFECTOR_FRAME
Definition: vpRobot.h:81
VISP_EXPORT int wait(double t0, double t)