Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
servoViper850Point2DCamVelocityKalman.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 * tests the control law
32 * eye-in-hand control
33 * velocity computed in camera frame
34 */
35
46
47#include <visp3/core/vpConfig.h>
48#include <visp3/core/vpDebug.h> // Debug trace
49
50#include <fstream>
51#include <iostream>
52#include <sstream>
53#include <stdio.h>
54#include <stdlib.h>
55
56#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394))
57
58#include <visp3/blob/vpDot2.h>
59#include <visp3/core/vpDisplay.h>
60#include <visp3/core/vpException.h>
61#include <visp3/core/vpHomogeneousMatrix.h>
62#include <visp3/core/vpImage.h>
63#include <visp3/core/vpIoTools.h>
64#include <visp3/core/vpLinearKalmanFilterInstantiation.h>
65#include <visp3/core/vpMath.h>
66#include <visp3/core/vpPoint.h>
67#include <visp3/gui/vpDisplayFactory.h>
68#include <visp3/io/vpImageIo.h>
69#include <visp3/robot/vpRobotViper850.h>
70#include <visp3/sensor/vp1394TwoGrabber.h>
71#include <visp3/visual_features/vpFeatureBuilder.h>
72#include <visp3/visual_features/vpFeaturePoint.h>
73#include <visp3/vs/vpAdaptiveGain.h>
74#include <visp3/vs/vpServo.h>
75#include <visp3/vs/vpServoDisplay.h>
76
77int main()
78{
79#ifdef ENABLE_VISP_NAMESPACE
80 using namespace VISP_NAMESPACE_NAME;
81#endif
82
83 // Log file creation in /tmp/$USERNAME/log.dat
84 // This file contains by line:
85 // - the 6 computed joint velocities (m/s, rad/s) to achieve the task
86 // - the 6 measured joint velocities (m/s, rad/s)
87 // - the 6 measured joint positions (m, rad)
88 // - the 2 values of s - s*
89 std::string username;
90 // Get the user login name
91 vpIoTools::getUserName(username);
92
93 // Create a log filename to save velocities...
94 std::string logdirname;
95 logdirname = "/tmp/" + username;
96
97 // Test if the output path exist. If no try to create it
98 if (vpIoTools::checkDirectory(logdirname) == false) {
99 try {
100 // Create the dirname
101 vpIoTools::makeDirectory(logdirname);
102 }
103 catch (...) {
104 std::cerr << std::endl << "ERROR:" << std::endl;
105 std::cerr << " Cannot create " << logdirname << std::endl;
106 return EXIT_FAILURE;
107 }
108 }
109 std::string logfilename;
110 logfilename = logdirname + "/log.dat";
111
112 // Open the log file name
113 std::ofstream flog(logfilename.c_str());
114
116
117#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
118 std::shared_ptr<vpDisplay> display;
119#else
120 vpDisplay *display = nullptr;
121#endif
122
123 try {
124 // Initialize linear Kalman filter
126
127 // Initialize the kalman filter
128 unsigned int nsignal = 2; // The two values of dedt
129 double rho = 0.3;
130 vpColVector sigma_state;
131 vpColVector sigma_measure(nsignal);
132 unsigned int state_size = 0; // Kalman state vector size
133
135 state_size = kalman.getStateSize();
136 sigma_state.resize(state_size * nsignal);
137 sigma_state = 0.00001; // Same state variance for all signals
138 sigma_measure = 0.05; // Same measure variance for all the signals
139 double dummy = 0; // non used parameter dt for the velocity state model
140 kalman.initFilter(nsignal, sigma_state, sigma_measure, rho, dummy);
141
142 // Initialize the robot
143 vpRobotViper850 robot;
144
146
147 bool reset = false;
148 vp1394TwoGrabber g(reset);
149
150#if 1
152 g.setFramerate(vp1394TwoGrabber::vpFRAMERATE_60);
153#else
156#endif
157 g.open(I);
158
159 double Tloop = 1. / 80.f;
160
162 g.getFramerate(fps);
163 switch (fps) {
165 Tloop = 1.f / 15.f;
166 break;
168 Tloop = 1.f / 30.f;
169 break;
171 Tloop = 1.f / 60.f;
172 break;
174 Tloop = 1.f / 120.f;
175 break;
176 default:
177 break;
178 }
179
180#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
181 display = vpDisplayFactory::createDisplay(I, static_cast<int>(100 + I.getWidth() + 30), 200, "Current image");
182#else
183 display = vpDisplayFactory::allocateDisplay(I, static_cast<int>(100 + I.getWidth() + 30), 200, "Current image");
184#endif
185
188
189 vpDot2 dot;
190 vpImagePoint cog;
191
192 dot.setGraphics(true);
193
194 for (int i = 0; i < 10; i++)
195 g.acquire(I);
196
197 std::cout << "Click on a dot..." << std::endl;
198 dot.initTracking(I);
199
200 cog = dot.getCog();
203
205 // Update camera parameters
206 robot.getCameraParameters(cam, I);
207
208 // sets the current position of the visual feature
210 // retrieve x,y and Z of the vpPoint structure
211 vpFeatureBuilder::create(p, cam, dot);
212
213 // sets the desired position of the visual feature
215 pd.buildFrom(0, 0, 1);
216
217 // define the task
218 // - we want an eye-in-hand control law
219 // - robot is controlled in the camera frame
221 task.setInteractionMatrixType(vpServo::DESIRED, vpServo::PSEUDO_INVERSE);
222
223 // - we want to see a point on a point
224 task.addFeature(p, pd);
225
226 // - set the constant gain
227 vpAdaptiveGain lambda;
228 lambda.initStandard(4, 0.2, 30);
229 task.setLambda(lambda);
230
231 // Display task information
232 task.print();
233
234 // Now the robot will be controlled in velocity
235 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
236
237 std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
238 vpColVector v, v1, v2;
239 int iter = 0;
240 vpColVector vm(6);
241 double t_0, t_1, Tv;
242 vpColVector err(2), err_1(2);
243 vpColVector dedt_filt(2), dedt_mes(2);
244 dc1394video_frame_t *frame = nullptr;
245
246 t_1 = vpTime::measureTimeMs();
247
248 for (;;) {
249 try {
250 t_0 = vpTime::measureTimeMs(); // t_0: current time
251
252 // Update loop time in second
253 Tv = static_cast<double>(t_0 - t_1) / 1000.0;
254
255 // Update time for next iteration
256 t_1 = t_0;
257
258 vm = robot.getVelocity(vpRobot::CAMERA_FRAME);
259
260 // Acquire a new image from the camera
261 frame = g.dequeue(I);
262
263 // Display this image
265
266 // Achieve the tracking of the dot in the image
267 dot.track(I);
268
269 // Get the dot cog
270 cog = dot.getCog();
271
272 // Display a green cross at the center of gravity position in the
273 // image
275
276 // Update the point feature from the dot location
277 vpFeatureBuilder::create(p, cam, dot);
278
279 // Compute the visual servoing skew vector
280 v1 = task.computeControlLaw();
281
282 // Get the error ||s-s*||
283 err = task.getError();
284
286 if (iter == 0) {
287 err_1 = 0;
288 dedt_mes = 0;
289 }
290 else {
291 vpMatrix J1 = task.getTaskJacobian();
292 dedt_mes = (err - err_1) / (Tv)-J1 * vm;
293 err_1 = err;
294 }
295
296 // Filter de/dt
297 if (iter < 2)
298 dedt_mes = 0;
299 kalman.filter(dedt_mes);
300 // Get the filtered values
301 for (unsigned int i = 0; i < nsignal; i++) {
302 dedt_filt[i] = kalman.Xest[i * state_size];
303 }
304 if (iter < 2)
305 dedt_filt = 0;
306
307 vpMatrix J1p = task.getTaskJacobianPseudoInverse();
308 v2 = -J1p * dedt_filt;
309
310 // Update the robot camera velocity
311 v = v1 + v2;
312
313 // Display the current and desired feature points in the image display
314 vpServoDisplay::display(task, cam, I);
315
316 // Apply the computed camera velocities to the robot
317 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
318
319 iter++;
320 // Synchronize the loop with the image frame rate
321 vpTime::wait(t_0, 1000. * Tloop);
322 // Release the ring buffer used for the last image to start a new acq
323 g.enqueue(frame);
324 }
325 catch (...) {
326 std::cout << "Tracking failed... Stop the robot." << std::endl;
327 v = 0;
328 // Stop robot
329 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
330#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
331 if (display != nullptr) {
332 delete display;
333 }
334#endif
335 return EXIT_FAILURE;
336 }
337
338 // Save velocities applied to the robot in the log file
339 // v[0], v[1], v[2] correspond to camera translation velocities in m/s
340 // v[3], v[4], v[5] correspond to camera rotation velocities in rad/s
341 flog << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << " " << v[4] << " " << v[5] << " ";
342
343 // Get the measured joint velocities of the robot
344 vpColVector qvel;
345 robot.getVelocity(vpRobot::ARTICULAR_FRAME, qvel);
346 // Save measured joint velocities of the robot in the log file:
347 // - qvel[0], qvel[1], qvel[2] correspond to measured joint translation
348 // velocities in m/s
349 // - qvel[3], qvel[4], qvel[5] correspond to measured joint rotation
350 // velocities in rad/s
351 flog << qvel[0] << " " << qvel[1] << " " << qvel[2] << " " << qvel[3] << " " << qvel[4] << " " << qvel[5] << " ";
352
353 // Get the measured joint positions of the robot
354 vpColVector q;
355 robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
356 // Save measured joint positions of the robot in the log file
357 // - q[0], q[1], q[2] correspond to measured joint translation
358 // positions in m
359 // - q[3], q[4], q[5] correspond to measured joint rotation
360 // positions in rad
361 flog << q[0] << " " << q[1] << " " << q[2] << " " << q[3] << " " << q[4] << " " << q[5] << " ";
362
363 // Save feature error (s-s*) for the feature point. For this feature
364 // point, we have 2 errors (along x and y axis). This error is
365 // expressed in meters in the camera frame
366 flog << (task.getError()).t() << std::endl; // s-s* for point
367
368 // Flush the display
370 }
371
372 flog.close(); // Close the log file
373
374 // Display task information
375 task.print();
376
377#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
378 if (display != nullptr) {
379 delete display;
380 }
381#endif
382 return EXIT_SUCCESS;
383 }
384 catch (const vpException &e) {
385 flog.close(); // Close the log file
386 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
387#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
388 if (display != nullptr) {
389 delete display;
390 }
391#endif
392 return EXIT_FAILURE;
393 }
394}
395
396#else
397int main()
398{
399 std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
400 return EXIT_SUCCESS;
401}
402#endif
Class for firewire ieee1394 video devices using libdc1394-2.x api.
Adaptive gain computation.
void initStandard(double gain_at_zero, double gain_at_infinity, double slope_at_zero)
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
vpRowVector t() const
void resize(unsigned int i, bool flagNullify=true)
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 void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition vpDot2.h:127
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition vpDot2.cpp:441
void setGraphics(bool activate)
Definition vpDot2.h:320
vpImagePoint getCog() const
Definition vpDot2.h:183
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition vpDot2.cpp:263
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...
vpFeaturePoint & buildFrom(const double &x, const double &y, const double &Z)
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 bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static void makeDirectory(const std::string &dirname)
This class provides an implementation of some specific linear Kalman filters.
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Control of Irisa's Viper S850 robot named Viper850.
@ ARTICULAR_FRAME
Definition vpRobot.h:77
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
static void display(const vpServo &s, const vpCameraParameters &cam, const vpImage< unsigned char > &I, vpColor currentColor=vpColor::green, vpColor desiredColor=vpColor::red, unsigned int thickness=1)
@ EYEINHAND_CAMERA
Definition vpServo.h:176
@ PSEUDO_INVERSE
Definition vpServo.h:250
@ DESIRED
Definition vpServo.h:223
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 double measureTimeMs()
VISP_EXPORT int wait(double t0, double t)