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