Robowflex  v0.1
Making MoveIt Easy
robowflex::Robot Class Reference

Loads information about a robot and maintains information about a robot's state. More...

#include <robot.h>

+ Inheritance diagram for robowflex::Robot:

Classes

struct  IKQuery
 Robot IK Query options. IK queries in Robowflex consist of: a) A position specified by some geometric region (a robowflex::Geometry) at a pose. b) An orientation specified by some base orientation with allowable deviations specified by tolerances on the XYZ Euler axes. It is recommended to use the provided constructors to specify a query, or to use the addRequest() function. Multiple target tips can be specified, but note that not all kinematics solvers support multi-tip IK. Additionally, a robowflex::Scene can be specified to do collision-aware IK. More...
 

Public Types

typedef std::function< bool(YAML::Node &)> PostProcessYAMLFunction
 A function that runs after loading a YAML file and can modify its contents. Returns true on success, false on failure. More...
 
typedef std::function< bool(tinyxml2::XMLDocument &)> PostProcessXMLFunction
 A function that runs after loading a XML file and can modify its contents. Returns true on success, false on failure. More...
 

Public Member Functions

 Robot (const std::string &name)
 Constructor. More...
 
 Robot (Robot const &)=delete
 
void operator= (Robot const &)=delete
 
Initialization and Loading
bool initialize (const std::string &urdf_file)
 Initializes a robot from a kinematic description. A default semantic description is used. More...
 
bool initializeKinematics (const std::string &kinematics_file)
 Initialize a robot with a kinematics description. More...
 
bool initialize (const std::string &urdf_file, const std::string &srdf_file, const std::string &limits_file="", const std::string &kinematics_file="")
 Initializes a robot from a kinematic and semantic description. All files are loaded under the robot's namespace. More...
 
bool initializeFromYAML (const std::string &config_file)
 Initializes a robot from a YAML config which includes URDF (urdf) and optional the SRDF (srdf), joint limits (joint_limits), IK plugins (kinematics) and a default state (robot_state). All files are loaded under the robot's namespace. The names of the YAML keys are in parenthesis. More...
 
bool loadYAMLFile (const std::string &name, const std::string &file)
 Loads a YAML file into the robot's namespace under name. More...
 
bool loadYAMLFile (const std::string &name, const std::string &file, const PostProcessYAMLFunction &function)
 Loads a YAML file into the robot's namespace under name, with a post-process function. More...
 
std::string loadXMLFile (const std::string &file)
 Loads an XML or .xacro file into a string. More...
 
void setURDFPostProcessFunction (const PostProcessXMLFunction &function)
 Sets a post processing function for loading the URDF. More...
 
bool isLinkURDF (tinyxml2::XMLDocument &doc, const std::string &name)
 Checks if a node link exist with named name_link. More...
 
void setSRDFPostProcessFunction (const PostProcessXMLFunction &function)
 Sets a post processing function for loading the SRDF. More...
 
void setLimitsPostProcessFunction (const PostProcessYAMLFunction &function)
 Sets a post processing function for loading the joint limits file. More...
 
void setKinematicsPostProcessFunction (const PostProcessYAMLFunction &function)
 Sets a post processing function for loading the kinematics plugin file. More...
 
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 three degrees of freedom: <name>/x, <name>/y, and <name>/theta. Will apply this joint between the world and the root frame. More...
 
void setSRDFPostProcessAddFloatingJoint (const std::string &name)
 Adds a planar virtual joint through the SRDF to the loaded robot with name name. This joint will have three degrees of freedom: <name>/x, <name>/y, and <name>/theta. Will apply this joint between the world and the root frame. More...
 
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. More...
 
Getters and Setters
const std::stringgetModelName () const
 Get the robot's model name. More...
 
const std::stringgetName () const
 Get the robot's name. More...
 
const robot_model::RobotModelPtr & getModelConst () const
 Get a const reference to the loaded robot model. More...
 
robot_model::RobotModelPtr & getModel ()
 Get a reference to the loaded robot model. More...
 
urdf::ModelInterfaceConstSharedPtr getURDF () const
 Get the raw URDF Model. More...
 
const std::stringgetURDFString () const
 Get the raw URDF Model as a string. More...
 
srdf::ModelConstSharedPtr getSRDF () const
 Get the raw SRDF Model. More...
 
const std::stringgetSRDFString () const
 Get the raw SRDF Model as a string. More...
 
const IO::HandlergetHandlerConst () const
 Get the underlying IO handler used for this robot. More...
 
IO::HandlergetHandler ()
 Get the underlying IO handler used for this robot. More...
 
Robot State Operations
const robot_model::RobotStatePtr & getScratchStateConst () const
 Get a const reference to the scratch robot state. More...
 
robot_model::RobotStatePtr & getScratchState ()
 Get a reference to the scratch robot state. More...
 
robot_model::RobotStatePtr cloneScratchState () const
 Allocate a new robot state that is a clone of the current scratch state. More...
 
robot_model::RobotStatePtr allocState () const
 Allocate a new robot state. More...
 
void setState (const std::vector< double > &positions)
 Sets the scratch state from a vector of joint positions (all must be specified) More...
 
void setState (const std::map< std::string, double > &variable_map)
 Sets the scratch state from a map of joint name to position. More...
 
void setState (const std::vector< std::string > &variable_names, const std::vector< double > &variable_position)
 Sets the scratch state from a vector of joint names and their positions. More...
 
void setState (const sensor_msgs::JointState &state)
 Sets the scratch state from a joint state message. More...
 
void setState (const moveit_msgs::RobotState &state)
 Sets the scratch state from a robot state message. More...
 
void setStateFromYAMLFile (const std::string &file)
 Sets the scratch state from a robot state message saved to a YAML file. More...
 
void setGroupState (const std::string &name, const std::vector< double > &positions)
 Sets the group of the scratch state to a vector of joint positions. More...
 
std::vector< double > getState () const
 Gets the current joint positions of the scratch state. More...
 
moveit_msgs::RobotState getStateMsg () const
 Get the current scratch state as a message. More...
 
void setStateMsgGroupState (moveit_msgs::RobotState &state, const std::string &group, const std::vector< double > &positions) const
 Set the group state of a MoveIt RobotState message. More...
 
std::vector< std::stringgetJointNames () const
 Gets the names of joints of the robot. More...
 
bool hasJoint (const std::string &joint) const
 Checks if a joint exists in the robot. More...
 
const RobotPosegetLinkTF (const std::string &name) const
 Get the current pose of a link on the scratch state. More...
 
const RobotPose getRelativeLinkTF (const std::string &base, const std::string &target) const
 Get the current pose of a link target in the frame of base. More...
 
Inverse Kinematics
bool setFromIK (const IKQuery &query)
 Sets a group of the scratch state from an IK query. If the IK query fails the scratch state retains its initial value. More...
 
bool setFromIK (const IKQuery &query, robot_state::RobotState &state) const
 Sets a robot state from an IK query. If the IK query fails the scratch state retains its initial value. More...
 
bool validateIKQuery (const IKQuery &query, const robot_state::RobotState &state) const
 Validates that a state satisfies an IK query's request poses. More...
 
double distanceToIKQuery (const IKQuery &query, const robot_state::RobotState &state) const
 Returns the distance of the state to satisfying the IK query. More...
 
std::vector< std::stringgetSolverTipFrames (const std::string &group) const
 Get the tip frames for the IK solver for a given joint model group group. More...
 
std::string getSolverBaseFrame (const std::string &group) const
 Get the base frame for the IK solver given a joint model group group. More...
 
IO
bool toYAMLFile (const std::string &file) const
 Dumps the current configuration of the robot as a YAML file. More...
 
bool dumpGeometry (const std::string &file) const
 Dumps the names of links and absolute paths to their visual mesh files to a YAML file. More...
 
bool dumpTransforms (const std::string &filename) const
 Dumps the tranforms of all links of a robot at its current state to a file. More...
 
bool dumpPathTransforms (const robot_trajectory::RobotTrajectory &path, const std::string &filename, double fps=30, double threshold=0.0) const
 Dumps the tranforms of all links of a robot through a robot trajectory to a file. More...
 
