Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
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 http://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 <iostream>
47 #include <thread>
48 #include <atomic>
49 #include <vector>
50 
51 #include <stdio.h>
52 
53 #include <franka/exception.h>
54 #include <franka/robot.h>
55 #include <franka/model.h>
56 #include <franka/gripper.h>
57 
58 #include <visp3/core/vpColVector.h>
59 #include <visp3/robot/vpRobot.h>
60 #include <visp3/core/vpException.h>
61 
223 class VISP_EXPORT vpRobotFranka : public vpRobot
224 {
225 private:
229  vpRobotFranka(const vpRobotFranka &robot);
234  franka::RobotState getRobotInternalState();
235  void init();
236 
237  franka::Robot *m_handler;
238  franka::Gripper *m_gripper;
239  franka::Model *m_model;
240  double m_positionningVelocity;
241 
242  std::thread m_controlThread;
243  std::atomic_bool m_controlThreadIsRunning;
244  std::atomic_bool m_controlThreadStopAsked;
245 
246  std::array<double, 7> m_q_min; // Joint min position
247  std::array<double, 7> m_q_max; // Joint max position
248  std::array<double, 7> m_dq_max; // Joint max velocity
249  std::array<double, 7> m_ddq_max; // Joint max acceleration
250 
251  franka::RobotState m_robot_state; // Robot state protected by mutex
252  std::mutex m_mutex; // Mutex to protect m_robot_state
253 
254  std::array<double, 7> m_dq_des; // Desired joint velocity
255  vpColVector m_v_cart_des; // Desired cartesian velocity either in reference, end-effector, camera, or tool frame
256  vpHomogeneousMatrix m_eMc;
257  std::string m_log_folder;
258  std::string m_franka_address;
259 
260 public:
261  vpRobotFranka();
262 
263  vpRobotFranka(const std::string &franka_address,
264  franka::RealtimeConfig realtime_config = franka::RealtimeConfig::kEnforce);
265 
266  virtual ~vpRobotFranka();
267 
268  void connect(const std::string &franka_address,
269  franka::RealtimeConfig realtime_config = franka::RealtimeConfig::kEnforce);
270 
271  vpHomogeneousMatrix get_fMe(const vpColVector &q);
272  vpHomogeneousMatrix get_fMc(const vpColVector &q);
273  vpHomogeneousMatrix get_eMc() const;
274 
275  void get_eJe(vpMatrix &eJe);
276  void get_eJe(const vpColVector &q, vpMatrix &eJe);
277  void get_fJe(vpMatrix &fJe);
278  void get_fJe(const vpColVector &q, vpMatrix &fJe);
279 
280  void getCoriolis(vpColVector &coriolis);
281  void getForceTorque(const vpRobot::vpControlFrameType frame, vpColVector &force);
282 
283  void getGravity(vpColVector &gravity);
284 
290  franka::Gripper *getGripperHandler() {
291  if (!m_gripper) {
292  throw(vpException(vpException::fatalError, "Cannot get Franka gripper handler: gripper is not connected"));
293  }
294 
295  return m_gripper;
296  }
297 
298 
304  franka::Robot *getHandler() {
305  if (!m_handler) {
306  throw(vpException(vpException::fatalError, "Cannot get Franka robot handler: robot is not connected"));
307  }
308 
309  return m_handler;
310  }
311 
312  vpColVector getJointMin() const;
313  vpColVector getJointMax() const;
314 
315  void getMass(vpMatrix &mass);
316 
317  void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position);
318  void getPosition(const vpRobot::vpControlFrameType frame, vpPoseVector &pose);
319 
320  void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &d_position);
321 
322  int gripperClose();
323  int gripperGrasp(double grasping_width, double force=60.);
324  void gripperHoming();
325  int gripperMove(double width);
326  int gripperOpen();
327  void gripperRelease();
328 
329  void move(const std::string &filename, double velocity_percentage=10.);
330 
331  bool readPosFile(const std::string &filename, vpColVector &q);
332  bool savePosFile(const std::string &filename, const vpColVector &q);
333 
334  void set_eMc(const vpHomogeneousMatrix &eMc);
335  void setLogFolder(const std::string &folder);
336  void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &position);
337  void setPositioningVelocity(const double velocity);
338 
340  void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel);
341 
342  void stopMotion();
343 };
344 
345 #endif
346 #endif // #ifndef __vpRobotFranka_h_
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
virtual void getDisplacement(const vpRobot::vpControlFrameType frame, vpColVector &q)=0
Implementation of an homogeneous matrix and operations on such kind of matrices.
error that can be emited by ViSP classes.
Definition: vpException.h:71
virtual void get_eJe(vpMatrix &_eJe)=0
Get the robot Jacobian expressed in the end-effector frame.
virtual void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &q)=0
Set a displacement (frame has to be specified) in position control.
Class that defines a generic virtual robot.
Definition: vpRobot.h:58
vpControlFrameType
Definition: vpRobot.h:75
franka::Gripper * getGripperHandler()
franka::Robot * getHandler()
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
Definition: vpRobot.cpp:201
vpRobotStateType
Definition: vpRobot.h:64
virtual void init()=0
virtual void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &q)=0
Get the robot position (frame has to be specified).
virtual void get_fJe(vpMatrix &_fJe)=0
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:92
virtual void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel)=0