reverse_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 
30 
31 namespace urcl
32 {
33 namespace control
34 {
35 ReverseInterface::ReverseInterface(uint32_t port, std::function<void(bool)> handle_program_state)
36  : client_fd_(-1), server_(port), handle_program_state_(handle_program_state), keepalive_count_(1)
37 {
38  handle_program_state_(false);
39  server_.setMessageCallback(std::bind(&ReverseInterface::messageCallback, this, std::placeholders::_1,
40  std::placeholders::_2, std::placeholders::_3));
41  server_.setConnectCallback(std::bind(&ReverseInterface::connectionCallback, this, std::placeholders::_1));
42  server_.setDisconnectCallback(std::bind(&ReverseInterface::disconnectionCallback, this, std::placeholders::_1));
44  server_.start();
45 }
46 
47 bool ReverseInterface::write(const vector6d_t* positions, const comm::ControlMode control_mode)
48 {
49  const int message_length = 7;
50  if (client_fd_ == -1)
51  {
52  return false;
53  }
54  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
55  uint8_t* b_pos = buffer;
56 
57  // The first element is always the keepalive signal.
58  int32_t val = htobe32(keepalive_count_);
59  b_pos += append(b_pos, val);
60 
61  if (positions != nullptr)
62  {
63  for (auto const& pos : *positions)
64  {
65  int32_t val = static_cast<int32_t>(pos * MULT_JOINTSTATE);
66  val = htobe32(val);
67  b_pos += append(b_pos, val);
68  }
69  }
70  else
71  {
72  b_pos += 6 * sizeof(int32_t);
73  }
74 
75  // writing zeros to allow usage with other script commands
76  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH - 1; i++)
77  {
78  val = htobe32(0);
79  b_pos += append(b_pos, val);
80  }
81 
82  val = htobe32(toUnderlying(control_mode));
83  b_pos += append(b_pos, val);
84 
85  size_t written;
86 
87  return server_.write(client_fd_, buffer, sizeof(buffer), written);
88 }
89 
91  const int point_number)
92 {
93  const int message_length = 3;
94  if (client_fd_ == -1)
95  {
96  return false;
97  }
98  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
99  uint8_t* b_pos = buffer;
100 
101  // The first element is always the keepalive signal.
102  int32_t val = htobe32(keepalive_count_);
103  b_pos += append(b_pos, val);
104 
105  val = htobe32(toUnderlying(trajectory_action));
106  b_pos += append(b_pos, val);
107 
108  val = htobe32(point_number);
109  b_pos += append(b_pos, val);
110 
111  // writing zeros to allow usage with other script commands
112  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH - 1; i++)
113  {
114  val = htobe32(0);
115  b_pos += append(b_pos, val);
116  }
117 
119  b_pos += append(b_pos, val);
120 
121  size_t written;
122 
123  return server_.write(client_fd_, buffer, sizeof(buffer), written);
124 }
125 
127 {
128  const int message_length = 2;
129  if (client_fd_ == -1)
130  {
131  return false;
132  }
133  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
134  uint8_t* b_pos = buffer;
135 
136  // The first element is always the keepalive signal.
137  int32_t val = htobe32(keepalive_count_);
138  b_pos += append(b_pos, val);
139 
140  val = htobe32(toUnderlying(freedrive_action));
141  b_pos += append(b_pos, val);
142 
143  // writing zeros to allow usage with other script commands
144  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH - 1; i++)
145  {
146  val = htobe32(0);
147  b_pos += append(b_pos, val);
148  }
149 
151  b_pos += append(b_pos, val);
152 
153  size_t written;
154 
155  return server_.write(client_fd_, buffer, sizeof(buffer), written);
156 }
157 
158 void ReverseInterface::connectionCallback(const int filedescriptor)
159 {
160  if (client_fd_ < 0)
161  {
162  URCL_LOG_INFO("Robot connected to reverse interface. Ready to receive control commands.");
163  client_fd_ = filedescriptor;
164  handle_program_state_(true);
165  }
166  else
167  {
168  URCL_LOG_ERROR("Connection request to ReverseInterface received while connection already established. Only one "
169  "connection is allowed at a time. Ignoring this request.");
170  }
171 }
172 
173 void ReverseInterface::disconnectionCallback(const int filedescriptor)
174 {
175  URCL_LOG_INFO("Connection to reverse interface dropped.", filedescriptor);
176  client_fd_ = -1;
177  handle_program_state_(false);
178 }
179 
180 void ReverseInterface::messageCallback(const int filedescriptor, char* buffer, int nbytesrecv)
181 {
182  URCL_LOG_WARN("Message on ReverseInterface received. The reverse interface currently does not support any message "
183  "handling. This message will be ignored.");
184 }
185 
186 } // namespace control
187 } // namespace urcl
void setDisconnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients disconnecting from the server.
Definition: tcp_server.h:83
#define URCL_LOG_ERROR(...)
Definition: log.h:26
void setMaxClientsAllowed(const uint32_t &max_clients_allowed)
Set the maximum number of clients allowed to connect to this server.
Definition: tcp_server.h:143
static const int32_t MULT_JOINTSTATE
bool writeFreedriveControlMessage(const FreedriveControlMessage freedrive_action)
Writes needed information to the robot to be read by the URScript program.
void setConnectCallback(std::function< void(const int)> func)
This callback will be triggered on clients connecting to the server.
Definition: tcp_server.h:72
Set when trajectory forwarding is active.
virtual void connectionCallback(const int filedescriptor)
FreedriveControlMessage
Control messages for starting and stopping freedrive mode.
size_t append(uint8_t *buffer, T &val)
bool writeTrajectoryControlMessage(const TrajectoryControlMessage trajectory_action, const int point_number=0)
Writes needed information to the robot to be read by the URScript program.
virtual bool write(const vector6d_t *positions, const comm::ControlMode control_mode=comm::ControlMode::MODE_IDLE)
Writes needed information to the robot to be read by the URCaps program.
void start()
Start event handling.
Definition: tcp_server.cpp:308
virtual void messageCallback(const int filedescriptor, char *buffer, int nbytesrecv)
Set when freedrive mode is active.
virtual void disconnectionCallback(const int filedescriptor)
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
void setMessageCallback(std::function< void(const int, char *, int)> func)
This callback will be triggered on messages received on the socket.
Definition: tcp_server.h:94
ControlMode
Control modes as interpreted from the script runnning on the robot.
Definition: control_mode.h:39
#define URCL_LOG_WARN(...)
Definition: log.h:24
constexpr std::underlying_type< E >::type toUnderlying(const E e) noexcept
Converts an enum type to its underlying type.
Definition: types.h:58
TrajectoryControlMessage
Control messages for forwarding and aborting trajectories.
std::function< void(bool)> handle_program_state_
std::array< double, 6 > vector6d_t
Definition: types.h:30
#define URCL_LOG_INFO(...)
Definition: log.h:25


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