Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
testVirtuoseAfma6.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 
42 #include <visp3/core/vpTime.h>
43 #include <visp3/robot/vpRobotAfma6.h>
44 #include <visp3/robot/vpVirtuose.h>
45 
46 int main()
47 {
48 #ifdef ENABLE_VISP_NAMESPACE
49  using namespace VISP_NAMESPACE_NAME;
50 #endif
51 #if defined(VISP_HAVE_VIRTUOSE) && defined(VISP_HAVE_AFMA6)
52  vpRobotAfma6 robot;
53  try {
54  vpVirtuose virtuose;
55  virtuose.setVerbose(true);
56  virtuose.setCommandType(COMMAND_TYPE_IMPEDANCE);
57  virtuose.setPowerOn();
58  // virtuose.setSaturation(1.0f,0.0f);
59 
60  vpColVector virt_velocity;
61  vpColVector robot_velocity;
62  vpColVector robot_joint_position;
63  vpColVector robot_cart_position;
64  vpColVector robot_cart_position_init;
65  vpColVector force_feedback_robot(3);
66  float force_limit = 15;
67  int force_increase_rate = 500;
68 
69  double cube_size = 0.15;
70 
72  rMv[0][0] = rMv[0][2] = 0;
73  rMv[1][1] = rMv[1][2] = 0;
74  rMv[2][0] = rMv[2][1] = 0;
75  rMv[0][1] = rMv[1][0] = rMv[2][2] = -1;
76  std::cout << "rMv:\n" << rMv << std::endl;
77  vpVelocityTwistMatrix rVv(rMv);
78 
79  // Set the extrinsic camera parameters obtained with a perspective
80  // projection model including a distortion parameter
82  // Initialize the controller to position control
84  // Moves the robot in the joint space
85  vpColVector q(6, 0);
86  robot.setPositioningVelocity(10);
87  robot.setPosition(vpRobot::ARTICULAR_FRAME, q);
88 
89  robot.getPosition(vpRobot::REFERENCE_FRAME, robot_cart_position_init);
90  vpColVector min(3), max(3);
91  for (unsigned int i = 0; i < 3; i++) {
92  min[i] = robot_cart_position_init[i] - cube_size / 2;
93  max[i] = robot_cart_position_init[i] + cube_size / 2;
94  }
95  std::cout << "min: " << min.t() << std::endl;
96  std::cout << "max: " << max.t() << std::endl;
97 
98  // Initialize the controller to position control
100 
101  for (unsigned int iter = 0; iter < 10000; iter++) {
102  virt_velocity = virtuose.getVelocity();
103  std::cout << "Virtuose velocity: " << virt_velocity.t() << std::endl;
104 
105  robot.getPosition(vpRobot::REFERENCE_FRAME, robot_cart_position);
106 
107  for (int i = 0; i < 3; i++) {
108  if (robot_cart_position[i] >= max[i]) {
109  force_feedback_robot[i] = (max[i] - robot_cart_position[i]) * force_increase_rate;
110  if (force_feedback_robot[i] <= -force_limit)
111  force_feedback_robot[i] = -force_limit;
112  }
113  else if (robot_cart_position[i] <= min[i]) {
114  force_feedback_robot[i] = (min[i] - robot_cart_position[i]) * force_increase_rate;
115  if (force_feedback_robot[i] >= force_limit)
116  force_feedback_robot[i] = force_limit;
117  }
118  else
119  force_feedback_robot[i] = 0;
120  }
121  vpColVector force_feedback_virt = rMv.getRotationMatrix().inverse() * force_feedback_robot;
122 
123  // Printing force feedback
124  std::cout << "Force feedback: " << force_feedback_virt.t() << std::endl;
125 
126  robot_velocity = rVv * virt_velocity;
127  robot.setVelocity(vpRobot::CAMERA_FRAME, robot_velocity);
128 
129  // Set force feedback
130  vpColVector force_feedback(6, 0);
131  force_feedback.insert(0, force_feedback_virt);
132 
133  virtuose.setForce(force_feedback);
134 
135  vpTime::wait(10);
136  }
137  robot.stopMotion();
138  virtuose.setPowerOff();
139  std::cout << "The end" << std::endl;
140  }
141  catch (const vpException &e) {
142  robot.stopMotion();
143  std::cout << "Catch an exception: " << e.getStringMessage() << std::endl;
144  }
145 #else
146  std::cout << "You should install Virtuose SDK to use this binary..." << std::endl;
147 #endif
148 }
@ TOOL_CCMOP
Definition: vpAfma6.h:127
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const std::string & getStringMessage() const
Definition: vpException.cpp:67
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRotationMatrix getRotationMatrix() const
Control of Irisa's gantry robot named Afma6.
Definition: vpRobotAfma6.h:212
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
@ REFERENCE_FRAME
Definition: vpRobot.h:78
@ ARTICULAR_FRAME
Definition: vpRobot.h:80
@ CAMERA_FRAME
Definition: vpRobot.h:84
@ STATE_POSITION_CONTROL
Initialize the position controller.
Definition: vpRobot.h:68
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:67
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:202
vpRotationMatrix inverse() const
vpColVector t() const
vpColVector getVelocity() const
Definition: vpVirtuose.cpp:523
void setPowerOff()
Definition: vpVirtuose.cpp:925
void setForce(const vpColVector &force)
Definition: vpVirtuose.cpp:749
void setCommandType(const VirtCommandType &type)
Definition: vpVirtuose.cpp:729
void setPowerOn()
Definition: vpVirtuose.cpp:938
void setVerbose(bool mode)
Definition: vpVirtuose.h:196
VISP_EXPORT int wait(double t0, double t)