Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port dynamics_solver to ROS 2 #67

Merged
merged 3 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions moveit_core/dynamics_solver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ set(MOVEIT_LIB_NAME moveit_dynamics_solver)
add_library(${MOVEIT_LIB_NAME} src/dynamics_solver.cpp)
set_target_properties(${MOVEIT_LIB_NAME} PROPERTIES VERSION "${${PROJECT_NAME}_VERSION}")

target_link_libraries(${MOVEIT_LIB_NAME} moveit_robot_state ${catkin_LIBRARIES} ${urdfdom_LIBRARIES} ${urdfdom_headers_LIBRARIES} ${Boost_LIBRARIES})
add_dependencies(${MOVEIT_LIB_NAME} ${catkin_EXPORTED_TARGETS})
ament_target_dependencies(${MOVEIT_LIB_NAME}
moveit_robot_state
urdf
urdfdom_headers
orocos_kdl
visualization_msgs
kdl_parser)

install(TARGETS ${MOVEIT_LIB_NAME}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION})
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)

install(DIRECTORY include/ DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION})
install(DIRECTORY include/
DESTINATION include)
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@
#include <kdl/chainidsolver_recursive_newton_euler.hpp>

#include <moveit/robot_state/robot_state.h>
#include <geometry_msgs/Vector3.h>
#include <geometry_msgs/Wrench.h>

#include <geometry_msgs/msg/vector3.hpp>
#include <geometry_msgs/msg/wrench.hpp>
#include <memory>

/** \brief This namespace includes the dynamics_solver library */
Expand All @@ -68,7 +67,7 @@ class DynamicsSolver
* @return False if initialization failed
*/
DynamicsSolver(const robot_model::RobotModelConstPtr& robot_model, const std::string& group_name,
const geometry_msgs::Vector3& gravity_vector);
const geometry_msgs::msg::Vector3& gravity_vector);

/**
* @brief Get the torques (the order of all input and output is the same
Expand All @@ -86,7 +85,7 @@ class DynamicsSolver
* @return False if any of the input vectors are of the wrong size
*/
bool getTorques(const std::vector<double>& joint_angles, const std::vector<double>& joint_velocities,
const std::vector<double>& joint_accelerations, const std::vector<geometry_msgs::Wrench>& wrenches,
const std::vector<double>& joint_accelerations, const std::vector<geometry_msgs::msg::Wrench>& wrenches,
std::vector<double>& torques) const;

