Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-ibvs-4pts-wireframe-robot-viper.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/gui/vpDisplayFactory.h>
4#include <visp3/robot/vpSimulatorViper850.h>
5#include <visp3/visual_features/vpFeatureBuilder.h>
6#include <visp3/vs/vpServo.h>
7
8#ifdef ENABLE_VISP_NAMESPACE
9using namespace VISP_NAMESPACE_NAME;
10#endif
11
12void display_trajectory(const vpImage<unsigned char> &I, std::vector<vpPoint> &point, const vpHomogeneousMatrix &cMo,
13 const vpCameraParameters &cam)
14{
15 unsigned int thickness = 3;
16 VP_ATTRIBUTE_NO_DESTROY static std::vector<vpImagePoint> traj[4];
17 vpImagePoint cog;
18 for (unsigned int i = 0; i < 4; i++) {
19 // Project the point at the given camera position
20 point[i].project(cMo);
21 vpMeterPixelConversion::convertPoint(cam, point[i].get_x(), point[i].get_y(), cog);
22 traj[i].push_back(cog);
23 }
24 for (unsigned int i = 0; i < 4; i++) {
25 for (unsigned int j = 1; j < traj[i].size(); j++) {
26 vpDisplay::displayLine(I, traj[i][j - 1], traj[i][j], vpColor::green, thickness);
27 }
28 }
29}
30
31int main()
32{
33#if defined(VISP_HAVE_THREADS)
34#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
35 std::shared_ptr<vpDisplay> display;
36#else
37 vpDisplay *display = nullptr;
38#endif
39 try {
40 vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
41 vpHomogeneousMatrix cMo(0.15, -0.1, 1., vpMath::rad(10), vpMath::rad(-10), vpMath::rad(50));
42
43 /*
44 Top view of the world frame, the camera frame and the object frame
45
46 world, also robot base frame : --> w_y
47 |
48 \|/
49 w_x
50
51 object :
52 o_y
53 /|\
54 |
55 o_x <--
56
57
58 camera :
59 c_y
60 /|\
61 |
62 c_x <--
63
64 */
65 vpHomogeneousMatrix wMo(vpTranslationVector(0.40, 0, -0.15), vpRotationMatrix(vpRxyzVector(-M_PI, 0, M_PI / 2.)));
66
67 std::vector<vpPoint> point;
68 point.push_back(vpPoint(-0.1, -0.1, 0));
69 point.push_back(vpPoint(0.1, -0.1, 0));
70 point.push_back(vpPoint(0.1, 0.1, 0));
71 point.push_back(vpPoint(-0.1, 0.1, 0));
72
75 task.setInteractionMatrixType(vpServo::CURRENT);
76 task.setLambda(0.5);
77
78 vpFeaturePoint p[4], pd[4];
79 for (unsigned int i = 0; i < 4; i++) {
80 point[i].track(cdMo);
81 vpFeatureBuilder::create(pd[i], point[i]);
82 point[i].track(cMo);
83 vpFeatureBuilder::create(p[i], point[i]);
84 task.addFeature(p[i], pd[i]);
85 }
86
87 vpSimulatorViper850 robot(true);
88 robot.setVerbose(true);
89
90 // Enlarge the default joint limits
91 vpColVector qmin = robot.getJointMin();
92 vpColVector qmax = robot.getJointMax();
93 qmin[0] = -vpMath::rad(180);
94 qmax[0] = vpMath::rad(180);
95 qmax[1] = vpMath::rad(0);
96 qmax[2] = vpMath::rad(270);
97 qmin[4] = -vpMath::rad(180);
98 qmax[4] = vpMath::rad(180);
99
100 robot.setJointLimit(qmin, qmax);
101
102 std::cout << "Robot joint limits: " << std::endl;
103 for (unsigned int i = 0; i < qmin.size(); i++)
104 std::cout << "Joint " << i << ": min " << vpMath::deg(qmin[i]) << " max " << vpMath::deg(qmax[i]) << " (deg)"
105 << std::endl;
106
108 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
110 robot.set_fMo(wMo);
111 bool ret = robot.initialiseCameraRelativeToObject(cMo);
112 if (ret == false)
113 return EXIT_FAILURE; // Not able to set the position
114 robot.setDesiredCameraPosition(cdMo);
115 // We modify the default external camera position
116 robot.setExternalCameraPosition(
118
119 vpImage<unsigned char> Iint(480, 640, 255);
120#if defined(VISP_HAVE_DISPLAY)
121#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
122 display = vpDisplayFactory::createDisplay(Iint, 700, 0, "Internal view");
123#else
124 display = vpDisplayFactory::allocateDisplay(Iint, 700, 0, "Internal view");
125#endif
126#else
127 std::cout << "No image viewer is available..." << std::endl;
128#endif
129
130 vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
131 // Modify the camera parameters to match those used in the other
132 // simulations
133 robot.setCameraParameters(cam);
134
135 bool start = true;
136 // for ( ; ; )
137 for (int iter = 0; iter < 275; iter++) {
138 cMo = robot.get_cMo();
139
140 for (unsigned int i = 0; i < 4; i++) {
141 point[i].track(cMo);
142 vpFeatureBuilder::create(p[i], point[i]);
143 }
144
145 vpDisplay::display(Iint);
146 robot.getInternalView(Iint);
147 if (!start) {
148 display_trajectory(Iint, point, cMo, cam);
149 vpDisplay::displayText(Iint, 40, 120, "Click to stop the servo...", vpColor::red);
150 }
151 vpDisplay::flush(Iint);
152
153 vpColVector v = task.computeControlLaw();
154 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
155
156 // A click to exit
157 if (vpDisplay::getClick(Iint, false))
158 break;
159
160 if (start) {
161 start = false;
162 v = 0;
163 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
164 vpDisplay::displayText(Iint, 40, 120, "Click to start the servo...", vpColor::blue);
165 vpDisplay::flush(Iint);
166 // vpDisplay::getClick(Iint);
167 }
168
169 vpTime::wait(1000 * robot.getSamplingTime());
170 }
171 }
172 catch (const vpException &e) {
173 std::cout << "Catch an exception: " << e << std::endl;
174 }
175#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
176 if (display != nullptr) {
177 delete display;
178 }
179#endif
180#endif
181}
unsigned int size() const
Return the number of elements of the 2D array.
Definition vpArray2D.h:435
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
Implementation of column vector and the associated operations.
static const vpColor red
Definition vpColor.h:198
static const vpColor blue
Definition vpColor.h:204
static const vpColor green
Definition vpColor.h:201
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition vpException.h:60
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...
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:131
static double rad(double deg)
Definition vpMath.h:129
static double deg(double rad)
Definition vpMath.h:119
static void convertPoint(const vpCameraParameters &cam, const double &x, const double &y, double &u, double &v)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
Implementation of a rotation matrix and operations on such kind of matrices.
Implementation of a rotation vector as Euler angle minimal representation.
@ EYEINHAND_CAMERA
Definition vpServo.h:176
@ CURRENT
Definition vpServo.h:217
Simulator of Irisa's Viper S850 robot named Viper850.
Class that consider the case of a translation vector.
@ TOOL_PTGREY_FLEA2_CAMERA
Definition vpViper850.h:122
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)