36 #include <visp3/core/vpConfig.h>
38 #if defined(VISP_HAVE_FRANKA) && defined(VISP_HAVE_THREADS)
40 #include <visp3/core/vpIoTools.h>
41 #include <visp3/robot/vpRobotException.h>
42 #include <visp3/robot/vpRobotFranka.h>
44 #include "vpForceTorqueGenerator_impl.h"
45 #include "vpJointPosTrajGenerator_impl.h"
46 #include "vpJointVelTrajGenerator_impl.h"
55 :
vpRobot(), m_handler(nullptr), m_gripper(nullptr), m_model(nullptr), m_positioningVelocity(20.), m_velControlThread(),
56 m_velControlThreadIsRunning(false), m_velControlThreadStopAsked(false), m_dq_des(), m_v_cart_des(),
57 m_ftControlThread(), m_ftControlThreadIsRunning(false), m_ftControlThreadStopAsked(false), m_tau_J_des(),
58 m_ft_cart_des(), m_q_min(), m_q_max(), m_dq_max(), m_ddq_max(), m_robot_state(), m_mutex(), m_eMc(), m_log_folder(),
71 :
vpRobot(), m_handler(nullptr), m_gripper(nullptr), m_model(nullptr), m_positioningVelocity(20.), m_velControlThread(),
72 m_velControlThreadIsRunning(false), m_velControlThreadStopAsked(false), m_dq_des(), m_v_cart_des(),
73 m_ftControlThread(), m_ftControlThreadIsRunning(false), m_ftControlThreadStopAsked(false), m_tau_J_des(),
74 m_ft_cart_des(), m_q_min(), m_q_max(), m_dq_max(), m_ddq_max(), m_robot_state(), m_mutex(), m_eMc(), m_log_folder(),
78 connect(franka_address, realtime_config);
84 void vpRobotFranka::init()
88 m_q_min = std::array<double, 7>{-2.8973, -1.7628, -2.8973, -3.0718, -2.8973, -0.0175, -2.8973};
89 m_q_max = std::array<double, 7>{2.8973, 1.7628, 2.8973, -0.0698, 2.8973, 3.7525, 2.8973};
90 m_dq_max = std::array<double, 7>{2.1750, 2.1750, 2.1750, 2.1750, 2.6100, 2.6100, 2.6100};
91 m_ddq_max = std::array<double, 7>{15.0, 7.5, 10.0, 12.5, 15.0, 20.0, 20.0};
107 std::cout <<
"Grasped object, will release it now." << std::endl;
126 if (franka_address.empty()) {
132 m_franka_address = franka_address;
133 m_handler =
new franka::Robot(franka_address, realtime_config);
135 std::array<double, 7> lower_torque_thresholds_nominal { {25.0, 25.0, 22.0, 20.0, 19.0, 17.0, 14.} };
136 std::array<double, 7> upper_torque_thresholds_nominal { {35.0, 35.0, 32.0, 30.0, 29.0, 27.0, 24.0} };
137 std::array<double, 7> lower_torque_thresholds_acceleration { {25.0, 25.0, 22.0, 20.0, 19.0, 17.0, 14.0} };
138 std::array<double, 7> upper_torque_thresholds_acceleration { {35.0, 35.0, 32.0, 30.0, 29.0, 27.0, 24.0} };
139 std::array<double, 6> lower_force_thresholds_nominal { {30.0, 30.0, 30.0, 25.0, 25.0, 25.0} };
140 std::array<double, 6> upper_force_thresholds_nominal { {40.0, 40.0, 40.0, 35.0, 35.0, 35.0} };
141 std::array<double, 6> lower_force_thresholds_acceleration { {30.0, 30.0, 30.0, 25.0, 25.0, 25.0} };
142 std::array<double, 6> upper_force_thresholds_acceleration { {40.0, 40.0, 40.0, 35.0, 35.0, 35.0} };
143 m_handler->setCollisionBehavior(lower_torque_thresholds_acceleration, upper_torque_thresholds_acceleration,
144 lower_torque_thresholds_nominal, upper_torque_thresholds_nominal,
145 lower_force_thresholds_acceleration, upper_force_thresholds_acceleration,
146 lower_force_thresholds_nominal, upper_force_thresholds_nominal);
148 m_handler->setJointImpedance({ {3000, 3000, 3000, 2500, 2500, 2000, 2000} });
149 m_handler->setCartesianImpedance({ {3000, 3000, 3000, 300, 300, 300} });
150 #if (VISP_HAVE_FRANKA_VERSION < 0x000500)
152 m_handler->setFilters(10, 10, 10, 10, 10);
159 m_model =
new franka::Model(m_handler->loadModel());
184 for (
int i = 0; i < 7; i++) {
185 q[i] = robot_state.q[i];
197 for (
size_t i = 0; i < 6; i++) {
198 position[i] = fPe[i];
206 for (
size_t i = 0; i < 6; i++) {
207 position[i] = fPc[i];
238 for (
int i = 0; i < 7; i++)
239 force[i] = robot_state.tau_J[i];
245 for (
int i = 0; i < 6; i++)
246 force[i] = robot_state.K_F_ext_hat_K[i];
252 for (
int i = 0; i < 6; i++)
253 eFe[i] = robot_state.K_F_ext_hat_K[i];
293 for (
int i = 0; i < 7; i++) {
294 d_position[i] = robot_state.dq[i];
316 std::array<double, 7> coriolis_;
320 coriolis_ = m_model->coriolis(robot_state);
323 for (
int i = 0; i < 7; i++) {
324 coriolis[i] = coriolis_[i];
338 std::array<double, 7> gravity_;
342 gravity_ = m_model->gravity(robot_state);
345 for (
int i = 0; i < 7; i++) {
346 gravity[i] = gravity_[i];
360 std::array<double, 49> mass_;
364 mass_ = m_model->mass(robot_state);
367 for (
size_t i = 0; i < 7; i++) {
368 for (
size_t j = 0; j < 7; j++) {
369 mass[i][j] = mass_[j * 7 + i];
390 std::array<double, 7> q_array;
391 for (
size_t i = 0; i < 7; i++)
396 std::array<double, 16> pose_array =
397 m_model->pose(franka::Frame::kEndEffector, q_array, robot_state.F_T_EE, robot_state.EE_T_K);
399 for (
unsigned int i = 0; i < 4; i++) {
400 for (
unsigned int j = 0; j < 4; j++) {
401 fMe[i][j] = pose_array[j * 4 + i];
425 return (fMe * m_eMc);
448 std::array<double, 16> pose_array = robot_state.O_T_EE;
450 for (
unsigned int i = 0; i < 4; i++) {
451 for (
unsigned int j = 0; j < 4; j++) {
452 fMe[i][j] = pose_array[j * 4 + i];
462 pose.
build(fMe * m_eMc);
484 std::array<double, 42> jacobian = m_model->bodyJacobian(franka::Frame::kEndEffector, robot_state);
486 for (
size_t i = 0; i < 6; i++) {
487 for (
size_t j = 0; j < 7; j++) {
488 eJe_[i][j] = jacobian[j * 6 + i];
507 std::array<double, 7> q_array;
508 for (
size_t i = 0; i < 7; i++)
511 std::array<double, 42> jacobian = m_model->bodyJacobian(franka::Frame::kEndEffector, q_array, robot_state.F_T_EE,
514 for (
size_t i = 0; i < 6; i++) {
515 for (
size_t j = 0; j < 7; j++) {
516 eJe_[i][j] = jacobian[j * 6 + i];
535 std::array<double, 42> jacobian = m_model->zeroJacobian(franka::Frame::kEndEffector, robot_state);
537 for (
size_t i = 0; i < 6; i++) {
538 for (
size_t j = 0; j < 7; j++) {
539 fJe_[i][j] = jacobian[j * 6 + i];
558 "Cannot get Franka robot fJe jacobian with an input joint position vector [%u] that is not a 7-dim vector",
564 std::array<double, 7> q_array;
565 for (
size_t i = 0; i < 7; i++)
568 std::array<double, 42> jacobian = m_model->zeroJacobian(franka::Frame::kEndEffector, q_array, robot_state.F_T_EE,
571 for (
size_t i = 0; i < 6; i++) {
572 for (
size_t j = 0; j < 7; j++) {
573 fJe_[i][j] = jacobian[j * 6 + i];
590 if (!folder.empty()) {
594 m_log_folder = folder;
598 error =
"Unable to create Franka log folder: " + folder;
603 m_log_folder = folder;
619 std::cout <<
"Robot was not in position-based control. "
620 "Modification of the robot state"
626 double speed_factor = m_positioningVelocity / 100.;
628 std::array<double, 7> q_goal;
629 for (
size_t i = 0; i < 7; i++) {
630 q_goal[i] = position[i];
633 vpJointPosTrajGenerator joint_pos_traj_generator(speed_factor, q_goal);
636 for (
int attempt = 1; attempt <= nbAttempts; attempt++) {
638 m_handler->control(joint_pos_traj_generator);
641 catch (
const franka::ControlException &e) {
642 std::cerr <<
"Warning: communication error: " << e.what() <<
"\nRetry attempt: " << attempt << std::endl;
643 m_handler->automaticErrorRecovery();
644 if (attempt == nbAttempts)
651 "Cannot move the robot to a cartesian position. Only joint positioning is implemented"));
678 m_velControlThreadStopAsked =
true;
679 if (m_velControlThread.joinable()) {
680 m_velControlThread.join();
681 m_velControlThreadStopAsked =
false;
682 m_velControlThreadIsRunning =
false;
687 m_ftControlThreadStopAsked =
true;
688 if (m_ftControlThread.joinable()) {
689 m_ftControlThread.join();
690 m_ftControlThreadStopAsked =
false;
691 m_ftControlThreadIsRunning =
false;
698 std::cout <<
"Change the control mode from velocity to position control.\n";
700 m_velControlThreadStopAsked =
true;
701 if (m_velControlThread.joinable()) {
702 m_velControlThread.join();
703 m_velControlThreadStopAsked =
false;
704 m_velControlThreadIsRunning =
false;
708 std::cout <<
"Change the control mode from force/torque to position control.\n";
710 m_ftControlThreadStopAsked =
true;
711 if (m_ftControlThread.joinable()) {
712 m_ftControlThread.join();
713 m_ftControlThreadStopAsked =
false;
714 m_ftControlThreadIsRunning =
false;
721 std::cout <<
"Change the control mode from stop to velocity control.\n";
724 std::cout <<
"Change the control mode from position to velocity control.\n";
727 std::cout <<
"Change the control mode from force/torque to velocity control.\n";
729 m_ftControlThreadStopAsked =
true;
730 if (m_ftControlThread.joinable()) {
731 m_ftControlThread.join();
732 m_ftControlThreadStopAsked =
false;
733 m_ftControlThreadIsRunning =
false;
740 std::cout <<
"Change the control mode from stop to force/torque control.\n";
743 std::cout <<
"Change the control mode from position to force/torque control.\n";
746 std::cout <<
"Change the control mode from velocity to force/torque control.\n";
748 m_velControlThreadStopAsked =
true;
749 if (m_velControlThread.joinable()) {
750 m_velControlThread.join();
751 m_velControlThreadStopAsked =
false;
752 m_velControlThreadIsRunning =
false;
822 "Cannot send a velocity to the robot. "
823 "Use setRobotState(vpRobot::STATE_VELOCITY_CONTROL) first.");
829 if (vel.
size() != 7) {
838 for (
size_t i = 0; i < m_dq_des.size(); i++) {
839 m_dq_des[i] = vel_sat[i];
849 if (vel.
size() != 6) {
855 for (
unsigned int i = 0; i < 3; i++)
857 for (
unsigned int i = 3; i < 6; i++)
870 if (!m_velControlThreadIsRunning) {
871 m_velControlThreadIsRunning =
true;
873 std::thread(&vpJointVelTrajGenerator::control_thread, vpJointVelTrajGenerator(), std::ref(m_handler),
874 std::ref(m_velControlThreadStopAsked), m_log_folder, frame, m_eMc, std::ref(m_v_cart_des),
875 std::ref(m_dq_des), std::cref(m_q_min), std::cref(m_q_max), std::cref(m_dq_max),
876 std::cref(m_ddq_max), std::ref(m_robot_state), std::ref(m_mutex));
893 const double &filter_gain,
const bool &activate_pi_controller)
898 if (ft.
size() != 7) {
903 for (
size_t i = 0; i < m_tau_J_des.size(); i++) {
904 m_tau_J_des[i] = ft[i];
915 if (ft.
size() != 6) {
931 if (!m_ftControlThreadIsRunning) {
933 m_ftControlThreadIsRunning =
true;
934 m_ftControlThread = std::thread(&vpForceTorqueGenerator::control_thread, vpForceTorqueGenerator(),
935 std::ref(m_handler), std::ref(m_ftControlThreadStopAsked), m_log_folder, frame,
936 std::ref(m_tau_J_des), std::ref(m_ft_cart_des), std::ref(m_robot_state),
937 std::ref(m_mutex), filter_gain, activate_pi_controller);
946 franka::RobotState robot_state;
948 if (!m_velControlThreadIsRunning && !m_ftControlThreadIsRunning) {
949 robot_state = m_handler->readOnce();
951 std::lock_guard<std::mutex> lock(m_mutex);
952 m_robot_state = robot_state;
955 std::lock_guard<std::mutex> lock(m_mutex);
956 robot_state = m_robot_state;
970 for (
size_t i = 0; i < m_q_min.size(); i++)
971 q_min[i] = m_q_min[i];
983 for (
size_t i = 0; i < m_q_max.size(); i++)
984 q_max[i] = m_q_max[i];
1080 std::ifstream fd(filename.c_str(), std::ios::in);
1082 if (!fd.is_open()) {
1087 std::string key(
"R:");
1088 std::string id(
"#PANDA - Joint position file");
1089 bool pos_found =
false;
1095 while (std::getline(fd, line)) {
1098 if (!(line.compare(0,
id.size(),
id) == 0)) {
1099 std::cout <<
"Error: this position file " << filename <<
" is not for Afma6 robot" << std::endl;
1103 if ((line.compare(0, 1,
"#") == 0)) {
1106 if ((line.compare(0, key.size(), key) == 0)) {
1109 if (chain.size() < njoints + 1)
1111 if (chain.size() < njoints + 1)
1114 std::istringstream ss(line);
1117 for (
unsigned int i = 0; i < njoints; i++)
1125 for (
unsigned int i = 0; i < njoints; i++) {
1132 std::cout <<
"Error: unable to find a position for Panda robot in " << filename << std::endl;
1165 fd = fopen(filename.c_str(),
"w");
1169 fprintf(fd,
"#PANDA - Joint position file\n"
1171 "# R: q1 q2 q3 q4 q5 q6 q7\n"
1172 "# with joint positions q1 to q7 expressed in degrees\n"
1207 if (m_franka_address.empty()) {
1210 if (m_gripper ==
nullptr)
1211 m_gripper =
new franka::Gripper(m_franka_address);
1213 m_gripper->homing();
1227 if (m_franka_address.empty()) {
1230 if (m_gripper ==
nullptr)
1231 m_gripper =
new franka::Gripper(m_franka_address);
1234 franka::GripperState gripper_state = m_gripper->readOnce();
1236 if (gripper_state.max_width < width) {
1237 std::cout <<
"Finger width request is too large for the current fingers on the gripper."
1238 <<
"Maximum possible width is " << gripper_state.max_width << std::endl;
1239 return EXIT_FAILURE;
1242 m_gripper->move(width, 0.1);
1243 return EXIT_SUCCESS;
1266 if (m_franka_address.empty()) {
1269 if (m_gripper ==
nullptr)
1270 m_gripper =
new franka::Gripper(m_franka_address);
1273 franka::GripperState gripper_state = m_gripper->readOnce();
1275 m_gripper->move(gripper_state.max_width, 0.1);
1276 return EXIT_SUCCESS;
1287 if (m_franka_address.empty()) {
1290 if (m_gripper ==
nullptr)
1291 m_gripper =
new franka::Gripper(m_franka_address);
1332 if (m_gripper ==
nullptr)
1333 m_gripper =
new franka::Gripper(m_franka_address);
1336 franka::GripperState gripper_state = m_gripper->readOnce();
1337 std::cout <<
"Gripper max witdh: " << gripper_state.max_width << std::endl;
1338 if (gripper_state.max_width < grasping_width) {
1339 std::cout <<
"Object is too large for the current fingers on the gripper."
1340 <<
"Maximum possible width is " << gripper_state.max_width << std::endl;
1341 return EXIT_FAILURE;
1345 if (!m_gripper->grasp(grasping_width, speed, force)) {
1346 std::cout <<
"Failed to grasp object." << std::endl;
1347 return EXIT_FAILURE;
1350 return EXIT_SUCCESS;
1353 #elif !defined(VISP_BUILD_SHARED_LIBS)
1356 void dummy_vpRobotFranka() { };
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
unsigned int size() const
Return the number of elements of the 2D array.
Implementation of column vector and the associated operations.
void resize(unsigned int i, bool flagNullify=true)
error that can be emitted by ViSP classes.
@ functionNotImplementedError
Function not implemented.
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double rad(double deg)
static double deg(double rad)
Implementation of a matrix and operations on matrices.
Implementation of a pose vector and operations on poses.
vpPoseVector & build(const double &tx, const double &ty, const double &tz, const double &tux, const double &tuy, const double &tuz)
Error that can be emitted by the vpRobot class and its derivatives.
@ wrongStateError
Wrong robot state.
void setVelocity(const vpRobot::vpControlFrameType frame, const vpColVector &vel) VP_OVERRIDE
franka::RobotState getRobotInternalState()
void move(const std::string &filename, double velocity_percentage=10.)
bool savePosFile(const std::string &filename, const vpColVector &q)
void getForceTorque(const vpRobot::vpControlFrameType frame, vpColVector &force)
vpHomogeneousMatrix get_eMc() const
int gripperGrasp(double grasping_width, double force=60.)
vpColVector getJointMin() const
void setLogFolder(const std::string &folder)
void getGravity(vpColVector &gravity)
int gripperMove(double width)
void getMass(vpMatrix &mass)
void getPosition(const vpRobot::vpControlFrameType frame, vpColVector &position) VP_OVERRIDE
void setForceTorque(const vpRobot::vpControlFrameType frame, const vpColVector &ft, const double &filter_gain=0.1, const bool &activate_pi_controller=false)
vpColVector getJointMax() const
void getCoriolis(vpColVector &coriolis)
void setPositioningVelocity(double velocity)
void get_fJe(vpMatrix &fJe) VP_OVERRIDE
void set_eMc(const vpHomogeneousMatrix &eMc)
bool readPosFile(const std::string &filename, vpColVector &q)
vpRobot::vpRobotStateType setRobotState(vpRobot::vpRobotStateType newState)
void get_eJe(vpMatrix &eJe) VP_OVERRIDE
void getVelocity(const vpRobot::vpControlFrameType frame, vpColVector &d_position)
void connect(const std::string &franka_address, franka::RealtimeConfig realtime_config=franka::RealtimeConfig::kEnforce)
vpHomogeneousMatrix get_fMe(const vpColVector &q)
void setPosition(const vpRobot::vpControlFrameType frame, const vpColVector &position) VP_OVERRIDE
vpHomogeneousMatrix get_fMc(const vpColVector &q)
Class that defines a generic virtual robot.
int nDof
number of degrees of freedom
virtual vpRobotStateType getRobotState(void) const
static vpColVector saturateVelocities(const vpColVector &v_in, const vpColVector &v_max, bool verbose=false)
@ STATE_POSITION_CONTROL
Initialize the position controller.
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
@ STATE_STOP
Stops robot motion especially in velocity and acceleration control.
@ STATE_FORCE_TORQUE_CONTROL
Initialize the force/torque controller.
double getMaxRotationVelocity(void) const
virtual vpRobotStateType setRobotState(const vpRobot::vpRobotStateType newState)
double getMaxTranslationVelocity(void) const