Visual Servoing Platform  version 3.6.1 under development (2024-05-08)
servoKinovaJacoJoint.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  * Example with Kinova Jaco robot.
33  *
34 *****************************************************************************/
35 
43 #include <iostream>
44 
45 #include <visp3/core/vpConfig.h>
46 #include <visp3/robot/vpRobotKinova.h>
47 
48 int main(int argc, char *argv[])
49 {
50 #ifdef VISP_HAVE_JACOSDK
51  std::string opt_plugin_path = "./";
53  bool opt_verbose = false;
54  unsigned int opt_dof = 6; // Consider a 6 DoF robot by default
55 
56  for (int i = 1; i < argc; i++) {
57  if (std::string(argv[i]) == "--plugin" && i + 1 < argc) {
58  opt_plugin_path = std::string(argv[i + 1]);
59  ;
60  }
61  if ((std::string(argv[i]) == "--command_layer" || std::string(argv[i]) == "-l") && i + 1 < argc) {
62  if (std::string(argv[i + 1]) == "usb") {
63  opt_command_layer = vpRobotKinova::CMD_LAYER_USB;
64  } else if (std::string(argv[i + 1]) == "ethernet") {
65  opt_command_layer = vpRobotKinova::CMD_LAYER_ETHERNET;
66  } else {
67  opt_command_layer = vpRobotKinova::CMD_LAYER_UNSET;
68  }
69  } else if (std::string(argv[i]) == "--dof") {
70  opt_dof = static_cast<unsigned int>(std::atoi(argv[i + 1]));
71  } else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
72  opt_verbose = true;
73  } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
74  std::cout << "SYNOPSYS" << std::endl
75  << " " << argv[0] << " [--plugin <path>] [--command_layer <name>] [--dof <4,6,7>] "
76  << "[--verbose] [--help] [-v] [-h]\n"
77  << std::endl;
78  std::cout << "DESCRIPTION" << std::endl
79  << " --plugin <path>" << std::endl
80  << " Path to Jaco SDK .so or .dll plugin location. Default: \"./\"." << std::endl
81  << std::endl
82  << " --command_layer <name>, -l <name>" << std::endl
83  << " Command layer name, either \"usb\" or \"ethernet\"." << std::endl
84  << std::endl
85  << " --dof" << std::endl
86  << " Degrees of freedom. Possible values are 4, 6 or 7. Default value: 6." << std::endl
87  << std::endl
88  << " --verbose, -v" << std::endl
89  << " Enable verbose mode to print addition information." << std::endl
90  << std::endl
91  << " --help, -h" << std::endl
92  << " Print this helper message." << std::endl
93  << std::endl;
94  std::cout << "EXAMPLE" << std::endl
95 #ifdef __linux__
96  << " " << argv[0] << " --plugin /opt/JACO-SDK/API"
97 #elif _WIN32
98  << " " << argv[0] << " --plugin \"C:\\Program Files(x86)\\JACO - SDK\\API\\x64\""
99 #endif
100  << " --command_layer usb" << std::endl
101  << 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
144  robot.getPosition(vpRobot::JOINT_STATE, q);
145 
146  // Move to first joint position
147  q1 = q;
148  q1[0] += vpMath::rad(30);
149  robot.setPosition(vpRobot::JOINT_STATE, q1);
150 
151  // Move to second joint position
152  q2 = q;
153  q2[0] += vpMath::rad(-60);
154  robot.setPosition(vpRobot::JOINT_STATE, q2);
155 
156  // Move back to home position
157  robot.setPosition(vpRobot::JOINT_STATE, q);
158  } catch (const vpException &e) {
159  std::cout << "Catch exception: " << e.getStringMessage() << std::endl;
160  }
161 
162  std::cout << "The end" << std::endl;
163  return EXIT_SUCCESS;
164 #else
165  (void)(argc);
166  (void)(argv);
167  std::cout << "Install Jaco SDK, configure and build again ViSP to use this example..." << std::endl;
168 #endif
169 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const std::string & getStringMessage() const
Definition: vpException.cpp:66
static double rad(double deg)
Definition: vpMath.h:127
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
void setVerbose(bool verbose)
Definition: vpRobot.h:168
@ JOINT_STATE
Definition: vpRobot.h:80
@ STATE_POSITION_CONTROL
Initialize the position controller.
Definition: vpRobot.h:66
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:65
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:198
VISP_EXPORT int wait(double t0, double t)