Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-ibvs-4pts-wireframe-robot-afma6.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/gui/vpDisplayFactory.h>
4#include <visp3/robot/vpSimulatorAfma6.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
40 try {
41 vpHomogeneousMatrix cdMo(0, 0, 0.75, 0, 0, 0);
42 vpHomogeneousMatrix cMo(-0.15, 0.1, 1., vpMath::rad(-10), vpMath::rad(10), vpMath::rad(50));
43
44 /*
45 Top view of the world frame, the camera frame and the object frame
46
47 world, also robot base frame :
48 w_y
49 /|\
50 |
51 w_x <--
52
53 object :
54 o_y
55 /|\
56 |
57 o_x <--
58
59
60 camera :
61 c_y
62 /|\
63 |
64 c_x <--
65
66 */
67 vpHomogeneousMatrix wMo(0, 0, 1., 0, 0, 0);
68
69 std::vector<vpPoint> point;
70 point.push_back(vpPoint(-0.1, -0.1, 0));
71 point.push_back(vpPoint(0.1, -0.1, 0));
72 point.push_back(vpPoint(0.1, 0.1, 0));
73 point.push_back(vpPoint(-0.1, 0.1, 0));
74
77 task.setInteractionMatrixType(vpServo::CURRENT);
78 task.setLambda(0.5);
79
80 vpFeaturePoint p[4], pd[4];
81 for (unsigned int i = 0; i < 4; i++) {
82 point[i].track(cdMo);
83 vpFeatureBuilder::create(pd[i], point[i]);
84 point[i].track(cMo);
85 vpFeatureBuilder::create(p[i], point[i]);
86 task.addFeature(p[i], pd[i]);
87 }
88
89 vpSimulatorAfma6 robot(true);
90 robot.setVerbose(true);
91
92 // Get the default joint limits
93 vpColVector qmin = robot.getJointMin();
94 vpColVector qmax = robot.getJointMax();
95
96 std::cout << "Robot joint limits: " << std::endl;
97 for (unsigned int i = 0; i < 3; i++)
98 std::cout << "Joint " << i << ": min " << qmin[i] << " max " << qmax[i] << " (m)" << std::endl;
99 for (unsigned int i = 3; i < qmin.size(); i++)
100 std::cout << "Joint " << i << ": min " << vpMath::deg(qmin[i]) << " max " << vpMath::deg(qmax[i]) << " (deg)"
101 << std::endl;
102
104 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
106 robot.set_fMo(wMo);
107 bool ret = robot.initialiseCameraRelativeToObject(cMo);
108 if (ret == false)
109 return EXIT_FAILURE; // Not able to set the position
110 robot.setDesiredCameraPosition(cdMo);
111
112 vpImage<unsigned char> Iint(480, 640, 255);
113#if defined(VISP_HAVE_DISPLAY)
114#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
115 display = vpDisplayFactory::createDisplay(Iint, 700, 0, "Internal view");
116#else
117 display = vpDisplayFactory::allocateDisplay(Iint, 700, 0, "Internal view");
118#endif
119#else
120 std::cout << "No image viewer is available..." << std::endl;
121#endif
122
123 vpCameraParameters cam(840, 840, Iint.getWidth() / 2, Iint.getHeight() / 2);
124 robot.setCameraParameters(cam);
125
126 bool start = true;
127 for (;;) {
128 cMo = robot.get_cMo();
129
130 for (unsigned int i = 0; i < 4; i++) {
131 point[i].track(cMo);
132 vpFeatureBuilder::create(p[i], point[i]);
133 }
134
135 vpDisplay::display(Iint);
136 robot.getInternalView(Iint);
137 if (!start) {
138 display_trajectory(Iint, point, cMo, cam);
139 vpDisplay::displayText(Iint, 40, 120, "Click to stop the servo...", vpColor::red);
140 }
141 vpDisplay::flush(Iint);
142
143 vpColVector v = task.computeControlLaw();
144 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
145
146 // A click to exit
147 if (vpDisplay::getClick(Iint, false))
148 break;
149
150 if (start) {
151 start = false;
152 v = 0;
153 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
154 vpDisplay::displayText(Iint, 40, 120, "Click to start the servo...", vpColor::blue);
155 vpDisplay::flush(Iint);
157 }
158
159 vpTime::wait(1000 * robot.getSamplingTime());
160 }
161 }
162 catch (const vpException &e) {
163 std::cout << "Catch an exception: " << e << std::endl;
164 }
165#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
166 if (display != nullptr) {
167 delete display;
168 }
169#endif
170#endif
171}
@ TOOL_CCMOP
Definition vpAfma6.h:125
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
@ EYEINHAND_CAMERA
Definition vpServo.h:176
@ CURRENT
Definition vpServo.h:217
Simulator of Irisa's gantry robot named Afma6.
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)