bool dumpToScene (const std::string &filename) const
 Dumps the current scratch configuration of the robot to a YAML file compatible with a scene. More...
 

Static Public Attributes

static const std::string ROBOT_DESCRIPTION = "robot_description"
 Default robot description name. More...
 
static const std::string ROBOT_SEMANTIC = "_semantic"
 Default robot semantic description suffix. More...
 
static const std::string ROBOT_PLANNING = "_planning"
 Default robot planning description suffix. More...
 
static const std::string ROBOT_KINEMATICS = "_kinematics"
 Default robot kinematics description suffix. More...
 

Protected Member Functions

Protected Initialization
bool loadURDFFile (const std::string &urdf_file)
 Loads the URDF file. More...
 
bool loadSRDFFile (const std::string &srdf_file)
 Loads the SRDF file. More...
 
void initializeInternal (bool namespaced=true)
 Initializes and loads the robot. Calls post-processing functions and creates scratch state. More...
 
void loadRobotModel (const std::string &description)
 Loads a robot model from the loaded information on the parameter server. More...
 
void updateXMLString (std::string &string, const PostProcessXMLFunction &function)
 Updates a loaded XML string based on an XML post-process function. Called after initial, unmodified robot is loaded. More...
 

Protected Attributes

const std::string name_
 Robot name. More...
 
IO::Handler handler_
 IO handler (namespaced with name_) More...
 
std::string urdf_
 The URDF as a string. More...
 
std::string srdf_
 The SRDF as a string. More...
 
PostProcessXMLFunction urdf_function_
 URDF post-processing function. More...
 
PostProcessXMLFunction srdf_function_
 SRDF post-processing function. More...
 
PostProcessYAMLFunction limits_function_
 Limits YAML post-processing function. More...
 
PostProcessYAMLFunction kinematics_function_
 Kinematics plugin YAML post-processing function. More...
 
std::shared_ptr< robot_model_loader::RobotModelLoader > loader_
 Robot model loader. More...
 
robot_model::RobotModelPtr model_
 Loaded robot model. More...
 
std::map< std::string, robot_model::SolverAllocatorFnimap_
 Kinematic solver allocator map. More...
 
kinematics_plugin_loader::KinematicsPluginLoaderPtr kinematics_
 Kinematic plugin loader. More...
 
robot_state::RobotStatePtr scratch_
 Scratch robot state. More...
 

Detailed Description

Loads information about a robot and maintains information about a robot's state.

The Robot class is a wrapper around MoveIt!'s robot_model::RobotModel and a "scratch" robot_state::RobotState. Mostly, this class handles loading the relevant information to the parameter server without the use of a launch file. The necessary files are the URDF and SRDF (both either XML or xacro files), and the joint limits and kinematics plugin (both YAML files). These pieces of information are loaded to the parameter server under the name provided to the constructor so that multiple robots can be used. Note that all information is also loaded under the unique namespace generated by the IO::Handler.

By default, no IK kinematics plugins are loaded due to startup costs (particularly for R2). You must call loadKinematics() with the desired planning groups (as defined by the SRDF) if you want IK.

Additionally, post-processing function hooks are provided if modifications need to be done to any of the necessary files. This enables the addition of extra joints, semantic information, joint limit overloading, etc. as needed. These functions are called after the robot is initially loaded, so it is possible to access robot model information in these functions. For example, see setSRDFPostProcessAddPlanarJoint().

Definition at line 68 of file robowflex_library/include/robowflex_library/robot.h.

Member Typedef Documentation

◆ PostProcessXMLFunction

typedef std::function<bool(tinyxml2::XMLDocument &)> robowflex::Robot::PostProcessXMLFunction

A function that runs after loading a XML file and can modify its contents. Returns true on success, false on failure.

Definition at line 79 of file robowflex_library/include/robowflex_library/robot.h.

◆ PostProcessYAMLFunction

A function that runs after loading a YAML file and can modify its contents. Returns true on success, false on failure.

Definition at line 74 of file robowflex_library/include/robowflex_library/robot.h.

Constructor & Destructor Documentation

◆ Robot() [1/2]

Robot::Robot ( const std::string name)

Constructor.

Parameters
[in]nameThe name of the robot. Used to namespace information under.

Definition at line 26 of file robowflex_library/src/robot.cpp.

26  : name_(name), handler_(name_)
27 {
28 }
IO::Handler handler_
IO handler (namespaced with name_)

◆ Robot() [2/2]

robowflex::Robot::Robot ( Robot const &  )
delete

Member Function Documentation

◆ allocState()

robot_model::RobotStatePtr Robot::allocState ( ) const

Allocate a new robot state.

Returns
The new robot state.

Definition at line 996 of file robowflex_library/src/robot.cpp.

997 {
998  // No make_shared() for indigo compatibility
999  robot_state::RobotStatePtr state;
1000  state.reset(new robot_state::RobotState(getModelConst()));
1001  state->setToDefaultValues();
1002 
1003  return state;
1004 }
const robot_model::RobotModelPtr & getModelConst() const
Get a const reference to the loaded robot model.

◆ cloneScratchState()

robot_model::RobotStatePtr Robot::cloneScratchState ( ) const

Allocate a new robot state that is a clone of the current scratch state.

Returns
The new robot state.

Definition at line 502 of file robowflex_library/src/robot.cpp.

503 {
504  auto state = allocState();
505  *state = *scratch_;
506 
507  return state;
508 }
robot_state::RobotStatePtr scratch_
Scratch robot state.
robot_model::RobotStatePtr allocState() const
Allocate a new robot state.

◆ distanceToIKQuery()

double Robot::distanceToIKQuery ( const IKQuery query,
const robot_state::RobotState &  state 
) const

Returns the distance of the state to satisfying the IK query.

Parameters
[in]queryThe query to check.
[in]stateThe state to check against the query.
Returns
The distance of the state to satisfaction of the query.

Definition at line 967 of file robowflex_library/src/robot.cpp.

968 {
969  const auto &constraints = query.getAsConstraints(*this);
970  const auto &result = constraints->decide(state, query.verbose);
971  return result.distance;
972 }

◆ dumpGeometry()

bool Robot::dumpGeometry ( const std::string file) const

Dumps the names of links and absolute paths to their visual mesh files to a YAML file.

Parameters
[in]fileFile to save to.The name of the link to find the transform of.
Returns
True on success, false on failure.

Definition at line 1163 of file robowflex_library/src/robot.cpp.

1164 {
1165  YAML::Node link_geometry;
1166  const auto &urdf = model_->getURDF();
1167 
1169  urdf->getLinks(links);
1170 
1171  for (const auto &link : links)
1172  {
1173  YAML::Node node;
1174 
1175  YAML::Node visual;
1176 #if ROBOWFLEX_AT_LEAST_KINETIC
1177  for (const auto &element : link->visual_array)
1178  if (element)
1179  visual["elements"].push_back(addLinkVisual(element));
1180 #else
1181  if (link->visual)
1182  visual["elements"].push_back(addLinkVisual(link->visual));
1183 #endif
1184 
1185  YAML::Node collision;
1186 #if ROBOWFLEX_AT_LEAST_KINETIC
1187  for (const auto &element : link->collision_array)
1188  if (element)
1189  collision["elements"].push_back(addLinkCollision(element));
1190 #else
1191  if (link->collision)
1192  collision["elements"].push_back(addLinkCollision(link->collision));
1193 #endif
1194 
1195  if (!visual.IsNull() || !collision.IsNull())
1196  {
1197  YAML::Node add;
1198  add["name"] = link->name;
1199 
1200  if (!visual.IsNull())
1201  add["visual"] = visual;
1202 
1203  if (!collision.IsNull())
1204  add["collision"] = collision;
1205 
1206  link_geometry.push_back(add);
1207  }
1208  }
1209 
1210  return IO::YAMLToFile(link_geometry, filename);
1211 }
robot_model::RobotModelPtr model_
Loaded robot model.
bool YAMLToFile(const YAML::Node &node, const std::string &file)
Write the contents of a YAML node out to a potentially new file.
T push_back(T... args)

