joint_trajectory_streamer.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Southwest Research Institute
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Southwest Research Institute, nor the names
16  * of its contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
33 
35 
37 {
38 namespace joint_trajectory_streamer
39 {
40 
41 bool JointTrajectoryStreamer::init(SmplMsgConnection* connection, const std::vector<std::string> &joint_names,
42  const std::map<std::string, double> &velocity_limits)
43 {
44  bool rtn = true;
45 
46  ROS_INFO("JointTrajectoryStreamer: init");
47 
48  rtn &= JointTrajectoryInterface::init(connection, joint_names, velocity_limits);
49 
50  this->mutex_.lock();
51  this->current_point_ = 0;
53  this->streaming_thread_ =
54  new boost::thread(boost::bind(&JointTrajectoryStreamer::streamingThread, this));
55  ROS_INFO("Unlocking mutex");
56  this->mutex_.unlock();
57 
58  return rtn;
59 }
60 
62 {
63  delete this->streaming_thread_;
64 }
65 
66 void JointTrajectoryStreamer::jointTrajectoryCB(const trajectory_msgs::JointTrajectoryConstPtr &msg)
67 {
68  ROS_INFO("Receiving joint trajectory message");
69 
70  // read current state value (should be atomic)
71  int state = this->state_;
72 
73  ROS_DEBUG("Current state is: %d", state);
74  if (TransferStates::IDLE != state)
75  {
76  if (msg->points.empty())
77  ROS_INFO("Empty trajectory received, canceling current trajectory");
78  else
79  ROS_ERROR("Trajectory splicing not yet implemented, stopping current motion.");
80 
81  this->mutex_.lock();
83  this->mutex_.unlock();
84  return;
85  }
86 
87  if (msg->points.empty())
88  {
89  ROS_INFO("Empty trajectory received while in IDLE state, nothing is done");
90  return;
91  }
92 
93  // calc new trajectory
94  std::vector<JointTrajPtMessage> new_traj_msgs;
95  if (!trajectory_to_msgs(msg, &new_traj_msgs))
96  return;
97 
98  // send command messages to robot
99  send_to_robot(new_traj_msgs);
100 }
101 
102 bool JointTrajectoryStreamer::send_to_robot(const std::vector<JointTrajPtMessage>& messages)
103 {
104  ROS_INFO("Loading trajectory, setting state to streaming");
105  this->mutex_.lock();
106  {
107  ROS_INFO("Executing trajectory of size: %d", (int)messages.size());
108  this->current_traj_ = messages;
109  this->current_point_ = 0;
112  }
113  this->mutex_.unlock();
114 
115  return true;
116 }
117 
118 bool JointTrajectoryStreamer::trajectory_to_msgs(const trajectory_msgs::JointTrajectoryConstPtr &traj, std::vector<JointTrajPtMessage>* msgs)
119 {
120  // use base function to transform points
122  return false;
123 
124  // pad trajectory as required for minimum streaming buffer size
125  if (!msgs->empty() && (msgs->size() < (size_t)min_buffer_size_))
126  {
127  ROS_DEBUG("Padding trajectory: current(%d) => minimum(%d)", (int)msgs->size(), min_buffer_size_);
128  while (msgs->size() < (size_t)min_buffer_size_)
129  msgs->push_back(msgs->back());
130  }
131 
132  return true;
133 }
134 
135 
137 {
138  JointTrajPtMessage jtpMsg;
139  int connectRetryCount = 1;
140 
141  ROS_INFO("Starting joint trajectory streamer thread");
142  while (ros::ok())
143  {
144  ros::Duration(0.005).sleep();
145 
146  // automatically re-establish connection, if required
147  if (connectRetryCount-- > 0)
148  {
149  ROS_INFO("Connecting to robot motion server");
150  this->connection_->makeConnect();
151  ros::Duration(0.250).sleep(); // wait for connection
152 
153  if (this->connection_->isConnected())
154  connectRetryCount = 0;
155  else if (connectRetryCount <= 0)
156  {
157  ROS_ERROR("Timeout connecting to robot controller. Send new motion command to retry.");
159  }
160  continue;
161  }
162 
163  this->mutex_.lock();
164 
165  SimpleMessage msg, reply;
166 
167  switch (this->state_)
168  {
170  ros::Duration(0.010).sleep(); // loop while waiting for new trajectory
171  break;
172 
174  if (this->current_point_ >= (int)this->current_traj_.size())
175  {
176  ROS_INFO("Trajectory streaming complete, setting state to IDLE");
178  break;
179  }
180 
181  if (!this->connection_->isConnected())
182  {
183  ROS_DEBUG("Robot disconnected. Attempting reconnect...");
184  connectRetryCount = 5;
185  break;
186  }
187 
188  jtpMsg = this->current_traj_[this->current_point_];
189  jtpMsg.toRequest(msg);
190 
191  ROS_DEBUG("Sending joint trajectory point");
192  if (this->connection_->sendAndReceiveMsg(msg, reply, false))
193  {
194  this->current_point_++;
195  ROS_INFO("Point[%d of %d] sent to controller",
196  this->current_point_, (int)this->current_traj_.size());
197  }
198  else
199  ROS_WARN("Failed sent joint point, will try again");
200 
201  break;
202  default:
203  ROS_ERROR("Joint trajectory streamer: unknown state");
205  break;
206  }
207 
208  this->mutex_.unlock();
209  }
210 
211  ROS_WARN("Exiting trajectory streamer thread");
212 }
213 
215 {
217 
218  ROS_DEBUG("Stop command sent, entering idle mode");
220 }
221 
222 } //joint_trajectory_streamer
223 } //industrial_robot_client
224 
bool sleep() const
#define ROS_WARN(...)
virtual bool toRequest(industrial::simple_message::SimpleMessage &msg)
bool sendAndReceiveMsg(industrial::simple_message::SimpleMessage &send, industrial::simple_message::SimpleMessage &recv, bool verbose=false)
bool send_to_robot(const std::vector< JointTrajPtMessage > &messages)
Send trajectory to robot, using this node&#39;s robot-connection. Specific method must be implemented in ...
#define ROS_INFO(...)
virtual bool init(std::string default_ip="", int default_port=StandardSocketPorts::MOTION)
Initialize robot connection using default method.
ROSCPP_DECL bool ok()
virtual bool init(SmplMsgConnection *connection, const std::vector< std::string > &joint_names, const std::map< std::string, double > &velocity_limits=std::map< std::string, double >())
Class initializer.
virtual void jointTrajectoryCB(const trajectory_msgs::JointTrajectoryConstPtr &msg)
Callback function registered to ROS topic-subscribe. Transform message into SimpleMessage objects and...
static Time now()
#define ROS_ERROR(...)
virtual bool trajectory_to_msgs(const trajectory_msgs::JointTrajectoryConstPtr &traj, std::vector< JointTrajPtMessage > *msgs)
Convert ROS trajectory message into stream of JointTrajPtMessages for sending to robot. Also includes various joint transforms that can be overridden for robot-specific behavior.
virtual bool trajectory_to_msgs(const trajectory_msgs::JointTrajectoryConstPtr &traj, std::vector< JointTrajPtMessage > *msgs)
Convert ROS trajectory message into stream of JointTrajPtMessages for sending to robot. Also includes various joint transforms that can be overridden for robot-specific behavior.
#define ROS_DEBUG(...)


industrial_robot_client
Author(s): Jeremy Zoss
autogenerated on Sat Sep 21 2019 03:30:13