Visual Servoing Platform  version 3.6.1 under development (2024-11-14)
tutorial-ibvs-4pts-wireframe-robot-afma6.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/vpSimulatorAfma6.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 :
47  w_y
48  /|\
49  |
50  w_x <--
51 
52  object :
53  o_y
54  /|\
55  |
56  o_x <--
57 
58 
59  camera :
60  c_y
61  /|\
62  |
63  c_x <--
64 
65  */
66  vpHomogeneousMatrix wMo(0, 0, 1., 0, 0, 0);
67 
68  std::vector<vpPoint> point;
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  point.push_back(vpPoint(-0.1, 0.1, 0));
73 
74  vpServo task;
77  task.setLambda(0.5);
78 
79  vpFeaturePoint p[4], pd[4];
80  for (unsigned int i = 0; i < 4; i++) {
81  point[i].track(cdMo);
82  vpFeatureBuilder::create(pd[i], point[i]);
83  point[i].track(cMo);
84  vpFeatureBuilder::create(p[i], point[i]);
85  task.addFeature(p[i], pd[i]);
86  }
87 
88  vpSimulatorAfma6 robot(true);
89  robot.setVerbose(true);
90 
91  // Get the default joint limits
92  vpColVector qmin = robot.getJointMin();
93  vpColVector qmax = robot.getJointMax();
94 
95  std::cout << "Robot joint limits: " << std::endl;
96  for (unsigned int i = 0; i < 3; i++)
97  std::cout << "Joint " << i << ": min " << qmin[i] << " max " << qmax[i] << " (m)" << std::endl;
98  for (unsigned int i = 3; i < qmin.size(); i++)
99  std::cout << "Joint " << i << ": min " << vpMath::deg(qmin[i]) << " max " << vpMath::deg(qmax[i]) << " (deg)"
100  << std::endl;
101 
105  robot.set_fMo(wMo);
106  bool ret = robot.initialiseCameraRelativeToObject(cMo);
107  if (ret == false)
108  return EXIT_FAILURE; // Not able to set the position
109  robot.setDesiredCameraPosition(cdMo);
110 
111  vpImage<unsigned char> Iint(480, 640, 255);
112 #if defined(VISP_HAVE_X11)
113  vpDisplayX displayInt(Iint, 700, 0, "Internal view");
114 #elif defined(VISP_HAVE_GDI)
115  vpDisplayGDI displayInt(Iint, 700, 0, "Internal view");
116 #elif defined(HAVE_OPENCV_HIGHGUI)
117  vpDisplayOpenCV displayInt(Iint, 700, 0, "Internal view");
118 #else
119  std::cout << "No image viewer is available..." << std::endl;
120 #endif
121 
122  vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
123  robot.setCameraParameters(cam);
124 
125  bool start = true;
126  for (;;) {
127  cMo = robot.get_cMo();
128 
129  for (unsigned int i = 0; i < 4; i++) {
130  point[i].track(cMo);
131  vpFeatureBuilder::create(p[i], point[i]);
132  }
133 
134  vpDisplay::display(Iint);
135  robot.getInternalView(Iint);
136  if (!start) {
137  display_trajectory(Iint, point, cMo, cam);
138  vpDisplay::displayText(Iint, 40, 120, "Click to stop the servo...", vpColor::red);
139  }
140  vpDisplay::flush(Iint);
141 
142  vpColVector v = task.computeControlLaw();
144 
145  // A click to exit
146  if (vpDisplay::getClick(Iint, false))
147  break;
148 
149  if (start) {
150  start = false;
151  v = 0;
153  vpDisplay::displayText(Iint, 40, 120, "Click to start the servo...", vpColor::blue);
154  vpDisplay::flush(Iint);
155  vpDisplay::getClick(Iint);
156  }
157 
158  vpTime::wait(1000 * robot.getSamplingTime());
159  }
160  }
161  catch (const vpException &e) {
162  std::cout << "Catch an exception: " << e << std::endl;
163  }
164 #endif
165 }
@ TOOL_CCMOP
Definition: vpAfma6.h:127
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
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
VISP_EXPORT int wait(double t0, double t)