◆ dumpPathTransforms()

bool Robot::dumpPathTransforms ( const robot_trajectory::RobotTrajectory path,
const std::string filename,
double  fps = 30,
double  threshold = 0.0 
) const

Dumps the tranforms of all links of a robot through a robot trajectory to a file.

Parameters
[in]pathPath to output.
[in]filenameFilename to output to.
[in]fpsThe transforms (frames) per second used to interpolate the given path.
[in]thresholdThe minimum distance between states before transforms are output.
Returns
True on success, false on failure.

Definition at line 1221 of file robowflex_library/src/robot.cpp.

1223 {
1224  YAML::Node node, values;
1225  const double rate = 1.0 / fps;
1226 
1227  // Find the total duration of the path.
1228  const std::deque<double> &durations = path.getWayPointDurations();
1229  double total_duration = std::accumulate(durations.begin(), durations.end(), 0.0);
1230 
1231  const auto &urdf = model_->getURDF();
1232 
1233  robot_state::RobotStatePtr previous, state(new robot_state::RobotState(model_));
1234 
1235  for (double duration = 0.0, delay = 0.0; duration <= total_duration; duration += rate, delay += rate)
1236  {
1237  YAML::Node point;
1238 
1239  path.getStateAtDurationFromStart(duration, state);
1240  if (previous && state->distance(*previous) < threshold)
1241  continue;
1242 
1243  state->update();
1244  for (const auto &link_name : model_->getLinkModelNames())
1245  {
1246  const auto &urdf_link = urdf->getLink(link_name);
1247  if (urdf_link->visual)
1248  {
1249  const auto &link = model_->getLinkModel(link_name);
1250  RobotPose tf =
1251  state->getGlobalLinkTransform(link); // * urdfPoseToEigen(urdf_link->visual->origin);
1252  point[link->getName()] = IO::toNode(TF::poseEigenToMsg(tf));
1253  }
1254  }
1255 
1256  YAML::Node value;
1257  value["point"] = point;
1258  value["duration"] = delay;
1259  values.push_back(value);
1260 
1261  delay = 0;
1262 
1263  if (!previous)
1264  previous.reset(new robot_state::RobotState(model_));
1265 
1266  *previous = *state;
1267  }
1268 
1269  node["transforms"] = values;
1270  node["fps"] = fps;
1271 
1272  return IO::YAMLToFile(node, filename);
1273 }
T accumulate(T... args)
T begin(T... args)
const std::deque< double > & getWayPointDurations() const
bool getStateAtDurationFromStart(const double request_duration, robot_state::RobotStatePtr &output_state) const
T end(T... args)
YAML::Node toNode(const geometry_msgs::Pose &msg)
Converts a pose message to YAML.
Definition: yaml.cpp:1874
geometry_msgs::Pose poseEigenToMsg(const RobotPose &pose)
Converts an RobotPose to a pose message.
Definition: tf.cpp:120
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

◆ dumpToScene()

bool Robot::dumpToScene ( const std::string filename) const

Dumps the current scratch configuration of the robot to a YAML file compatible with a scene.

Parameters
[in]filenameFilename to output to.
Returns
True on success, false on failure.

Definition at line 1275 of file robowflex_library/src/robot.cpp.

1276 {
1277  YAML::Node node;
1278 
1279  const auto &urdf = model_->getURDF();
1280  for (const auto &link_name : model_->getLinkModelNames())
1281  {
1282  const auto &urdf_link = urdf->getLink(link_name);
1283  if (urdf_link->visual)
1284  {
1285  const auto &link = model_->getLinkModel(link_name);
1286 
1287  std::vector<YAML::Node> visuals;
1288 #if ROBOWFLEX_AT_LEAST_KINETIC
1289  for (const auto &element : urdf_link->visual_array)
1290  if (element)
1291  visuals.emplace_back(addLinkVisual(element, false));
1292 #else
1293  if (urdf_link->visual)
1294  visuals.push_back(addLinkVisual(urdf_link->visual, false));
1295 #endif
1296 
1297  YAML::Node object;
1298  object["id"] = link->getName();
1299 
1300  YAML::Node meshes;
1301  YAML::Node mesh_poses;
1302  YAML::Node primitives;
1303  YAML::Node primitive_poses;
1304 
1305  RobotPose tf = scratch_->getGlobalLinkTransform(link);
1306  for (const auto &visual : visuals)
1307  {
1308  RobotPose pose = tf;
1309  if (IO::isNode(visual["origin"]))
1310  {
1311  RobotPose offset = TF::poseMsgToEigen(IO::poseFromNode(visual["origin"]));
1312  pose = pose * offset;
1313  }
1314 
1315  const auto &posey = IO::toNode(TF::poseEigenToMsg(pose));
1316 
1317  const auto &type = visual["type"].as<std::string>();
1318  if (type == "mesh")
1319  {
1320  YAML::Node mesh;
1321  mesh["resource"] = visual["resource"];
1322  if (IO::isNode(visual["dimensions"]))
1323  mesh["dimensions"] = visual["dimensions"];
1324  else
1325  {
1326  mesh["dimensions"].push_back(1.);
1327  mesh["dimensions"].push_back(1.);
1328  mesh["dimensions"].push_back(1.);
1329  }
1330 
1331  ROBOWFLEX_YAML_FLOW(mesh["dimensions"]);
1332 
1333  meshes.push_back(mesh);
1334  mesh_poses.push_back(posey);
1335  }
1336  else
1337  {
1338  YAML::Node primitive;
1339  primitive["type"] = type;
1340  primitive["dimensions"] = visual["dimensions"];
1341  primitive_poses.push_back(posey);
1342  }
1343  }
1344 
1345  if (meshes.size())
1346  {
1347  object["meshes"] = meshes;
1348  object["mesh_poses"] = mesh_poses;
1349  }
1350 
1351  if (primitives.size())
1352  {
1353  object["primitives"] = primitives;
1354  object["primitive_poses"] = primitive_poses;
1355  }
1356 
1357  node["collision_objects"].push_back(object);
1358  }
1359  }
1360 
1361  YAML::Node scene;
1362  scene["world"] = node;
1363 
1364  return IO::YAMLToFile(scene, filename);
1365 }
T emplace_back(T... args)
#define ROBOWFLEX_YAML_FLOW(n)
Definition: macros.h:54
Functions for constructing shapes in Blender.
bool isNode(const YAML::Node &node)
Checks if a key exists within a YAML node.
Definition: yaml.cpp:1847
geometry_msgs::Pose poseFromNode(const YAML::Node &node)
Converts a pose YAML to a goemetry message.
Definition: yaml.cpp:1881
RobotPose poseMsgToEigen(const geometry_msgs::Pose &msg)
Converts a pose message to RobotPose.
Definition: tf.cpp:114
Functions for loading and animating scenes in Blender.

◆ dumpTransforms()

bool Robot::dumpTransforms ( const std::string filename) const

Dumps the tranforms of all links of a robot at its current state to a file.

Parameters
[in]filenameFilename to output to.
Returns
True on success, false on failure.

Definition at line 1213 of file robowflex_library/src/robot.cpp.

1214 {
1217 
1218  return dumpPathTransforms(trajectory, filename, 0., 0.);
1219 }
void addSuffixWayPoint(const robot_state::RobotState &state, double dt)
bool dumpPathTransforms(const robot_trajectory::RobotTrajectory &path, const std::string &filename, double fps=30, double threshold=0.0) const
Dumps the tranforms of all links of a robot through a robot trajectory to a file.
robot_trajectory::RobotTrajectory trajectory(rmodel,"right_arm")

◆ getHandler()

IO::Handler & Robot::getHandler ( )

Get the underlying IO handler used for this robot.

Returns
A reference to the IO handler.

Definition at line 515 of file robowflex_library/src/robot.cpp.

516 {
517  return handler_;
518 }

◆ getHandlerConst()

const IO::Handler & Robot::getHandlerConst ( ) const

Get the underlying IO handler used for this robot.

Returns
A reference to the IO handler.

