Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoAfma6TwoLines2DCamVelocity.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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 * tests the control law
32 * eye-in-hand control
33 * velocity computed in the camera frame
34 */
35
44
45#include <iostream>
46#include <visp3/core/vpConfig.h>
47
48#if defined(VISP_HAVE_REALSENSE2) && defined(VISP_HAVE_DISPLAY) && defined(VISP_HAVE_AFMA6)
49
50#include <visp3/core/vpImage.h>
51#include <visp3/core/vpHomogeneousMatrix.h>
52#include <visp3/core/vpLine.h>
53#include <visp3/core/vpMath.h>
54#include <visp3/gui/vpDisplayFactory.h>
55#include <visp3/robot/vpRobotAfma6.h>
56#include <visp3/sensor/vpRealSense2.h>
57#include <visp3/me/vpMeLine.h>
58#include <visp3/visual_features/vpFeatureBuilder.h>
59#include <visp3/visual_features/vpFeatureLine.h>
60#include <visp3/vs/vpServo.h>
61#include <visp3/vs/vpServoDisplay.h>
62
63int main()
64{
65#ifdef ENABLE_VISP_NAMESPACE
66 using namespace VISP_NAMESPACE_NAME;
67#endif
68
69 try {
70 vpRealSense2 rs;
71 rs2::config config;
72 unsigned int width = 640, height = 480, fps = 60;
73 config.enable_stream(RS2_STREAM_COLOR, width, height, RS2_FORMAT_RGBA8, fps);
74 config.enable_stream(RS2_STREAM_DEPTH, width, height, RS2_FORMAT_Z16, fps);
75 config.enable_stream(RS2_STREAM_INFRARED, width, height, RS2_FORMAT_Y8, fps);
76 rs.open(config);
77
79
80 // Warm up camera
81 for (size_t i = 0; i < 10; ++i) {
82 rs.acquire(I);
83 }
84
85 std::shared_ptr<vpDisplay> d = vpDisplayFactory::createDisplay(I, 10, 10, "Current image");
86
89
90 std::cout << "-------------------------------------------------------" << std::endl;
91 std::cout << " Test program for vpServo " << std::endl;
92 std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
93 std::cout << " Simulation " << std::endl;
94 std::cout << " task : servo a point " << std::endl;
95 std::cout << "-------------------------------------------------------" << std::endl;
96
97 int nb_lines = 2;
98
99 std::vector<vpMeLine> line(nb_lines);
100
101 vpMe me;
102 me.setRange(10);
103 me.setPointsToTrack(100);
105 me.setThreshold(15);
106 me.setSampleStep(10);
107
108 // Initialize the tracking. Define the two lines to track
109 // The two lines to track must be parallels
110 for (int i = 0; i < nb_lines; ++i) {
111 line[i].setDisplay(vpMeSite::RANGE_RESULT);
112 line[i].setMe(&me);
113
114 line[i].initTracking(I);
115 line[i].track(I);
117 }
118
119 vpRobotAfma6 robot;
121
122 // Get camera intrinsics
124 robot.getCameraParameters(cam, I);
125
126 // Sets the current position of the visual feature
127 std::vector<vpFeatureLine> s_line(nb_lines);
128 for (int i = 0; i < nb_lines; ++i) {
129 vpFeatureBuilder::create(s_line[i], cam, line[i]);
130 }
131
132 // Sets the desired position of the visual feature
133 std::vector<vpLine> line_d(2);
134 line_d[0].setWorldCoordinates(1, 0, 0, -0.05, 0, 0, 1, 0);
135 line_d[1].setWorldCoordinates(1, 0, 0, 0.05, 0, 0, 1, 0);
136
137 vpHomogeneousMatrix c_M_o(0, 0, 0.5, 0, 0, vpMath::rad(0));
138
139 line_d[0].project(c_M_o);
140 line_d[1].project(c_M_o);
141
142 // Those lines are needed to keep the conventions define in vpMeLine
143 // (Those in vpLine are less restrictive) Another way to have the
144 // coordinates of the desired features is to learn them before executing
145 // the program.
146 line_d[0].setRho(-fabs(line_d[0].getRho()));
147 line_d[0].setTheta(0);
148 line_d[1].setRho(-fabs(line_d[1].getRho()));
149 line_d[1].setTheta(M_PI);
150
151 std::vector<vpFeatureLine> s_line_d(nb_lines);
152 vpFeatureBuilder::create(s_line_d[0], line_d[0]);
153 vpFeatureBuilder::create(s_line_d[1], line_d[1]);
154
155 // Define the task
156 // - we want an eye-in-hand control law
157 // - robot is controlled in the camera frame
160
161 // - we want to see a line on a line
162 for (int i = 0; i < nb_lines; ++i) {
163 task.addFeature(s_line[i], s_line_d[i]);
164 }
165
166 // - set the gain
167 task.setLambda(0.2);
168
169 // -Display task information
170 task.print();
171
172 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
173
174 bool quit = false;
175 while (!quit) {
176 rs.acquire(I);
178
179 // Track the lines and update the features
180 for (int i = 0; i < nb_lines; ++i) {
181 line[i].track(I);
182 line[i].display(I, vpColor::red);
183
184 vpFeatureBuilder::create(s_line[i], cam, line[i]);
185
186 s_line[i].display(cam, I, vpColor::red);
187 s_line_d[i].display(cam, I, vpColor::green);
188 }
189
190 vpColVector v_c = task.computeControlLaw();
191
192 robot.setVelocity(vpRobot::CAMERA_FRAME, v_c);
193
194 vpDisplay::displayText(I, 20, 20, "Click to quit...", vpColor::red);
195 if (vpDisplay::getClick(I, false)) {
196 quit = true;
197 }
198
200 }
201
202 // Display task information
203 task.print();
204
205 return EXIT_SUCCESS;
206 }
207 catch (const vpException &e) {
208 std::cout << "Visual servo failed with exception: " << e << std::endl;
209 return EXIT_FAILURE;
210 }
211}
212
213#else
214int main()
215{
216 std::cout << "You do not have an afma6 robot connected to your computer..." << std::endl;
217 return EXIT_SUCCESS;
218}
219
220#endif
@ TOOL_INTEL_D435_CAMERA
Definition vpAfma6.h:129
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 green
Definition vpColor.h:201
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
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)
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition of the vpImage class member functions.
Definition vpImage.h:131
static double rad(double deg)
Definition vpMath.h:129
@ RANGE_RESULT
Definition vpMeSite.h:85
Definition vpMe.h:143
void setPointsToTrack(const int &points_to_track)
Definition vpMe.h:431
void setRange(const unsigned int &range)
Definition vpMe.h:438
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition vpMe.h:531
void setThreshold(const double &threshold)
Definition vpMe.h:489
void setSampleStep(const double &sample_step)
Definition vpMe.h:445
@ NORMALIZED_THRESHOLD
Definition vpMe.h:154
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
Control of Irisa's gantry robot named Afma6.
void init(void)
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
@ EYEINHAND_CAMERA
Definition vpServo.h:176
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.