Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
servoKinovaJacoJoint.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  * Example with Kinova Jaco robot.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
46 #include <iostream>
47 
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/robot/vpRobotKinova.h>
50 
51 int main(int argc, char*argv[])
52 {
53 #ifdef VISP_HAVE_JACOSDK
54  std::string opt_plugin_path = "./";
56  bool opt_verbose = false;
57  unsigned int opt_dof = 6; // Consider a 6 DoF robot by default
58 
59  for (int i = 1; i < argc; i++) {
60  if (std::string(argv[i]) == "--plugin" && i + 1 < argc) {
61  opt_plugin_path = std::string(argv[i + 1]);;
62  }
63  if ((std::string(argv[i]) == "--command_layer" || std::string(argv[i]) == "-l") && i + 1 < argc) {
64  if (std::string(argv[i + 1]) == "usb") {
65  opt_command_layer = vpRobotKinova::CMD_LAYER_USB;
66  }
67  else if (std::string(argv[i + 1]) == "ethernet") {
68  opt_command_layer = vpRobotKinova::CMD_LAYER_ETHERNET;
69  }
70  else {
71  opt_command_layer = vpRobotKinova::CMD_LAYER_UNSET;
72  }
73  }
74  else if (std::string(argv[i]) == "--dof") {
75  opt_dof = static_cast<unsigned int>(std::atoi(argv[i + 1]));
76  }
77  else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
78  opt_verbose = true;
79  }
80  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
81  std::cout << "SYNOPSYS" << std::endl
82  << " " << argv[0] << " [--plugin <path>] [--command_layer <name>] [--dof <4,6,7>] "
83  << "[--verbose] [--help] [-v] [-h]\n" << std::endl;
84  std::cout << "DESCRIPTION" << std::endl
85  << " --plugin <path>" << std::endl
86  << " Path to Jaco SDK .so or .dll plugin location. Default: \"./\"." << std::endl << std::endl
87  << " --command_layer <name>, -l <name>" << std::endl
88  << " Command layer name, either \"usb\" or \"ethernet\"." << std::endl << std::endl
89  << " --dof" << std::endl
90  << " Degrees of freedom. Possible values are 4, 6 or 7. Default value: 6." << std::endl << std::endl
91  << " --verbose, -v" << std::endl
92  << " Enable verbose mode to print addition information." << std::endl << std::endl
93  << " --help, -h" << std::endl
94  << " Print this helper message." << std::endl << std::endl;
95  std::cout << "EXAMPLE" << std::endl
96 #ifdef __linux__
97  << " " << argv[0] << " --plugin /opt/JACO-SDK/API"
98 #elif _WIN32
99  << " " << argv[0] << " --plugin \"C:\\Program Files(x86)\\JACO - SDK\\API\\x64\""
100 #endif
101  << " --command_layer usb" << std::endl << std::endl;
102 
103  return EXIT_SUCCESS;
104  }
105  }
106 
107  try {
108  vpRobotKinova robot;
109  robot.setDoF(opt_dof);
110  robot.setVerbose(opt_verbose);
111  robot.setPluginLocation(opt_plugin_path);
112  robot.setCommandLayer(opt_command_layer);
113 
114  int n_devices = robot.connect();
115 
116  if (!n_devices) {
117  std::cout << "There is no Kinova device connected." << std::endl;
118  return EXIT_SUCCESS;
119  }
120 
121  // Move robot to home position
122  robot.homing();
123 
124  // Control robot in joint velocity
126  vpColVector qdot(opt_dof);
127  qdot[opt_dof - 1] = vpMath::rad(20); // send 20 deg/s on last joint
128 
129  // Sent new joint velocities each 5 ms
130  for (unsigned int i = 0; i < 300; i++) {
131  // We send the velocity vector as long as we want the robot to move along that vector
132  robot.setVelocity(vpRobot::JOINT_STATE, qdot);
133  vpTime::wait(5);
134  }
135 
136  // Control robot in joint position
138  vpColVector q, q1, q2;
139 
140  // Move robot to home position
141  robot.homing();
142 
143  // Get current joint position
145 
146  // Move to first joint position
147  q1 = q;
148  q1[0] += vpMath::rad(30);
150 
151  // Move to second joint position
152  q2 = q;
153  q2[0] += vpMath::rad(-60);
155 
156  // Move back to home position
158  }
159  catch (const vpException &e) {
160  std::cout << "Catch exception: " << e.getStringMessage() << std::endl;
161  }
162 
163  std::cout << "The end" << std::endl;
164  return EXIT_SUCCESS;
165 #else
166  (void)(argc);
167  (void)(argv);
168  std::cout << "Install Jaco SDK, configure and build again ViSP to use this example..." << std::endl;
169 #endif
170 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
Initialize the position controller.
Definition: vpRobot.h:67
error that can be emited by ViSP classes.
Definition: vpException.h:71
void setVerbose(bool verbose)
void setCommandLayer(CommandLayer command_layer)
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
Initialize the velocity controller.
Definition: vpRobot.h:66
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)
static double rad(double deg)
Definition: vpMath.h:110
void setDoF(unsigned int dof)
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
const std::string & getStringMessage() const
Send a reference (constant) related the error message (can be empty).
Definition: vpException.cpp:92
void setPluginLocation(const std::string &plugin_location)
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)