Definition at line 510 of file robowflex_library/src/robot.cpp.

511 {
512  return handler_;
513 }

◆ getJointNames()

std::vector< std::string > Robot::getJointNames ( ) const

Gets the names of joints of the robot.

Returns
A vector of joint names.

Definition at line 592 of file robowflex_library/src/robot.cpp.

593 {
594  return scratch_->getVariableNames();
595 }

◆ getLinkTF()

const RobotPose & Robot::getLinkTF ( const std::string name) const

Get the current pose of a link on the scratch state.

Parameters
[in]nameThe name of the link to find the transform of.
Returns
The transform of link name.

Definition at line 974 of file robowflex_library/src/robot.cpp.

975 {
976  return scratch_->getGlobalLinkTransform(name);
977 }

◆ getModel()

robot_model::RobotModelPtr & Robot::getModel ( )

Get a reference to the loaded robot model.

Returns
The robot model.

Definition at line 467 of file robowflex_library/src/robot.cpp.

468 {
469  return model_;
470 }

◆ getModelConst()

const robot_model::RobotModelPtr & Robot::getModelConst ( ) const

Get a const reference to the loaded robot model.

Returns
The robot model.

Definition at line 462 of file robowflex_library/src/robot.cpp.

463 {
464  return model_;
465 }

◆ getModelName()

const std::string & Robot::getModelName ( ) const

Get the robot's model name.

Returns
The robot's model name.

Definition at line 452 of file robowflex_library/src/robot.cpp.

453 {
454  return model_->getName();
455 }

◆ getName()

const std::string & Robot::getName ( ) const

Get the robot's name.

Returns
The robot's name.

Definition at line 457 of file robowflex_library/src/robot.cpp.

458 {
459  return name_;
460 }

◆ getRelativeLinkTF()

const RobotPose Robot::getRelativeLinkTF ( const std::string base,
const std::string target 
) const

Get the current pose of a link target in the frame of base.

Parameters
[in]baseThe link to use as the base frame.
[in]targetThe link to find the transform of.
Returns
The transform of link target in the frame of base.

Definition at line 979 of file robowflex_library/src/robot.cpp.

980 {
981  auto base_tf = scratch_->getGlobalLinkTransform(base);
982  auto target_tf = scratch_->getGlobalLinkTransform(target);
983 
984  return base_tf.inverse() * target_tf;
985 }

◆ getScratchState()

robot_model::RobotStatePtr & Robot::getScratchState ( )

Get a reference to the scratch robot state.

Returns
The scratch robot state.

Definition at line 497 of file robowflex_library/src/robot.cpp.

498 {
499  return scratch_;
500 }

◆ getScratchStateConst()

const robot_model::RobotStatePtr & Robot::getScratchStateConst ( ) const

Get a const reference to the scratch robot state.

Returns
The scratch robot state.

Definition at line 492 of file robowflex_library/src/robot.cpp.

493 {
494  return scratch_;
495 }

◆ getSolverBaseFrame()

std::string Robot::getSolverBaseFrame ( const std::string group) const

Get the base frame for the IK solver given a joint model group group.

Parameters
[in]groupThe group to get the base frame for.
Returns
The base frame. Will return an empty string on error.

Definition at line 1016 of file robowflex_library/src/robot.cpp.

1017 {
1018  const auto &jmg = model_->getJointModelGroup(group);
1019  const auto &solver = jmg->getSolverInstance();
1020  if (solver)
1021  return solver->getBaseFrame();
1022 
1023  return "";
1024 }

◆ getSolverTipFrames()

std::vector< std::string > Robot::getSolverTipFrames ( const std::string group) const

Get the tip frames for the IK solver for a given joint model group group.

Parameters
[in]groupThe group to get the tip frames for.
Returns
The list of tip frames. Will return an empty list on error.

Definition at line 1006 of file robowflex_library/src/robot.cpp.

1007 {
1008  const auto &jmg = model_->getJointModelGroup(group);
1009  const auto &solver = jmg->getSolverInstance();
1010  if (solver)
1011  return solver->getTipFrames();
1012 
1013  return {};
1014 }

◆ getSRDF()

srdf::ModelConstSharedPtr Robot::getSRDF ( ) const

Get the raw SRDF Model.

Returns
The SRDF model.

Definition at line 482 of file robowflex_library/src/robot.cpp.

483 {
484  return model_->getSRDF();
485 }

◆ getSRDFString()

const std::string & Robot::getSRDFString ( ) const

Get the raw SRDF Model as a string.

Returns
The SRDF model as a string.

Definition at line 487 of file robowflex_library/src/robot.cpp.

488 {
489  return srdf_;
490 }

◆ getState()

std::vector< double > Robot::getState ( ) const

Gets the current joint positions of the scratch state.

Returns
A vector of joint positions.

Definition at line 565 of file robowflex_library/src/robot.cpp.

566 {
567  const double *positions = scratch_->getVariablePositions();
568  std::vector<double> state(positions, positions + scratch_->getVariableCount());
569  return state;
570 }

◆ getStateMsg()

moveit_msgs::RobotState Robot::getStateMsg ( ) const

Get the current scratch state as a message.

Returns
The scratch state as a MoveIt message.

Definition at line 572 of file robowflex_library/src/robot.cpp.

573 {
574  moveit_msgs::RobotState message;
576 
577  return message;
578 }
void robotStateToRobotStateMsg(const RobotState &state, moveit_msgs::RobotState &robot_state, bool copy_attached_bodies=true)

◆ getURDF()

urdf::ModelInterfaceConstSharedPtr Robot::getURDF ( ) const

Get the raw URDF Model.

Returns
The URDF Model.

Definition at line 472 of file robowflex_library/src/robot.cpp.

473 {
474  return model_->getURDF();
475 }

◆ getURDFString()

const std::string & Robot::getURDFString ( ) const

Get the raw URDF Model as a string.

Returns
The URDF Model as a string.

Definition at line 477 of file robowflex_library/src/robot.cpp.

478 {
479  return urdf_;
480 }

◆ hasJoint()

bool Robot::hasJoint ( const std::string joint) const

Checks if a joint exists in the robot.

Returns
True if the joint exists, false otherwise.

Definition at line 597 of file robowflex_library/src/robot.cpp.

598 {
599  const auto &joint_names = getJointNames();
600  return (std::find(joint_names.begin(), joint_names.end(), joint) != joint_names.end());
601 }
std::vector< std::string > getJointNames() const
Gets the names of joints of the robot.
T find(T... args)

◆ initialize() [1/2]

bool Robot::initialize ( const std::string urdf_file)

Initializes a robot from a kinematic description. A default semantic description is used.

Parameters
[in]urdf_fileLocation of the robot's URDF (XML or .xacro file).
Returns
True on success, false on failure.

Definition at line 175 of file robowflex_library/src/robot.cpp.

176 {
177  if (loader_)
178  {
179  RBX_ERROR("Already initialized!");
180  return false;
181  }
182 
183  if (not loadURDFFile(urdf_file))
184  return false;
185 
186  // Generate basic SRDF on the fly.
187  tinyxml2::XMLDocument doc;
188  doc.Parse(urdf_.c_str());
189  const auto &name = doc.FirstChildElement("robot")->Attribute("name");
190  const std::string &srdf = "<?xml version=\"1.0\" ?><robot name=\"" + std::string(name) + "\"></robot>";
191 
192  srdf_ = srdf;
193 
195 
196  return true;
197 }
T c_str(T... args)
void initializeInternal(bool namespaced=true)
Initializes and loads the robot. Calls post-processing functions and creates scratch state.
bool loadURDFFile(const std::string &urdf_file)
Loads the URDF file.
std::shared_ptr< robot_model_loader::RobotModelLoader > loader_
Robot model loader.
#define RBX_ERROR(fmt,...)
Output a error logging message.
Definition: log.h:102

◆ initialize() [2/2]

bool Robot::initialize ( const std::string urdf_file,
const std::string srdf_file,
const std::string limits_file = "",
const std::string kinematics_file = "" 
)

Initializes a robot from a kinematic and semantic description. All files are loaded under the robot's namespace.

