Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
frankaGripper.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Franka robot tool.
32  */
33 
39 #include <iostream>
40 
41 #include <visp3/core/vpConfig.h>
42 #include <visp3/robot/vpRobotFranka.h>
43 
44 #if defined(VISP_HAVE_FRANKA)
45 
46 typedef enum
47 {
48  Gripper_Home,
49  Gripper_Open,
50  Gripper_Close,
51  Gripper_Grasp,
52  Gripper_Release,
53  Gripper_Test,
54  Gripper_None
55 } GripperState_t;
56 
57 int main(int argc, char **argv)
58 {
59 #ifdef ENABLE_VISP_NAMESPACE
60  using namespace VISP_NAMESPACE_NAME;
61 #endif
62 
63  std::string opt_robot_ip = "192.168.1.1";
64  double opt_grasping_width = 0.;
65  double opt_grasping_speed = 0.1;
66  double opt_grasping_force = 60;
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  ++i;
72  opt_robot_ip = std::string(argv[i]);
73  }
74  else if (std::string(argv[i]) == "--home") {
75  opt_gripper_state = Gripper_Home;
76  }
77  else if (std::string(argv[i]) == "--open") {
78  opt_gripper_state = Gripper_Open;
79  }
80  else if (std::string(argv[i]) == "--close") {
81  opt_gripper_state = Gripper_Close;
82  }
83  else if (std::string(argv[i]) == "--release") {
84  opt_gripper_state = Gripper_Release;
85  }
86  else if (std::string(argv[i]) == "--grasp" && i + 1 < argc) {
87  ++i;
88  opt_gripper_state = Gripper_Grasp;
89  opt_grasping_width = std::atof(argv[i]);
90  }
91  else if (std::string(argv[i]) == "--grasp-speed" && i + 1 < argc) {
92  ++i;
93  opt_grasping_speed = std::atof(argv[i]);
94  }
95  else if (std::string(argv[i]) == "--grasp-force" && i + 1 < argc) {
96  ++i;
97  opt_grasping_force = std::atof(argv[i]);
98  }
99  else if (std::string(argv[i]) == "--test" && i + 1 < argc) {
100  ++i;
101  opt_gripper_state = Gripper_Test;
102  opt_grasping_width = std::atof(argv[i]);
103  }
104  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
105  std::cout << "SYNOPSYS" << std::endl
106  << " " << argv[0]
107  << " [--ip <controller ip>]"
108  << " [--home]"
109  << " [--open]"
110  << " [--close]"
111  << " [--grasp <width>]"
112  << " [--grasp-speed <speed>]"
113  << " [--grasp-force <force>]"
114  << " [--release]"
115  << " [--test <width>]"
116  << " [--help] [-h]\n" << std::endl;
117  std::cout << "DESCRIPTION" << std::endl
118  << " Control Panda gripper." << std::endl
119  << std::endl
120  << " --ip <controller ip>" << std::endl
121  << " Franka controller ip address" << std::endl
122  << " Default: " << opt_robot_ip << std::endl
123  << std::endl
124  << " --home" << std::endl
125  << " Performs homing of the gripper." << std::endl
126  << std::endl
127  << " --open" << std::endl
128  << " Performs opening of the gripper." << std::endl
129  << std::endl
130  << " --close" << std::endl
131  << " Performs closing of the gripper." << std::endl
132  << std::endl
133  << " --grasp <object width>" << std::endl
134  << " Performs grasping of an object." << std::endl
135  << " Object width is to specify in [m]." << std::endl
136  << " Default width: " << opt_grasping_width << " [m]" << std::endl
137  << std::endl
138  << " --grasp-speed <speed>" << std::endl
139  << " Closing speed in [m/s] applied during grasping." << std::endl
140  << " Default: " << opt_grasping_speed << " [m/s]" << std::endl
141  << std::endl
142  << " --grasp-force <force>" << std::endl
143  << " Force in [N] applied during grasping." << std::endl
144  << " Default: " << opt_grasping_force << " [N]" << std::endl
145  << std::endl
146  << " --release" << std::endl
147  << " Release an object that is grasped." << std::endl
148  << std::endl
149  << " --test <object width>" << std::endl
150  << " Performs a gripper test on an object width specified in [m]." << std::endl
151  << " Default width: " << opt_grasping_width << " [m]" << std::endl
152  << std::endl
153  << " --help, -h" << std::endl
154  << " Print this helper message." << std::endl
155  << std::endl;
156  std::cout << "EXAMPLE" << std::endl
157  << " To grasp a 4cm width object by first opening the gripper, then grasping the object:\n"
158  << " " << argv[0] << " --ip 192.168.100.1 --open\n"
159  << " " << argv[0] << " --ip 192.168.100.1 --grasp 0.04\n"
160  << std::endl;
161 
162  return EXIT_SUCCESS;
163  }
164  }
165 
166  if (opt_gripper_state == Gripper_None) {
167  std::cout << "Specify which action you want to achieve. Run \"" << argv[0]
168  << " --help\" to see how to use this binary." << std::endl;
169  return EXIT_SUCCESS;
170  }
171  else if ((opt_gripper_state == Gripper_Grasp || opt_gripper_state == Gripper_Test) && opt_grasping_width <= 0) {
172  std::cout << "Object with in meter should be > 0. Run \"" << argv[0] << " --help\" to see how to use this binary."
173  << std::endl;
174  return EXIT_SUCCESS;
175  }
176 
177  vpRobotFranka robot;
178 
179  try {
180  robot.connect(opt_robot_ip);
181 
182  if (opt_gripper_state == Gripper_Home) {
183  std::cout << "Gripper homing..." << std::endl;
184  robot.gripperHoming();
185  }
186  else if (opt_gripper_state == Gripper_Close) {
187  std::cout << "Gripper closing..." << std::endl;
188  robot.gripperClose();
189  }
190  else if (opt_gripper_state == Gripper_Open) {
191  std::cout << "Gripper opening..." << std::endl;
192  robot.gripperOpen();
193  }
194  else if (opt_gripper_state == Gripper_Grasp) {
195  std::cout << "Gripper grasp " << opt_grasping_width << "m object width, with speed: " << opt_grasping_speed << " and force: " << opt_grasping_force << "N..." << std::endl;
196  robot.gripperGrasp(opt_grasping_width, opt_grasping_speed, opt_grasping_force);
197  }
198  else if (opt_gripper_state == Gripper_Release) {
199  std::cout << "Gripper release object..." << std::endl;
200  robot.gripperRelease();
201  }
202  else if (opt_gripper_state == Gripper_Test) {
203  std::cout << "Test gripper performing the following actions:" << std::endl;
204  std::cout << "- Gripper homing..." << std::endl;
205  robot.gripperHoming();
206  vpTime::sleepMs(1000);
207  std::cout << "- Gripper closing..." << std::endl;
208  robot.gripperClose();
209  vpTime::sleepMs(1000);
210  std::cout << "- Gripper open 5cm..." << std::endl;
211  robot.gripperMove(0.05);
212  vpTime::sleepMs(3000);
213  std::cout << "- Gripper opening..." << std::endl;
214  robot.gripperOpen();
215  vpTime::sleepMs(3000);
216  std::cout << "- Gripper grasp " << opt_grasping_width << "m object width, with speed: " << opt_grasping_speed << " and force: " << opt_grasping_force << "N..." << std::endl;
217  robot.gripperGrasp(opt_grasping_width, opt_grasping_speed, opt_grasping_force);
218  vpTime::sleepMs(3000);
219  std::cout << "- Gripper release object..." << std::endl;
220  robot.gripperRelease();
221  }
222  std::cout << "The end!" << std::endl;
223  }
224  catch (const vpException &e) {
225  std::cout << "ViSP exception: " << e.what() << std::endl;
226  return EXIT_FAILURE;
227  }
228  catch (const franka::NetworkException &e) {
229  std::cout << "Franka network exception: " << e.what() << std::endl;
230  std::cout << "Check if you are connected to the Franka robot"
231  << " or if you specified the right IP using --ip command line option set by default to 192.168.1.1. "
232  << std::endl;
233  return EXIT_FAILURE;
234  }
235  catch (const std::exception &e) {
236  std::cout << "Franka exception: " << e.what() << std::endl;
237  return EXIT_FAILURE;
238  }
239 
240  return EXIT_SUCCESS;
241 }
242 #else
243 int main()
244 {
245 #if !defined(VISP_HAVE_FRANKA)
246  std::cout << "Install libfranka 3rd party, configure and build again ViSP to use this example..." << std::endl;
247 #endif
248  return EXIT_SUCCESS;
249 }
250 #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)