Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testVirtuoseJointLimits.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
41
42#include <visp3/core/vpTime.h>
43#include <visp3/robot/vpVirtuose.h>
44
45#if defined(VISP_HAVE_VIRTUOSE)
46
47#ifdef ENABLE_VISP_NAMESPACE
48using namespace VISP_NAMESPACE_NAME;
49#endif
50
51void CallBackVirtuose(VirtContext VC, void *ptr)
52{
53 (void)VC;
54 vpVirtuose *p_virtuose = (vpVirtuose *)ptr;
55
56 float maxQ[6] = { 0.7811045051f, -0.07668215036f, 2.481732368f, 2.819076777f, 1.044736624f, 2.687076807f };
57 float minQ[6] = { -0.8011951447f, -1.648244739f, 0.7439950705f, -3.022218227f, -1.260564089f, -2.054088593f };
58 unsigned int numJoint = 6;
59
60 vpColVector feedbackRegion(numJoint, 0);
61 vpColVector forceFeedback(numJoint, 0);
62
63 int feedbackRegionFactor = 10;
64 float saturationForce[6] = { 5, 5, 5, 2.5, 2.5, 2.5 };
65
66 for (unsigned int iter = 0; iter < numJoint; iter++)
67 feedbackRegion[iter] = (maxQ[iter] - minQ[iter]) / feedbackRegionFactor;
68
69 vpColVector currentQ = p_virtuose->getArticularPosition();
70
71 // force feedback definition
72 for (unsigned int iter = 0; iter < numJoint; iter++) {
73 if (currentQ[iter] >= (maxQ[iter] - feedbackRegion[iter])) {
74 forceFeedback[iter] =
75 -saturationForce[iter] * pow((currentQ[iter] - maxQ[iter] + feedbackRegion[iter]) / feedbackRegion[iter], 2);
76 std::cout << "WARNING! Getting close to the maximum joint limit. Joint #" << iter + 1 << std::endl;
77 }
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 }
83 else {
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
98int main(int argc, char **argv)
99{
100 std::string opt_ip = "localhost";
101 int opt_port = 5000;
102 for (int i = 1; i < argc; i++) {
103 if (std::string(argv[i]) == "--ip" && i + 1 < argc) {
104 opt_ip = std::string(argv[++i]);
105 }
106 else if (std::string(argv[i]) == "--port" && i + 1 < argc) {
107 opt_port = std::atoi(argv[++i]);
108 }
109 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
110 std::cout << "\nUsage: " << argv[0]
111 << " [--ip <localhost>]"
112 << " [--port <port>]"
113 << " [--help] [-h]\n"
114 << std::endl
115 << "Description: " << std::endl
116 << " --ip <localhost>" << std::endl
117 << "\tHost IP address. Default value: \"localhost\"." << std::endl
118 << std::endl
119 << " --port <port>" << std::endl
120 << "\tCommunication port. Default value: 5000." << std::endl
121 << "\tSuggested values: " << std::endl
122 << "\t- 5000 to communicate with the Virtuose." << std::endl
123 << "\t- 53210 to communicate with the Virtuose equipped with the Glove." << std::endl
124 << std::endl;
125 return EXIT_SUCCESS;
126 ;
127 }
128 }
129
130 try {
131 float period = 0.001f;
132 vpVirtuose virtuose;
133 virtuose.setTimeStep(period);
134 std::cout << "Try to connect to " << opt_ip << " port " << opt_port << std::endl;
135 virtuose.setIpAddressAndPort(opt_ip, opt_port);
136 virtuose.setVerbose(true);
137 virtuose.setPowerOn();
138
139 // setArticularForce only works in COMMAND_TYPE_ARTICULAR_IMPEDANCE.
140 virtuose.setCommandType(COMMAND_TYPE_ARTICULAR_IMPEDANCE);
141
142 // -----------------------------------------------------------
143 // Code to obtain (experimentally) the Virtuose joint limits
144 // -----------------------------------------------------------
145
146 /*
147 // Move the Virtuose in all its workspace while running this code
148
149 vpColVector joints(6);
150 vpColVector max_joint(6,-1000);
151 vpColVector min_joint(6,1000);
152
153 for(unsigned int iter=0; iter<10000; iter++) {
154 virtuose.getArticularPosition(joints);
155 for(unsigned int i=0; i<6; i++) {
156 if (joints[i] > max_joint[i])
157 max_joint[i] = joints[i];
158 if (joints[i] < min_joint[i])
159 min_joint[i] = joints[i];
160 }
161 // Printing joint values
162 std::cout << "Joint values: " << joints.t() << std::endl;
163 vpTime::wait(10);
164 }
165
166 std::cout << "Max Joint values: " << max_joint.t() << std::endl;
167 std::cout << "Min Joint values: " << min_joint.t() << std::endl;
168
169 // Best Result (small errors are to be expected)
170 // Max Joint values: 0.7811045051 -0.07668215036 2.481732368
171 2.819076777 1.044736624 2.687076807
172 // Min Joint values: -0.8011951447 -1.648244739 0.7439950705
173 -3.022218227 -1.260564089 -2.054088593
174*/
175
176 virtuose.setPeriodicFunction(CallBackVirtuose);
177 virtuose.startPeriodicFunction();
178
179 int counter = 0;
180 bool swtch = true;
181
182 while (swtch) {
183 if (counter >= 10) {
184 virtuose.stopPeriodicFunction();
185 virtuose.setPowerOff();
186 swtch = false;
187 }
188 counter++;
189 vpTime::sleepMs(1000);
190 }
191
192 std::cout << "The end" << std::endl;
193 }
194 catch (const vpException &e) {
195 std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
196 return EXIT_FAILURE;
197 }
198 return EXIT_SUCCESS;
199}
200
201#else
202int main()
203{
204 std::cout << "You should install Virtuose API to use this binary..." << std::endl;
205 return EXIT_SUCCESS;
206}
207#endif
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:60
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:194
void setArticularForce(const vpColVector &articularForce)
void startPeriodicFunction()
VISP_EXPORT void sleepMs(double t)