Parameters
[in]urdf_fileLocation of the robot's URDF (XML or .xacro file).
[in]srdf_fileLocation of the robot's SRDF (XML or .xacro file).
[in]limits_fileLocation of the joint limit information (a YAML file). Optional.
[in]kinematics_fileLocation of the kinematics plugin information (a YAML file). Optional.
Returns
True on success, false on failure.

Definition at line 50 of file robowflex_library/src/robot.cpp.

52 {
53  if (loader_)
54  {
55  RBX_ERROR("Already initialized!");
56  return false;
57  }
58 
59  if (not loadURDFFile(urdf_file))
60  {
61  RBX_ERROR("Failed to load URDF!");
62  return false;
63  }
64 
65  if (not loadSRDFFile(srdf_file))
66  {
67  RBX_ERROR("Failed to load SRDF!");
68  return false;
69  }
70 
71  if (not limits_file.empty())
73  {
74  RBX_ERROR("Failed to load joint limits!");
75  return false;
76  }
77 
78  if (not kinematics_file.empty())
79  if (not initializeKinematics(kinematics_file))
80  {
81  RBX_ERROR("Failed to load kinematics!");
82  return false;
83  }
84 
86  return true;
87 }
PostProcessYAMLFunction limits_function_
Limits YAML post-processing function.
bool loadYAMLFile(const std::string &name, const std::string &file)
Loads a YAML file into the robot's namespace under name.
static const std::string ROBOT_DESCRIPTION
Default robot description name.
static const std::string ROBOT_PLANNING
Default robot planning description suffix.
bool initializeKinematics(const std::string &kinematics_file)
Initialize a robot with a kinematics description.
bool loadSRDFFile(const std::string &srdf_file)
Loads the SRDF file.
T empty(T... args)

◆ initializeFromYAML()

bool Robot::initializeFromYAML ( const std::string config_file)

Initializes a robot from a YAML config which includes URDF (urdf) and optional the SRDF (srdf), joint limits (joint_limits), IK plugins (kinematics) and a default state (robot_state). All files are loaded under the robot's namespace. The names of the YAML keys are in parenthesis.

Parameters
[in]config_fileLocation of the yaml config file.
Returns
True on success, false on failure.

Definition at line 89 of file robowflex_library/src/robot.cpp.

90 {
91  if (loader_)
92  {
93  RBX_ERROR("Already initialized!");
94  return false;
95  }
96 
97  const auto &yaml = IO::loadFileToYAML(config_file);
98  if (not yaml.first)
99  {
100  RBX_ERROR("Failed to load YAML file `%s`.", config_file);
101  return false;
102  }
103 
104  const auto &node = yaml.second;
105 
106  // URDF
107  std::string urdf_file;
108  if (IO::isNode(node["urdf"]))
109  urdf_file = node["urdf"].as<std::string>();
110  else
111  {
112  RBX_ERROR("No URDF entry in YAML file `%s`!", config_file);
113  return false;
114  }
115 
116  // SRDF
117  std::string srdf_file;
118  if (IO::isNode(node["srdf"]))
119  srdf_file = node["srdf"].as<std::string>();
120  else
121  RBX_WARN("No SRDF entry in YAML!");
122 
123  // Joint limits
124  std::string limits_file;
125  if (IO::isNode(node["limits"]))
126  {
127  if (srdf_file.empty())
128  {
129  RBX_ERROR("Cannot provide joint limits without SRDF in YAML file `%s`!", config_file);
130  return false;
131  }
132 
133  limits_file = node["limits"].as<std::string>();
134  }
135  else
136  RBX_WARN("No joint limits provided!");
137 
138  // Kinematics plugins
139  std::string kinematics_file;
140  if (IO::isNode(node["kinematics"]))
141  {
142  if (srdf_file.empty())
143  {
144  RBX_ERROR("Cannot provide kinematics without SRDF in YAML file `%s`!", config_file);
145  return false;
146  }
147 
148  kinematics_file = node["kinematics"].as<std::string>();
149  }
150  else
151  RBX_WARN("No kinematics plugins provided!");
152 
153  // Initialize robot
154  bool r;
155  if (srdf_file.empty())
156  r = initialize(urdf_file);
157  else
158  r = initialize(urdf_file, srdf_file, limits_file, kinematics_file);
159 
160  // Set default state if provided in file.
161  if (r)
162  {
163  if (IO::isNode(node["robot_state"]))
164  {
165  const auto &robot_state = IO::robotStateFromNode(node["robot_state"]);
166  setState(robot_state);
167  }
168  else
169  RBX_WARN("No default state provided!");
170  }
171 
172  return r;
173 }
bool initialize(const std::string &urdf_file)
Initializes a robot from a kinematic description. A default semantic description is used.
void setState(const std::vector< double > &positions)
Sets the scratch state from a vector of joint positions (all must be specified)
#define RBX_WARN(fmt,...)
Output a warning logging message.
Definition: log.h:110
std::pair< bool, YAML::Node > loadFileToYAML(const std::string &path)
Loads a file to a YAML node.
moveit_msgs::RobotState robotStateFromNode(const YAML::Node &node)
Converts a robot state YAML to a robot_state message.
Definition: yaml.cpp:1869

◆ initializeInternal()

void Robot::initializeInternal ( bool  namespaced = true)
protected

Initializes and loads the robot. Calls post-processing functions and creates scratch state.

Parameters
[in]namespacedWhether or not the parameter server description is under the handler namespace.

Definition at line 308 of file robowflex_library/src/robot.cpp.

309 {
310  const std::string &description = ((namespaced) ? handler_.getNamespace() : "") + "/" + ROBOT_DESCRIPTION;
311 
312  loadRobotModel(description);
313  if (urdf_function_)
315 
316  if (srdf_function_)
318 
319  // If either function was called, reload robot.
321  {
322  RBX_INFO("Reloading model after URDF/SRDF post-process function...");
323  loadRobotModel(description);
324  }
325 
326  // set strings on parameter server
329 
330  scratch_.reset(new robot_state::RobotState(model_));
331  scratch_->setToDefaultValues();
332 }
void setParam(const std::string &key, const T &value)
Sets a parameter on the parameter server.
Definition: handler.h:53
const std::string & getNamespace() const
Gets the namespace of the handler.
void loadRobotModel(const std::string &description)
Loads a robot model from the loaded information on the parameter server.
PostProcessXMLFunction urdf_function_
URDF post-processing function.
PostProcessXMLFunction srdf_function_
SRDF post-processing function.
void updateXMLString(std::string &string, const PostProcessXMLFunction &function)
Updates a loaded XML string based on an XML post-process function. Called after initial,...
static const std::string ROBOT_SEMANTIC
Default robot semantic description suffix.
#define RBX_INFO(fmt,...)
Output a info logging message.
Definition: log.h:118

◆ initializeKinematics()

bool Robot::initializeKinematics ( const std::string kinematics_file)

Initialize a robot with a kinematics description.

Parameters
[in]kinematics_fileLocation of the kinematics plugin information (a YAML file).
Returns
True on success, false on failure.

Definition at line 199 of file robowflex_library/src/robot.cpp.

200 {
201  if (kinematics_)
202  {
203  RBX_ERROR("Already loaded kinematics!");
204  return false;
205  }
206 
208 }
static const std::string ROBOT_KINEMATICS
Default robot kinematics description suffix.
kinematics_plugin_loader::KinematicsPluginLoaderPtr kinematics_
Kinematic plugin loader.
PostProcessYAMLFunction kinematics_function_
Kinematics plugin YAML post-processing function.

◆ isLinkURDF()

bool Robot::isLinkURDF ( tinyxml2::XMLDocument &  doc,
const std::string name 
)

Checks if a node link exist with named name_link.

Parameters
[in]docThe URDF description.
[in]nameThe name of the link to find.
Returns
True if link exists, false otherwise.

Definition at line 215 of file robowflex_library/src/robot.cpp.

216 {
217  auto *node = doc.FirstChildElement("robot")->FirstChildElement("link");
218  while (node != nullptr)
219  {
220  if (node->Attribute("name", name.c_str()))
221  return true;
222 
223  node = node->NextSiblingElement("link");
224  }
225  return false;
226 }

