Visual Servoing Platform  version 3.6.1 under development (2024-04-16)

Joint limits avoidance using a secondary task for joint limit avoidance [36] using the new large projection operator (see equation(24) in the paper [35]).

* 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:
* tests the control law
* eye-in-hand control
* velocity computed in articular
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpDebug.h> // Debug trace
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#if (defined(VISP_HAVE_VIPER850) && defined(VISP_HAVE_DC1394_2) && defined(VISP_HAVE_DISPLAY))
#include <visp3/blob/vpDot2.h>
#include <visp3/core/vpDisplay.h>
#include <visp3/core/vpException.h>
#include <visp3/core/vpHomogeneousMatrix.h>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpMath.h>
#include <visp3/core/vpPoint.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/gui/vpPlot.h>
#include <visp3/robot/vpRobotViper850.h>
#include <visp3/sensor/vp1394TwoGrabber.h>
#include <visp3/visual_features/vpFeatureBuilder.h>
#include <visp3/visual_features/vpFeaturePoint.h>
#include <visp3/vs/vpServo.h>
#include <visp3/vs/vpServoDisplay.h>
int main()
try {
vpServo task;
bool reset = false;
vp1394TwoGrabber g(reset);;
#ifdef VISP_HAVE_X11
vpDisplayX display(I, 800, 100, "Current image");
#elif defined(HAVE_OPENCV_HIGHGUI)
vpDisplayOpenCV display(I, 800, 100, "Current image");
#elif defined(VISP_HAVE_GTK)
vpDisplayGTK display(I, 800, 100, "Current image");
vpColVector jointMin(6), jointMax(6);
jointMin = robot.getJointMin();
jointMax = robot.getJointMax();
vpColVector Qmiddle(6);
vpColVector data(12);
Qmiddle = (jointMin + jointMax) / 2.;
// double rho1 = 0.1 ;
double rho = 0.1;
double rho1 = 0.3;
// Create a window with two graphics
// - first graphic to plot q(t), Qmin, Qmax, Ql0min, Ql1min, Ql0max and
// Ql1max
vpPlot plot(2);
// The first graphic contains 12 data to plot: q(t), Low Limits, Upper
// Limits, ql0min, ql1min, ql0max and ql1max
plot.initGraph(0, 12);
// The second graphic contains the values of the secondaty task velocities
plot.initGraph(1, 6);
// For the first graphic :
// - along the x axis the expected values are between 0 and 200
// - along the y axis the expected values are between -1.2 and 1.2
plot.initRange(0, 0., 200., -1.2, 1.2);
plot.setTitle(0, "Joint behavior");
// For the second graphic :
plot.setTitle(1, "Q secondary task");
// For the first and second graphic, set the curves legend
std::string legend;
for (unsigned int i = 0; i < 6; i++) {
legend = "q" + i + 1;
plot.setLegend(0, i, legend);
plot.setLegend(1, i, legend);
plot.setLegend(0, 6, "Low Limit");
plot.setLegend(0, 7, "Upper Limit");
plot.setLegend(0, 8, "ql0 min");
plot.setLegend(0, 9, "ql0 max");
plot.setLegend(0, 10, "ql1 min");
plot.setLegend(0, 11, "ql1 max");
// Set the curves color
plot.setColor(0, 0, vpColor::red);
plot.setColor(0, 1, vpColor::green);
plot.setColor(0, 2, vpColor::blue);
plot.setColor(0, 4, vpColor(0, 128, 0));
plot.setColor(0, 5, vpColor::cyan);
for (unsigned int i = 6; i < 12; i++)
plot.setColor(0, i, vpColor::black); // for Q and tQ [min,max]
vpColVector sec_task(6);
vpDot2 dot;
std::cout << "Click on a dot..." << std::endl;
vpImagePoint cog = dot.getCog();
// Update camera parameters
robot.getCameraParameters(cam, I);
// sets the current position of the visual feature
vpFeatureBuilder::create(p, cam, dot); // retrieve x,y and Z of the vpPoint structure
// sets the desired position of the visual feature
pd.buildFrom(0, 0, 1);
// Define the task
// - we want an eye-in-hand control law
// - articular velocity are computed
std::cout << cVe << std::endl;
// - Set the Jacobian (expressed in the end-effector frame)") ;
vpMatrix eJe;
// - we want to see a point on a point..") ;
std::cout << std::endl;
task.addFeature(p, pd);
// - set the gain
// Display task information " ) ;
int iter = 0;
std::cout << "\nHit CTRL-C to stop the loop...\n" << std::flush;
for (;;) {
// Acquire a new image from the camera
// Display this image
// Achieve the tracking of the dot in the image
cog = dot.getCog();
// Display a green cross at the center of gravity position in the image
// Get the measured joint positions of the robot
robot.getPosition(vpRobot::ARTICULAR_FRAME, q);
// Update the point feature from the dot location
// Get the jacobian of the robot
// Update this jacobian in the task structure. It will be used to
// compute the velocity skew (as an articular velocity) qdot = -lambda *
// L^+ * cVe * eJe * (s-s*)
vpColVector prim_task;
// Compute the visual servoing skew vector
prim_task = task.computeControlLaw();
// Compute the secondary task for the joint limit avoidance
sec_task = task.secondaryTaskJointLimitAvoidance(q, prim_task, jointMin, jointMax, rho, rho1);
v = prim_task + sec_task;
// Display the current and desired feature points in the image display
vpServoDisplay::display(task, cam, I);
// Apply the computed joint velocities to the robot
// Add the material to plot curves
// q normalized between (entre -1 et 1)
for (unsigned int i = 0; i < 6; i++) {
data[i] = (q[i] - Qmiddle[i]);
data[i] /= (jointMax[i] - jointMin[i]);
data[i] *= 2;
data[6] = -1.0;
data[7] = 1.0;
unsigned int joint = 2;
double tQmin_l0 = jointMin[joint] + rho * (jointMax[joint] - jointMin[joint]);
double tQmax_l0 = jointMax[joint] - rho * (jointMax[joint] - jointMin[joint]);
double tQmin_l1 = tQmin_l0 - rho * rho1 * (jointMax[joint] - jointMin[joint]);
double tQmax_l1 = tQmax_l0 + rho * rho1 * (jointMax[joint] - jointMin[joint]);
data[8] = 2 * (tQmin_l0 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
data[9] = 2 * (tQmax_l0 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
data[10] = 2 * (tQmin_l1 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
data[11] = 2 * (tQmax_l1 - Qmiddle[joint]) / (jointMax[joint] - jointMin[joint]);
plot.plot(0, iter, data); // plot q(t), Low Limits, Upper Limits,
// ql0min, ql1min, ql0max and ql1max
plot.plot(1, iter, sec_task); // plot secondary task velocities
// Display task information
} catch (const vpException &e) {
std::cout << "Catch an exception: " << e.getMessage() << std::endl;
int main()
std::cout << "You do not have an Viper 850 robot connected to your computer..." << std::endl;
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void acquire(vpImage< unsigned char > &I)
void setVideoMode(vp1394TwoVideoModeType videomode)
void setFramerate(vp1394TwoFramerateType fps)
void open(vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor red
Definition: vpColor.h:211
static const vpColor black
Definition: vpColor.h:205
static const vpColor cyan
Definition: vpColor.h:220
static const vpColor orange
Definition: vpColor.h:221
static const vpColor blue
Definition: vpColor.h:217
static const vpColor green
Definition: vpColor.h:214
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
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:124
void track(const vpImage< unsigned char > &I, bool canMakeTheWindowGrow=true)
Definition: vpDot2.cpp:448
vpImagePoint getCog() const
Definition: vpDot2.h:176
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:253
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const char * getMessage() const
Definition: vpException.cpp:64
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...
void buildFrom(double x, double y, double Z)
void set_Z(double Z)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
This class enables real time drawing of 2D or 3D graphics. An instance of the class open a window whi...
Definition: vpPlot.h:109
void initGraph(unsigned int graphNum, unsigned int curveNbr)
Definition: vpPlot.cpp:202
void initRange(unsigned int graphNum, double xmin, double xmax, double ymin, double ymax)
Definition: vpPlot.cpp:214
void setLegend(unsigned int graphNum, unsigned int curveNum, const std::string &legend)
Definition: vpPlot.cpp:545
void plot(unsigned int graphNum, unsigned int curveNum, double x, double y)
Definition: vpPlot.cpp:269
void setColor(unsigned int graphNum, unsigned int curveNum, vpColor color)
Definition: vpPlot.cpp:245
void setTitle(unsigned int graphNum, const std::string &title)
Definition: vpPlot.cpp:503
void get_eJe(vpMatrix &eJe) vp_override
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) vp_override
Definition: vpRobot.h:78
Initialize the velocity controller.
Definition: vpRobot.h:65
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:198
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)
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition: vpServo.cpp:378
Definition: vpServo.h:162
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition: vpServo.cpp:329
void set_cVe(const vpVelocityTwistMatrix &cVe_)
Definition: vpServo.h:1028
vpColVector secondaryTaskJointLimitAvoidance(const vpColVector &q, const vpColVector &dq, const vpColVector &qmin, const vpColVector &qmax, const double &rho=0.1, const double &rho1=0.3, const double &lambda_tune=0.7)
Definition: vpServo.cpp:1155
void print(const vpServo::vpServoPrintType display_level=ALL, std::ostream &os=std::cout)
Definition: vpServo.cpp:169
void setLambda(double c)
Definition: vpServo.h:976
void set_eJe(const vpMatrix &eJe_)
Definition: vpServo.h:1091
void setServo(const vpServoType &servo_type)
Definition: vpServo.cpp:132
Definition: vpServo.h:229
vpColVector computeControlLaw()
Definition: vpServo.cpp:703
Definition: vpServo.h:202
vpVelocityTwistMatrix get_cVe() const
Definition: vpUnicycle.h:70
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.