Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
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 #ifdef VISP_HAVE_QBDEVICE
38 
39 #include <regex>
40 
41 #include <visp3/robot/vpQbSoftHand.h>
42 
48 
54 
60 void vpQbSoftHand::getCurrent(vpColVector &current, const int &id)
61 {
62  if (!m_init_done) {
63  init(id);
64  }
65 
66  current.resize(1);
67  if (!isInConnectedSet(id)) {
68  throw(vpException(vpException::fatalError, "Cannot get current, Qb device is not connected"));
69  }
70 
71  std::vector<short int> currents(2);
72  int failures = getCurrents(id, m_max_repeats, currents); // blocks while reading
73 
74  if (!isReliable(failures, m_max_repeats)) {
76  "Cannot get current, communication error with Qb device after %d attempts", m_max_repeats));
77  }
78  current[0] = static_cast<double>(currents[0]);
79 }
80 
87 void vpQbSoftHand::getPosition(vpColVector &position, const int &id)
88 {
89  if (!m_init_done) {
90  init(id);
91  }
92 
93  position.resize(1);
94  if (!isInConnectedSet(id)) {
95  throw(vpException(vpException::fatalError, "Cannot get position, Qb device is not connected"));
96  }
97 
98  std::vector<short int> positions;
99  int failures = getPositions(id, m_max_repeats, positions); // blocks while reading
100 
101  position[0] = static_cast<double>(positions[0]) / static_cast<double>(getPositionLimits()[1]);
102 
103  if (!isReliable(failures, m_max_repeats)) {
105  "Cannot get position, communication error with Qb device after %d attempts", m_max_repeats));
106  }
107 }
108 
115 void vpQbSoftHand::setPosition(const vpColVector &position, const int &id)
116 {
117  if (!m_init_done) {
118  init(id);
119  }
120 
121  std::vector<short int> commands(2);
122  if (position.size() != 1) {
123  throw(vpException(vpException::fatalError, "Command vector size %d is not equal to 2", position.size()));
124  }
125 
126  std::vector<short int> position_limits = getPositionLimits();
127 
128  commands[0] = static_cast<short int>(position[0] * position_limits[1]);
129 
130  if (commands[0] < position_limits[0]) {
131  commands[0] = position_limits[0];
132  } else if (commands[0] > position_limits[1]) {
133  commands[0] = position_limits[1];
134  }
135 
136  if (!isInConnectedSet(id)) {
137  throw(vpException(vpException::fatalError, "Cannot set position, Qb device is not connected"));
138  }
139 
140  // int failures = setCommandsAndWait(id, m_max_repeats, commands); // FS: doesn't work
141  int failures = setCommandsAsync(id, commands);
142 
143  if (!isReliable(failures, m_max_repeats)) {
145  "Cannot set position, communication error with Qb device after %d attempts", m_max_repeats));
146  }
147 }
148 
159 void vpQbSoftHand::setPosition(const vpColVector &position, double speed_factor, double stiffness, const int &id)
160 {
161  vpColVector q_mes(1), q(1), current;
162  getPosition(q_mes, id);
163  double current_max = getCurrentMax();
164 
165  double max_delta_q = 1; // 0 opened, 1 closed
166  double min_delta_t = 2.0; // number of [sec] to open or close with the max velocity
167  double precision = 0.05;
168  double delta_t = 40; // [ms]
169  double max_slope = max_delta_q / min_delta_t;
170  double sign = (position[0] > q_mes[0]) ? 1.0 : -1.0;
171  double vel = speed_factor;
172  if (vel < 0.01) {
173  vel = 0.01;
174  } else if (vel > 1.) {
175  vel = 1.0;
176  }
177  double current_factor = stiffness;
178  if (current_factor < 0.0) {
179  current_factor = 0.0;
180  } else if (current_factor > 1.) {
181  current_factor = 1.0;
182  }
183  double slope = sign * max_slope * vel;
184 
185  unsigned int i = 0;
186  int current_failures = 0;
187  do {
188  double t0 = vpTime::measureTimeMs();
189  q[0] = q_mes[0] + slope * delta_t / 1000.0 * i;
190  if (q[0] < getPositionLimits()[0]) {
191  q[0] = getPositionLimits()[0];
192  } else if (q[0] > getPositionLimits()[1]) {
193  q[0] = getPositionLimits()[1];
194  }
195  setPosition(q, id);
196  getCurrent(current, id);
197  i++;
198 
199  if (std::fabs(current[0]) > current_factor * current_max) {
200  current_failures++;
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 #endif
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:292
Implementation of column vector and the associated operations.
Definition: vpColVector.h:167
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:351
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:369
bool isReliable(int const &failures, int const &max_repeats)
Definition: vpQbDevice.cpp:688
double getCurrentMax() const
Definition: vpQbDevice.cpp:526
virtual bool isInConnectedSet(const int &id)
Definition: vpQbDevice.cpp:672
virtual bool init(const int &id)
Definition: vpQbDevice.cpp:636
int m_max_repeats
Max number of trials to send a command.
Definition: vpQbDevice.h:112
virtual int getCurrents(const int &id, const int &max_repeats, std::vector< short int > &currents)
Definition: vpQbDevice.cpp:538
std::vector< short int > getPositionLimits() const
Definition: vpQbDevice.cpp:596
virtual int getPositions(const int &id, const int &max_repeats, std::vector< short int > &positions)
Definition: vpQbDevice.cpp:609
virtual int setCommandsAsync(const int &id, std::vector< short int > &commands)
Definition: vpQbDevice.cpp:724
bool m_init_done
Flag used to indicate if the device is initialized.
Definition: vpQbDevice.h:113
void getCurrent(vpColVector &current, const int &id=1)
void setPosition(const vpColVector &position, const int &id=1)
virtual ~vpQbSoftHand()
void getPosition(vpColVector &position, const int &id=1)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()