Robowflex  v0.1
Making MoveIt Easy
broadcaster.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #include <sensor_msgs/JointState.h>
4 
8 #include <robowflex_library/tf.h>
9 
10 using namespace robowflex;
11 
12 namespace
13 {
14  void arrayToVector(double *array, unsigned int n, std::vector<double> &dst)
15  {
16  dst = std::vector<double>(array, array + n);
17  }
18 } // namespace
19 
21  const std::string &name)
22  : robot_(robot), base_(base_frame), nh_("/" + name)
23 {
24  state_pub_ = nh_.advertise<sensor_msgs::JointState>("/joint_states", 1);
25 }
26 
28 {
29  active_ = false;
30  while (not done_)
31  {
32  ros::WallDuration pause(1. / rate_);
33  pause.sleep();
34  }
35 }
36 
38 {
39  active_ = true;
40  thread_.reset(new std::thread([&]() {
41  done_ = false;
42 
43  while (active_)
44  {
45  update();
46 
47  ros::WallDuration pause(1. / rate_);
48  pause.sleep();
49  }
50 
51  done_ = true;
52  }));
53 }
54 
56 {
57  active_ = false;
58 }
59 
61  const std::string &target, const RobotPose &tf)
62 {
63  if (static_.find(name) == static_.end())
64  {
65  StaticTransform stf;
66  stf.base = base;
67  stf.target = target;
68  stf.tf = tf;
69 
70  static_.emplace(name, stf);
71  }
72  else
73  RBX_ERROR("Static transform %s already in map!", name);
74 }
75 
77 {
78  auto it = static_.find(name);
79  if (it != static_.end())
80  static_.erase(it);
81  else
82  RBX_ERROR("Static transform %s does not exist in map!", name);
83 }
84 
86 {
87  const auto &state = robot_->getScratchStateConst();
88  const auto &model = robot_->getModelConst();
89 
90  for (const auto &link : model->getLinkModels())
91  {
92  const auto &parent = link->getParentLinkModel();
93 
94  RobotPose tf =
95  link->getJointOriginTransform() * state->getJointTransform(link->getParentJointModel());
96 
97  std::string source = (parent) ? parent->getName() : base_;
98  const std::string &target = link->getName();
99 
100  auto msg = TF::transformEigenToMsg(source, target, tf);
101 
102  tf2br_.sendTransform(msg);
103  }
104 
105  for (const auto &pair : static_)
106  {
107  const auto &stf = pair.second;
108  auto static_msg = TF::transformEigenToMsg(stf.base, stf.target, stf.tf);
109  tf2br_.sendTransform(static_msg);
110  }
111 
112  unsigned int n = state->getVariableCount();
113 
114  sensor_msgs::JointState msg;
115  msg.header.stamp = ros::Time::now();
116  msg.name = state->getVariableNames();
117 
118  arrayToVector(state->getVariablePositions(), n, msg.position);
119  arrayToVector(state->getVariableVelocities(), n, msg.velocity);
120  arrayToVector(state->getVariableEffort(), n, msg.effort);
121 
122  state_pub_.publish(msg);
123 }
void addStaticTransform(const std::string &name, const std::string &base, const std::string &target, const RobotPose &tf=RobotPose::Identity())
Add a new static transform to the broadcaster.
Definition: broadcaster.cpp:60
RobotBroadcaster(const RobotConstPtr &robot, const std::string &base_frame="world", const std::string &name="robowflex")
Constructor. Sets up TF2 broadcaster.
Definition: broadcaster.cpp:20
ros::NodeHandle nh_
Handle for publishing.
Definition: broadcaster.h:71
void stop()
Stop publishing TF and joint information.
Definition: broadcaster.cpp:55
void start()
Begin publishing TF and joint information.
Definition: broadcaster.cpp:37
ros::Publisher state_pub_
State publisher.
Definition: broadcaster.h:73
void update()
Send out the TF and joint information.
Definition: broadcaster.cpp:85
void removeStaticTransform(const std::string &name)
Remove a named static transform.
Definition: broadcaster.cpp:76
A const shared pointer wrapper for robowflex::Robot.
#define RBX_ERROR(fmt,...)
Output a error logging message.
Definition: log.h:102
Functions for loading and animating robots in Blender.
geometry_msgs::TransformStamped transformEigenToMsg(const std::string &source, const std::string &target, const RobotPose &tf)
Encode a transform as a message.
Definition: tf.cpp:262
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
Information for a static transform.
Definition: broadcaster.h:78