Visual Servoing Platform  version 3.6.1 under development (2024-10-13)
vpQbSoftHand.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  * Interface for the qb robotics devices.
33  *
34 *****************************************************************************/
35 
36 #include <visp3/core/vpConfig.h>
37 #if defined(VISP_HAVE_QBDEVICE) && defined(VISP_HAVE_THREADS)
38 
39 #include <regex>
40 
41 #include <visp3/robot/vpQbSoftHand.h>
42 
43 BEGIN_VISP_NAMESPACE
49 
55 void vpQbSoftHand::getCurrent(vpColVector &current, const int &id)
56 {
57  if (!m_init_done) {
58  init(id);
59  }
60 
61  current.resize(1);
62  if (!isInConnectedSet(id)) {
63  throw(vpException(vpException::fatalError, "Cannot get current, Qb device is not connected"));
64  }
65 
66  std::vector<short int> currents(2);
67  int failures = getCurrents(id, m_max_repeats, currents); // blocks while reading
68 
69  if (!isReliable(failures, m_max_repeats)) {
71  "Cannot get current, communication error with Qb device after %d attempts", m_max_repeats));
72  }
73  current[0] = static_cast<double>(currents[0]);
74 }
75 
82 void vpQbSoftHand::getPosition(vpColVector &position, const int &id)
83 {
84  if (!m_init_done) {
85  init(id);
86  }
87 
88  position.resize(1);
89  if (!isInConnectedSet(id)) {
90  throw(vpException(vpException::fatalError, "Cannot get position, Qb device is not connected"));
91  }
92 
93  std::vector<short int> positions;
94  int failures = getPositions(id, m_max_repeats, positions); // blocks while reading
95 
96  position[0] = static_cast<double>(positions[0]) / static_cast<double>(getPositionLimits()[1]);
97 
98  if (!isReliable(failures, m_max_repeats)) {
100  "Cannot get position, communication error with Qb device after %d attempts", m_max_repeats));
101  }
102 }
103 
110 void vpQbSoftHand::setPosition(const vpColVector &position, const int &id)
111 {
112  if (!m_init_done) {
113  init(id);
114  }
115 
116  std::vector<short int> commands(2);
117  if (position.size() != 1) {
118  throw(vpException(vpException::fatalError, "Command vector size %d is not equal to 2", position.size()));
119  }
120 
121  std::vector<short int> position_limits = getPositionLimits();
122 
123  commands[0] = static_cast<short int>(position[0] * position_limits[1]);
124 
125  if (commands[0] < position_limits[0]) {
126  commands[0] = position_limits[0];
127  }
128  else if (commands[0] > position_limits[1]) {
129  commands[0] = position_limits[1];
130  }
131 
132  if (!isInConnectedSet(id)) {
133  throw(vpException(vpException::fatalError, "Cannot set position, Qb device is not connected"));
134  }
135 
136  // int failures = setCommandsAndWait(id, m_max_repeats, commands); // FS: doesn't work
137  int failures = setCommandsAsync(id, commands);
138 
139  if (!isReliable(failures, m_max_repeats)) {
141  "Cannot set position, communication error with Qb device after %d attempts", m_max_repeats));
142  }
143 }
144 
155 void vpQbSoftHand::setPosition(const vpColVector &position, double speed_factor, double stiffness, const int &id)
156 {
157  vpColVector q_mes(1), q(1), current;
158  getPosition(q_mes, id);
159  double current_max = getCurrentMax();
160 
161  double max_delta_q = 1; // 0 opened, 1 closed
162  double min_delta_t = 2.0; // number of [sec] to open or close with the max velocity
163  double precision = 0.05;
164  double delta_t = 40; // [ms]
165  double max_slope = max_delta_q / min_delta_t;
166  double sign = (position[0] > q_mes[0]) ? 1.0 : -1.0;
167  double vel = speed_factor;
168  if (vel < 0.01) {
169  vel = 0.01;
170  }
171  else if (vel > 1.) {
172  vel = 1.0;
173  }
174  double current_factor = stiffness;
175  if (current_factor < 0.0) {
176  current_factor = 0.0;
177  }
178  else if (current_factor > 1.) {
179  current_factor = 1.0;
180  }
181  double slope = sign * max_slope * vel;
182 
183  unsigned int i = 0;
184  int current_failures = 0;
185  do {
186  double t0 = vpTime::measureTimeMs();
187  q[0] = q_mes[0] + slope * delta_t / 1000.0 * i;
188  if (q[0] < getPositionLimits()[0]) {
189  q[0] = getPositionLimits()[0];
190  }
191  else if (q[0] > getPositionLimits()[1]) {
192  q[0] = getPositionLimits()[1];
193  }
194  setPosition(q, id);
195  getCurrent(current, id);
196  i++;
197 
198  if (std::fabs(current[0]) > current_factor * current_max) {
199  current_failures++;
200  }
201  else {
202  current_failures = 0;
203  }
204 
205  vpTime::wait(t0, delta_t);
206  } while (!vpMath::equal(q[0], position[0], precision) && !(current_failures > 1));
207 }
208 END_VISP_NAMESPACE
209 #endif
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1143
error that can be emitted by ViSP classes.
Definition: vpException.h:60
@ fatalError
Fatal error.
Definition: vpException.h:72
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:459
bool isReliable(int const &failures, int const &max_repeats)
Definition: vpQbDevice.cpp:691
double getCurrentMax() const
Definition: vpQbDevice.cpp:529
virtual bool isInConnectedSet(const int &id)
Definition: vpQbDevice.cpp:675
virtual bool init(const int &id)
Definition: vpQbDevice.cpp:639
int m_max_repeats
Max number of trials to send a command.
Definition: vpQbDevice.h:113
virtual int getCurrents(const int &id, const int &max_repeats, std::vector< short int > &currents)
Definition: vpQbDevice.cpp:541
std::vector< short int > getPositionLimits() const
Definition: vpQbDevice.cpp:599
virtual int getPositions(const int &id, const int &max_repeats, std::vector< short int > &positions)
Definition: vpQbDevice.cpp:612
virtual int setCommandsAsync(const int &id, std::vector< short int > &commands)
Definition: vpQbDevice.cpp:727
bool m_init_done
Flag used to indicate if the device is initialized.
Definition: vpQbDevice.h:114
void getCurrent(vpColVector &current, const int &id=1)
void setPosition(const vpColVector &position, const int &id=1)
void getPosition(vpColVector &position, const int &id=1)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()