Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
frankaGripper.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  * Franka robot tool.
33  *
34 *****************************************************************************/
35 
41 #include <iostream>
42 
43 #include <visp3/core/vpConfig.h>
44 #include <visp3/robot/vpRobotFranka.h>
45 
46 #if defined(VISP_HAVE_FRANKA)
47 
48 typedef enum
49 {
50  Gripper_Home,
51  Gripper_Open,
52  Gripper_Close,
53  Gripper_Grasp,
54  Gripper_Release,
55  Gripper_Test,
56  Gripper_None
57 } GripperState_t;
58 
59 int main(int argc, char **argv)
60 {
61 #ifdef ENABLE_VISP_NAMESPACE
62  using namespace VISP_NAMESPACE_NAME;
63 #endif
64 
65  std::string opt_robot_ip = "192.168.1.1";
66  double opt_grasping_width = 0.;
67  GripperState_t opt_gripper_state = Gripper_None;
68 
69  for (int i = 1; i < argc; i++) {
70  if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
71  opt_robot_ip = std::string(argv[i + 1]);
72  }
73  else if (std::string(argv[i]) == "--home") {
74  opt_gripper_state = Gripper_Home;
75  }
76  else if (std::string(argv[i]) == "--open") {
77  opt_gripper_state = Gripper_Open;
78  }
79  else if (std::string(argv[i]) == "--close") {
80  opt_gripper_state = Gripper_Close;
81  }
82  else if (std::string(argv[i]) == "--grasp" && i + 1 < argc) {
83  opt_gripper_state = Gripper_Grasp;
84  opt_grasping_width = std::atof(argv[i + 1]);
85  }
86  else if (std::string(argv[i]) == "--test" && i + 1 < argc) {
87  opt_gripper_state = Gripper_Test;
88  opt_grasping_width = std::atof(argv[i + 1]);
89  }
90  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
91  std::cout << "Control Panda gripper." << std::endl;
92  std::cout << argv[0] << " [--ip <default " << opt_robot_ip
93  << ">] [--home] [--open] [--close] [--grasp <object width in meter>] [--release] [--test <object width "
94  "in meter>] [--help] [-h]\n"
95  << std::endl;
96  std::cout << "Example to grasp a 4cm width object by first opening the gripper, then grasping the object :\n"
97  << argv[0] << " --ip 192.168.100.1 --open\n"
98  << argv[0] << " --ip 192.168.100.1 --grasp 0.04\n"
99  << std::endl;
100 
101  return EXIT_SUCCESS;
102  }
103  }
104 
105  if (opt_gripper_state == Gripper_None) {
106  std::cout << "Specify which action you want to achieve. Run \"" << argv[0]
107  << " --help\" to see how to use this binary." << std::endl;
108  return EXIT_SUCCESS;
109  }
110  else if ((opt_gripper_state == Gripper_Grasp || opt_gripper_state == Gripper_Test) && opt_grasping_width <= 0) {
111  std::cout << "Object with in meter should be > 0. Run \"" << argv[0] << " --help\" to see how to use this binary."
112  << std::endl;
113  return EXIT_SUCCESS;
114  }
115 
116  vpRobotFranka robot;
117 
118  try {
119  robot.connect(opt_robot_ip);
120 
121  if (opt_gripper_state == Gripper_Home) {
122  std::cout << "Gripper homing..." << std::endl;
123  robot.gripperHoming();
124  }
125  else if (opt_gripper_state == Gripper_Close) {
126  std::cout << "Gripper closing..." << std::endl;
127  robot.gripperClose();
128  }
129  else if (opt_gripper_state == Gripper_Open) {
130  std::cout << "Gripper opening..." << std::endl;
131  robot.gripperOpen();
132  }
133  else if (opt_gripper_state == Gripper_Grasp) {
134  std::cout << "Gripper grasp " << opt_grasping_width << "m object width..." << std::endl;
135  robot.gripperGrasp(opt_grasping_width);
136  }
137  else if (opt_gripper_state == Gripper_Release) {
138  std::cout << "Gripper release object..." << std::endl;
139  robot.gripperRelease();
140  }
141  else if (opt_gripper_state == Gripper_Test) {
142  std::cout << "Test gripper performing the following actions:" << std::endl;
143  std::cout << "- Gripper homing..." << std::endl;
144  robot.gripperHoming();
145  vpTime::sleepMs(1000);
146  std::cout << "- Gripper closing..." << std::endl;
147  robot.gripperClose();
148  vpTime::sleepMs(1000);
149  std::cout << "- Gripper open 5cm..." << std::endl;
150  robot.gripperMove(0.05);
151  vpTime::sleepMs(3000);
152  std::cout << "- Gripper opening..." << std::endl;
153  robot.gripperOpen();
154  vpTime::sleepMs(3000);
155  std::cout << "- Gripper grasp " << opt_grasping_width << "m object width..." << std::endl;
156  robot.gripperGrasp(opt_grasping_width);
157  vpTime::sleepMs(3000);
158  std::cout << "- Gripper release object..." << std::endl;
159  robot.gripperRelease();
160  }
161  std::cout << "The end!" << std::endl;
162  }
163  catch (const vpException &e) {
164  std::cout << "ViSP exception: " << e.what() << std::endl;
165  return EXIT_FAILURE;
166  }
167  catch (const franka::NetworkException &e) {
168  std::cout << "Franka network exception: " << e.what() << std::endl;
169  std::cout << "Check if you are connected to the Franka robot"
170  << " or if you specified the right IP using --ip command line option set by default to 192.168.1.1. "
171  << std::endl;
172  return EXIT_FAILURE;
173  }
174  catch (const std::exception &e) {
175  std::cout << "Franka exception: " << e.what() << std::endl;
176  return EXIT_FAILURE;
177  }
178 
179  return EXIT_SUCCESS;
180 }
181 #else
182 int main()
183 {
184 #if !defined(VISP_HAVE_FRANKA)
185  std::cout << "Install libfranka 3rd party, configure and build again ViSP to use this example..." << std::endl;
186 #endif
187  return EXIT_SUCCESS;
188 }
189 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * what() const
Definition: vpException.cpp:71
VISP_EXPORT void sleepMs(double t)