Skip to content

Commit

Permalink
feat(franka_gazebo): implement 'set_franka_model_configuration' service
Browse files Browse the repository at this point in the history
This commit implements a `set_franka_model_configuration` service. This
service can be used to set the Franka configuration in the Gazebo
simulation. Under the hood, this service calls Gazebo's
'set_model_configuration' service while ensuring that the reported joint
positions stay within joint limits (see
#225 for more
information).
  • Loading branch information
rickstaa committed Feb 18, 2022
1 parent 61acc4d commit 0fa2a0f
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 3 deletions.
3 changes: 3 additions & 0 deletions franka_gazebo/include/franka_gazebo/franka_hw_sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class FrankaHWSim : public gazebo_ros_control::RobotHWSim {
ros::ServiceServer service_set_k_;
ros::ServiceServer service_set_load_;
ros::ServiceServer service_collision_behavior_;
ros::ServiceServer service_set_model_configuration_;

ros::ServiceClient gazebo_set_model_configuration_client_;

std::vector<double> lower_force_thresholds_nominal_;
std::vector<double> upper_force_thresholds_nominal_;
Expand Down
14 changes: 14 additions & 0 deletions franka_gazebo/include/franka_gazebo/joint.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <angles/angles.h>
#include <joint_limits_interface/joint_limits.h>
#include <ros/ros.h>
#include <Eigen/Dense>
#include <gazebo/physics/Joint.hh>
Expand Down Expand Up @@ -35,6 +36,10 @@ struct Joint {
/// http://docs.ros.org/en/diamondback/api/urdf/html/classurdf_1_1Joint.html
int type;

/// Joint limits @see
/// https://docs.ros.org/en/diamondback/api/urdf/html/classurdf_1_1JointLimits.html
joint_limits_interface::JointLimits limits;

/// The axis of rotation/translation of this joint in local coordinates
Eigen::Vector3d axis;

Expand Down Expand Up @@ -87,9 +92,18 @@ struct Joint {
*/
bool isInCollision() const;

/**
* Sets the joint position.
*/
void setJointPosition(const double joint_position);

private:
double lastVelocity = std::numeric_limits<double>::quiet_NaN();
double lastAcceleration = std::numeric_limits<double>::quiet_NaN();

// Used to track whether the user requested a joint position to be set.
bool setPositionRequested_ = false;
double requestedPosition_ = 0.0;
};

} // namespace franka_gazebo
113 changes: 113 additions & 0 deletions franka_gazebo/src/franka_hw_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
#include <franka_hw/services.h>
#include <franka_msgs/SetEEFrame.h>
#include <franka_msgs/SetForceTorqueCollisionBehavior.h>
#include <franka_msgs/SetJointConfiguration.h>
#include <franka_msgs/SetKFrame.h>
#include <franka_msgs/SetLoad.h>
#include <gazebo_msgs/SetModelConfiguration.h>
#include <gazebo_ros_control/robot_hw_sim.h>
#include <joint_limits_interface/joint_limits_urdf.h>
#include <Eigen/Dense>
#include <iostream>
#include <sstream>
#include <string>
#include <boost/algorithm/clamp.hpp>
#include "ros/ros.h"

namespace franka_gazebo {

Expand Down Expand Up @@ -85,6 +90,7 @@ bool FrankaHWSim::initSim(const std::string& robot_namespace,
return false;
}
joint->type = urdf_joint->type;
joint_limits_interface::getJointLimits(urdf_joint, joint->limits);
joint->axis = Eigen::Vector3d(urdf_joint->axis.x, urdf_joint->axis.y, urdf_joint->axis.z);

// Get a handle to the underlying Gazebo Joint
Expand Down Expand Up @@ -160,6 +166,10 @@ bool FrankaHWSim::initSim(const std::string& robot_namespace,
// Initialize ROS Services
initServices(model_nh);

// Connect to '/gazebo/set_model_configuration' service
this->gazebo_set_model_configuration_client_ =
model_nh.serviceClient<gazebo_msgs::SetModelConfiguration>("/gazebo/set_model_configuration");

return readParameters(model_nh, *urdf);
}

Expand Down Expand Up @@ -305,6 +315,109 @@ void FrankaHWSim::initServices(ros::NodeHandle& nh) {
response.success = true;
return true;
});
this->service_set_model_configuration_ =
franka_hw::advertiseService<franka_msgs::SetJointConfiguration>(
nh, "set_franka_model_configuration", [&](auto& request, auto& response) {
// Check if positions equals the number of joints
if (request.configuration.name.size() == 1 && request.configuration.name[0].empty()) {
ROS_ERROR_STREAM_NAMED("franka_hw_sim",
"Setting of Franka model configuration failed since "
"no joints were specified in the request.");
response.success = false;
response.error = "no joints specified";
return false;
}
if (request.configuration.name.size() != request.configuration.position.size()) {
ROS_ERROR_STREAM_NAMED("franka_hw_sim",
"Setting of Franka model configuration failed since "
"the 'position' and 'name' fields were of unequal length.");
response.success = false;
response.error = "'position' and 'name' fields length unequal";
return false;
}

// Print request information
std::string requested_configuration_string;
for (int ii = 0; ii < request.configuration.name.size(); ii++) {
requested_configuration_string += request.configuration.name[ii] + ": " + std::to_string(request.configuration.position[ii]) + ", ";
}
requested_configuration_string = requested_configuration_string.substr(0, requested_configuration_string.length() - 2); //Remove last 2 characters
ROS_INFO_STREAM_NAMED("franka_hw_sim", "Setting joint configuration: " << requested_configuration_string);

// Validate joint names and joint limits
std::map<std::string, double> model_configuration;
for (int ii = 0; ii < request.configuration.name.size(); ii++) {
std::string joint(request.configuration.name[ii]);
double position(request.configuration.position[ii]);

if (this->joints_.find(joint) != this->joints_.end()) { // If joint exists
double min_position(this->joints_[joint]->limits.min_position);
double max_position(this->joints_[joint]->limits.max_position);

// Check if position is within joint limits
if (position == boost::algorithm::clamp(position, min_position, max_position)) {
model_configuration.emplace(std::make_pair(joint, position));
} else {
ROS_WARN_STREAM_NAMED("franka_hw_sim",
"Joint configuration of joint '"
<< joint << "' was not set since the requested joint position ("
<< position << ") is not within joint limits (i.e. "
<< min_position << " - " << max_position << ").");
}
} else {
ROS_WARN_STREAM_NAMED("franka_hw_sim",
"Joint configuration of joint '"
<< joint
<< "' not set since it is not a valid panda joint.");
}
}

// Return if no valid positions were found
if (model_configuration.size() == 0){
ROS_ERROR_STREAM_NAMED("franka_hw_sim",
"Setting of Franka model configuration aborted since no valid "
"joint configuration were found.");
response.success = false;
response.error = "no valid joint configurations";
return false;
}

// Throw warnings about unused request fields
if (request.configuration.effort.size() != 0) {
ROS_WARN_STREAM_ONCE_NAMED("franka_hw_sim",
"The 'set_franka_model_configuration' service does not use the "
"'effort' field.");
}
if (request.configuration.velocity.size() != 0) {
ROS_WARN_STREAM_ONCE_NAMED("franka_hw_sim",
"The 'set_franka_model_configuration' service does not use the "
"'velocity' field.");
}

// Call Gazebo 'set_model_configuration' service and update Franka joint positions
gazebo_msgs::SetModelConfiguration gazebo_model_configuration;
gazebo_model_configuration.request.model_name = "panda";
for (const auto& pair : model_configuration) {
gazebo_model_configuration.request.joint_names.push_back(pair.first);
gazebo_model_configuration.request.joint_positions.push_back(pair.second);
}
if (this->gazebo_set_model_configuration_client_.call(gazebo_model_configuration)) {
// Update franka positions
for (const auto& pair : model_configuration) {
this->joints_[pair.first]->setJointPosition(pair.second);
}

response.success = true;
return true;
} else {
ROS_WARN_STREAM_NAMED(
"franka_hw_sim",
"Setting of Franka model configuration failed since "
"a problem occurred when setting the joint configuration in Gazebo.");
response.success = false;
return false;
}
});
}