/**
Expand Down
66 changes: 35 additions & 31 deletions moveit_core/dynamics_solver/src/dynamics_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,22 @@
#include <kdl/chainfksolverpos_recursive.hpp>
#include <kdl/chainjnttojacsolver.hpp>
#include <kdl/tree.hpp>
#include "rclcpp/rclcpp.hpp"

namespace dynamics_solver
{

rclcpp::Logger LOGGER = rclcpp::get_logger("moveit").get_child("dynamics_solver");

namespace
{
inline geometry_msgs::Vector3 transformVector(const Eigen::Isometry3d& transform, const geometry_msgs::Vector3& vector)
inline geometry_msgs::msg::Vector3 transformVector(const Eigen::Isometry3d& transform, const geometry_msgs::msg::Vector3& vector)
{
Eigen::Vector3d p;
p = Eigen::Vector3d(vector.x, vector.y, vector.z);
p = transform.rotation() * p;

geometry_msgs::Vector3 result;
geometry_msgs::msg::Vector3 result;
result.x = p.x();
result.y = p.y();
result.z = p.z();
Expand All @@ -65,7 +69,7 @@ inline geometry_msgs::Vector3 transformVector(const Eigen::Isometry3d& transform
} // namespace

DynamicsSolver::DynamicsSolver(const robot_model::RobotModelConstPtr& robot_model, const std::string& group_name,
const geometry_msgs::Vector3& gravity_vector)
const geometry_msgs::msg::Vector3& gravity_vector)
{
robot_model_ = robot_model;
joint_model_group_ = robot_model_->getJointModelGroup(group_name);
Expand All @@ -74,15 +78,15 @@ DynamicsSolver::DynamicsSolver(const robot_model::RobotModelConstPtr& robot_mode

if (!joint_model_group_->isChain())
{
ROS_ERROR_NAMED("dynamics_solver", "Group '%s' is not a chain. Will not initialize dynamics solver",
RCLCPP_ERROR(LOGGER, "Group '%s' is not a chain. Will not initialize dynamics solver",
group_name.c_str());
joint_model_group_ = nullptr;
return;
}

if (!joint_model_group_->getMimicJointModels().empty())
{
ROS_ERROR_NAMED("dynamics_solver", "Group '%s' has a mimic joint. Will not initialize dynamics solver",
RCLCPP_ERROR(LOGGER, "Group '%s' has a mimic joint. Will not initialize dynamics solver",
group_name.c_str());
joint_model_group_ = nullptr;
return;
Expand All @@ -91,29 +95,29 @@ DynamicsSolver::DynamicsSolver(const robot_model::RobotModelConstPtr& robot_mode
const robot_model::JointModel* joint = joint_model_group_->getJointRoots()[0];
if (!joint->getParentLinkModel())
{
ROS_ERROR_NAMED("dynamics_solver", "Group '%s' does not have a parent link", group_name.c_str());
RCLCPP_ERROR(LOGGER, "Group '%s' does not have a parent link", group_name.c_str());
joint_model_group_ = nullptr;
return;
}

base_name_ = joint->getParentLinkModel()->getName();

tip_name_ = joint_model_group_->getLinkModelNames().back();
ROS_DEBUG_NAMED("dynamics_solver", "Base name: '%s', Tip name: '%s'", base_name_.c_str(), tip_name_.c_str());
RCLCPP_DEBUG(LOGGER, "Base name: '%s', Tip name: '%s'", base_name_.c_str(), tip_name_.c_str());

const urdf::ModelInterfaceSharedPtr urdf_model = robot_model_->getURDF();
const srdf::ModelConstSharedPtr srdf_model = robot_model_->getSRDF();
KDL::Tree tree;

if (!kdl_parser::treeFromUrdfModel(*urdf_model, tree))
{
ROS_ERROR_NAMED("dynamics_solver", "Could not initialize tree object");
RCLCPP_ERROR(LOGGER, "Could not initialize tree object");
joint_model_group_ = nullptr;
return;
}
if (!tree.getChain(base_name_, tip_name_, kdl_chain_))
{
ROS_ERROR_NAMED("dynamics_solver", "Could not initialize chain object");
RCLCPP_ERROR(LOGGER, "Could not initialize chain object");
joint_model_group_ = nullptr;
return;
}
Expand All @@ -136,44 +140,44 @@ DynamicsSolver::DynamicsSolver(const robot_model::RobotModelConstPtr& robot_mode
KDL::Vector gravity(gravity_vector.x, gravity_vector.y,
gravity_vector.z); // \todo Not sure if KDL expects the negative of this (Sachin)
gravity_ = gravity.Norm();
ROS_DEBUG_NAMED("dynamics_solver", "Gravity norm set to %f", gravity_);
RCLCPP_DEBUG(LOGGER, "Gravity norm set to %f", gravity_);

chain_id_solver_.reset(new KDL::ChainIdSolver_RNE(kdl_chain_, gravity));
}

bool DynamicsSolver::getTorques(const std::vector<double>& joint_angles, const std::vector<double>& joint_velocities,
const std::vector<double>& joint_accelerations,
const std::vector<geometry_msgs::Wrench>& wrenches, std::vector<double>& torques) const
const std::vector<geometry_msgs::msg::Wrench>& wrenches, std::vector<double>& torques) const
{
if (!joint_model_group_)
{
ROS_DEBUG_NAMED("dynamics_solver", "Did not construct DynamicsSolver object properly. "
RCLCPP_DEBUG(LOGGER, "Did not construct DynamicsSolver object properly. "
"Check error logs.");
return false;
}
if (joint_angles.size() != num_joints_)
{
ROS_ERROR_NAMED("dynamics_solver", "Joint angles vector should be size %d", num_joints_);
RCLCPP_ERROR(LOGGER, "Joint angles vector should be size %d", num_joints_);
return false;
}
if (joint_velocities.size() != num_joints_)
{
ROS_ERROR_NAMED("dynamics_solver", "Joint velocities vector should be size %d", num_joints_);
RCLCPP_ERROR(LOGGER, "Joint velocities vector should be size %d", num_joints_);
return false;
}
if (joint_accelerations.size() != num_joints_)
{
ROS_ERROR_NAMED("dynamics_solver", "Joint accelerations vector should be size %d", num_joints_);
RCLCPP_ERROR(LOGGER, "Joint accelerations vector should be size %d", num_joints_);
return false;
}
if (wrenches.size() != num_segments_)
{
ROS_ERROR_NAMED("dynamics_solver", "Wrenches vector should be size %d", num_segments_);
RCLCPP_ERROR(LOGGER, "Wrenches vector should be size %d", num_segments_);
return false;
}
if (torques.size() != num_joints_)
{
ROS_ERROR_NAMED("dynamics_solver", "Torques vector should be size %d", num_joints_);
RCLCPP_ERROR(LOGGER, "Torques vector should be size %d", num_joints_);
return false;
}

Expand Down Expand Up @@ -201,7 +205,7 @@ bool DynamicsSolver::getTorques(const std::vector<double>& joint_angles, const s

if (chain_id_solver_->CartToJnt(kdl_angles, kdl_velocities, kdl_accelerations, kdl_wrenches, kdl_torques) < 0)
{
ROS_ERROR_NAMED("dynamics_solver", "Something went wrong computing torques");
RCLCPP_ERROR(LOGGER, "Something went wrong computing torques");
return false;
}

Expand All @@ -216,19 +220,19 @@ bool DynamicsSolver::getMaxPayload(const std::vector<double>& joint_angles, doub
{
if (!joint_model_group_)
{
ROS_DEBUG_NAMED("dynamics_solver", "Did not construct DynamicsSolver object properly. "
RCLCPP_DEBUG(LOGGER, "Did not construct DynamicsSolver object properly. "
"Check error logs.");
return false;
}
if (joint_angles.size() != num_joints_)
{
ROS_ERROR_NAMED("dynamics_solver", "Joint angles vector should be size %d", num_joints_);
RCLCPP_ERROR(LOGGER, "Joint angles vector should be size %d", num_joints_);
return false;
}
std::vector<double> joint_velocities(num_joints_, 0.0), joint_accelerations(num_joints_, 0.0);
std::vector<double> torques(num_joints_, 0.0), zero_torques(num_joints_, 0.0);

std::vector<geometry_msgs::Wrench> wrenches(num_segments_);
std::vector<geometry_msgs::msg::Wrench> wrenches(num_segments_);
if (!getTorques(joint_angles, joint_velocities, joint_accelerations, wrenches, zero_torques))
return false;

Expand All @@ -250,7 +254,7 @@ bool DynamicsSolver::getMaxPayload(const std::vector<double>& joint_angles, doub
wrenches.back().force = transformVector(transform, wrenches.back().force);
wrenches.back().torque = transformVector(transform, wrenches.back().torque);

ROS_DEBUG_NAMED("dynamics_solver", "New wrench (local frame): %f %f %f", wrenches.back().force.x,
RCLCPP_DEBUG(LOGGER, "New wrench (local frame): %f %f %f", wrenches.back().force.x,
wrenches.back().force.y, wrenches.back().force.z);

if (!getTorques(joint_angles, joint_velocities, joint_accelerations, wrenches, torques))
Expand All @@ -262,17 +266,17 @@ bool DynamicsSolver::getMaxPayload(const std::vector<double>& joint_angles, doub
double payload_joint = std::max<double>((max_torques_[i] - zero_torques[i]) / (torques[i] - zero_torques[i]),
(-max_torques_[i] - zero_torques[i]) /
(torques[i] - zero_torques[i])); // because we set the payload to 1.0
ROS_DEBUG_NAMED("dynamics_solver", "Joint: %d, Actual Torque: %f, Max Allowed: %f, Gravity: %f", i, torques[i],
RCLCPP_DEBUG(LOGGER, "Joint: %d, Actual Torque: %f, Max Allowed: %f, Gravity: %f", i, torques[i],
max_torques_[i], zero_torques[i]);
ROS_DEBUG_NAMED("dynamics_solver", "Joint: %d, Payload Allowed (N): %f", i, payload_joint);
RCLCPP_DEBUG(LOGGER, "Joint: %d, Payload Allowed (N): %f", i, payload_joint);
if (payload_joint < min_payload)
{
min_payload = payload_joint;
joint_saturated = i;
}
}
payload = min_payload / gravity_;
ROS_DEBUG_NAMED("dynamics_solver", "Max payload (kg): %f", payload);
RCLCPP_DEBUG(LOGGER, "Max payload (kg): %f", payload);
return true;
}

Expand All @@ -281,22 +285,22 @@ bool DynamicsSolver::getPayloadTorques(const std::vector<double>& joint_angles,
{
if (!joint_model_group_)
{
ROS_DEBUG_NAMED("dynamics_solver", "Did not construct DynamicsSolver object properly. "
RCLCPP_DEBUG(LOGGER, "Did not construct DynamicsSolver object properly. "
"Check error logs.");
return false;
}
if (joint_angles.size() != num_joints_)
{
ROS_ERROR_NAMED("dynamics_solver", "Joint angles vector should be size %d", num_joints_);
RCLCPP_ERROR(LOGGER, "Joint angles vector should be size %d", num_joints_);
return false;
}
if (joint_torques.size() != num_joints_)
{
ROS_ERROR_NAMED("dynamics_solver", "Joint torques vector should be size %d", num_joints_);
RCLCPP_ERROR(LOGGER, "Joint torques vector should be size %d", num_joints_);
return false;
}
std::vector<double> joint_velocities(num_joints_, 0.0), joint_accelerations(num_joints_, 0.0);
std::vector<geometry_msgs::Wrench> wrenches(num_segments_);
std::vector<geometry_msgs::msg::Wrench> wrenches(num_segments_);
state_->setJointGroupPositions(joint_model_group_, joint_angles);

const Eigen::Isometry3d& base_frame = state_->getFrameTransform(base_name_);
Expand All @@ -306,7 +310,7 @@ bool DynamicsSolver::getPayloadTorques(const std::vector<double>& joint_angles,
wrenches.back().force = transformVector(transform, wrenches.back().force);
wrenches.back().torque = transformVector(transform, wrenches.back().torque);

ROS_DEBUG_NAMED("dynamics_solver", "New wrench (local frame): %f %f %f", wrenches.back().force.x,
RCLCPP_DEBUG(LOGGER, "New wrench (local frame): %f %f %f", wrenches.back().force.x,
wrenches.back().force.y, wrenches.back().force.z);

return getTorques(joint_angles, joint_velocities, joint_accelerations, wrenches, joint_torques);
Expand All @@ -317,4 +321,4 @@ const std::vector<double>& DynamicsSolver::getMaxTorques() const
return max_torques_;
}

} // end of namespace dynamics_solver
} // end of namespace dynamics_solver