Visual Servoing Platform  version 3.6.1 under development (2024-06-18)

Simulation of a 3D visual servoing where the current visual feature is given by $s=({^{c}}{\bf t}_{c^*}, \; \theta u_{{^{c}}{\bf R}_{c^*}})$ and the desired one $s^*=(0, \; 0)$.

The control law is set as:

To compute the camera velocities, we use here the vpServo class.

This example is to make into relation with servoSimu3D_cdMc_CamVelocity.cpp where the current visual feature is $s=({^{c^*}}{\bf t}_c, \; \theta u_{{^{c^*}}{\bf R}_c})$.

* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2023 by Inria. All rights reserved.
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
* See for more information.
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
* If you have questions regarding the use of this file, please contact
* Inria at
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* Description:
* Simulation of a 3D visual servoing.
#include <stdio.h>
#include <stdlib.h>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpHomogeneousMatrix.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpMath.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/robot/vpSimulatorCamera.h>
#include <visp3/visual_features/vpFeatureThetaU.h>
#include <visp3/visual_features/vpFeatureTranslation.h>
#include <visp3/vs/vpServo.h>
// List of allowed command line options
#define GETOPTARGS "h"
using namespace VISP_NAMESPACE_NAME;
void usage(const char *name, const char *badparam);
bool getOptions(int argc, const char **argv);
void usage(const char *name, const char *badparam)
fprintf(stdout, "\n\
Simulation of a 3D visual servoing:\n\
- eye-in-hand control law,\n\
- velocity computed in the camera frame,\n\
- without display.\n\
%s [-h]\n",
fprintf(stdout, "\n\
OPTIONS: Default\n\
Print the help.\n");
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
bool getOptions(int argc, const char **argv)
const char *optarg_;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
switch (c) {
case 'h':
usage(argv[0], nullptr);
return false;
usage(argv[0], optarg_);
return false;
if ((c == 1) || (c == -1)) {
// standalone param or error
usage(argv[0], nullptr);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
return true;
int main(int argc, const char **argv)
#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
try {
// Read the command line options
if (getOptions(argc, argv) == false) {
// Log file creation in /tmp/$USERNAME/log.dat
// This file contains by line:
// - the 6 computed camera velocities (m/s, rad/s) to achieve the task
// - the 6 values of s - s*
std::string username;
// Get the user login name
// Create a log filename to save velocities...
std::string logdirname;
#if defined(_WIN32)
logdirname = "C:/temp/" + username;
logdirname = "/tmp/" + username;
// Test if the output path exist. If no try to create it
if (vpIoTools::checkDirectory(logdirname) == false) {
try {
// Create the dirname
catch (...) {
std::cerr << std::endl << "ERROR:" << std::endl;
std::cerr << " Cannot create " << logdirname << std::endl;
std::string logfilename;
logfilename = logdirname + "/log.dat";
// Open the log file name
std::ofstream flog(logfilename.c_str());
vpServo task;
std::cout << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << " Test program for vpServo " << std::endl;
std::cout << " Eye-in-hand task control, velocity computed in the camera frame" << std::endl;
std::cout << " Simulation " << std::endl;
std::cout << " task : 3D visual servoing " << std::endl;
std::cout << "-------------------------------------------------------" << std::endl;
std::cout << std::endl;
// Sets the initial camera location
vpPoseVector c_r_o( // Translation tx,ty,tz
0.1, 0.2, 2,
// ThetaU rotation
// From the camera pose build the corresponding homogeneous matrix
vpHomogeneousMatrix cMo(c_r_o);
// Set the robot initial position
wMo = wMc * cMo; // Compute the position of the object in the world frame
// Sets the desired camera location
vpPoseVector cd_r_o( // Translation tx,ty,tz
0, 0, 1,
// ThetaU rotation
// From the camera desired pose build the corresponding homogeneous matrix
vpHomogeneousMatrix cdMo(cd_r_o);
// Compute the transformation from the initial camera position to the
// desired one
cMcd = cMo * cdMo.inverse();
// Build the 3D translation feature: ctc*;
// Build the 3D rotation feature: thetaU_cRc*
vpFeatureThetaU tu(vpFeatureThetaU::cRcd); // current feature;
// Sets the desired rotation (always zero !) since s is the
// rotation that the camera has to achieve. Here s* = (0, 0)^T
vpFeatureThetaU tud(vpFeatureThetaU::cRcd); // desired feature
// Define the task
// - we want an eye-in-hand control law
// - the robot is controlled in the camera frame
// - we use here the interaction matrix computed with the current
// features
// Add the current and desired visual features
task.addFeature(t, td); // 3D translation
task.addFeature(tu, tud); // 3D rotation theta u
// - set the constant gain to 1.0
// Display task information
unsigned int iter = 0;
// Start the visual servoing loop. We stop the servo after 200 iterations
while (iter++ < 200) {
std::cout << "------------------------------------" << iter << std::endl;
// get the robot position
// Compute the position of the object frame in the camera frame
cMo = wMc.inverse() * wMo;
// new displacement to achieve
cMcd = cMo * cdMo.inverse();
// Update the current visual features;;
// Compute the control law
v = task.computeControlLaw();
// Display task information
if (iter == 1)
// Send the camera velocity to the controller
// Retrieve the error
std::cout << "|| s - s* || = " << (task.getError()).sumSquare() << std::endl;
// Save log
flog << v.t() << " " << (task.getError()).t() << std::endl;
// Display task information
// Kill the task
// Close the log file
catch (const vpException &e) {
std::cout << "Catch a ViSP exception: " << e << std::endl;
std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
Implementation of column vector and the associated operations.
Definition: vpColVector.h:171
vpRowVector t() const
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Class that defines a 3D visual feature from a axis/angle parametrization that represent the rotatio...
Class that defines the translation visual feature .
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix & build(const vpTranslationVector &t, const vpRotationMatrix &R)
vpHomogeneousMatrix inverse() const
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:837
static std::string getUserName()
Definition: vpIoTools.cpp:730
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:986
static double rad(double deg)
Definition: vpMath.h:129
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:71
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:187
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
Definition: vpRobot.h:84
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:380
Definition: vpServo.h:157
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:331
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:171
void setLambda(double c)
Definition: vpServo.h:978
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:134
vpColVector getError() const
Definition: vpServo.h:506
vpColVector computeControlLaw()
Definition: vpServo.cpp:705
Definition: vpServo.h:198
Class that defines the simplest robot: a free flying camera.