Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testVirtuoseJointLimits.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test for Virtuose SDK wrapper.
32  *
33  * Authors:
34  * Nicolò Pedemonte
35  *
36  *****************************************************************************/
37 
45 #include <visp3/core/vpTime.h>
46 #include <visp3/robot/vpVirtuose.h>
47 
48 #if defined(VISP_HAVE_VIRTUOSE)
49 
50 void CallBackVirtuose(VirtContext VC, void* ptr)
51 {
52  (void) VC;
53  vpVirtuose* p_virtuose=(vpVirtuose*)ptr;
54 
55  float maxQ[6] = {0.7811045051f, -0.07668215036f, 2.481732368f, 2.819076777f, 1.044736624f, 2.687076807f};
56  float minQ[6] ={-0.8011951447f, -1.648244739f, 0.7439950705f, -3.022218227f, -1.260564089f, -2.054088593f};
57  unsigned int numJoint = 6;
58 
59  vpColVector feedbackRegion(numJoint, 0);
60  vpColVector forceFeedback(numJoint, 0);
61 
62  int feedbackRegionFactor = 10;
63  float saturationForce[6] = {5,5,5,2.5,2.5,2.5};
64 
65  for (unsigned int iter=0; iter<numJoint; iter++)
66  feedbackRegion[iter] = (maxQ[iter] - minQ[iter])/feedbackRegionFactor;
67 
68  vpColVector currentQ = p_virtuose->getArticularPosition();
69 
70  // force feedback definition
71  for (unsigned int iter = 0; iter < numJoint; iter++){
72  if (currentQ[iter] >= (maxQ[iter] - feedbackRegion[iter]))
73  {
74  forceFeedback[iter] = -saturationForce[iter] * pow((currentQ[iter] - maxQ[iter] + feedbackRegion[iter]) / feedbackRegion[iter], 2);
75  std::cout << "WARNING! Getting close to the maximum joint limit. Joint #" << iter + 1 << std::endl;
76  }
77  else if (currentQ[iter] <= (minQ[iter] + feedbackRegion[iter]))
78  {
79  forceFeedback[iter] = saturationForce[iter] * pow((minQ[iter] + feedbackRegion[iter] - currentQ[iter]) / feedbackRegion[iter], 2);
80  std::cout << "WARNING! Getting close to the minimum joint limit. Joint #" << iter + 1 << std::endl;
81  }
82  else
83  {
84  forceFeedback[iter] = 0;
85  std::cout << "Safe zone" << std::endl;
86  }
87  }
88 
89  // Printing force feedback
90  // std::cout << "Force feedback: " << forceFeedback.t() << std::endl;
91 
92  // Set force feedback
93  p_virtuose->setArticularForce(forceFeedback);
94 
95  return;
96 }
97 
98 int main()
99 {
100  try {
101  float period = 0.001f;
102  vpVirtuose virtuose;
103  virtuose.setTimeStep(period);
104  virtuose.setIpAddress("localhost#5000");
105  virtuose.setVerbose(true);
106  virtuose.setPowerOn();
107 
108  // setArticularForce only works in COMMAND_TYPE_ARTICULAR_IMPEDANCE.
109  virtuose.setCommandType(COMMAND_TYPE_ARTICULAR_IMPEDANCE);
110 
111  // -----------------------------------------------------------
112  // Code to obtain (experimentally) the Virtuose joint limits
113  // -----------------------------------------------------------
114 
115  /*
116  // Move the Virtuose in all its workspace while running this code
117 
118  vpColVector joints(6);
119  vpColVector max_joint(6,-1000);
120  vpColVector min_joint(6,1000);
121 
122  for(unsigned int iter=0; iter<10000; iter++) {
123  virtuose.getArticularPosition(joints);
124  for(unsigned int i=0; i<6; i++) {
125  if (joints[i] > max_joint[i])
126  max_joint[i] = joints[i];
127  if (joints[i] < min_joint[i])
128  min_joint[i] = joints[i];
129  }
130  // Printing joint values
131  std::cout << "Joint values: " << joints.t() << std::endl;
132  vpTime::wait(10);
133  }
134 
135  std::cout << "Max Joint values: " << max_joint.t() << std::endl;
136  std::cout << "Min Joint values: " << min_joint.t() << std::endl;
137 
138  // Best Result (small errors are to be expected)
139  // Max Joint values: 0.7811045051 -0.07668215036 2.481732368 2.819076777 1.044736624 2.687076807
140  // Min Joint values: -0.8011951447 -1.648244739 0.7439950705 -3.022218227 -1.260564089 -2.054088593
141 */
142 
143  virtuose.setPeriodicFunction(CallBackVirtuose);
144  virtuose.startPeriodicFunction();
145 
146  int counter = 0;
147  bool swtch = true;
148 
149  while(swtch) {
150  if (counter>=10)
151  {
152  virtuose.stopPeriodicFunction();
153  virtuose.setPowerOff();
154  swtch = false;
155  }
156  counter++;
157  vpTime::sleepMs(1000);
158  }
159 
160  std::cout << "The end" << std::endl;
161  }
162  catch(vpException &e) {
163  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
164  }
165 }
166 
167 #else
168 int main()
169 {
170  std::cout << "You should install Virtuose API to use this binary..." << std::endl;
171 }
172 #endif
void startPeriodicFunction()
Definition: vpVirtuose.cpp:974
error that can be emited by ViSP classes.
Definition: vpException.h:73
void setPeriodicFunction(VirtPeriodicFunction CallBackVirt)
Definition: vpVirtuose.cpp:825
vpColVector getArticularPosition() const
Definition: vpVirtuose.cpp:120
void setPowerOn()
Definition: vpVirtuose.cpp:881
void setPowerOff()
Definition: vpVirtuose.cpp:867
void setVerbose(bool mode)
Definition: vpVirtuose.h:183
VISP_EXPORT void sleepMs(double t)
Definition: vpTime.cpp:266
void setCommandType(const VirtCommandType &type)
Definition: vpVirtuose.cpp:681
void setIpAddress(const std::string &ip)
Definition: vpVirtuose.h:169
void setArticularForce(const vpColVector &articularForce)
Definition: vpVirtuose.cpp:569
void stopPeriodicFunction()
Definition: vpVirtuose.cpp:992
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
const std::string & getStringMessage(void) const
Send a reference (constant) related the error message (can be empty).
void setTimeStep(const float &timeStep)
Definition: vpVirtuose.cpp:913