Robowflex  v0.1
Making MoveIt Easy
conversions.cpp
Go to the documentation of this file.
1 /* Author: Carlos Quintero, Bryce Willey */
2 
3 // MoveIt!
4 #include <moveit/robot_state/conversions.h>
5 
6 // Robowflex
8 
9 // Tesseract
10 #include <tesseract_ros/ros_tesseract_utils.h>
11 
12 // Conversions
14 
15 using namespace robowflex;
16 
18  tesseract::tesseract_ros::KDLEnvPtr env)
19 {
20  if (env->checkInitialized())
21  {
22  // Clear environment from scene objects.
23  env->clearAttachableObjects();
24  env->clearAttachedBodies();
25 
26  // Loop over collision objects in the scene.
27  const auto &scene_msg = scene->getMessage();
28  for (const auto &collision_object : scene_msg.world.collision_objects)
29  {
30  // Add collision and visual objects
31  tesseract_msgs::AttachableObject obj;
32  obj.name = collision_object.id;
33  obj.operation = tesseract_msgs::AttachableObject::ADD;
34 
35  for (const shape_msgs::SolidPrimitive &primitive : collision_object.primitives)
36  {
37  obj.visual.primitives.emplace_back(primitive);
38  obj.collision.primitives.emplace_back(primitive);
39  std_msgs::Int32 cot;
40  cot.data = tesseract::CollisionObjectType::UseShapeType;
41  obj.collision.primitive_collision_object_types.emplace_back(cot);
42  }
43 
44  for (const shape_msgs::Mesh &mesh : collision_object.meshes)
45  {
46  obj.visual.meshes.emplace_back(mesh);
47  obj.collision.meshes.emplace_back(mesh);
48  std_msgs::Int32 cot;
49  cot.data = tesseract::CollisionObjectType::ConvexHull;
50  obj.collision.mesh_collision_object_types.emplace_back(cot);
51  }
52 
53  for (const shape_msgs::Plane &plane : collision_object.planes)
54  {
55  obj.visual.planes.emplace_back(plane);
56  obj.collision.planes.emplace_back(plane);
57  }
58 
59  for (const geometry_msgs::Pose &pose : collision_object.primitive_poses)
60  {
61  obj.visual.primitive_poses.emplace_back(pose);
62  obj.collision.primitive_poses.emplace_back(pose);
63  }
64 
65  for (const geometry_msgs::Pose &pose : collision_object.mesh_poses)
66  {
67  obj.visual.mesh_poses.emplace_back(pose);
68  obj.collision.mesh_poses.emplace_back(pose);
69  }
70 
71  for (const geometry_msgs::Pose &pose : collision_object.plane_poses)
72  {
73  obj.visual.plane_poses.emplace_back(pose);
74  obj.collision.plane_poses.emplace_back(pose);
75  }
76 
77  // Add collision object as attachable object.
78  auto ao = std::make_shared<tesseract::AttachableObject>();
79  tesseract::tesseract_ros::attachableObjectMsgToAttachableObject(ao, obj);
80  env->addAttachableObject(ao);
81 
82  // Attach the object to the environment (to a parent frame).
83  tesseract::AttachedBodyInfo attached_body_info;
84  attached_body_info.object_name = collision_object.id;
85  attached_body_info.parent_link_name = env->getRootLinkName();
86  attached_body_info.transform = Eigen::Isometry3d::Identity();
87  env->attachBody(attached_body_info);
88  }
89 
90  // Add octomap information.
91  if (scene_msg.world.octomap.octomap.data.size() > 0)
92  {
93  const std::string &name = "octomap";
94  tesseract_msgs::AttachableObject obj;
95  obj.name = name;
96  obj.operation = tesseract_msgs::AttachableObject::ADD;
97  obj.collision.octomaps.emplace_back(scene_msg.world.octomap.octomap);
98  obj.collision.octomap_poses.emplace_back(scene_msg.world.octomap.origin);
99  std_msgs::Int32 cot;
100  cot.data = tesseract::CollisionObjectType::UseShapeType;
101  obj.collision.octomap_collision_object_types.emplace_back(cot);
102 
103  auto ao = std::make_shared<tesseract::AttachableObject>();
104  tesseract::tesseract_ros::attachableObjectMsgToAttachableObject(ao, obj);
105  env->addAttachableObject(ao);
106 
107  tesseract::AttachedBodyInfo attached_body_info;
108  attached_body_info.object_name = name;
109  attached_body_info.parent_link_name = scene_msg.world.octomap.header.frame_id;
110  attached_body_info.transform = Eigen::Isometry3d::Identity();
111  env->attachBody(attached_body_info);
112  }
113 
114  return true;
115  }
116 
117  RBX_ERROR("Tesseract environment not initialized");
118 
119  return false;
120 }
121 
122 bool hypercube::addAttachedBodiesToTesseractEnv(const robot_state::RobotStatePtr &state,
123  tesseract::tesseract_ros::KDLEnvPtr env)
124 {
125  moveit_msgs::RobotState state_msg;
126  moveit::core::robotStateToRobotStateMsg(*state, state_msg);
127 
128  std_msgs::Int32 cotp;
129  cotp.data = tesseract::CollisionObjectType::UseShapeType;
130  std_msgs::Int32 cotm;
131  cotm.data = tesseract::CollisionObjectType::ConvexHull;
132 
133  for (const auto &co : state_msg.attached_collision_objects)
134  {
135  // Declare an attachable object for this attached body.
136  tesseract_msgs::AttachableObject obj;
137  obj.name = co.object.id;
138  obj.operation = tesseract_msgs::AttachableObject::ADD;
139 
140  // Add visual object.
141  obj.visual.primitives = co.object.primitives;
142  obj.visual.primitive_poses = co.object.primitive_poses;
143  obj.visual.meshes = co.object.meshes;
144  obj.visual.mesh_poses = co.object.mesh_poses;
145  obj.visual.planes = co.object.planes;
146  obj.visual.plane_poses = co.object.plane_poses;
147 
148  // Add collision object.
149  obj.collision.primitives = co.object.primitives;
150  obj.collision.primitive_poses = co.object.primitive_poses;
151  obj.collision.primitive_collision_object_types.resize(co.object.primitives.size());
152  std::fill(obj.collision.primitive_collision_object_types.begin(),
153  obj.collision.primitive_collision_object_types.end(), cotp);
154  obj.collision.meshes = co.object.meshes;
155  obj.collision.mesh_poses = co.object.mesh_poses;
156  obj.collision.mesh_collision_object_types.resize(co.object.meshes.size());
157  std::fill(obj.collision.mesh_collision_object_types.begin(),
158  obj.collision.mesh_collision_object_types.end(), cotm);
159  obj.collision.planes = co.object.planes;
160  obj.collision.plane_poses = co.object.plane_poses;
161 
162  // Create Tesseract attachable object.
163  auto ao = std::make_shared<tesseract::AttachableObject>();
164  tesseract::tesseract_ros::attachableObjectMsgToAttachableObject(ao, obj);
165  env->addAttachableObject(ao);
166 
167  // Attach the object to the environment (to a parent frame).
168  tesseract::AttachedBodyInfo attached_body_info;
169  attached_body_info.object_name = co.object.id;
170  attached_body_info.parent_link_name = co.link_name;
171  attached_body_info.transform = Eigen::Isometry3d::Identity();
172  env->attachBody(attached_body_info);
173  }
174 
175  return true;
176 }
177 
178 void hypercube::robotStateToManipState(const robot_state::RobotStatePtr &robot_state,
179  const std::vector<std::string> &manip_joint_names,
180  std::vector<double> &manip_joint_values)
181 {
182  manip_joint_values.clear();
183  manip_joint_values.resize(0);
184 
185  // Extract the state to a raw vector.
186  double *raw_state_values = robot_state->getVariablePositions();
187 
188  // Loop over manip joints and add their values to goal_state in the correct order.
189  const auto &robot_state_joint_names = robot_state->getVariableNames();
190  for (const auto &joint_name : manip_joint_names)
191  {
192  int index = std::distance(
193  robot_state_joint_names.begin(),
194  std::find(robot_state_joint_names.begin(), robot_state_joint_names.end(), joint_name));
195  manip_joint_values.push_back(raw_state_values[index]);
196  }
197 }
198 
199 void hypercube::manipStateToRobotState(const Eigen::Ref<const Eigen::VectorXd> &manip_state,
200  const std::string &manip,
201  const tesseract::tesseract_ros::KDLEnvPtr &env,
202  robot_state::RobotStatePtr robot_state)
203 {
204  // Initialize it with the env state (includes both group and non-group joints).
205  const auto &joint_values = env->getCurrentJointValues();
206  std::vector<double> tmp_current_values(joint_values.data(), joint_values.data() + joint_values.size());
207  robot_state->setVariablePositions(env->getJointNames(), tmp_current_values);
208 
209  // Set (only) group joints from tesseract waypoint.
210  const auto &joint_manip_names = env->getManipulator(manip)->getJointNames();
211  std::vector<double> tmp_group_values(manip_state.data(), manip_state.data() + manip_state.size());
212  robot_state->setVariablePositions(joint_manip_names, tmp_group_values);
213 }
214 
215 void hypercube::manipTesseractTrajToRobotTraj(const tesseract::TrajArray &tesseract_traj,
216  const robot_state::RobotStatePtr &ref_state,
217  const std::string &manip,
218  const tesseract::tesseract_ros::KDLEnvPtr &env,
219  robot_trajectory::RobotTrajectoryPtr trajectory)
220 {
221  const robot_state::RobotState &copy = *ref_state;
222  trajectory->clear();
223 
224  for (int i = 0; i < tesseract_traj.rows(); i++)
225  {
226  // Create a tmp state for every waypoint.
227  auto tmp_state = std::make_shared<robot_state::RobotState>(copy);
228 
229  // Transform tesseract manip ith waypoint to robot state.
230  manipStateToRobotState(tesseract_traj.row(i), manip, env, tmp_state);
231 
232  // Add waypoint to trajectory.
233  trajectory->addSuffixWayPoint(tmp_state, 0.0);
234  }
235 }
236 
237 void hypercube::robotTrajToManipTesseractTraj(const robot_trajectory::RobotTrajectoryPtr &robot_traj,
238  const std::string &manip,
239  const tesseract::tesseract_ros::KDLEnvPtr &env,
240  tesseract::TrajArray &trajectory)
241 {
242  trajectory.resize(0, 0);
243  trajectory.resize(robot_traj->getWayPointCount(), robot_traj->getGroup()->getVariableCount());
244 
245  for (unsigned int i = 0; i < robot_traj->getWayPointCount(); ++i)
246  {
247  // Transform each traj waypoint to tesseract manip state.
248  std::vector<double> manip_joint_values;
249  robotStateToManipState(robot_traj->getWayPointPtr(i), env->getManipulator(manip)->getJointNames(),
250  manip_joint_values);
251 
252  // Push manip state to manip tesseract trajectory .
253  trajectory.row(i) = Eigen::VectorXd::Map(manip_joint_values.data(), manip_joint_values.size());
254  }
255 }
void addSuffixWayPoint(const robot_state::RobotState &state, double dt)
A const shared pointer wrapper for robowflex::Scene.
T clear(T... args)
T data(T... args)
T distance(T... args)
T fill(T... args)
T find(T... args)
#define RBX_ERROR(fmt,...)
Output a error logging message.
Definition: log.h:102
void robotStateToRobotStateMsg(const RobotState &state, moveit_msgs::RobotState &robot_state, bool copy_attached_bodies=true)
void manipStateToRobotState(const Eigen::Ref< const Eigen::VectorXd > &manip_state, const std::string &manip, const tesseract::tesseract_ros::KDLEnvPtr &env, robot_state::RobotStatePtr robot_state)
Transform a tesseract waypoint (manip state) to robot state. Joint values for non-manip joints are ta...
void manipTesseractTrajToRobotTraj(const tesseract::TrajArray &tesseract_traj, const robot_state::RobotStatePtr &ref_state, const std::string &manip, const tesseract::tesseract_ros::KDLEnvPtr &env, robot_trajectory::RobotTrajectoryPtr trajectory)
Transform a tesseract trajectory to a robot trajectory.
bool sceneToTesseractEnv(const robowflex::SceneConstPtr &scene, tesseract::tesseract_ros::KDLEnvPtr env)
Add scene collision objects to a (previously initialized) KDL environment.
Definition: conversions.cpp:17
bool addAttachedBodiesToTesseractEnv(const robot_state::RobotStatePtr &state, tesseract::tesseract_ros::KDLEnvPtr env)
Add bodies attached to the robot scratch state to the KDL environment.
void robotTrajToManipTesseractTraj(const robot_trajectory::RobotTrajectoryPtr &robot_traj, const std::string &manip, const tesseract::tesseract_ros::KDLEnvPtr &env, tesseract::TrajArray &trajectory)
Transform a robot_trajectory to a tesseract manipulator trajectory.
void robotStateToManipState(const robot_state::RobotStatePtr &robot_state, const std::vector< std::string > &manip_joint_names, std::vector< double > &manip_joint_values)
Transform a robot_state to a vector representing joint values for the manipulator (in the order given...
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
Functions for loading and animating scenes in Blender.
T push_back(T... args)
T resize(T... args)
T size(T... args)
robot_trajectory::RobotTrajectory trajectory(rmodel,"right_arm")