ViSP  2.9.0
tutorial-ibvs-4pts-ogre.cpp
1 
3 #include <visp/vpAROgre.h>
4 #include <visp/vpFeatureBuilder.h>
5 #include <visp/vpServo.h>
6 #include <visp/vpSimulatorCamera.h>
7 
8 int main()
9 {
10  try {
11  vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
12  vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
13 
14  // Define the target as 4 points
15  vpPoint point[4] ;
16  point[0].setWorldCoordinates(-0.1,-0.1, 0);
17  point[1].setWorldCoordinates( 0.1,-0.1, 0);
18  point[2].setWorldCoordinates( 0.1, 0.1, 0);
19  point[3].setWorldCoordinates(-0.1, 0.1, 0);
20 
21 #if defined(VISP_HAVE_OGRE)
22  // Color image used as background texture.
23  vpImage<unsigned char> background(480, 640, 255);
24 
25  // Parameters of our camera
26  vpCameraParameters cam(840, 840, background.getWidth()/2, background.getHeight()/2);
27 
28  // Our object
29  // A simulator with the camera parameters defined above,
30  // and the background image size
31  vpAROgre ogre;
32  ogre.setShowConfigDialog(false);
33  ogre.setCameraParameters(cam);
34  ogre.addResource("./"); // Add the path to the Sphere.mesh resource
35  ogre.init(background, false, true);
36 
37  // Create the scene that contains 4 spheres
38  // Sphere.mesh contains a sphere with 1 meter radius
39  std::vector<std::string> name(4);
40  for (int i=0; i<4; i++) {
41  std::ostringstream s; s << "Sphere" << i; name[i] = s.str();
42  ogre.load(name[i], "Sphere.mesh");
43  ogre.setScale(name[i], 0.02f, 0.02f, 0.02f); // Rescale the sphere to 2 cm radius
44  // Set the position of each sphere in the object frame
45  ogre.setPosition(name[i], vpTranslationVector(point[i].get_oX(), point[i].get_oY(), point[i].get_oZ()));
46  }
47 
48  // Add an optional point light source
49  Ogre::Light * light = ogre.getSceneManager()->createLight();
50  light->setDiffuseColour(1, 1, 1); // scaled RGB values
51  light->setSpecularColour(1, 1, 1); // scaled RGB values
52  light->setPosition((Ogre::Real)cdMo[0][3], (Ogre::Real)cdMo[1][3], (Ogre::Real)(-cdMo[2][3]));
53  light->setType(Ogre::Light::LT_POINT);
54 #endif
55 
56  vpServo task ;
59  task.setLambda(0.5);
60 
61  vpFeaturePoint p[4], pd[4] ;
62  for (int i = 0 ; i < 4 ; i++) {
63  point[i].track(cdMo);
64  vpFeatureBuilder::create(pd[i], point[i]);
65  point[i].track(cMo);
66  vpFeatureBuilder::create(p[i], point[i]);
67  task.addFeature(p[i], pd[i]);
68  }
69 
70  vpHomogeneousMatrix wMc, wMo;
71  vpSimulatorCamera robot;
72  robot.setSamplingTime(0.040);
73  robot.getPosition(wMc);
74  wMo = wMc * cMo;
75 
76  for (unsigned int iter=0; iter < 150; iter ++) {
77  robot.getPosition(wMc);
78  cMo = wMc.inverse() * wMo;
79  for (int i = 0 ; i < 4 ; i++) {
80  point[i].track(cMo);
81  vpFeatureBuilder::create(p[i], point[i]);
82  }
83 #if defined(VISP_HAVE_OGRE)
84  // Update the scene from the new camera position
85  ogre.display(background, cMo);
86 #endif
87  vpColVector v = task.computeControlLaw();
89  vpTime::wait( robot.getSamplingTime() * 1000);
90  }
91  task.kill();
92  }
93  catch(vpException e) {
94  std::cout << "Catch an exception: " << e << std::endl;
95  }
96  catch(...) {
97  std::cout << "Catch an exception " << std::endl;
98  }
99 }
100 
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)
double getSamplingTime() const
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Class that defines the simplest robot: a free flying camera.
void setShowConfigDialog(const bool showConfigDialog)
Definition: vpAROgre.h:221
void addFeature(vpBasicFeature &s, vpBasicFeature &s_star, const unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:449
error that can be emited by ViSP classes.
Definition: vpException.h:76
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:90
static int wait(double t0, double t)
Definition: vpTime.cpp:149
Class that defines what is a point.
Definition: vpPoint.h:65
virtual void setSamplingTime(const double &delta_t)
void kill()
Definition: vpServo.cpp:189
vpColVector computeControlLaw()
Definition: vpServo.cpp:902
Generic class defining intrinsic camera parameters.
void getPosition(vpHomogeneousMatrix &wMc) const
void setLambda(double c)
Definition: vpServo.h:370
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:522
static double rad(double deg)
Definition: vpMath.h:100
Class that provides a data structure for the column vectors as well as a set of operations on these v...
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:220
Class that consider the case of a translation vector.
void setWorldCoordinates(const double ox, const double oy, const double oz)
Set the point world coordinates. We mean here the coordinates of the point in the object frame...
Definition: vpPoint.cpp:74