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