Visual Servoing Platform  version 3.4.0
testVirtuoseJointLimits.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  * Test for Virtuose SDK wrapper.
33  *
34  * Authors:
35  * Nicolò Pedemonte
36  *
37  *****************************************************************************/
38 
47 #include <visp3/core/vpTime.h>
48 #include <visp3/robot/vpVirtuose.h>
49 
50 #if defined(VISP_HAVE_VIRTUOSE)
51 
52 void CallBackVirtuose(VirtContext VC, void *ptr)
53 {
54  (void)VC;
55  vpVirtuose *p_virtuose = (vpVirtuose *)ptr;
56 
57  float maxQ[6] = {0.7811045051f, -0.07668215036f, 2.481732368f, 2.819076777f, 1.044736624f, 2.687076807f};
58  float minQ[6] = {-0.8011951447f, -1.648244739f, 0.7439950705f, -3.022218227f, -1.260564089f, -2.054088593f};
59  unsigned int numJoint = 6;
60 
61  vpColVector feedbackRegion(numJoint, 0);
62  vpColVector forceFeedback(numJoint, 0);
63 
64  int feedbackRegionFactor = 10;
65  float saturationForce[6] = {5, 5, 5, 2.5, 2.5, 2.5};
66 
67  for (unsigned int iter = 0; iter < numJoint; iter++)
68  feedbackRegion[iter] = (maxQ[iter] - minQ[iter]) / feedbackRegionFactor;
69 
70  vpColVector currentQ = p_virtuose->getArticularPosition();
71 
72  // force feedback definition
73  for (unsigned int iter = 0; iter < numJoint; iter++) {
74  if (currentQ[iter] >= (maxQ[iter] - feedbackRegion[iter])) {
75  forceFeedback[iter] =
76  -saturationForce[iter] * pow((currentQ[iter] - maxQ[iter] + feedbackRegion[iter]) / feedbackRegion[iter], 2);
77  std::cout << "WARNING! Getting close to the maximum joint limit. Joint #" << iter + 1 << std::endl;
78  } else if (currentQ[iter] <= (minQ[iter] + feedbackRegion[iter])) {
79  forceFeedback[iter] =
80  saturationForce[iter] * pow((minQ[iter] + feedbackRegion[iter] - currentQ[iter]) / feedbackRegion[iter], 2);
81  std::cout << "WARNING! Getting close to the minimum joint limit. Joint #" << iter + 1 << std::endl;
82  } else {
83  forceFeedback[iter] = 0;
84  std::cout << "Safe zone" << std::endl;
85  }
86  }
87 
88  // Printing force feedback
89  // std::cout << "Force feedback: " << forceFeedback.t() << std::endl;
90 
91  // Set force feedback
92  p_virtuose->setArticularForce(forceFeedback);
93 
94  return;
95 }
96 
97 int main(int argc, char **argv)
98 {
99  std::string opt_ip = "localhost";
100  int opt_port = 5000;
101  for (int i = 0; i < argc; i++) {
102  if (std::string(argv[i]) == "--ip")
103  opt_ip = std::string(argv[i + 1]);
104  else if (std::string(argv[i]) == "--port")
105  opt_port = std::atoi(argv[i + 1]);
106  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
107  std::cout << "\nUsage: " << argv[0]
108  << " [--ip <localhost>] [--port <port>]"
109  " [--help] [-h]\n"
110  << std::endl
111  << "Description: " << std::endl
112  << " --ip <localhost>" << std::endl
113  << "\tHost IP address. Default value: \"localhost\"." << std::endl
114  << std::endl
115  << " --port <port>" << std::endl
116  << "\tCommunication port. Default value: 5000." << std::endl
117  << "\tSuggested values: " << std::endl
118  << "\t- 5000 to communicate with the Virtuose." << std::endl
119  << "\t- 53210 to communicate with the Virtuose equipped with the Glove." << std::endl
120  << std::endl;
121  return 0;
122  }
123  }
124 
125  try {
126  float period = 0.001f;
127  vpVirtuose virtuose;
128  virtuose.setTimeStep(period);
129  std::cout << "Try to connect to " << opt_ip << " port " << opt_port << std::endl;
130  virtuose.setIpAddressAndPort(opt_ip, opt_port);
131  virtuose.setVerbose(true);
132  virtuose.setPowerOn();
133 
134  // setArticularForce only works in COMMAND_TYPE_ARTICULAR_IMPEDANCE.
135  virtuose.setCommandType(COMMAND_TYPE_ARTICULAR_IMPEDANCE);
136 
137  // -----------------------------------------------------------
138  // Code to obtain (experimentally) the Virtuose joint limits
139  // -----------------------------------------------------------
140 
141  /*
142  // Move the Virtuose in all its workspace while running this code
143 
144  vpColVector joints(6);
145  vpColVector max_joint(6,-1000);
146  vpColVector min_joint(6,1000);
147 
148  for(unsigned int iter=0; iter<10000; iter++) {
149  virtuose.getArticularPosition(joints);
150  for(unsigned int i=0; i<6; i++) {
151  if (joints[i] > max_joint[i])
152  max_joint[i] = joints[i];
153  if (joints[i] < min_joint[i])
154  min_joint[i] = joints[i];
155  }
156  // Printing joint values
157  std::cout << "Joint values: " << joints.t() << std::endl;
158  vpTime::wait(10);
159  }
160 
161  std::cout << "Max Joint values: " << max_joint.t() << std::endl;
162  std::cout << "Min Joint values: " << min_joint.t() << std::endl;
163 
164  // Best Result (small errors are to be expected)
165  // Max Joint values: 0.7811045051 -0.07668215036 2.481732368
166  2.819076777 1.044736624 2.687076807
167  // Min Joint values: -0.8011951447 -1.648244739 0.7439950705
168  -3.022218227 -1.260564089 -2.054088593
169 */
170 
171  virtuose.setPeriodicFunction(CallBackVirtuose);
172  virtuose.startPeriodicFunction();
173 
174  int counter = 0;
175  bool swtch = true;
176 
177  while (swtch) {
178  if (counter >= 10) {
179  virtuose.stopPeriodicFunction();
180  virtuose.setPowerOff();
181  swtch = false;
182  }
183  counter++;
184  vpTime::sleepMs(1000);
185  }
186 
187  std::cout << "The end" << std::endl;
188  } catch (const vpException &e) {
189  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
190  }
191 }
192 
193 #else
194 int main() { std::cout << "You should install Virtuose API to use this binary..." << std::endl; }
195 #endif
void startPeriodicFunction()
error that can be emited by ViSP classes.
Definition: vpException.h:71
void setPeriodicFunction(VirtPeriodicFunction CallBackVirt)
Definition: vpVirtuose.cpp:878
void setIpAddressAndPort(const std::string &ip, int port)
Definition: vpVirtuose.cpp:87
vpColVector getArticularPosition() const
Definition: vpVirtuose.cpp:140
void setPowerOn()
Definition: vpVirtuose.cpp:931
void setPowerOff()
Definition: vpVirtuose.cpp:918
void setVerbose(bool mode)
Definition: vpVirtuose.h:195
VISP_EXPORT void sleepMs(double t)
Definition: vpTime.cpp:271
void setCommandType(const VirtCommandType &type)
Definition: vpVirtuose.cpp:726
void setArticularForce(const vpColVector &articularForce)
Definition: vpVirtuose.cpp:603
void stopPeriodicFunction()
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 setTimeStep(const float &timeStep)
Definition: vpVirtuose.cpp:961