void FrankaHWSim::readSim(ros::Time time, ros::Duration period) {
Expand Down
13 changes: 13 additions & 0 deletions franka_gazebo/src/joint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ void Joint::update(const ros::Duration& dt) {
return;
}

// Set joint position to requested joint position
// NOTE: Implemented like this to prevent racing conditions.
if (this->setPositionRequested_) {
this->position = this->requestedPosition_;
this->setPositionRequested_ = false;
}

this->velocity = this->handle->GetVelocity(0);
#if GAZEBO_MAJOR_VERSION >= 8
double position = this->handle->Position(0);
Expand Down Expand Up @@ -70,4 +77,10 @@ bool Joint::isInCollision() const {
bool Joint::isInContact() const {
return std::abs(this->effort - this->command) > this->contact_threshold;
}

void Joint::setJointPosition(const double joint_position) {
this->requestedPosition_ = joint_position;
this->setPositionRequested_ = true;
}

} // namespace franka_gazebo
7 changes: 4 additions & 3 deletions franka_msgs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.4)
project(franka_msgs)

find_package(catkin REQUIRED COMPONENTS message_generation std_msgs actionlib_msgs)
find_package(catkin REQUIRED COMPONENTS message_generation std_msgs sensor_msgs actionlib_msgs)

add_message_files(FILES Errors.msg FrankaState.msg)

Expand All @@ -10,13 +10,14 @@ add_service_files(FILES
SetEEFrame.srv
SetForceTorqueCollisionBehavior.srv
SetFullCollisionBehavior.srv
SetJointConfiguration.srv
SetJointImpedance.srv
SetKFrame.srv
SetLoad.srv
)

add_action_files(FILES ErrorRecovery.action)

generate_messages(DEPENDENCIES std_msgs actionlib_msgs)
generate_messages(DEPENDENCIES std_msgs sensor_msgs actionlib_msgs)

catkin_package(CATKIN_DEPENDS message_runtime std_msgs actionlib_msgs)
catkin_package(CATKIN_DEPENDS message_runtime std_msgs sensor_msgs actionlib_msgs)
1 change: 1 addition & 0 deletions franka_msgs/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<build_depend>message_generation</build_depend>

<depend>std_msgs</depend>
<depend>sensor_msgs</depend>
<depend>actionlib_msgs</depend>

<exec_depend>message_runtime</exec_depend>
Expand Down
4 changes: 4 additions & 0 deletions franka_msgs/srv/SetJointConfiguration.srv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sensor_msgs/JointState configuration
---
bool success
string error

0 comments on commit 0fa2a0f

Please sign in to comment.