Visual Servoing Platform  version 3.6.1 under development (2025-02-18)
tutorial-ibvs-4pts-wireframe-robot-afma6.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayFactory.h>
4 #include <visp3/robot/vpSimulatorAfma6.h>
5 #include <visp3/visual_features/vpFeatureBuilder.h>
6 #include <visp3/vs/vpServo.h>
7 
8 #ifdef ENABLE_VISP_NAMESPACE
9 using namespace VISP_NAMESPACE_NAME;
10 #endif
11 
12 void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
13  const vpCameraParameters &cam)
14 {
15  unsigned int thickness = 3;
16  static std::vector<vpImagePoint> traj[4];
17  vpImagePoint cog;
18  for (unsigned int i = 0; i < 4; i++) {
19  // Project the point at the given camera position
20  point[i].project(cMo);
21  vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
22  traj[i].push_back(cog);
23  }
24  for (unsigned int i = 0; i < 4; i++) {
25  for (unsigned int j = 1; j < traj[i].size(); j++) {
26  vpDisplay::displayLine(I, traj[i][j - 1], traj[i][j], vpColor::green, thickness);
27  }
28  }
29 }
30 
31 int main()
32 {
33 #if defined(VISP_HAVE_THREADS)
34 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
35  std::shared_ptr<vpDisplay> display;
36 #else
37  vpDisplay *display = nullptr;
38 #endif
39 
40  try {
41  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
42  vpHomogeneousMatrix cMo(-0.15, 0.1, 1., vpMath::rad(-10), vpMath::rad(10), vpMath::rad(50));
43 
44  /*
45  Top view of the world frame, the camera frame and the object frame
46 
47  world, also robot base frame :
48  w_y
49  /|\
50  |
51  w_x <--
52 
53  object :
54  o_y
55  /|\
56  |
57  o_x <--
58 
59 
60  camera :
61  c_y
62  /|\
63  |
64  c_x <--
65 
66  */
67  vpHomogeneousMatrix wMo(0, 0, 1., 0, 0, 0);
68 
69  std::vector<vpPoint> point;
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  point.push_back(vpPoint(-0.1, 0.1, 0));
74 
75  vpServo task;
78  task.setLambda(0.5);
79 
80  vpFeaturePoint p[4], pd[4];
81  for (unsigned int i = 0; i < 4; i++) {
82  point[i].track(cdMo);
83  vpFeatureBuilder::create(pd[i], point[i]);
84  point[i].track(cMo);
85  vpFeatureBuilder::create(p[i], point[i]);
86  task.addFeature(p[i], pd[i]);
87  }
88 
89  vpSimulatorAfma6 robot(true);
90  robot.setVerbose(true);
91 
92  // Get the default joint limits
93  vpColVector qmin = robot.getJointMin();
94  vpColVector qmax = robot.getJointMax();
95 
96  std::cout << "Robot joint limits: " << std::endl;
97  for (unsigned int i = 0; i < 3; i++)
98  std::cout << "Joint " << i << ": min " << qmin[i] << " max " << qmax[i] << " (m)" << std::endl;
99  for (unsigned int i = 3; i < qmin.size(); i++)
100  std::cout << "Joint " << i << ": min " << vpMath::deg(qmin[i]) << " max " << vpMath::deg(qmax[i]) << " (deg)"
101  << std::endl;
102 
106  robot.set_fMo(wMo);
107  bool ret = robot.initialiseCameraRelativeToObject(cMo);
108  if (ret == false)
109  return EXIT_FAILURE; // Not able to set the position
110  robot.setDesiredCameraPosition(cdMo);
111 
112  vpImage<unsigned char> Iint(480, 640, 255);
113 #if defined(VISP_HAVE_DISPLAY)
114 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
115  display = vpDisplayFactory::createDisplay(Iint, 700, 0, "Internal view");
116 #else
117  display = vpDisplayFactory::allocateDisplay(Iint, 700, 0, "Internal view");
118 #endif
119 #else
120  std::cout << "No image viewer is available..." << std::endl;
121 #endif
122 
123  vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
124  robot.setCameraParameters(cam);
125 
126  bool start = true;
127  for (;;) {
128  cMo = robot.get_cMo();
129 
130  for (unsigned int i = 0; i < 4; i++) {
131  point[i].track(cMo);
132  vpFeatureBuilder::create(p[i], point[i]);
133  }
134 
135  vpDisplay::display(Iint);
136  robot.getInternalView(Iint);
137  if (!start) {
138  display_trajectory(Iint, point, cMo, cam);
139  vpDisplay::displayText(Iint, 40, 120, "Click to stop the servo...", vpColor::red);
140  }
141  vpDisplay::flush(Iint);
142 
143  vpColVector v = task.computeControlLaw();
145 
146  // A click to exit
147  if (vpDisplay::getClick(Iint, false))
148  break;
149 
150  if (start) {
151  start = false;
152  v = 0;
154  vpDisplay::displayText(Iint, 40, 120, "Click to start the servo...", vpColor::blue);
155  vpDisplay::flush(Iint);
156  vpDisplay::getClick(Iint);
157  }
158 
159  vpTime::wait(1000 * robot.getSamplingTime());
160  }
161  }
162  catch (const vpException &e) {
163  std::cout << "Catch an exception: " << e << std::endl;
164  }
165 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
166  if (display != nullptr) {
167  delete display;
168  }
169 #endif
170 #endif
171 }
@ TOOL_CCMOP
Definition: vpAfma6.h:127
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:429
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:198
static const vpColor blue
Definition: vpColor.h:204
static const vpColor green
Definition: vpColor.h:201
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
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:991
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
@ CURRENT
Definition: vpServo.h:202
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)