File: servoKinovaJacoJoint.cpp

package info (click to toggle)
visp 3.7.0-7
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 166,380 kB
  • sloc: cpp: 392,705; ansic: 224,448; xml: 23,444; python: 13,701; java: 4,792; sh: 206; objc: 145; makefile: 118
file content (177 lines) | stat: -rw-r--r-- 5,778 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Example with Kinova Jaco robot.
 */

/*!
  \example servoKinovaJacoJoint.cpp

    Example that allows to control Kinova Jaco robot joints.

*/

#include <iostream>

#include <visp3/core/vpConfig.h>
#include <visp3/robot/vpRobotKinova.h>

int main(int argc, char *argv[])
{
#ifdef VISP_HAVE_JACOSDK
#ifdef ENABLE_VISP_NAMESPACE
  using namespace VISP_NAMESPACE_NAME;
#endif

  std::string opt_plugin_path = "./";
  vpRobotKinova::CommandLayer opt_command_layer = vpRobotKinova::CMD_LAYER_UNSET;
  bool opt_verbose = false;
  unsigned int opt_dof = 6; // Consider a 6 DoF robot by default

  for (int i = 1; i < argc; i++) {
    if (std::string(argv[i]) == "--plugin" && i + 1 < argc) {
      opt_plugin_path = std::string(argv[i + 1]);
      ;
    }
    if ((std::string(argv[i]) == "--command_layer" || std::string(argv[i]) == "-l") && i + 1 < argc) {
      if (std::string(argv[i + 1]) == "usb") {
        opt_command_layer = vpRobotKinova::CMD_LAYER_USB;
      }
      else if (std::string(argv[i + 1]) == "ethernet") {
        opt_command_layer = vpRobotKinova::CMD_LAYER_ETHERNET;
      }
      else {
        opt_command_layer = vpRobotKinova::CMD_LAYER_UNSET;
      }
    }
    else if (std::string(argv[i]) == "--dof") {
      opt_dof = static_cast<unsigned int>(std::atoi(argv[i + 1]));
    }
    else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
      opt_verbose = true;
    }
    else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
      std::cout << "SYNOPSYS" << std::endl
        << "  " << argv[0] << " [--plugin <path>] [--command_layer <name>] [--dof <4,6,7>] "
        << "[--verbose] [--help] [-v] [-h]\n"
        << std::endl;
      std::cout << "DESCRIPTION" << std::endl
        << "  --plugin <path>" << std::endl
        << "      Path to Jaco SDK .so or .dll plugin location. Default: \"./\"." << std::endl
        << std::endl
        << "  --command_layer <name>, -l <name>" << std::endl
        << "      Command layer name, either \"usb\" or \"ethernet\"." << std::endl
        << std::endl
        << "  --dof" << std::endl
        << "      Degrees of freedom. Possible values are 4, 6 or 7. Default value: 6." << std::endl
        << std::endl
        << "  --verbose, -v" << std::endl
        << "      Enable verbose mode to print addition information." << std::endl
        << std::endl
        << "  --help, -h" << std::endl
        << "      Print this helper message." << std::endl
        << std::endl;
      std::cout << "EXAMPLE" << std::endl
#ifdef __linux__
        << "  " << argv[0] << " --plugin /opt/JACO-SDK/API"
#elif _WIN32
        << "  " << argv[0] << " --plugin \"C:\\Program Files(x86)\\JACO - SDK\\API\\x64\""
#endif
        << " --command_layer usb" << std::endl
        << std::endl;

      return EXIT_SUCCESS;
    }
  }

  try {
    vpRobotKinova robot;
    robot.setDoF(opt_dof);
    robot.setVerbose(opt_verbose);
    robot.setPluginLocation(opt_plugin_path);
    robot.setCommandLayer(opt_command_layer);

    int n_devices = robot.connect();

    if (!n_devices) {
      std::cout << "There is no Kinova device connected." << std::endl;
      return EXIT_SUCCESS;
    }

    // Move robot to home position
    robot.homing();

    // Control robot in joint velocity
    robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
    vpColVector qdot(opt_dof);
    qdot[opt_dof - 1] = vpMath::rad(20); // send 20 deg/s on last joint

    // Sent new joint velocities each 5 ms
    for (unsigned int i = 0; i < 300; i++) {
      // We send the velocity vector as long as we want the robot to move along that vector
      robot.setVelocity(vpRobot::JOINT_STATE, qdot);
      vpTime::wait(5);
    }

    // Control robot in joint position
    robot.setRobotState(vpRobot::STATE_POSITION_CONTROL);
    vpColVector q, q1, q2;

    // Move robot to home position
    robot.homing();

    // Get current joint position
    robot.getPosition(vpRobot::JOINT_STATE, q);

    // Move to first joint position
    q1 = q;
    q1[0] += vpMath::rad(30);
    robot.setPosition(vpRobot::JOINT_STATE, q1);

    // Move to second joint position
    q2 = q;
    q2[0] += vpMath::rad(-60);
    robot.setPosition(vpRobot::JOINT_STATE, q2);

    // Move back to home position
    robot.setPosition(vpRobot::JOINT_STATE, q);
  }
  catch (const vpException &e) {
    std::cout << "Catch exception: " << e.getStringMessage() << std::endl;
  }

  std::cout << "The end" << std::endl;
  return EXIT_SUCCESS;
#else
  (void)(argc);
  (void)(argv);
  std::cout << "Install Jaco SDK, configure and build again ViSP to use this example..." << std::endl;
#endif
}