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

Update Force Example Controller.

parent d417c02f
No related branches found
No related tags found
No related merge requests found
...@@ -5,8 +5,8 @@ from dynamic_reconfigure.parameter_generator_catkin import * ...@@ -5,8 +5,8 @@ from dynamic_reconfigure.parameter_generator_catkin import *
gen = ParameterGenerator() gen = ParameterGenerator()
gen.add("desired_mass", double_t, 0, "desired mass for rendered force due to gravity applied in the z axis", 0.0, 0.0, 5.0) gen.add("desired_mass", double_t, 0, "desired mass for rendered force due to gravity applied in the z axis", 0.0, 0.0, 2.0)
gen.add("k_p", double_t, 0, "force P gain", 0.0, 0.0, 20.0) gen.add("k_p", double_t, 0, "force P gain", 0.0, 0.0, 2.0)
gen.add("k_i", double_t, 0, "force I gain", 0.0, 0.0, 20.0) gen.add("k_i", double_t, 0, "force I gain", 0.0, 0.0, 2.0)
exit(gen.generate(PACKAGE, "dynamic_mass", "desired_mass_param")) exit(gen.generate(PACKAGE, "dynamic_mass", "desired_mass_param"))
...@@ -30,9 +30,16 @@ class ForceExampleController : public controller_interface::MultiInterfaceContro ...@@ -30,9 +30,16 @@ class ForceExampleController : public controller_interface::MultiInterfaceContro
void update(const ros::Time&, const ros::Duration& period) override; void update(const ros::Time&, const ros::Duration& period) override;
private: private:
// 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);
std::unique_ptr<franka_hw::FrankaModelHandle> model_handle_; std::unique_ptr<franka_hw::FrankaModelHandle> model_handle_;
std::unique_ptr<franka_hw::FrankaStateHandle> state_handle_; std::unique_ptr<franka_hw::FrankaStateHandle> state_handle_;
std::vector<hardware_interface::JointHandle> joint_handles_; std::vector<hardware_interface::JointHandle> joint_handles_;
double desired_mass_{0.0}; double desired_mass_{0.0};
double target_mass_{0.0}; double target_mass_{0.0};
double k_p_{0.0}; double k_p_{0.0};
...@@ -42,6 +49,7 @@ class ForceExampleController : public controller_interface::MultiInterfaceContro ...@@ -42,6 +49,7 @@ class ForceExampleController : public controller_interface::MultiInterfaceContro
double filter_gain_{0.001}; double filter_gain_{0.001};
Eigen::Matrix<double, 7, 1> tau_ext_initial_; Eigen::Matrix<double, 7, 1> tau_ext_initial_;
Eigen::Matrix<double, 7, 1> tau_error_; Eigen::Matrix<double, 7, 1> tau_error_;
const double delta_tau_max_{1.0};
// Dynamic reconfigure // Dynamic reconfigure
std::unique_ptr<dynamic_reconfigure::Server<franka_example_controllers::desired_mass_paramConfig>> std::unique_ptr<dynamic_reconfigure::Server<franka_example_controllers::desired_mass_paramConfig>>
......
...@@ -89,7 +89,7 @@ bool ForceExampleController::init(hardware_interface::RobotHW* robot_hw, ...@@ -89,7 +89,7 @@ bool ForceExampleController::init(hardware_interface::RobotHW* robot_hw,
void ForceExampleController::starting(const ros::Time& /*time*/) { void ForceExampleController::starting(const ros::Time& /*time*/) {
franka::RobotState robot_state = state_handle_->getRobotState(); franka::RobotState robot_state = state_handle_->getRobotState();
std::array<double, 7> gravity_array = std::array<double, 7> gravity_array =
model_handle_->getGravity(robot_state, 0.0, {{0.0, 0.0, 0.0}}); model_handle_->getGravity(robot_state, robot_state.m_total, robot_state.F_x_Ctotal);
Eigen::Map<Eigen::Matrix<double, 7, 1> > tau_measured(robot_state.tau_J.data()); Eigen::Map<Eigen::Matrix<double, 7, 1> > tau_measured(robot_state.tau_J.data());
Eigen::Map<Eigen::Matrix<double, 7, 1> > gravity(gravity_array.data()); Eigen::Map<Eigen::Matrix<double, 7, 1> > gravity(gravity_array.data());
// Bias correction for the current external torque // Bias correction for the current external torque
...@@ -102,9 +102,11 @@ void ForceExampleController::update(const ros::Time& /*time*/, const ros::Durati ...@@ -102,9 +102,11 @@ void ForceExampleController::update(const ros::Time& /*time*/, const ros::Durati
std::array<double, 42> jacobian_array = std::array<double, 42> jacobian_array =
model_handle_->getZeroJacobian(franka::Frame::kEndEffector, robot_state); model_handle_->getZeroJacobian(franka::Frame::kEndEffector, robot_state);
std::array<double, 7> gravity_array = std::array<double, 7> gravity_array =
model_handle_->getGravity(robot_state, 0.0, {{0.0, 0.0, 0.0}}); model_handle_->getGravity(robot_state, robot_state.m_total, robot_state.F_x_Ctotal);
Eigen::Map<Eigen::Matrix<double, 6, 7> > jacobian(jacobian_array.data()); Eigen::Map<Eigen::Matrix<double, 6, 7> > jacobian(jacobian_array.data());
Eigen::Map<Eigen::Matrix<double, 7, 1> > tau_measured(robot_state.tau_J.data()); Eigen::Map<Eigen::Matrix<double, 7, 1> > tau_measured(robot_state.tau_J.data());
Eigen::Map<Eigen::Matrix<double, 7, 1> > tau_J_d( // NOLINT (readability-identifier-naming)
robot_state.tau_J_d.data());
Eigen::Map<Eigen::Matrix<double, 7, 1> > gravity(gravity_array.data()); Eigen::Map<Eigen::Matrix<double, 7, 1> > gravity(gravity_array.data());
Eigen::VectorXd tau_d(7), desired_force_torque(6), tau_cmd(7), tau_ext(7); Eigen::VectorXd tau_d(7), desired_force_torque(6), tau_cmd(7), tau_ext(7);
...@@ -115,6 +117,8 @@ void ForceExampleController::update(const ros::Time& /*time*/, const ros::Durati ...@@ -115,6 +117,8 @@ void ForceExampleController::update(const ros::Time& /*time*/, const ros::Durati
tau_error_ = tau_error_ + period.toSec() * (tau_d - tau_ext); tau_error_ = tau_error_ + period.toSec() * (tau_d - tau_ext);
// FF + PI control (PI gains are intially all 0) // FF + PI control (PI gains are intially all 0)
tau_cmd = tau_d + k_p_ * (tau_d - tau_ext) + k_i_ * tau_error_; tau_cmd = tau_d + k_p_ * (tau_d - tau_ext) + k_i_ * tau_error_;
tau_cmd << saturateTorqueRate(tau_cmd, 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_cmd(i)); joint_handles_[i].setCommand(tau_cmd(i));
} }
...@@ -133,6 +137,20 @@ void ForceExampleController::desiredMassParamCallback( ...@@ -133,6 +137,20 @@ void ForceExampleController::desiredMassParamCallback(
target_k_i_ = config.k_i; target_k_i_ = config.k_i;
} }
Eigen::Matrix<double, 7, 1> ForceExampleController::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) {
Eigen::Matrix<double, 7, 1> tau_d_saturated{};
for (size_t i = 0; i < 7; i++) {
// 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]);
tau_d_saturated[i] =
(tau_J_d[i] - gravity[i]) + std::max(std::min(difference, delta_tau_max_), -delta_tau_max_);
}
return tau_d_saturated;
}
} // namespace franka_example_controllers } // namespace franka_example_controllers
PLUGINLIB_EXPORT_CLASS(franka_example_controllers::ForceExampleController, PLUGINLIB_EXPORT_CLASS(franka_example_controllers::ForceExampleController,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment