Robowflex  v0.1
Making MoveIt Easy
fetch.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #include <cmath>
4 
6 #include <robowflex_library/io.h>
8 
9 using namespace robowflex;
10 
11 const std::string FetchRobot::DEFAULT_URDF{"package://fetch_description/robots/fetch.urdf"};
12 const std::string FetchRobot::DEFAULT_SRDF{"package://fetch_moveit_config/config/fetch.srdf"};
13 const std::string FetchRobot::DEFAULT_LIMITS{"package://fetch_moveit_config/config/joint_limits.yaml"};
14 const std::string FetchRobot::DEFAULT_KINEMATICS{"package://fetch_moveit_config/config/kinematics.yaml"};
15 const std::string //
16  OMPL::FetchOMPLPipelinePlanner::DEFAULT_CONFIG{"package://fetch_moveit_config/config/ompl_planning.yaml"};
17 
18 const std::string FetchRobot::RESOURCE_URDF{"package://robowflex_resources/fetch/robots/fetch.urdf"};
19 const std::string FetchRobot::RESOURCE_SRDF{"package://robowflex_resources/fetch/config/fetch.srdf"};
20 const std::string FetchRobot::RESOURCE_LIMITS{"package://robowflex_resources/fetch/config/joint_limits.yaml"};
21 const std::string //
22  FetchRobot::RESOURCE_LIMITS_LOW{"package://robowflex_resources/fetch/config/joint_limits_low.yaml"};
23 const std::string //
24  FetchRobot::RESOURCE_KINEMATICS{"package://robowflex_resources/fetch/config/kinematics.yaml"};
25 const std::string //
26  OMPL::FetchOMPLPipelinePlanner::RESOURCE_CONFIG{"package://robowflex_resources/fetch/config/"
27  "ompl_planning.yaml"};
28 
30 {
31 }
32 
33 bool FetchRobot::initialize(bool addVirtual, bool use_low_limits)
34 {
35  if (addVirtual)
37 
38  setURDFPostProcessFunction([this](tinyxml2::XMLDocument &doc) { return addCastersURDF(doc); });
39 
40  bool success = false;
41  // First attempt the `robowflex_resources` package, then attempt the "actual" resource files.
43  {
44  RBX_INFO("Initializing Fetch with `fetch_{description, moveit_config}`");
46  }
47  else
48  {
49  RBX_INFO("Initializing Fetch with `robowflex_resources`");
50  if (use_low_limits)
51  success =
53  else
55  }
56 
57  loadKinematics("arm");
58  loadKinematics("arm_with_torso");
59 
61 
62  return success;
63 }
64 
65 bool FetchRobot::addCastersURDF(tinyxml2::XMLDocument &doc)
66 {
67  for (const auto &name : {"bl_caster", "br_caster", "fl_caster", "fr_caster"})
68  {
69  auto link_name = std::string(name) + "_link";
70  if (not isLinkURDF(doc, link_name))
71  {
72  tinyxml2::XMLElement *caster_link = doc.NewElement("link");
73  caster_link->SetAttribute("name", link_name.c_str());
74  doc.FirstChildElement("robot")->InsertFirstChild(caster_link);
75 
76  auto joint_name = std::string(name) + "_joint";
77  tinyxml2::XMLElement *caster_joint = doc.NewElement("joint");
78  caster_joint->SetAttribute("name", joint_name.c_str());
79  caster_joint->SetAttribute("type", "fixed");
80 
81  tinyxml2::XMLElement *parent = doc.NewElement("parent");
82  parent->SetAttribute("link", "base_link");
83  caster_joint->InsertFirstChild(parent);
84 
85  tinyxml2::XMLElement *child = doc.NewElement("child");
86  child->SetAttribute("link", link_name.c_str());
87  caster_joint->InsertFirstChild(child);
88 
89  doc.FirstChildElement("robot")->InsertFirstChild(caster_joint);
90  }
91  }
92 
93  return true;
94 }
95 
96 void FetchRobot::pointHead(const Eigen::Vector3d &point)
97 {
98  const RobotPose point_pose = RobotPose(Eigen::Translation3d(point));
99  const RobotPose point_pan = getLinkTF("head_pan_link").inverse() * point_pose;
100  const RobotPose point_tilt = getLinkTF("head_tilt_link").inverse() * point_pose;
101 
102  const double pan = atan2(point_pan.translation().y(), point_pan.translation().x());
103  const double tilt = -atan2(point_tilt.translation().z(),
104  hypot(point_tilt.translation().x(), point_tilt.translation().y()));
105 
106  const std::map<std::string, double> angles = {{"head_pan_joint", pan}, {"head_tilt_joint", tilt}};
107 
108  Robot::setState(angles);
109 }
110 
112 {
113  const std::map<std::string, double> angles = {{"l_gripper_finger_joint", 0.04},
114  {"r_gripper_finger_joint", 0.04}};
115 
116  Robot::setState(angles);
117 }
118 
120 {
121  const std::map<std::string, double> angles = {{"l_gripper_finger_joint", 0.0},
122  {"r_gripper_finger_joint", 0.0}};
123 
124  Robot::setState(angles);
125 }
126 
127 void FetchRobot::setBasePose(double x, double y, double theta)
128 {
129  if (hasJoint("base_joint/x") && hasJoint("base_joint/y") && hasJoint("base_joint/theta"))
130  {
131  const std::map<std::string, double> pose = {
132  {"base_joint/x", x}, {"base_joint/y", y}, {"base_joint/theta", theta}};
133 
134  scratch_->setVariablePositions(pose);
135  scratch_->update();
136  }
137  else
138  RBX_WARN("base_joint does not exist, cannot move base! You need to set addVirtual to true");
139 }
140 
142  : OMPLPipelinePlanner(robot, name)
143 {
144 }
145 
147  const std::vector<std::string> &adapters)
148 {
149  if (IO::resolvePackage(RESOURCE_CONFIG).empty())
150  return OMPLPipelinePlanner::initialize(DEFAULT_CONFIG, settings, DEFAULT_PLUGIN, adapters);
151  return OMPLPipelinePlanner::initialize(RESOURCE_CONFIG, settings, DEFAULT_PLUGIN, adapters);
152 }
static const std::string RESOURCE_SRDF
SRDF from robowflex_resources.
Definition: fetch.h:73
static const std::string DEFAULT_URDF
Default URDF.
Definition: fetch.h:67
static const std::string DEFAULT_SRDF
Default SRDF.
Definition: fetch.h:68
FetchRobot()
Constructor.
Definition: fetch.cpp:29
void openGripper()
Opens the Fetch's gripper.
Definition: fetch.cpp:111
static const std::string RESOURCE_KINEMATICS
kinematics from robowflex_resources
Definition: fetch.h:76
static const std::string RESOURCE_URDF
URDF from robowflex_resources.
Definition: fetch.h:72
static const std::string RESOURCE_LIMITS_LOW
Lower limits from robowflex_resources.
Definition: fetch.h:75
static const std::string RESOURCE_LIMITS
Limits from robowflex_resources.
Definition: fetch.h:74
bool initialize(bool addVirtual=true, bool use_low_limits=false)
Initialize the robot with arm and arm_with_torso kinematics.
Definition: fetch.cpp:33
void closeGripper()
Closes the Fetch's gripper.
Definition: fetch.cpp:119
static const std::string DEFAULT_KINEMATICS
Default kinematics.
Definition: fetch.h:70
void setBasePose(double x, double y, double theta)
Sets the base pose of the Fetch robot (a virtual planar joint)
Definition: fetch.cpp:127
bool addCastersURDF(tinyxml2::XMLDocument &doc)
Inserts the caster links if they don't exist.
Definition: fetch.cpp:65
void pointHead(const Eigen::Vector3d &point)
Points the Fetch's head to a point in the world frame.
Definition: fetch.cpp:96
static const std::string DEFAULT_LIMITS
Default Limits.
Definition: fetch.h:69
static const std::string RESOURCE_CONFIG
Planning configuration from robowflex_resources.
Definition: fetch.h:112
bool initialize(const Settings &settings=Settings(), const std::vector< std::string > &adapters=DEFAULT_ADAPTERS)
Initialize the planning context. All parameter provided are defaults.
Definition: fetch.cpp:146
static const std::string DEFAULT_CONFIG
Default planning configuration.
Definition: fetch.h:111
FetchOMPLPipelinePlanner(const RobotPtr &robot, const std::string &name="")
Constructor.
Definition: fetch.cpp:141
A robowflex::PipelinePlanner that uses the MoveIt! default OMPL planning pipeline.
bool initialize(const std::string &config_file="", const Settings &settings=Settings(), const std::string &plugin=DEFAULT_PLUGIN, const std::vector< std::string > &adapters=DEFAULT_ADAPTERS)
Initialize planning pipeline. Loads OMPL planning plugin plugin with the planning adapters adapters....
Settings descriptor for settings provided by the default MoveIt! OMPL planning pipeline.
A shared pointer wrapper for robowflex::Robot.
Loads information about a robot and maintains information about a robot's state.
const RobotPose & getLinkTF(const std::string &name) const
Get the current pose of a link on the scratch state.
void setURDFPostProcessFunction(const PostProcessXMLFunction &function)
Sets a post processing function for loading the URDF.
bool isLinkURDF(tinyxml2::XMLDocument &doc, const std::string &name)
Checks if a node link exist with named name_link.
bool initialize(const std::string &urdf_file)
Initializes a robot from a kinematic description. A default semantic description is used.
void setSRDFPostProcessAddPlanarJoint(const std::string &name)
Adds a planar virtual joint through the SRDF to the loaded robot with name name. This joint will have...
bool loadKinematics(const std::string &group, bool load_subgroups=true)
Loads the kinematics plugin for a joint group and its subgroups. No kinematics are loaded by default.
robot_state::RobotStatePtr scratch_
Scratch robot state.
void setState(const std::vector< double > &positions)
Sets the scratch state from a vector of joint positions (all must be specified)
bool hasJoint(const std::string &joint) const
Checks if a joint exists in the robot.
T empty(T... args)
#define RBX_WARN(fmt,...)
Output a warning logging message.
Definition: log.h:110
#define RBX_INFO(fmt,...)
Output a info logging message.
Definition: log.h:118
Functions for loading and animating robots in Blender.
std::string resolvePackage(const std::string &path)
Resolves package:// URLs to their canonical form. The path does not need to exist,...
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Definition: adapter.h:24