1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <array> #include <cmath> #include <functional> #include <iostream> #include <Eigen/Dense> #include <franka/duration.h> #include <franka/exception.h> #include <franka/model.h> #include <franka/robot.h> #include "examples_common.h" int main(int argc, char** argv) { if (argc != 2) { std::cerr << "Usage: " << argv[0] << " <robot-hostname>" << std::endl; return -1; } const double translational_stiffness{150.0}; const double rotational_stiffness{10.0}; Eigen::MatrixXd stiffness(6, 6), damping(6, 6); stiffness.setZero(); stiffness.topLeftCorner(3, 3) << translational_stiffness * Eigen::MatrixXd::Identity(3, 3); stiffness.bottomRightCorner(3, 3) << rotational_stiffness * Eigen::MatrixXd::Identity(3, 3); damping.setZero(); damping.topLeftCorner(3, 3) << 2.0 * sqrt(translational_stiffness) * Eigen::MatrixXd::Identity(3, 3); damping.bottomRightCorner(3, 3) << 2.0 * sqrt(rotational_stiffness) * Eigen::MatrixXd::Identity(3, 3); try { franka::Robot robot(argv[1]); setDefaultBehavior(robot); franka::Model model = robot.loadModel(); franka::RobotState initial_state = robot.readOnce(); Eigen::Affine3d initial_transform(Eigen::Matrix4d::Map(initial_state.O_T_EE.data())); Eigen::Vector3d position_d(initial_transform.translation()); Eigen::Quaterniond orientation_d(initial_transform.rotation()); robot.setCollisionBehavior({{100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0}}, {{100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0}}, {{100.0, 100.0, 100.0, 100.0, 100.0, 100.0}}, {{100.0, 100.0, 100.0, 100.0, 100.0, 100.0}}); std::function<franka::Torques(const franka::RobotState&, franka::Duration)> impedance_control_callback = [&](const franka::RobotState& robot_state, franka::Duration ) -> franka::Torques { std::array<double, 7> coriolis_array = model.coriolis(robot_state); std::array<double, 42> jacobian_array = model.zeroJacobian(franka::Frame::kEndEffector, robot_state); Eigen::Map<const Eigen::Matrix<double, 7, 1>> coriolis(coriolis_array.data()); Eigen::Map<const Eigen::Matrix<double, 6, 7>> jacobian(jacobian_array.data()); Eigen::Map<const Eigen::Matrix<double, 7, 1>> q(robot_state.q.data()); Eigen::Map<const Eigen::Matrix<double, 7, 1>> dq(robot_state.dq.data()); Eigen::Affine3d transform(Eigen::Matrix4d::Map(robot_state.O_T_EE.data())); Eigen::Vector3d position(transform.translation()); Eigen::Quaterniond orientation(transform.rotation()); Eigen::Matrix<double, 6, 1> error; error.head(3) << position - position_d; if (orientation_d.coeffs().dot(orientation.coeffs()) < 0.0) { orientation.coeffs() << -orientation.coeffs(); } Eigen::Quaterniond error_quaternion(orientation.inverse() * orientation_d); error.tail(3) << error_quaternion.x(), error_quaternion.y(), error_quaternion.z(); error.tail(3) << -transform.rotation() * error.tail(3); Eigen::VectorXd tau_task(7), tau_d(7); tau_task << jacobian.transpose() * (-stiffness * error - damping * (jacobian * dq)); tau_d << tau_task + coriolis; std::array<double, 7> tau_d_array{}; Eigen::VectorXd::Map(&tau_d_array[0], 7) = tau_d; return tau_d_array; }; std::cout << "WARNING: Collision thresholds are set to high values. " << "Make sure you have the user stop at hand!" << std::endl << "After starting try to push the robot and see how it reacts." << std::endl << "Press Enter to continue..." << std::endl; std::cin.ignore(); robot.control(impedance_control_callback); } catch (const franka::Exception& ex) { std::cout << ex.what() << std::endl; } return 0; }
|