Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
manServoMomentsSimple.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Example of visual servoing with moments using a polygon as object container
32 */
33
39
40#include <visp3/core/vpConfig.h>
41#include <visp3/core/vpPoint.h> //the basic tracker
42
43#include <iostream> //some console output
44#include <limits>
45#include <vector> //store the polygon
46#include <visp3/core/vpException.h>
47#include <visp3/core/vpMomentCommon.h> //update the common database with the object
48#include <visp3/core/vpMomentObject.h> //transmit the polygon to the object
49#include <visp3/core/vpPlane.h>
50#include <visp3/robot/vpSimulatorCamera.h>
51#include <visp3/visual_features/vpFeatureMomentCommon.h> //init the feature database using the information about moment dependencies
52#include <visp3/vs/vpServo.h> //visual servoing task
53
54#ifdef ENABLE_VISP_NAMESPACE
55using namespace VISP_NAMESPACE_NAME;
56#endif
57
58// this function converts the plane defined by the cMo to 1/Z=Ax+By+C plane
59// form
60void cMoToABC(vpHomogeneousMatrix &cMo, double &A, double &B, double &C);
61
62void cMoToABC(vpHomogeneousMatrix &cMo, double &A, double &B, double &C)
63{
64 vpPlane pl;
65 pl.setABCD(0, 0, 1.0, 0);
66 pl.changeFrame(cMo);
67
68 if (fabs(pl.getD()) < std::numeric_limits<double>::epsilon()) {
69 std::cout << "Invalid position:" << std::endl;
70 std::cout << cMo << std::endl;
71 std::cout << "Cannot put plane in the form 1/Z=Ax+By+C." << std::endl;
72 throw vpException(vpException::divideByZeroError, "invalid position!");
73 }
74 A = -pl.getA() / pl.getD();
75 B = -pl.getB() / pl.getD();
76 C = -pl.getC() / pl.getD();
77}
78
79int main()
80{
81 try {
82 double x[8] = { 1, 3, 4, -1, -3, -2, -1, 1 };
83 double y[8] = { 0, 1, 4, 4, -2, -2, 1, 0 };
84 double A, B, C, Ad, Bd, Cd;
85
86 int nbpoints = 8;
87 std::vector<vpPoint> vec_p,
88 vec_p_d; // vectors that contain the vertices of the contour polygon
89
92 vpHomogeneousMatrix wMo; // Set to identity
93 vpHomogeneousMatrix wMc; // Camera position in the world frame
94
95 cMoToABC(cMo, A, B, C);
96 cMoToABC(cdMo, Ad, Bd, Cd);
97 // Define source and destination polygons
98 for (int i = 0; i < nbpoints; i++) {
99 vpPoint p(x[i], y[i], 0.0);
100 p.track(cMo);
101 vec_p.push_back(p);
102 p.track(cdMo);
103 vec_p_d.push_back(p);
104 }
105
106 vpMomentObject cur(6); // Create a source moment object with 6 as maximum order
107 cur.setType(vpMomentObject::DENSE_POLYGON); // The object is defined by a
108 // countour polygon
109 cur.fromVector(vec_p); // Init the dense object with the source polygon
110
111 vpMomentObject dst(6); // Create a destination moment object with 6 as maximum order
112 dst.setType(vpMomentObject::DENSE_POLYGON); // The object is defined by a
113 // countour polygon
114 dst.fromVector(vec_p_d); // Init the dense object with the destination polygon
115
116 // init classic moment primitives (for source)
118 vpMomentCommon::getAlpha(dst)); // Init classic features
119 vpFeatureMomentCommon fmdb_cur(mdb_cur);
120
123 vpMomentCommon::getAlpha(dst)); // Init classic features
124 vpFeatureMomentCommon fmdb_dst(mdb_dst);
125
126 // update+compute moment primitives from object (for destination)
127 mdb_dst.updateAll(dst);
128 // update+compute features (+interaction matrixes) from plane
129 fmdb_dst.updateAll(Ad, Bd, Cd);
130
131 // define visual servoing task
134 task.setInteractionMatrixType(vpServo::CURRENT);
135 task.setLambda(1);
136
137 task.addFeature(fmdb_cur.getFeatureGravityNormalized(), fmdb_dst.getFeatureGravityNormalized());
138 task.addFeature(fmdb_cur.getFeatureAn(), fmdb_dst.getFeatureAn());
139 // the object is NOT symmetric
140 // select C4 and C6
141 task.addFeature(fmdb_cur.getFeatureCInvariant(), fmdb_dst.getFeatureCInvariant(),
143 task.addFeature(fmdb_cur.getFeatureAlpha(), fmdb_dst.getFeatureAlpha());
144
145 vpBasicFeature *al = new vpFeatureMomentAlpha(mdb_dst, 0, 0, 1.);
146 al->init();
147 al->error(*al);
148 // param robot
149 vpSimulatorCamera robot;
150 float sampling_time = 0.010f; // Sampling period in seconds
151 robot.setSamplingTime(sampling_time);
152 wMc = wMo * cMo.inverse();
153 robot.setPosition(wMc);
154
155 do {
156 wMc = robot.getPosition();
157 cMo = wMc.inverse() * wMo;
158 vec_p.clear();
159
160 for (int i = 0; i < nbpoints; i++) {
161 vpPoint p(x[i], y[i], 0.0);
162 p.track(cMo);
163 vec_p.push_back(p);
164 }
165 cMoToABC(cMo, A, B, C);
166
167 cur.fromVector(vec_p);
168 // update+compute moment primitives from object (for source)
169 mdb_cur.updateAll(cur);
170 // update+compute features (+interaction matrixes) from plane
171 fmdb_cur.updateAll(A, B, C);
172
173 vpColVector v = task.computeControlLaw();
174 task.print();
175 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
176 double t = vpTime::measureTimeMs();
177 vpTime::wait(t, sampling_time * 1000); // Wait 10 ms
178 } while ((task.getError()).sumSquare() > 0.005);
179 std::cout << "final error=" << (task.getError()).sumSquare() << std::endl;
180 return EXIT_SUCCESS;
181 }
182 catch (const vpException &e) {
183 std::cout << "Catch an exception: " << e << std::endl;
184 return EXIT_FAILURE;
185 }
186}
class that defines what is a visual feature
virtual vpColVector error(const vpBasicFeature &s_star, unsigned int select=FEATURE_ALL)
virtual void init()=0
Implementation of column vector and the associated operations.
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ divideByZeroError
Division by zero.
Definition vpException.h:70
Functionality computation for in-plane rotation moment feature : computes the interaction matrix asso...
This class allows to access common vpFeatureMoments in a pre-filled database.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
Definition vpMath.h:129
This class initializes and allows access to commonly used moments.
static std::vector< double > getMu3(vpMomentObject &object)
static double getAlpha(vpMomentObject &object)
static double getSurface(vpMomentObject &object)
Class for generic objects.
This class defines the container for a plane geometrical structure.
Definition vpPlane.h:56
void changeFrame(const vpHomogeneousMatrix &cMo)
Definition vpPlane.cpp:465
double getD() const
Definition vpPlane.h:106
double getA() const
Definition vpPlane.h:100
double getC() const
Definition vpPlane.h:104
void setABCD(double a, double b, double c, double d)
Definition vpPlane.h:88
double getB() const
Definition vpPlane.h:102
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
virtual void setSamplingTime(const double &delta_t)
@ CAMERA_FRAME
Definition vpRobot.h:81
@ EYEINHAND_CAMERA
Definition vpServo.h:176
@ CURRENT
Definition vpServo.h:217
Class that defines the simplest robot: a free flying camera.
VISP_EXPORT double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)