Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
tutorial-ibvs-4pts-ogre.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #ifdef VISP_HAVE_MODULE_AR
4 #include <visp3/ar/vpAROgre.h>
5 #endif
6 #include <visp3/robot/vpSimulatorCamera.h>
7 #include <visp3/visual_features/vpFeatureBuilder.h>
8 #include <visp3/vs/vpServo.h>
9 
10 int main()
11 {
12  try {
13  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
14  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
15 
16  // Define the target as 4 points
17  vpPoint point[4];
18  point[0].setWorldCoordinates(-0.1, -0.1, 0);
19  point[1].setWorldCoordinates(0.1, -0.1, 0);
20  point[2].setWorldCoordinates(0.1, 0.1, 0);
21  point[3].setWorldCoordinates(-0.1, 0.1, 0);
22 
23 #if defined(VISP_HAVE_OGRE)
24  // Color image used as background texture.
25  vpImage<unsigned char> background(480, 640, 255);
26 
27  // Parameters of our camera
28  vpCameraParameters cam(840, 840, background.getWidth() / 2, background.getHeight() / 2);
29 
30  // Our object
31  // A simulator with the camera parameters defined above,
32  // and the background image size
33  vpAROgre ogre;
34  ogre.setShowConfigDialog(false);
35  ogre.setCameraParameters(cam);
36  ogre.addResource("./"); // Add the path to the Sphere.mesh resource
37  ogre.init(background, false, true);
38 
39  // Create the scene that contains 4 spheres
40  // Sphere.mesh contains a sphere with 1 meter radius
41  std::vector<std::string> name(4);
42  for (unsigned int i = 0; i < 4; i++) {
43  std::ostringstream s;
44  s << "Sphere" << i;
45  name[i] = s.str();
46  ogre.load(name[i], "Sphere.mesh");
47  ogre.setScale(name[i], 0.02f, 0.02f,
48  0.02f); // Rescale the sphere to 2 cm radius
49  // Set the position of each sphere in the object frame
50  ogre.setPosition(name[i], vpTranslationVector(point[i].get_oX(), point[i].get_oY(), point[i].get_oZ()));
51  }
52 
53  // Add an optional point light source
54  Ogre::Light *light = ogre.getSceneManager()->createLight();
55  light->setDiffuseColour(1, 1, 1); // scaled RGB values
56  light->setSpecularColour(1, 1, 1); // scaled RGB values
57  light->setPosition((Ogre::Real)cdMo[0][3], (Ogre::Real)cdMo[1][3], (Ogre::Real)(-cdMo[2][3]));
58  light->setType(Ogre::Light::LT_POINT);
59 #endif
60 
61  vpServo task;
64  task.setLambda(0.5);
65 
66  vpFeaturePoint p[4], pd[4];
67  for (int i = 0; i < 4; i++) {
68  point[i].track(cdMo);
69  vpFeatureBuilder::create(pd[i], point[i]);
70  point[i].track(cMo);
71  vpFeatureBuilder::create(p[i], point[i]);
72  task.addFeature(p[i], pd[i]);
73  }
74 
75  vpHomogeneousMatrix wMc, wMo;
76  vpSimulatorCamera robot;
77  robot.setSamplingTime(0.040);
78  robot.getPosition(wMc);
79  wMo = wMc * cMo;
80 
81  for (unsigned int iter = 0; iter < 150; iter++) {
82  robot.getPosition(wMc);
83  cMo = wMc.inverse() * wMo;
84  for (int i = 0; i < 4; i++) {
85  point[i].track(cMo);
86  vpFeatureBuilder::create(p[i], point[i]);
87  }
88 #if defined(VISP_HAVE_OGRE)
89  // Update the scene from the new camera position
90  ogre.display(background, cMo);
91 #endif
92  vpColVector v = task.computeControlLaw();
94  vpTime::wait(robot.getSamplingTime() * 1000);
95  }
96  } catch (const vpException &e) {
97  std::cout << "Catch an exception: " << e << std::endl;
98  } catch (...) {
99  std::cout << "Catch an exception " << std::endl;
100  }
101 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:113
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:490
error that can be emited by ViSP classes.
Definition: vpException.h:71
void track(const vpHomogeneousMatrix &cMo)
vpHomogeneousMatrix inverse() const
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:90
vpHomogeneousMatrix getPosition() const
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:81
virtual void setSamplingTime(const double &delta_t)
vpColVector computeControlLaw()
Definition: vpServo.cpp:929
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:404
void setShowConfigDialog(bool showConfigDialog)
Definition: vpAROgre.h:258
double getSamplingTime() const
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:567
static double rad(double deg)
Definition: vpMath.h:110
Implementation of column vector and the associated operations.
Definition: vpColVector.h:130
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:218
Class that consider the case of a translation vector.