Visual Servoing Platform  version 3.0.0
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/visual_features/vpFeatureBuilder.h>
7 #include <visp3/vs/vpServo.h>
8 #include <visp3/robot/vpSimulatorCamera.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 (int i=0; i<4; i++) {
43  std::ostringstream s; s << "Sphere" << i; name[i] = s.str();
44  ogre.load(name[i], "Sphere.mesh");
45  ogre.setScale(name[i], 0.02f, 0.02f, 0.02f); // Rescale the sphere to 2 cm radius
46  // Set the position of each sphere in the object frame
47  ogre.setPosition(name[i], vpTranslationVector(point[i].get_oX(), point[i].get_oY(), point[i].get_oZ()));
48  }
49 
50  // Add an optional point light source
51  Ogre::Light * light = ogre.getSceneManager()->createLight();
52  light->setDiffuseColour(1, 1, 1); // scaled RGB values
53  light->setSpecularColour(1, 1, 1); // scaled RGB values
54  light->setPosition((Ogre::Real)cdMo[0][3], (Ogre::Real)cdMo[1][3], (Ogre::Real)(-cdMo[2][3]));
55  light->setType(Ogre::Light::LT_POINT);
56 #endif
57 
58  vpServo task ;
61  task.setLambda(0.5);
62 
63  vpFeaturePoint p[4], pd[4] ;
64  for (int i = 0 ; i < 4 ; i++) {
65  point[i].track(cdMo);
66  vpFeatureBuilder::create(pd[i], point[i]);
67  point[i].track(cMo);
68  vpFeatureBuilder::create(p[i], point[i]);
69  task.addFeature(p[i], pd[i]);
70  }
71 
72  vpHomogeneousMatrix wMc, wMo;
73  vpSimulatorCamera robot;
74  robot.setSamplingTime(0.040);
75  robot.getPosition(wMc);
76  wMo = wMc * cMo;
77 
78  for (unsigned int iter=0; iter < 150; iter ++) {
79  robot.getPosition(wMc);
80  cMo = wMc.inverse() * wMo;
81  for (int i = 0 ; i < 4 ; i++) {
82  point[i].track(cMo);
83  vpFeatureBuilder::create(p[i], point[i]);
84  }
85 #if defined(VISP_HAVE_OGRE)
86  // Update the scene from the new camera position
87  ogre.display(background, cMo);
88 #endif
89  vpColVector v = task.computeControlLaw();
91  vpTime::wait( robot.getSamplingTime() * 1000);
92  }
93  task.kill();
94  }
95  catch(vpException e) {
96  std::cout << "Catch an exception: " << e << std::endl;
97  }
98  catch(...) {
99  std::cout << "Catch an exception " << std::endl;
100  }
101 }
102 
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
double getSamplingTime() const
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines the simplest robot: a free flying camera.
void setShowConfigDialog(const bool showConfigDialog)
Definition: vpAROgre.h:250
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:446
error that can be emited by ViSP classes.
Definition: vpException.h:73
void track(const vpHomogeneousMatrix &cMo)
Class that defines a 2D point visual feature which is composed by two parameters that are the cartes...
Implementation of an augmented reality viewer.
Definition: vpAROgre.h:86
Class that defines what is a point.
Definition: vpPoint.h:59
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:186
vpColVector computeControlLaw()
Definition: vpServo.cpp:899
Generic class defining intrinsic camera parameters.
void setLambda(double c)
Definition: vpServo.h:390
vpHomogeneousMatrix getPosition() const
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:519
static double rad(double deg)
Definition: vpMath.h:104
void setWorldCoordinates(const double oX, const double oY, const double oZ)
Definition: vpPoint.cpp:111
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
static void create(vpFeaturePoint &s, const vpCameraParameters &cam, const vpDot &d)
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:217
Class that consider the case of a translation vector.