Visual Servoing Platform  version 3.6.1 under development (2024-04-12)
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 }
Implementation of an augmented reality viewer using Ogre3D 3rd party.
Definition: vpAROgre.h:96
void setCameraParameters(const vpCameraParameters &cameraP)
Definition: vpAROgre.cpp:656
void setShowConfigDialog(bool showConfigDialog)
Definition: vpAROgre.h:258
void addResource(const std::string &resourceLocation)
Definition: vpAROgre.h:126
Ogre::SceneManager * getSceneManager()
Definition: vpAROgre.h:163
virtual void init(vpImage< unsigned char > &I, bool bufferedKeys=false, bool hidden=false)
Definition: vpAROgre.cpp:115
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMw)
Definition: vpAROgre.cpp:622
void load(const std::string &entityName, const std::string &model)
Definition: vpAROgre.cpp:663
void setPosition(const std::string &sceneName, const vpTranslationVector &wTo)
Definition: vpAROgre.cpp:676
void setScale(const std::string &sceneName, float factorx, float factory, float factorz)
Definition: vpAROgre.cpp:763
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition: vpMath.h:127
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:110
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
@ CAMERA_FRAME
Definition: vpRobot.h:82
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:378
@ EYEINHAND_CAMERA
Definition: vpServo.h:155
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:329
void setLambda(double c)
Definition: vpServo.h:976
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:132
vpColVector computeControlLaw()
Definition: vpServo.cpp:703
@ CURRENT
Definition: vpServo.h:196
Class that defines the simplest robot: a free flying camera.
Class that consider the case of a translation vector.
VISP_EXPORT int wait(double t0, double t)