Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testVirtuoseJointLimits.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 * 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
52void 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
97int 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 EXIT_SUCCESS;
122 ;
123 }
124 }
125
126 try {
127 float period = 0.001f;
128 vpVirtuose virtuose;
129 virtuose.setTimeStep(period);
130 std::cout << "Try to connect to " << opt_ip << " port " << opt_port << std::endl;
131 virtuose.setIpAddressAndPort(opt_ip, opt_port);
132 virtuose.setVerbose(true);
133 virtuose.setPowerOn();
134
135 // setArticularForce only works in COMMAND_TYPE_ARTICULAR_IMPEDANCE.
136 virtuose.setCommandType(COMMAND_TYPE_ARTICULAR_IMPEDANCE);
137
138 // -----------------------------------------------------------
139 // Code to obtain (experimentally) the Virtuose joint limits
140 // -----------------------------------------------------------
141
142 /*
143 // Move the Virtuose in all its workspace while running this code
144
145 vpColVector joints(6);
146 vpColVector max_joint(6,-1000);
147 vpColVector min_joint(6,1000);
148
149 for(unsigned int iter=0; iter<10000; iter++) {
150 virtuose.getArticularPosition(joints);
151 for(unsigned int i=0; i<6; i++) {
152 if (joints[i] > max_joint[i])
153 max_joint[i] = joints[i];
154 if (joints[i] < min_joint[i])
155 min_joint[i] = joints[i];
156 }
157 // Printing joint values
158 std::cout << "Joint values: " << joints.t() << std::endl;
159 vpTime::wait(10);
160 }
161
162 std::cout << "Max Joint values: " << max_joint.t() << std::endl;
163 std::cout << "Min Joint values: " << min_joint.t() << std::endl;
164
165 // Best Result (small errors are to be expected)
166 // Max Joint values: 0.7811045051 -0.07668215036 2.481732368
167 2.819076777 1.044736624 2.687076807
168 // Min Joint values: -0.8011951447 -1.648244739 0.7439950705
169 -3.022218227 -1.260564089 -2.054088593
170*/
171
172 virtuose.setPeriodicFunction(CallBackVirtuose);
173 virtuose.startPeriodicFunction();
174
175 int counter = 0;
176 bool swtch = true;
177
178 while (swtch) {
179 if (counter >= 10) {
180 virtuose.stopPeriodicFunction();
181 virtuose.setPowerOff();
182 swtch = false;
183 }
184 counter++;
185 vpTime::sleepMs(1000);
186 }
187
188 std::cout << "The end" << std::endl;
189 } catch (const vpException &e) {
190 std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
191 return EXIT_FAILURE;
192 }
193 return EXIT_SUCCESS;
194}
195
196#else
197int main()
198{
199 std::cout << "You should install Virtuose API to use this binary..." << std::endl;
200 return EXIT_SUCCESS;
201}
202#endif
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:59
const std::string & getStringMessage() const
vpColVector getArticularPosition() const
void setIpAddressAndPort(const std::string &ip, int port)
void setPowerOff()
void setTimeStep(const float &timeStep)
void setCommandType(const VirtCommandType &type)
void setPeriodicFunction(VirtPeriodicFunction CallBackVirt)
void stopPeriodicFunction()
void setPowerOn()
void setVerbose(bool mode)
Definition vpVirtuose.h:192
void setArticularForce(const vpColVector &articularForce)
void startPeriodicFunction()
VISP_EXPORT void sleepMs(double t)