Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
tutorial-ibvs-4pts-wireframe-robot-viper.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/robot/vpSimulatorViper850.h>
7 #include <visp3/visual_features/vpFeatureBuilder.h>
8 #include <visp3/vs/vpServo.h>
9 
10 #ifdef ENABLE_VISP_NAMESPACE
11 using namespace VISP_NAMESPACE_NAME;
12 #endif
13 
14 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
15  const vpCameraParameters &cam);
16 
17 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
18  const vpCameraParameters &cam)
19 {
20  unsigned int thickness = 3;
21  static std::vector<vpImagePoint> traj[4];
22  vpImagePoint cog;
23  for (unsigned int i = 0; i < 4; i++) {
24  // Project the point at the given camera position
25  point[i].project(cMo);
26  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
27  traj[i].push_back(cog);
28  }
29  for (unsigned int i = 0; i < 4; i++) {
30  for (unsigned int j = 1; j < traj[i].size(); j++) {
31  vpDisplay::displayLine(I, traj[i][j - 1], traj[i][j], vpColor::green, thickness);
32  }
33  }
34 }
35 
36 int main()
37 {
38 #if defined(VISP_HAVE_THREADS)
39  try {
40  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
41  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
42 
43  /*
44  Top view of the world frame, the camera frame and the object frame
45 
46  world, also robot base frame : --> w_y
47  |
48  \|/
49  w_x
50 
51  object :
52  o_y
53  /|\
54  |
55  o_x <--
56 
57 
58  camera :
59  c_y
60  /|\
61  |
62  c_x <--
63 
64  */
65  vpHomogeneousMatrix wMo(vpTranslationVector(0.40, 0, -0.15), vpRotationMatrix(vpRxyzVector(-M_PI, 0, M_PI / 2.)));
66 
67  std::vector<vpPoint> point;
68  point.push_back(vpPoint(-0.1, -0.1, 0));
69  point.push_back(vpPoint(0.1, -0.1, 0));
70  point.push_back(vpPoint(0.1, 0.1, 0));
71  point.push_back(vpPoint(-0.1, 0.1, 0));
72 
73  vpServo task;
76  task.setLambda(0.5);
77 
78  vpFeaturePoint p[4], pd[4];
79  for (unsigned int i = 0; i < 4; i++) {
80  point[i].track(cdMo);
81  vpFeatureBuilder::create(pd[i], point[i]);
82  point[i].track(cMo);
83  vpFeatureBuilder::create(p[i], point[i]);
84  task.addFeature(p[i], pd[i]);
85  }
86 
87  vpSimulatorViper850 robot(true);
88  robot.setVerbose(true);
89 
90  // Enlarge the default joint limits
91  vpColVector qmin = robot.getJointMin();
92  vpColVector qmax = robot.getJointMax();
93  qmin[0] = -vpMath::rad(180);
94  qmax[0] = vpMath::rad(180);
95  qmax[1] = vpMath::rad(0);
96  qmax[2] = vpMath::rad(270);
97  qmin[4] = -vpMath::rad(180);
98  qmax[4] = vpMath::rad(180);
99 
100  robot.setJointLimit(qmin, qmax);
101 
102  std::cout << "Robot joint limits: " << std::endl;
103  for (unsigned int i = 0; i < qmin.size(); i++)
104  std::cout << "Joint " << i << ": min " << vpMath::deg(qmin[i]) << " max " << vpMath::deg(qmax[i]) << " (deg)"
105  << std::endl;
106 
110  robot.set_fMo(wMo);
111  bool ret = robot.initialiseCameraRelativeToObject(cMo);
112  if (ret == false)
113  return EXIT_FAILURE; // Not able to set the position
114  robot.setDesiredCameraPosition(cdMo);
115  // We modify the default external camera position
116  robot.setExternalCameraPosition(
117  vpHomogeneousMatrix(vpTranslationVector(-0.4, 0.4, 2), vpRotationMatrix(vpRxyzVector(M_PI / 2, 0, 0))));
118 
119  vpImage<unsigned char> Iint(480, 640, 255);
120 #if defined(VISP_HAVE_X11)
121  vpDisplayX displayInt(Iint, 700, 0, "Internal view");
122 #elif defined(VISP_HAVE_GDI)
123  vpDisplayGDI displayInt(Iint, 700, 0, "Internal view");
124 #elif defined(HAVE_OPENCV_HIGHGUI)
125  vpDisplayOpenCV displayInt(Iint, 700, 0, "Internal view");
126 #else
127  std::cout << "No image viewer is available..." << std::endl;
128 #endif
129 
130  vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
131  // Modify the camera parameters to match those used in the other
132  // simulations
133  robot.setCameraParameters(cam);
134 
135  bool start = true;
136  // for ( ; ; )
137  for (int iter = 0; iter < 275; iter++) {
138  cMo = robot.get_cMo();
139 
140  for (unsigned int i = 0; i < 4; i++) {
141  point[i].track(cMo);
142  vpFeatureBuilder::create(p[i], point[i]);
143  }
144 
145  vpDisplay::display(Iint);
146  robot.getInternalView(Iint);
147  if (!start) {
148  display_trajectory(Iint, point, cMo, cam);
149  vpDisplay::displayText(Iint, 40, 120, "Click to stop the servo...", vpColor::red);
150  }
151  vpDisplay::flush(Iint);
152 
153  vpColVector v = task.computeControlLaw();
155 
156  // A click to exit
157  if (vpDisplay::getClick(Iint, false))
158  break;
159 
160  if (start) {
161  start = false;
162  v = 0;
164  vpDisplay::displayText(Iint, 40, 120, "Click to start the servo...", vpColor::blue);
165  vpDisplay::flush(Iint);
166  // vpDisplay::getClick(Iint);
167  }
168 
169  vpTime::wait(1000 * robot.getSamplingTime());
170  }
171  }
172  catch (const vpException &e) {
173  std::cout << "Catch an exception: " << e << std::endl;
174  }
175 #endif
176 }
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:349
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
static const vpColor red
Definition: vpColor.h:217
static const vpColor blue
Definition: vpColor.h:223
static const vpColor green
Definition: vpColor.h:220
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpImagePoint &t)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static double rad(double deg)
Definition: vpMath.h:129
static double deg(double rad)
Definition: vpMath.h:119
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
void setVerbose(bool verbose)
Definition: vpRobot.h:170
@ CAMERA_FRAME
Definition: vpRobot.h:84
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition: vpRobot.h:67
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:202
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
Definition: vpRxyzVector.h:183
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition: vpServo.h:161
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void setLambda(double c)
Definition: vpServo.h:986
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
Class that consider the case of a translation vector.
@ TOOL_PTGREY_FLEA2_CAMERA
Definition: vpViper850.h:122
VISP_EXPORT int wait(double t0, double t)