◆ loadKinematics()

bool Robot::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.

Parameters
[in]groupJoint group name to load.
[in]load_subgroupsLoad kinematic solvers for subgroups of the requested group.
Returns
True on success, false on failure.

Definition at line 347 of file robowflex_library/src/robot.cpp.

348 {
349  // Needs to be called first to read the groups defined in the SRDF from the ROS params.
350  robot_model::SolverAllocatorFn allocator = kinematics_->getLoaderFunction(loader_->getSRDF());
351 
352  const auto &groups = kinematics_->getKnownGroups();
353  if (groups.empty())
354  {
355  RBX_ERROR("No kinematics plugins defined. Fill and load kinematics.yaml!");
356  return false;
357  }
358 
359  if (!model_->hasJointModelGroup(group_name))
360  {
361  RBX_ERROR("No JMG defined for `%s`!", group_name);
362  return false;
363  }
364 
365  std::vector<std::string> load_names;
366 
367  // If requested, also attempt to load the kinematics solvers for subgroups.
368  if (load_subgroups)
369  {
370  const auto &subgroups = model_->getJointModelGroup(group_name)->getSubgroupNames();
371  load_names.insert(load_names.end(), subgroups.begin(), subgroups.end());
372  }
373 
374  // Check if this group also has an associated kinematics solver to load.
375  if (std::find(groups.begin(), groups.end(), group_name) != groups.end())
376  load_names.emplace_back(group_name);
377 
378  auto timeout = kinematics_->getIKTimeout();
379 
380  for (const auto &name : load_names)
381  {
382  // Check if kinematics have already been loaded for this group.
383  if (imap_.find(name) != imap_.end())
384  continue;
385 
386  if (!model_->hasJointModelGroup(name) ||
387  std::find(groups.begin(), groups.end(), name) == groups.end())
388  {
389  RBX_ERROR("No JMG or Kinematics defined for `%s`!", name);
390  return false;
391  }
392 
393  robot_model::JointModelGroup *jmg = model_->getJointModelGroup(name);
394  kinematics::KinematicsBasePtr solver = allocator(jmg);
395 
396  if (solver)
397  {
398  std::string error_msg;
399  if (solver->supportsGroup(jmg, &error_msg))
400  imap_[name] = allocator;
401  else
402  {
403  RBX_ERROR("Kinematics solver %s does not support joint group %s. Error: %s",
404  typeid(*solver).name(), name, error_msg);
405  return false;
406  }
407  }
408  else
409  {
410  RBX_ERROR("Kinematics solver could not be instantiated for joint group `%s`.", name);
411  return false;
412  }
413 
414  RBX_INFO("Loaded Kinematics Solver for `%s`", name);
415  jmg->setDefaultIKTimeout(timeout[name]);
416  }
417 
418  model_->setKinematicsAllocators(imap_);
419  return true;
420 }
std::map< std::string, robot_model::SolverAllocatorFn > imap_
Kinematic solver allocator map.
T insert(T... args)

◆ loadRobotModel()

void Robot::loadRobotModel ( const std::string description)
protected

Loads a robot model from the loaded information on the parameter server.

Parameters
[in]descriptionRobot description on parameter server.

Definition at line 334 of file robowflex_library/src/robot.cpp.

335 {
336  robot_model_loader::RobotModelLoader::Options options(description);
337  options.load_kinematics_solvers_ = false;
338  options.urdf_string_ = urdf_;
339  options.srdf_string_ = srdf_;
340 
341  loader_.reset(new robot_model_loader::RobotModelLoader(options));
342  kinematics_.reset(new kinematics_plugin_loader::KinematicsPluginLoader(description));
343 
344  model_ = loader_->getModel();
345 }
T reset(T... args)

◆ loadSRDFFile()

bool Robot::loadSRDFFile ( const std::string srdf_file)
protected

Loads the SRDF file.

Parameters
[in]srdf_fileThe SRDF file name.
Returns
True on success, false on failure.

Definition at line 40 of file robowflex_library/src/robot.cpp.

41 {
42  std::string srdf = loadXMLFile(srdf_file);
43  if (srdf.empty())
44  return false;
45 
46  srdf_ = srdf;
47  return true;
48 }
std::string loadXMLFile(const std::string &file)
Loads an XML or .xacro file into a string.

◆ loadURDFFile()

bool Robot::loadURDFFile ( const std::string urdf_file)
protected

Loads the URDF file.

Parameters
[in]urdf_fileThe URDF file name.
Returns
True on success, false on failure.

Definition at line 30 of file robowflex_library/src/robot.cpp.

31 {
32  std::string urdf = loadXMLFile(urdf_file);
33  if (urdf.empty())
34  return false;
35 
36  urdf_ = urdf;
37  return true;
38 }

◆ loadXMLFile()

std::string Robot::loadXMLFile ( const std::string file)

Loads an XML or .xacro file into a string.

Parameters
[in]fileFile to load.
Returns
XML string upon success, empty string on failure.

Definition at line 276 of file robowflex_library/src/robot.cpp.

277 {
278  std::string string = IO::loadXMLToString(file);
279  if (string.empty())
280  {
281  RBX_ERROR("Failed to load XML file `%s`.", file);
282  return "";
283  }
284 
285  return string;
286 }
std::string loadXMLToString(const std::string &path)
Loads an XML or .xacro file to a string.

◆ loadYAMLFile() [1/2]

bool Robot::loadYAMLFile ( const std::string name,
const std::string file 
)

Loads a YAML file into the robot's namespace under name.

Parameters
[in]nameName to load file under.
[in]fileFile to load.
Returns
True on success, false on failure.

Definition at line 243 of file robowflex_library/src/robot.cpp.

244 {
245  PostProcessYAMLFunction function;
246  return loadYAMLFile(name, file, function);
247 }
std::function< bool(YAML::Node &)> PostProcessYAMLFunction
A function that runs after loading a YAML file and can modify its contents. Returns true on success,...

◆ loadYAMLFile() [2/2]

bool Robot::loadYAMLFile ( const std::string name,
const std::string file,
const PostProcessYAMLFunction function 
)

Loads a YAML file into the robot's namespace under name, with a post-process function.

Parameters
[in]nameName to load file under.
[in]fileFile to load.
[in]functionOptional post processing function.
Returns
True on success, false on failure.

Definition at line 249 of file robowflex_library/src/robot.cpp.

251 {
252  const auto &yaml = IO::loadFileToYAML(file);
253  if (!yaml.first)
254  {
255  RBX_ERROR("Failed to load YAML file `%s`.", file);
256  return false;
257  }
258 
259  if (function)
260  {
261  YAML::Node copy = yaml.second;
262  if (!function(copy))
263  {
264  RBX_ERROR("Failed to process YAML file `%s`.", file);
265  return false;
266  }
267 
268  handler_.loadYAMLtoROS(copy, name);
269  }
270  else
271  handler_.loadYAMLtoROS(yaml.second, name);
272 
273  return true;
274 }
void loadYAMLtoROS(const YAML::Node &node, const std::string &prefix="")
Loads the contents of a YAML node to the parameter server under a prefix.
T copy(T... args)

◆ operator=()

void robowflex::Robot::operator= ( Robot const &  )
delete

◆ setFromIK() [1/2]

bool Robot::setFromIK ( const IKQuery query)

Sets a group of the scratch state from an IK query. If the IK query fails the scratch state retains its initial value.

Parameters
[in]queryQuery for inverse kinematics. See Robot::IKQuery documentation for more.
Returns
True on success, false on failure.

Definition at line 858 of file robowflex_library/src/robot.cpp.

859 {
860  return setFromIK(query, *scratch_);
861 }
bool setFromIK(const IKQuery &query)
Sets a group of the scratch state from an IK query. If the IK query fails the scratch state retains i...

◆ setFromIK() [2/2]

bool Robot::setFromIK ( const IKQuery query,
robot_state::RobotState &  state 
) const

Sets a robot state from an IK query. If the IK query fails the scratch state retains its initial value.

