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