trajectory_point_interface.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // Copyright 2021 FZI Forschungszentrum Informatik
5 // Created on behalf of Universal Robots A/S
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // -- END LICENSE BLOCK ------------------------------------------------
19 
20 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 
31 
32 namespace urcl
33 {
34 namespace control
35 {
36 TrajectoryPointInterface::TrajectoryPointInterface(uint32_t port) : ReverseInterface(port, [](bool foo) { return foo; })
37 {
38 }
39 
40 bool TrajectoryPointInterface::writeTrajectoryPoint(const vector6d_t* positions, const float goal_time,
41  const float blend_radius, const bool cartesian)
42 {
43  if (client_fd_ == -1)
44  {
45  return false;
46  }
47  uint8_t buffer[sizeof(int32_t) * MESSAGE_LENGTH];
48  uint8_t* b_pos = buffer;
49 
50  if (positions != nullptr)
51  {
52  for (auto const& pos : *positions)
53  {
54  int32_t val = static_cast<int32_t>(pos * MULT_JOINTSTATE);
55  val = htobe32(val);
56  b_pos += append(b_pos, val);
57  }
58  }
59  else
60  {
61  b_pos += 6 * sizeof(int32_t);
62  }
63 
64  // Fill in velocity and acceleration, not used for this point type
65  b_pos += 6 * sizeof(int32_t);
66  b_pos += 6 * sizeof(int32_t);
67 
68  int32_t val = static_cast<int32_t>(goal_time * MULT_TIME);
69  val = htobe32(val);
70  b_pos += append(b_pos, val);
71 
72  val = static_cast<int32_t>(blend_radius * MULT_TIME);
73  val = htobe32(val);
74  b_pos += append(b_pos, val);
75 
76  if (cartesian)
77  {
78  val = static_cast<int32_t>(control::TrajectoryMotionType::CARTESIAN_POINT);
79  }
80  else
81  {
82  val = static_cast<int32_t>(control::TrajectoryMotionType::JOINT_POINT);
83  }
84 
85  val = htobe32(val);
86  b_pos += append(b_pos, val);
87 
88  size_t written;
89 
90  return server_.write(client_fd_, buffer, sizeof(buffer), written);
91 }
92 
94  const vector6d_t* accelerations, const float goal_time)
95 {
96  if (client_fd_ == -1)
97  {
98  return false;
99  }
100 
102 
103  // 6 positions, 6 velocities, 6 accelerations, 1 goal time, spline type, 1 point type
104  uint8_t buffer[sizeof(int32_t) * MESSAGE_LENGTH] = { 0 };
105  uint8_t* b_pos = buffer;
106  if (positions != nullptr)
107  {
108  for (auto const& pos : *positions)
109  {
110  int32_t val = static_cast<int32_t>(pos * MULT_JOINTSTATE);
111  val = htobe32(val);
112  b_pos += append(b_pos, val);
113  }
114  }
115  else
116  {
117  throw urcl::UrException("TrajectoryPointInterface::writeTrajectorySplinePoint is only getting a nullptr for "
118  "positions\n");
119  }
120 
121  if (velocities != nullptr)
122  {
124  for (auto const& vel : *velocities)
125  {
126  int32_t val = static_cast<int32_t>(vel * MULT_JOINTSTATE);
127  val = htobe32(val);
128  b_pos += append(b_pos, val);
129  }
130  }
131  else
132  {
133  throw urcl::UrException("TrajectoryPointInterface::writeTrajectorySplinePoint is only getting a nullptr for "
134  "velocities\n");
135  }
136 
137  if (accelerations != nullptr)
138  {
140  for (auto const& acc : *accelerations)
141  {
142  int32_t val = static_cast<int32_t>(acc * MULT_JOINTSTATE);
143  val = htobe32(val);
144  b_pos += append(b_pos, val);
145  }
146  }
147  else
148  {
149  b_pos += 6 * sizeof(int32_t);
150  }
151 
152  int32_t val = static_cast<int32_t>(goal_time * MULT_TIME);
153  val = htobe32(val);
154  b_pos += append(b_pos, val);
155 
156  val = static_cast<int32_t>(spline_type);
157  val = htobe32(val);
158  b_pos += append(b_pos, val);
159 
160  val = static_cast<int32_t>(control::TrajectoryMotionType::JOINT_POINT_SPLINE);
161  val = htobe32(val);
162  b_pos += append(b_pos, val);
163 
164  size_t written;
165  return server_.write(client_fd_, buffer, sizeof(buffer), written);
166 }
167 
168 void TrajectoryPointInterface::connectionCallback(const int filedescriptor)
169 {
170  if (client_fd_ < 0)
171  {
172  URCL_LOG_DEBUG("Robot connected to trajectory interface.");
173  client_fd_ = filedescriptor;
174  }
175  else
176  {
177  URCL_LOG_ERROR("Connection request to TrajectoryPointInterface received while connection already established. Only "
178  "one connection is allowed at a time. Ignoring this request.");
179  }
180 }
181 
183 {
184  URCL_LOG_DEBUG("Connection to trajectory interface dropped.", filedescriptor);
185  client_fd_ = -1;
186 }
187 
188 void TrajectoryPointInterface::messageCallback(const int filedescriptor, char* buffer, int nbytesrecv)
189 {
190  if (nbytesrecv == 4)
191  {
192  int32_t* status = reinterpret_cast<int*>(buffer);
193  URCL_LOG_DEBUG("Received message %d on TrajectoryPointInterface", be32toh(*status));
194 
196  {
197  handle_trajectory_end_(static_cast<TrajectoryResult>(be32toh(*status)));
198  }
199  else
200  {
201  URCL_LOG_DEBUG("Trajectory execution finished with result %d, but no callback was given.");
202  }
203  }
204  else
205  {
206  URCL_LOG_WARN("Received %d bytes on TrajectoryPointInterface. Expecting 4 bytes, so ignoring this message",
207  nbytesrecv);
208  }
209 }
210 } // namespace control
211 } // namespace urcl
#define URCL_LOG_ERROR(...)
Definition: log.h:26
static const int32_t MULT_JOINTSTATE
size_t append(uint8_t *buffer, T &val)
bool writeTrajectoryPoint(const vector6d_t *positions, const float goal_time, const float blend_radius, const bool cartesian)
Writes needed information to the robot to be read by the URScript program.
virtual void connectionCallback(const int filedescriptor) override
virtual void messageCallback(const int filedescriptor, char *buffer, int nbytesrecv) override
virtual void disconnectionCallback(const int filedescriptor) override
#define URCL_LOG_DEBUG(...)
Definition: log.h:23
bool write(const int fd, const uint8_t *buf, const size_t buf_len, size_t &written)
Writes to a client.
Definition: tcp_server.cpp:315
#define URCL_LOG_WARN(...)
Definition: log.h:24
std::function< void(TrajectoryResult)> handle_trajectory_end_
std::array< double, 6 > vector6d_t
Definition: types.h:30
The ReverseInterface class handles communication to the robot. It starts a server and waits for the r...
Our base class for exceptions. Specialized exceptions should inherit from those.
Definition: exceptions.h:41
bool writeTrajectorySplinePoint(const vector6d_t *positions, const vector6d_t *velocities, const vector6d_t *accelerations, const float goal_time)
Writes needed information to the robot to be read by the URScript program including velocity and acce...


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Tue Jul 4 2023 02:09:47