Visual Servoing Platform  version 3.5.1 under development (2023-03-14)
vpRobotFranka.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Interface for the Franka robot.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #ifndef _vpRobotFranka_h_
40 #define _vpRobotFranka_h_
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #ifdef VISP_HAVE_FRANKA
45 
46 #include <atomic>
47 #include <iostream>
48 #include <thread>
49 #include <vector>
50 
51 #include <stdio.h>
52 
53 #include <franka/exception.h>
54 #include <franka/gripper.h>
55 #include <franka/model.h>
56 #include <franka/robot.h>
57 
58 #include <visp3/core/vpColVector.h>
59 #include <visp3/core/vpException.h>
60 #include <visp3/robot/vpRobot.h>
61 
225 class VISP_EXPORT vpRobotFranka : public vpRobot
226 {
227 private:
231  vpRobotFranka(const vpRobotFranka &robot);
236 
237  void init();
238 
239  franka::Robot *m_handler;
240  franka::Gripper *m_gripper;
241  franka::Model *m_model;
242  double m_positionningVelocity;
243 
244  // Velocity controller
245  std::thread m_velControlThread;
246  std::atomic_bool m_velControlThreadIsRunning;
247  std::atomic_bool m_velControlThreadStopAsked;
248  std::array<double, 7> m_dq_des; // Desired joint velocity
249  vpColVector m_v_cart_des; // Desired cartesian velocity either in reference, end-effector, camera, or tool frame
250 
251  // Force/torque controller
252  std::thread m_ftControlThread;
253  std::atomic_bool m_ftControlThreadIsRunning;
254  std::atomic_bool m_ftControlThreadStopAsked;
255  std::array<double, 7> m_tau_J_des; // Desired joint torques
256  vpColVector m_ft_cart_des; // Desired cartesian force/torque either in reference, end-effector, camera, or tool frame
257 
258  std::array<double, 7> m_q_min; // Joint min position
259  std::array<double, 7> m_q_max; // Joint max position
260  std::array<double, 7> m_dq_max; // Joint max velocity
261  std::array<double, 7> m_ddq_max; // Joint max acceleration
262 
263  franka::RobotState m_robot_state; // Robot state protected by mutex
264  std::mutex m_mutex; // Mutex to protect m_robot_state
265 
266  vpHomogeneousMatrix m_eMc;
267  std::string m_log_folder;
268  std::string m_franka_address;
269 
270 public:
271  vpRobotFranka();
272 
273  vpRobotFranka(const std::string &franka_address,
274  franka::RealtimeConfig realtime_config = franka::RealtimeConfig::kEnforce);
275 
276  virtual ~vpRobotFranka();
277 
278  void connect(const std::string &franka_address,
279  franka::RealtimeConfig realtime_config = franka::RealtimeConfig::kEnforce);
280 
281  vpHomogeneousMatrix get_fMe(const vpColVector &q);
282  vpHomogeneousMatrix get_fMc(const vpColVector &q);
283  vpHomogeneousMatrix get_eMc() const;
284 
285  void get_eJe(vpMatrix &eJe);
286  void get_eJe(const vpColVector &q, vpMatrix &eJe);
287  void get_fJe(vpMatrix &fJe);
288  void get_fJe(const vpColVector &q, vpMatrix &fJe);
289 
290  void getCoriolis(vpColVector &coriolis);
291  void getForceTorque(const vpRobot::vpControlFrameType frame, vpColVector &force);
292 
293  void getGravity(vpColVector &gravity);
294 
295  franka::RobotState getRobotInternalState();
296 
302  franka::Gripper *getGripperHandler()
303  {
304  if (!m_gripper) {
305  throw(vpException(vpException::fatalError, "Cannot get Franka gripper handler: gripper is not connected"));
306  }
307 
308  return m_gripper;
309  }
310 
316  franka::Robot *getHandler()
317  {
318  if (!m_handler) {
319  throw(vpException(vpException::fatalError, "Cannot get Franka robot handler: robot is not connected"));
320  }
321 
322  return m_handler;
323  }
324 
325  vpColVector getJointMin() const;
326  vpColVector getJointMax() const;
327 
328  void getMass(vpMatrix &mass);
329 
330  void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position);
331  void getPosition(const vpRobot::vpControlFrameType frame, vpPoseVector &pose);
332 
333  void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &d_position);
334 
335  int gripperClose();
336  int gripperGrasp(double grasping_width, double force = 60.);
337  void gripperHoming();
338  int gripperMove(double width);
339  int gripperOpen();
340  void gripperRelease();
341 
342  void move(const std::string &filename, double velocity_percentage = 10.);
343 
344  bool readPosFile(const std::string &filename, vpColVector &q);
345  bool savePosFile(const std::string &filename, const vpColVector &q);
346 
347  void set_eMc(const vpHomogeneousMatrix &eMc);
348  void setForceTorque(const vpRobot::vpControlFrameType frame, const vpColVector &ft, const double &filter_gain = 0.1,
349  const bool &activate_pi_controller = false);
350  void setLogFolder(const std::string &folder);
351  void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &position);
352  void setPositioningVelocity(double velocity);
353 
355  void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel);
356 
357  void stopMotion();
358 };
359 
360 #endif
361 #endif // #ifndef __vpRobotFranka_h_
Implementation of column vector and the associated operations.
Definition: vpColVector.h:131
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ fatalError
Fatal error.
Definition: vpException.h:96
Implementation of an homogeneous matrix and operations on such kind of matrices.
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:154
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:152
franka::Robot * getHandler()
franka::Gripper * getGripperHandler()
Class that defines a generic virtual robot.
Definition: vpRobot.h:60
vpControlFrameType
Definition: vpRobot.h:76
virtual void get_eJe(vpMatrix &_eJe)=0
Get the robot Jacobian expressed in the end-effector frame.
virtual void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)=0
virtual void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position)=0
Get the robot position (frame has to be specified).
vpRobotStateType
Definition: vpRobot.h:65
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
virtual void init()=0
virtual void get_fJe(vpMatrix &_fJe)=0
virtual void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &q)=0
virtual void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)=0
Set a displacement (frame has to be specified) in position control.