Skip to content
Snippets Groups Projects
Commit 113dd49a authored by Simon Gabl's avatar Simon Gabl
Browse files

Refactoring examples.

parent d98a9b12
Branches
Tags
No related merge requests found
Showing
with 33 additions and 38 deletions
...@@ -31,12 +31,16 @@ class CartesianImpedanceExampleController : public controller_interface::MultiIn ...@@ -31,12 +31,16 @@ class CartesianImpedanceExampleController : public controller_interface::MultiIn
void update(const ros::Time&, const ros::Duration& period) override; void update(const ros::Time&, const ros::Duration& period) override;
private: private:
std::string arm_id_; // Saturation
ros::NodeHandle node_handle_; Eigen::Matrix<double, 7, 1> saturateTorqueRate(
const Eigen::Matrix<double, 7, 1>& tau_d_calculated,
const Eigen::Matrix<double, 7, 1>& tau_J_d, // NOLINT (readability-identifier-naming)
const Eigen::Matrix<double, 7, 1>& gravity);
std::unique_ptr<franka_hw::FrankaStateHandle> state_handle_; std::unique_ptr<franka_hw::FrankaStateHandle> state_handle_;
std::unique_ptr<franka_hw::FrankaModelHandle> model_handle_; std::unique_ptr<franka_hw::FrankaModelHandle> model_handle_;
std::vector<hardware_interface::JointHandle> joint_handles_; std::vector<hardware_interface::JointHandle> joint_handles_;
std::vector<std::string> joint_names_;
double filter_params_{0.005}; double filter_params_{0.005};
double nullspace_stiffness_{20.0}; double nullspace_stiffness_{20.0};
double nullspace_stiffness_target_{20.0}; double nullspace_stiffness_target_{20.0};
...@@ -51,12 +55,6 @@ class CartesianImpedanceExampleController : public controller_interface::MultiIn ...@@ -51,12 +55,6 @@ class CartesianImpedanceExampleController : public controller_interface::MultiIn
Eigen::Vector3d position_d_target_; Eigen::Vector3d position_d_target_;
Eigen::Quaterniond orientation_d_target_; Eigen::Quaterniond orientation_d_target_;
// Saturation
Eigen::Matrix<double, 7, 1> saturateTorqueRate(
const Eigen::Matrix<double, 7, 1>& tau_d_calculated,
const Eigen::Matrix<double, 7, 1>& tau_J_d, // NOLINT (readability-identifier-naming)
const Eigen::Matrix<double, 7, 1>& gravity);
// Dynamic reconfigure // Dynamic reconfigure
std::unique_ptr<dynamic_reconfigure::Server<franka_example_controllers::compliance_paramConfig>> std::unique_ptr<dynamic_reconfigure::Server<franka_example_controllers::compliance_paramConfig>>
dynamic_server_compliance_param_; dynamic_server_compliance_param_;
......
...@@ -27,7 +27,6 @@ class CartesianPoseExampleController ...@@ -27,7 +27,6 @@ class CartesianPoseExampleController
void update(const ros::Time&, const ros::Duration& period) override; void update(const ros::Time&, const ros::Duration& period) override;
private: private:
std::string arm_id_;
franka_hw::FrankaPoseCartesianInterface* cartesian_pose_interface_; franka_hw::FrankaPoseCartesianInterface* cartesian_pose_interface_;
std::unique_ptr<franka_hw::FrankaCartesianPoseHandle> cartesian_pose_handle_; std::unique_ptr<franka_hw::FrankaCartesianPoseHandle> cartesian_pose_handle_;
ros::Duration elapsed_time_; ros::Duration elapsed_time_;
......
...@@ -26,7 +26,6 @@ class CartesianVelocityExampleController : public controller_interface::MultiInt ...@@ -26,7 +26,6 @@ class CartesianVelocityExampleController : public controller_interface::MultiInt
void stopping(const ros::Time&) override; void stopping(const ros::Time&) override;
private: private:
std::string arm_id_;
franka_hw::FrankaVelocityCartesianInterface* velocity_cartesian_interface_; franka_hw::FrankaVelocityCartesianInterface* velocity_cartesian_interface_;
std::unique_ptr<franka_hw::FrankaCartesianVelocityHandle> velocity_cartesian_handle_; std::unique_ptr<franka_hw::FrankaCartesianVelocityHandle> velocity_cartesian_handle_;
ros::Duration elapsed_time_; ros::Duration elapsed_time_;
......
...@@ -30,8 +30,8 @@ class JointImpedanceExampleController : public controller_interface::MultiInterf ...@@ -30,8 +30,8 @@ class JointImpedanceExampleController : public controller_interface::MultiInterf
void update(const ros::Time&, const ros::Duration& period) override; void update(const ros::Time&, const ros::Duration& period) override;
private: private:
// Saturation
std::array<double, 7> saturateTorqueRate( std::array<double, 7> saturateTorqueRate(
const double delta_tau_max,
const std::array<double, 7>& tau_d_calculated, const std::array<double, 7>& tau_d_calculated,
const std::array<double, 7>& tau_J_d, // NOLINT (readability-identifier-naming) const std::array<double, 7>& tau_J_d, // NOLINT (readability-identifier-naming)
const std::array<double, 7>& gravity); const std::array<double, 7>& gravity);
...@@ -40,6 +40,7 @@ class JointImpedanceExampleController : public controller_interface::MultiInterf ...@@ -40,6 +40,7 @@ class JointImpedanceExampleController : public controller_interface::MultiInterf
std::unique_ptr<franka_hw::FrankaModelHandle> model_handle_; std::unique_ptr<franka_hw::FrankaModelHandle> model_handle_;
std::vector<hardware_interface::JointHandle> joint_handles_; std::vector<hardware_interface::JointHandle> joint_handles_;
const double delta_tau_max_{1.0};
double radius_{0.1}; double radius_{0.1};
double acceleration_time_{2.0}; double acceleration_time_{2.0};
double vel_max_{0.05}; double vel_max_{0.05};
......
...@@ -25,7 +25,6 @@ class JointPositionExampleController : public controller_interface::MultiInterfa ...@@ -25,7 +25,6 @@ class JointPositionExampleController : public controller_interface::MultiInterfa
void update(const ros::Time&, const ros::Duration& period) override; void update(const ros::Time&, const ros::Duration& period) override;
private: private:
std::vector<std::string> joint_names_;
hardware_interface::PositionJointInterface* position_joint_interface_; hardware_interface::PositionJointInterface* position_joint_interface_;
std::vector<hardware_interface::JointHandle> position_joint_handles_; std::vector<hardware_interface::JointHandle> position_joint_handles_;
ros::Duration elapsed_time_; ros::Duration elapsed_time_;
......
...@@ -26,7 +26,6 @@ class JointVelocityExampleController : public controller_interface::MultiInterfa ...@@ -26,7 +26,6 @@ class JointVelocityExampleController : public controller_interface::MultiInterfa
void stopping(const ros::Time&) override; void stopping(const ros::Time&) override;
private: private:
std::vector<std::string> joint_names_;
hardware_interface::VelocityJointInterface* velocity_joint_interface_; hardware_interface::VelocityJointInterface* velocity_joint_interface_;
std::vector<hardware_interface::JointHandle> velocity_joint_handles_; std::vector<hardware_interface::JointHandle> velocity_joint_handles_;
ros::Duration elapsed_time_; ros::Duration elapsed_time_;
......
...@@ -17,17 +17,18 @@ bool CartesianImpedanceExampleController::init(hardware_interface::RobotHW* robo ...@@ -17,17 +17,18 @@ bool CartesianImpedanceExampleController::init(hardware_interface::RobotHW* robo
ros::NodeHandle& node_handle) { ros::NodeHandle& node_handle) {
std::vector<double> cartesian_stiffness_vector; std::vector<double> cartesian_stiffness_vector;
std::vector<double> cartesian_damping_vector; std::vector<double> cartesian_damping_vector;
node_handle_ = node_handle; std::string arm_id;
std::vector<std::string> joint_names;
sub_equilibrium_pose_ = node_handle_.subscribe( sub_equilibrium_pose_ = node_handle.subscribe(
"/equilibrium_pose", 20, &CartesianImpedanceExampleController::equilibriumPoseCallback, this, "/equilibrium_pose", 20, &CartesianImpedanceExampleController::equilibriumPoseCallback, this,
ros::TransportHints().reliable().tcpNoDelay()); ros::TransportHints().reliable().tcpNoDelay());
if (!node_handle_.getParam("arm_id", arm_id_)) { if (!node_handle.getParam("arm_id", arm_id)) {
ROS_ERROR_STREAM("CartesianImpedanceExampleController: Could not read parameter arm_id"); ROS_ERROR_STREAM("CartesianImpedanceExampleController: Could not read parameter arm_id");
return false; return false;
} }
if (!node_handle_.getParam("joint_names", joint_names_) || joint_names_.size() != 7) { if (!node_handle.getParam("joint_names", joint_names) || joint_names.size() != 7) {
ROS_ERROR( ROS_ERROR(
"CartesianImpedanceExampleController: Invalid or no joint_names parameters provided, " "CartesianImpedanceExampleController: Invalid or no joint_names parameters provided, "
"aborting " "aborting "
...@@ -44,7 +45,7 @@ bool CartesianImpedanceExampleController::init(hardware_interface::RobotHW* robo ...@@ -44,7 +45,7 @@ bool CartesianImpedanceExampleController::init(hardware_interface::RobotHW* robo
} }
try { try {
model_handle_.reset( model_handle_.reset(
new franka_hw::FrankaModelHandle(model_interface->getHandle(arm_id_ + "_model"))); new franka_hw::FrankaModelHandle(model_interface->getHandle(arm_id + "_model")));
} catch (hardware_interface::HardwareInterfaceException& ex) { } catch (hardware_interface::HardwareInterfaceException& ex) {
ROS_ERROR_STREAM( ROS_ERROR_STREAM(
"CartesianImpedanceExampleController: Exception getting model handle from interface: " "CartesianImpedanceExampleController: Exception getting model handle from interface: "
...@@ -61,7 +62,7 @@ bool CartesianImpedanceExampleController::init(hardware_interface::RobotHW* robo ...@@ -61,7 +62,7 @@ bool CartesianImpedanceExampleController::init(hardware_interface::RobotHW* robo
} }
try { try {
state_handle_.reset( state_handle_.reset(
new franka_hw::FrankaStateHandle(state_interface->getHandle(arm_id_ + "_robot"))); new franka_hw::FrankaStateHandle(state_interface->getHandle(arm_id + "_robot")));
} catch (hardware_interface::HardwareInterfaceException& ex) { } catch (hardware_interface::HardwareInterfaceException& ex) {
ROS_ERROR_STREAM( ROS_ERROR_STREAM(
"CartesianImpedanceExampleController: Exception getting state handle from interface: " "CartesianImpedanceExampleController: Exception getting state handle from interface: "
...@@ -78,7 +79,7 @@ bool CartesianImpedanceExampleController::init(hardware_interface::RobotHW* robo ...@@ -78,7 +79,7 @@ bool CartesianImpedanceExampleController::init(hardware_interface::RobotHW* robo
} }
for (size_t i = 0; i < 7; ++i) { for (size_t i = 0; i < 7; ++i) {
try { try {
joint_handles_.push_back(effort_joint_interface->getHandle(joint_names_[i])); joint_handles_.push_back(effort_joint_interface->getHandle(joint_names[i]));
} catch (const hardware_interface::HardwareInterfaceException& ex) { } catch (const hardware_interface::HardwareInterfaceException& ex) {
ROS_ERROR_STREAM( ROS_ERROR_STREAM(
"CartesianImpedanceExampleController: Exception getting joint handles: " << ex.what()); "CartesianImpedanceExampleController: Exception getting joint handles: " << ex.what());
......
...@@ -18,6 +18,7 @@ namespace franka_example_controllers { ...@@ -18,6 +18,7 @@ namespace franka_example_controllers {
bool CartesianPoseExampleController::init(hardware_interface::RobotHW* robot_hardware, bool CartesianPoseExampleController::init(hardware_interface::RobotHW* robot_hardware,
ros::NodeHandle& root_node_handle, ros::NodeHandle& root_node_handle,
ros::NodeHandle& /* controller_node_handle */) { ros::NodeHandle& /* controller_node_handle */) {
std::string arm_id;
cartesian_pose_interface_ = robot_hardware->get<franka_hw::FrankaPoseCartesianInterface>(); cartesian_pose_interface_ = robot_hardware->get<franka_hw::FrankaPoseCartesianInterface>();
if (cartesian_pose_interface_ == nullptr) { if (cartesian_pose_interface_ == nullptr) {
ROS_ERROR( ROS_ERROR(
...@@ -26,14 +27,14 @@ bool CartesianPoseExampleController::init(hardware_interface::RobotHW* robot_har ...@@ -26,14 +27,14 @@ bool CartesianPoseExampleController::init(hardware_interface::RobotHW* robot_har
return false; return false;
} }
if (!root_node_handle.getParam("arm_id", arm_id_)) { if (!root_node_handle.getParam("arm_id", arm_id)) {
ROS_ERROR("CartesianPoseExampleController: Could not get parameter arm_id"); ROS_ERROR("CartesianPoseExampleController: Could not get parameter arm_id");
return false; return false;
} }
try { try {
cartesian_pose_handle_.reset(new franka_hw::FrankaCartesianPoseHandle( cartesian_pose_handle_.reset(new franka_hw::FrankaCartesianPoseHandle(
cartesian_pose_interface_->getHandle(arm_id_ + "_robot"))); cartesian_pose_interface_->getHandle(arm_id + "_robot")));
} catch (const hardware_interface::HardwareInterfaceException& e) { } catch (const hardware_interface::HardwareInterfaceException& e) {
ROS_ERROR_STREAM( ROS_ERROR_STREAM(
"CartesianPoseExampleController: Exception getting Cartesian handle: " << e.what()); "CartesianPoseExampleController: Exception getting Cartesian handle: " << e.what());
...@@ -47,7 +48,7 @@ bool CartesianPoseExampleController::init(hardware_interface::RobotHW* robot_har ...@@ -47,7 +48,7 @@ bool CartesianPoseExampleController::init(hardware_interface::RobotHW* robot_har
} }
try { try {
auto state_handle = state_interface->getHandle(arm_id_ + "_robot"); auto state_handle = state_interface->getHandle(arm_id + "_robot");
std::array<double, 7> q_start{{0, -M_PI_4, 0, -3 * M_PI_4, 0, M_PI_2, M_PI_4}}; std::array<double, 7> q_start{{0, -M_PI_4, 0, -3 * M_PI_4, 0, M_PI_2, M_PI_4}};
for (size_t i = 0; i < q_start.size(); i++) { for (size_t i = 0; i < q_start.size(); i++) {
......
...@@ -18,7 +18,8 @@ namespace franka_example_controllers { ...@@ -18,7 +18,8 @@ namespace franka_example_controllers {
bool CartesianVelocityExampleController::init(hardware_interface::RobotHW* robot_hardware, bool CartesianVelocityExampleController::init(hardware_interface::RobotHW* robot_hardware,
ros::NodeHandle& root_node_handle, ros::NodeHandle& root_node_handle,
ros::NodeHandle& /* controller_node_handle */) { ros::NodeHandle& /* controller_node_handle */) {
if (!root_node_handle.getParam("arm_id", arm_id_)) { std::string arm_id;
if (!root_node_handle.getParam("arm_id", arm_id)) {
ROS_ERROR("CartesianVelocityExampleController: Could not get parameter arm_id"); ROS_ERROR("CartesianVelocityExampleController: Could not get parameter arm_id");
return false; return false;
} }
...@@ -33,7 +34,7 @@ bool CartesianVelocityExampleController::init(hardware_interface::RobotHW* robot ...@@ -33,7 +34,7 @@ bool CartesianVelocityExampleController::init(hardware_interface::RobotHW* robot
} }
try { try {
velocity_cartesian_handle_.reset(new franka_hw::FrankaCartesianVelocityHandle( velocity_cartesian_handle_.reset(new franka_hw::FrankaCartesianVelocityHandle(
velocity_cartesian_interface_->getHandle(arm_id_ + "_robot"))); velocity_cartesian_interface_->getHandle(arm_id + "_robot")));
} catch (const hardware_interface::HardwareInterfaceException& e) { } catch (const hardware_interface::HardwareInterfaceException& e) {
ROS_ERROR_STREAM( ROS_ERROR_STREAM(
"CartesianVelocityExampleController: Exception getting Cartesian handle: " << e.what()); "CartesianVelocityExampleController: Exception getting Cartesian handle: " << e.what());
...@@ -47,7 +48,7 @@ bool CartesianVelocityExampleController::init(hardware_interface::RobotHW* robot ...@@ -47,7 +48,7 @@ bool CartesianVelocityExampleController::init(hardware_interface::RobotHW* robot
} }
try { try {
auto state_handle = state_interface->getHandle(arm_id_ + "_robot"); auto state_handle = state_interface->getHandle(arm_id + "_robot");
std::array<double, 7> q_start = {{0, -M_PI_4, 0, -3 * M_PI_4, 0, M_PI_2, M_PI_4}}; std::array<double, 7> q_start = {{0, -M_PI_4, 0, -3 * M_PI_4, 0, M_PI_2, M_PI_4}};
for (size_t i = 0; i < q_start.size(); i++) { for (size_t i = 0; i < q_start.size(); i++) {
......
...@@ -174,9 +174,8 @@ void JointImpedanceExampleController::update(const ros::Time& /*time*/, ...@@ -174,9 +174,8 @@ void JointImpedanceExampleController::update(const ros::Time& /*time*/,
// Maximum torque difference with a sampling rate of 1 kHz. The maximum torque rate is // Maximum torque difference with a sampling rate of 1 kHz. The maximum torque rate is
// 1000 * (1 / sampling_time). // 1000 * (1 / sampling_time).
const double delta_tau_max = 1.0; // NOLINT (readability-identifier-naming)
std::array<double, 7> tau_d_saturated = std::array<double, 7> tau_d_saturated =
saturateTorqueRate(delta_tau_max, tau_d_calculated, robot_state.tau_J_d, gravity); saturateTorqueRate(tau_d_calculated, robot_state.tau_J_d, gravity);
for (size_t i = 0; i < 7; ++i) { for (size_t i = 0; i < 7; ++i) {
joint_handles_[i].setCommand(tau_d_saturated[i]); joint_handles_[i].setCommand(tau_d_saturated[i]);
...@@ -205,7 +204,6 @@ void JointImpedanceExampleController::update(const ros::Time& /*time*/, ...@@ -205,7 +204,6 @@ void JointImpedanceExampleController::update(const ros::Time& /*time*/,
} }
std::array<double, 7> JointImpedanceExampleController::saturateTorqueRate( std::array<double, 7> JointImpedanceExampleController::saturateTorqueRate(
const double delta_tau_max, // NOLINT (readability-identifier-naming)
const std::array<double, 7>& tau_d_calculated, const std::array<double, 7>& tau_d_calculated,
const std::array<double, 7>& tau_J_d, // NOLINT (readability-identifier-naming) const std::array<double, 7>& tau_J_d, // NOLINT (readability-identifier-naming)
const std::array<double, 7>& gravity) { const std::array<double, 7>& gravity) {
...@@ -214,7 +212,7 @@ std::array<double, 7> JointImpedanceExampleController::saturateTorqueRate( ...@@ -214,7 +212,7 @@ std::array<double, 7> JointImpedanceExampleController::saturateTorqueRate(
// TODO(sga): After gravity is removed from tau_J_d, do not subtract it any more. // TODO(sga): After gravity is removed from tau_J_d, do not subtract it any more.
double difference = tau_d_calculated[i] - (tau_J_d[i] - gravity[i]); double difference = tau_d_calculated[i] - (tau_J_d[i] - gravity[i]);
tau_d_saturated[i] = tau_d_saturated[i] =
(tau_J_d[i] - gravity[i]) + std::max(std::min(difference, delta_tau_max), -delta_tau_max); (tau_J_d[i] - gravity[i]) + std::max(std::min(difference, delta_tau_max_), -delta_tau_max_);
} }
return tau_d_saturated; return tau_d_saturated;
} }
......
...@@ -32,11 +32,11 @@ bool JointPositionExampleController::init(hardware_interface::RobotHW* robot_har ...@@ -32,11 +32,11 @@ bool JointPositionExampleController::init(hardware_interface::RobotHW* robot_har
return false; return false;
} }
position_joint_handles_.resize(7); position_joint_handles_.resize(7);
joint_names_.resize(7); std::array<std::string, 7> joint_names;
for (size_t i = 0; i < 7; ++i) { for (size_t i = 0; i < 7; ++i) {
joint_names_[i] = static_cast<std::string>(parameters[i]); joint_names[i] = static_cast<std::string>(parameters[i]);
try { try {
position_joint_handles_[i] = position_joint_interface_->getHandle(joint_names_[i]); position_joint_handles_[i] = position_joint_interface_->getHandle(joint_names[i]);
} catch (const hardware_interface::HardwareInterfaceException& e) { } catch (const hardware_interface::HardwareInterfaceException& e) {
ROS_ERROR_STREAM( ROS_ERROR_STREAM(
"JointPositionExampleController: Exception getting joint handles: " << e.what()); "JointPositionExampleController: Exception getting joint handles: " << e.what());
......
...@@ -32,11 +32,11 @@ bool JointVelocityExampleController::init(hardware_interface::RobotHW* robot_har ...@@ -32,11 +32,11 @@ bool JointVelocityExampleController::init(hardware_interface::RobotHW* robot_har
return false; return false;
} }
velocity_joint_handles_.resize(7); velocity_joint_handles_.resize(7);
joint_names_.resize(7); std::array<std::string, 7> joint_names;
for (size_t i = 0; i < 7; ++i) { for (size_t i = 0; i < 7; ++i) {
joint_names_[i] = static_cast<std::string>(parameters[i]); joint_names[i] = static_cast<std::string>(parameters[i]);
try { try {
velocity_joint_handles_[i] = velocity_joint_interface_->getHandle(joint_names_[i]); velocity_joint_handles_[i] = velocity_joint_interface_->getHandle(joint_names[i]);
} catch (const hardware_interface::HardwareInterfaceException& ex) { } catch (const hardware_interface::HardwareInterfaceException& ex) {
ROS_ERROR_STREAM( ROS_ERROR_STREAM(
"JointVelocityExampleController: Exception getting joint handles: " << ex.what()); "JointVelocityExampleController: Exception getting joint handles: " << ex.what());
......
...@@ -32,7 +32,6 @@ namespace franka_example_controllers { ...@@ -32,7 +32,6 @@ namespace franka_example_controllers {
bool ModelExampleController::init(hardware_interface::RobotHW* robot_hw, bool ModelExampleController::init(hardware_interface::RobotHW* robot_hw,
ros::NodeHandle& node_handle) { ros::NodeHandle& node_handle) {
std::string arm_id; std::string arm_id;
franka_state_interface_ = robot_hw->get<franka_hw::FrankaStateInterface>(); franka_state_interface_ = robot_hw->get<franka_hw::FrankaStateInterface>();
if (franka_state_interface_ == nullptr) { if (franka_state_interface_ == nullptr) {
ROS_ERROR("ModelExampleController: Could not get Franka state interface from hardware"); ROS_ERROR("ModelExampleController: Could not get Franka state interface from hardware");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment