Visual Servoing Platform  version 3.5.1 under development (2022-12-02)
vpQbSoftHand.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 #ifdef VISP_HAVE_QBDEVICE
41 
42 #include <regex>
43 
44 #include <visp3/robot/vpQbSoftHand.h>
45 
51 
57 
63 void vpQbSoftHand::getCurrent(vpColVector &current, const int &id)
64 {
65  if (!m_init_done) {
66  init(id);
67  }
68 
69  current.resize(1);
70  if (!isInConnectedSet(id)) {
71  throw(vpException(vpException::fatalError, "Cannot get current, Qb device is not connected"));
72  }
73 
74  std::vector<short int> currents(2);
75  int failures = getCurrents(id, m_max_repeats, currents); // blocks while reading
76 
77  if (!isReliable(failures, m_max_repeats)) {
79  "Cannot get current, communication error with Qb device after %d attempts", m_max_repeats));
80  }
81  current[0] = static_cast<double>(currents[0]);
82 }
83 
90 void vpQbSoftHand::getPosition(vpColVector &position, const int &id)
91 {
92  if (!m_init_done) {
93  init(id);
94  }
95 
96  position.resize(1);
97  if (!isInConnectedSet(id)) {
98  throw(vpException(vpException::fatalError, "Cannot get position, Qb device is not connected"));
99  }
100 
101  std::vector<short int> positions;
102  int failures = getPositions(id, m_max_repeats, positions); // blocks while reading
103 
104  position[0] = static_cast<double>(positions[0]) / static_cast<double>(getPositionLimits()[1]);
105 
106  if (!isReliable(failures, m_max_repeats)) {
108  "Cannot get position, communication error with Qb device after %d attempts", m_max_repeats));
109  }
110 }
111 
118 void vpQbSoftHand::setPosition(const vpColVector &position, const int &id)
119 {
120  if (!m_init_done) {
121  init(id);
122  }
123 
124  std::vector<short int> commands(2);
125  if (position.size() != 1) {
126  throw(vpException(vpException::fatalError, "Command vector size %d is not equal to 2", position.size()));
127  }
128 
129  std::vector<short int> position_limits = getPositionLimits();
130 
131  commands[0] = static_cast<short int>(position[0] * position_limits[1]);
132 
133  if (commands[0] < position_limits[0]) {
134  commands[0] = position_limits[0];
135  } else if (commands[0] > position_limits[1]) {
136  commands[0] = position_limits[1];
137  }
138 
139  if (!isInConnectedSet(id)) {
140  throw(vpException(vpException::fatalError, "Cannot set position, Qb device is not connected"));
141  }
142 
143  // int failures = setCommandsAndWait(id, m_max_repeats, commands); // FS: doesn't work
144  int failures = setCommandsAsync(id, commands);
145 
146  if (!isReliable(failures, m_max_repeats)) {
148  "Cannot set position, communication error with Qb device after %d attempts", m_max_repeats));
149  }
150 }
151 
162 void vpQbSoftHand::setPosition(const vpColVector &position, double speed_factor, double stiffness, const int &id)
163 {
164  vpColVector q_mes(1), q(1), current;
165  getPosition(q_mes, id);
166  double current_max = getCurrentMax();
167 
168  double max_delta_q = 1; // 0 opened, 1 closed
169  double min_delta_t = 2.0; // number of [sec] to open or close with the max velocity
170  double precision = 0.05;
171  double delta_t = 40; // [ms]
172  double max_slope = max_delta_q / min_delta_t;
173  double sign = (position[0] > q_mes[0]) ? 1.0 : -1.0;
174  double vel = speed_factor;
175  if (vel < 0.01) {
176  vel = 0.01;
177  } else if (vel > 1.) {
178  vel = 1.0;
179  }
180  double current_factor = stiffness;
181  if (current_factor < 0.0) {
182  current_factor = 0.0;
183  } else if (current_factor > 1.) {
184  current_factor = 1.0;
185  }
186  double slope = sign * max_slope * vel;
187 
188  unsigned int i = 0;
189  int current_failures = 0;
190  do {
191  double t0 = vpTime::measureTimeMs();
192  q[0] = q_mes[0] + slope * delta_t / 1000.0 * i;
193  if (q[0] < getPositionLimits()[0]) {
194  q[0] = getPositionLimits()[0];
195  } else if (q[0] > getPositionLimits()[1]) {
196  q[0] = getPositionLimits()[1];
197  }
198  setPosition(q, id);
199  getCurrent(current, id);
200  i++;
201 
202  if (std::fabs(current[0]) > current_factor * current_max) {
203  current_failures++;
204  } else {
205  current_failures = 0;
206  }
207 
208  vpTime::wait(t0, delta_t);
209  } while (!vpMath::equal(q[0], position[0], precision) && !(current_failures > 1));
210 }
211 #endif
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:293
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:314
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ fatalError
Fatal error.
Definition: vpException.h:96
static bool equal(double x, double y, double s=0.001)
Definition: vpMath.h:364
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:115
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:116
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()