Parameters
[in]queryQuery for inverse kinematics. See Robot::IKQuery documentation for more.
[out]stateRobot state to set from IK.
Returns
True on success, false on failure.

Definition at line 863 of file robowflex_library/src/robot.cpp.

864 {
865  // copy query for unconstness
866  IKQuery query_copy(query);
867 
868  // If there are no tips in the query,
869  // we use the tip frame from the original query group
870  if (query_copy.tips[0].empty())
871  query_copy.tips = getSolverTipFrames(query.group);
872 
873  const robot_model::JointModelGroup *jmg = model_->getJointModelGroup(query_copy.group);
874  const auto &gsvcf = (query_copy.scene) ? query_copy.scene->getGSVCF(query_copy.verbose) :
876 
877  bool evaluate = not query_copy.metrics.empty() or query_copy.validate;
879 
880  const auto &constraints = (evaluate) ? query_copy.getAsConstraints(*this) : nullptr;
881 
882  // Best state if evaluating metrics.
883  auto best = (query_copy.metrics.empty()) ? nullptr : std::make_shared<robot_state::RobotState>(state);
884  double best_value = constants::inf;
885 
886  bool success = false;
887  RobotPoseVector targets;
888  for (std::size_t i = 0; i < query_copy.attempts and not success; ++i)
889  {
890  // Sample new target poses from regions.
891  query_copy.sampleRegions(targets);
892 
893 #if ROBOWFLEX_AT_LEAST_MELODIC
894  // Multi-tip IK: Will delegate automatically to RobotState::setFromIKSubgroups() if the kinematics
895  // solver doesn't support multi-tip queries.
896  success =
897  state.setFromIK(jmg, targets, query_copy.tips, query_copy.timeout, gsvcf, query_copy.options);
898 #else
899  // attempts was a prior field that was deprecated in melodic
900  success =
901  state.setFromIK(jmg, targets, query_copy.tips, 1, query_copy.timeout, gsvcf, query_copy.options);
902 #endif
903 
904  if (evaluate)
905  {
906  state.update();
907  result = constraints->decide(state, query_copy.verbose);
908  }
909 
910  // Externally validate result
911  if (query_copy.validate)
912  {
913  if (query_copy.verbose)
914  RBX_INFO("Constraint Distance: %1%", result.distance);
915 
916  bool no_collision =
917  (query_copy.scene) ? not query_copy.scene->checkCollision(state).collision : true;
918 
919  success = //
920  no_collision and //
921  ((query_copy.valid_distance > 0.) ? result.distance <= query_copy.valid_distance :
922  result.satisfied);
923  }
924 
925  // If success, evaluate state for metrics.
926  if (success and not query_copy.metrics.empty())
927  {
928  double v = query_copy.getMetricValue(state, result);
929 
930  if (query_copy.verbose)
931  RBX_INFO("State Metric Value: %1%", v);
932 
933  if (v < best_value)
934  {
935  if (query_copy.verbose)
936  RBX_INFO("State is current best!");
937 
938  best_value = v;
939  *best = state;
940  }
941 
942  success = false;
943  }
944 
945  if (query_copy.random_restart and not success)
946  state.setToRandomPositions(jmg);
947  }
948 
949  // If evaluating metric, copy best state.
950  if (std::isfinite(best_value))
951  {
952  state = *best;
953  success = true;
954  }
955 
956  state.update();
957  return success;
958 }
std::vector< std::string > getSolverTipFrames(const std::string &group) const
Get the tip frames for the IK solver for a given joint model group group.
T isfinite(T... args)
boost::function< bool(RobotState *robot_state, const JointModelGroup *joint_group, const double *joint_group_variable_values)> GroupStateValidityCallbackFn
static const double inf
Definition: constants.h:17

◆ setGroupState()

void Robot::setGroupState ( const std::string name,
const std::vector< double > &  positions 
)

Sets the group of the scratch state to a vector of joint positions.

Parameters
[in]nameName of group to set.
[in]positionsPositions to set.

Definition at line 559 of file robowflex_library/src/robot.cpp.

560 {
561  scratch_->setJointGroupPositions(name, positions);
562  scratch_->update();
563 }

◆ setKinematicsPostProcessFunction()

void Robot::setKinematicsPostProcessFunction ( const PostProcessYAMLFunction function)

Sets a post processing function for loading the kinematics plugin file.

Parameters
[in]functionThe function to use.

Definition at line 238 of file robowflex_library/src/robot.cpp.

239 {
240  kinematics_function_ = function;
241 }

◆ setLimitsPostProcessFunction()

void Robot::setLimitsPostProcessFunction ( const PostProcessYAMLFunction function)

Sets a post processing function for loading the joint limits file.

Parameters
[in]functionThe function to use.

Definition at line 233 of file robowflex_library/src/robot.cpp.

234 {
235  limits_function_ = function;
236 }

◆ setSRDFPostProcessAddFloatingJoint()

void Robot::setSRDFPostProcessAddFloatingJoint ( const std::string name)

Adds a planar virtual joint through the SRDF to the loaded robot with name name. This joint will have three degrees of freedom: <name>/x, <name>/y, and <name>/theta. Will apply this joint between the world and the root frame.

Parameters
[in]nameName for new joint.

Definition at line 437 of file robowflex_library/src/robot.cpp.

438 {
439  setSRDFPostProcessFunction([&, name](tinyxml2::XMLDocument &doc) -> bool {
440  tinyxml2::XMLElement *virtual_joint = doc.NewElement("virtual_joint");
441  virtual_joint->SetAttribute("name", name.c_str());
442  virtual_joint->SetAttribute("type", "floating");
443  virtual_joint->SetAttribute("parent_frame", "world");
444  virtual_joint->SetAttribute("child_link", model_->getRootLink()->getName().c_str());
445 
446  doc.FirstChildElement("robot")->InsertFirstChild(virtual_joint);
447 
448  return true;
449  });
450 }
void setSRDFPostProcessFunction(const PostProcessXMLFunction &function)
Sets a post processing function for loading the SRDF.

◆ setSRDFPostProcessAddPlanarJoint()

void Robot::setSRDFPostProcessAddPlanarJoint ( const std::string name)

Adds a planar virtual joint through the SRDF to the loaded robot with name name. This joint will have three degrees of freedom: <name>/x, <name>/y, and <name>/theta. Will apply this joint between the world and the root frame.

Parameters
[in]nameName for new joint.

Definition at line 422 of file robowflex_library/src/robot.cpp.

423 {
424  setSRDFPostProcessFunction([&, name](tinyxml2::XMLDocument &doc) -> bool {
425  tinyxml2::XMLElement *virtual_joint = doc.NewElement("virtual_joint");
426  virtual_joint->SetAttribute("name", name.c_str());
427  virtual_joint->SetAttribute("type", "planar");
428  virtual_joint->SetAttribute("parent_frame", "world");
429  virtual_joint->SetAttribute("child_link", model_->getRootLink()->getName().c_str());
430 
431  doc.FirstChildElement("robot")->InsertFirstChild(virtual_joint);
432 
433  return true;
434  });
435 }

◆ setSRDFPostProcessFunction()

void Robot::setSRDFPostProcessFunction ( const PostProcessXMLFunction function)

Sets a post processing function for loading the SRDF.

Parameters
[in]functionThe function to use.

Definition at line 228 of file robowflex_library/src/robot.cpp.

229 {
230  srdf_function_ = function;
231 }

◆ setState() [1/5]

void Robot::setState ( const moveit_msgs::RobotState &  state)

Sets the scratch state from a robot state message.

Parameters
[in]stateThe state to set.

Definition at line 545 of file robowflex_library/src/robot.cpp.

546 {
548  scratch_->update();
549 }
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)

◆ setState() [2/5]

void Robot::setState ( const sensor_msgs::JointState &  state)

Sets the scratch state from a joint state message.

Parameters
[in]stateThe state to set.

Definition at line 539 of file robowflex_library/src/robot.cpp.

540 {
541  scratch_->setVariableValues(state);
542  scratch_->update();
543 }

◆ setState() [3/5]

void Robot::setState ( const std::map< std::string, double > &  variable_map)

Sets the scratch state from a map of joint name to position.

Parameters
[in]variable_mapJoint positions to set.

Definition at line 526 of file robowflex_library/src/robot.cpp.

527 {
528  scratch_->setVariablePositions(variable_map);
529  scratch_->update();
530 }

◆ setState() [4/5]

void Robot::setState ( const std::vector< double > &  positions)

Sets the scratch state from a vector of joint positions (all must be specified)

Parameters
[in]positionsJoint positions to set.

Definition at line 520 of file robowflex_library/src/robot.cpp.

521 {
522  scratch_->setVariablePositions(positions);
523  scratch_->update();
524 }

◆ setState() [5/5]

void Robot::setState ( const std::vector< std::string > &  variable_names,
const std::vector< double > &  variable_position 
)

Sets the scratch state from a vector of joint names and their positions.

Parameters
[in]variable_namesJoint names.
[in]variable_positionPosition of joint variable (index matches entry in variable_names)

Definition at line 532 of file robowflex_library/src/robot.cpp.

534 {
535  scratch_->setVariablePositions(variable_names, variable_position);
536  scratch_->update();
537 }

◆ setStateFromYAMLFile()

void Robot::setStateFromYAMLFile ( const std::string file)

Sets the scratch state from a robot state message saved to a YAML file.

Parameters
[in]fileThe YAML file to load.

Definition at line 551 of file robowflex_library/src/robot.cpp.

552 {
553  moveit_msgs::RobotState state;
554  IO::fromYAMLFile(state, file);
555 
556  setState(state);
557 }
bool fromYAMLFile(moveit_msgs::PlanningScene &msg, const std::string &file)
Loads a planning scene from a YAML file.
Definition: yaml.cpp:1914

◆ setStateMsgGroupState()

void Robot::setStateMsgGroupState ( moveit_msgs::RobotState &  state,
const std::string group,
const std::vector< double > &  positions 
) const

Set the group state of a MoveIt RobotState message.

Parameters
[out]stateThe state message to set.
[in]groupGroup in state to set.
[in]positionsPositions to set group state to.

Definition at line 580 of file robowflex_library/src/robot.cpp.

582 {
583  robot_state::RobotState temp(getModelConst());
585 
586  auto *jmg = getModelConst()->getJointModelGroup(group);
587  temp.setJointGroupPositions(jmg, positions);
588 
590 }

◆ setURDFPostProcessFunction()

void Robot::setURDFPostProcessFunction ( const PostProcessXMLFunction function)

Sets a post processing function for loading the URDF.

Parameters
[in]functionThe function to use.

Definition at line 210 of file robowflex_library/src/robot.cpp.

211 {
212  urdf_function_ = function;
213 }

◆ toYAMLFile()

bool Robot::toYAMLFile ( const std::string file) const

Dumps the current configuration of the robot as a YAML file.

Parameters
[in]fileFile to write to.
Returns
True on success, false on failure.

Definition at line 987 of file robowflex_library/src/robot.cpp.

988 {
989  moveit_msgs::RobotState msg;
991 
992  const auto &yaml = IO::toNode(msg);
993  return IO::YAMLToFile(yaml, file);
994 }

◆ updateXMLString()

void Robot::updateXMLString ( std::string string,
const PostProcessXMLFunction function 
)
protected

Updates a loaded XML string based on an XML post-process function. Called after initial, unmodified robot is loaded.

Parameters
[in,out]stringInput XML string.
[in]functionXML processing function.

Definition at line 288 of file robowflex_library/src/robot.cpp.

289 {
290  if (function)
291  {
292  tinyxml2::XMLDocument doc;
293  doc.Parse(string.c_str());
294 
295  if (not function(doc))
296  {
297  RBX_ERROR("Failed to process XML string `%s`.", string);
298  return;
299  }
300 
301  tinyxml2::XMLPrinter printer;
302  doc.Print(&printer);
303 
304  string = std::string(printer.CStr());
305  }
306 }

◆ validateIKQuery()

bool Robot::validateIKQuery ( const IKQuery query,
const robot_state::RobotState &  state 
) const

Validates that a state satisfies an IK query's request poses.

Parameters
[in]queryThe query to validate.
[in]stateThe state to validate against the query.
Returns
True if the query is satisfied, false otherwise.

Definition at line 960 of file robowflex_library/src/robot.cpp.

961 {
962  const auto &constraints = query.getAsConstraints(*this);
963  const auto &result = constraints->decide(state, query.verbose);
964  return (query.valid_distance > 0.) ? result.distance <= query.valid_distance : result.satisfied;
965 }

Member Data Documentation

◆ handler_

IO::Handler robowflex::Robot::handler_
protected

IO handler (namespaced with name_)

Definition at line 793 of file robowflex_library/include/robowflex_library/robot.h.

◆ imap_

std::map<std::string, robot_model::SolverAllocatorFn> robowflex::Robot::imap_
protected

Kinematic solver allocator map.

Definition at line 805 of file robowflex_library/include/robowflex_library/robot.h.

◆ kinematics_

kinematics_plugin_loader::KinematicsPluginLoaderPtr robowflex::Robot::kinematics_
protected

Kinematic plugin loader.

Definition at line 806 of file robowflex_library/include/robowflex_library/robot.h.

◆ kinematics_function_

PostProcessYAMLFunction robowflex::Robot::kinematics_function_
protected

Kinematics plugin YAML post-processing function.

Definition at line 801 of file robowflex_library/include/robowflex_library/robot.h.

◆ limits_function_

PostProcessYAMLFunction robowflex::Robot::limits_function_
protected

Limits YAML post-processing function.

Definition at line 800 of file robowflex_library/include/robowflex_library/robot.h.

◆ loader_

std::shared_ptr<robot_model_loader::RobotModelLoader> robowflex::Robot::loader_
protected

Robot model loader.

Definition at line 803 of file robowflex_library/include/robowflex_library/robot.h.

◆ model_

robot_model::RobotModelPtr robowflex::Robot::model_
protected

Loaded robot model.

Definition at line 804 of file robowflex_library/include/robowflex_library/robot.h.

◆ name_

const std::string robowflex::Robot::name_
protected

◆ ROBOT_DESCRIPTION

const std::string Robot::ROBOT_DESCRIPTION = "robot_description"
static

Default robot description name.

Definition at line 81 of file robowflex_library/include/robowflex_library/robot.h.

◆ ROBOT_KINEMATICS

const std::string Robot::ROBOT_KINEMATICS = "_kinematics"
static

Default robot kinematics description suffix.

Definition at line 84 of file robowflex_library/include/robowflex_library/robot.h.

◆ ROBOT_PLANNING

const std::string Robot::ROBOT_PLANNING = "_planning"
static

Default robot planning description suffix.

Definition at line 83 of file robowflex_library/include/robowflex_library/robot.h.

◆ ROBOT_SEMANTIC

const std::string Robot::ROBOT_SEMANTIC = "_semantic"
static

Default robot semantic description suffix.

Definition at line 82 of file robowflex_library/include/robowflex_library/robot.h.

◆ scratch_

robot_state::RobotStatePtr robowflex::Robot::scratch_
protected

Scratch robot state.

Definition at line 808 of file robowflex_library/include/robowflex_library/robot.h.

◆ srdf_

std::string robowflex::Robot::srdf_
protected

The SRDF as a string.

Definition at line 796 of file robowflex_library/include/robowflex_library/robot.h.

◆ srdf_function_

PostProcessXMLFunction robowflex::Robot::srdf_function_
protected

SRDF post-processing function.

Definition at line 799 of file robowflex_library/include/robowflex_library/robot.h.

◆ urdf_

std::string robowflex::Robot::urdf_
protected

The URDF as a string.

Definition at line 795 of file robowflex_library/include/robowflex_library/robot.h.

◆ urdf_function_

PostProcessXMLFunction robowflex::Robot::urdf_function_
protected

URDF post-processing function.

Definition at line 798 of file robowflex_library/include/robowflex_library/robot.h.


The documentation for this class was generated from the following files: