reverse_interface.h
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 2019 FZI Forschungszentrum Informatik
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // -- END LICENSE BLOCK ------------------------------------------------
18 
19 //----------------------------------------------------------------------
26 //----------------------------------------------------------------------
27 
28 #ifndef UR_CLIENT_LIBRARY_REVERSE_INTERFACE_H_INCLUDED
29 #define UR_CLIENT_LIBRARY_REVERSE_INTERFACE_H_INCLUDED
30 
33 #include "ur_client_library/log.h"
34 #include <cstring>
35 #include <endian.h>
36 #include <condition_variable>
37 
38 namespace urcl
39 {
40 namespace comm
41 {
45 enum class ControlMode : int32_t
46 {
47  MODE_STOPPED = -2,
48  MODE_UNINITIALIZED = -1,
49  MODE_IDLE = 0,
50  MODE_SERVOJ = 1,
51  MODE_SPEEDJ = 2
52 };
53 
59 {
60 public:
61  ReverseInterface() = delete;
68  ReverseInterface(uint32_t port, std::function<void(bool)> handle_program_state)
69  : client_fd_(-1), server_(port), handle_program_state_(handle_program_state), keepalive_count_(1)
70  {
71  handle_program_state_(false);
72  server_.setMessageCallback(
73  std::bind(&ReverseInterface::messageCallback, this, std::placeholders::_1, std::placeholders::_2));
74  server_.setConnectCallback(std::bind(&ReverseInterface::connectionCallback, this, std::placeholders::_1));
75  server_.setDisconnectCallback(std::bind(&ReverseInterface::disconnectionCallback, this, std::placeholders::_1));
76  server_.setMaxClientsAllowed(1);
77  server_.start();
78  }
83  {
84  }
85 
95  bool write(const vector6d_t* positions, const ControlMode control_mode = ControlMode::MODE_IDLE)
96  {
97  if (client_fd_ == -1)
98  {
99  return false;
100  }
101  uint8_t buffer[sizeof(int32_t) * 8];
102  uint8_t* b_pos = buffer;
103 
104  // The first element is always the keepalive signal.
105  int32_t val = htobe32(keepalive_count_);
106  b_pos += append(b_pos, val);
107 
108  if (positions != nullptr)
109  {
110  for (auto const& pos : *positions)
111  {
112  int32_t val = static_cast<int32_t>(pos * MULT_JOINTSTATE);
113  val = htobe32(val);
114  b_pos += append(b_pos, val);
115  }
116  }
117  else
118  {
119  b_pos += 6 * sizeof(int32_t);
120  }
121 
122  val = htobe32(toUnderlying(control_mode));
123  b_pos += append(b_pos, val);
124 
125  size_t written;
126 
127  return server_.write(client_fd_, buffer, sizeof(buffer), written);
128  }
129 
135  void setKeepaliveCount(const uint32_t& count)
136  {
137  keepalive_count_ = count;
138  }
139 
140 private:
141  void connectionCallback(const int filedescriptor)
142  {
143  if (client_fd_ < 0)
144  {
145  URCL_LOG_INFO("Robot connected to reverse interface. Ready to receive control commands.");
146  client_fd_ = filedescriptor;
147  handle_program_state_(true);
148  }
149  else
150  {
151  URCL_LOG_ERROR("Connection request to ReverseInterface received while connection already established. Only one "
152  "connection is allowed at a time. Ignoring this request.");
153  }
154  }
155 
156  void disconnectionCallback(const int filedescriptor)
157  {
158  URCL_LOG_INFO("Connection to reverse interface dropped.", filedescriptor);
159  client_fd_ = -1;
160  handle_program_state_(false);
161  }
162 
163  void messageCallback(const int filedescriptor, char* buffer)
164  {
165  URCL_LOG_WARN("Messge on ReverseInterface received. The reverse interface currently does not support any message "
166  "handling. This message will be ignored.");
167  }
168 
171 
172  static const int32_t MULT_JOINTSTATE = 1000000;
173 
174  template <typename T>
175  size_t append(uint8_t* buffer, T& val)
176  {
177  size_t s = sizeof(T);
178  std::memcpy(buffer, &val, s);
179  return s;
180  }
181 
182  std::function<void(bool)> handle_program_state_;
184 };
185 
186 } // namespace comm
187 } // namespace urcl
188 
189 #endif // UR_CLIENT_LIBRARY_REVERSE_INTERFACE_H_INCLUDED
#define URCL_LOG_ERROR(...)
Definition: log.h:37
void setKeepaliveCount(const uint32_t &count)
Set the Keepalive count. This will set the number of allowed timeout reads on the robot...
Set when speedj control is active.
void disconnectionCallback(const int filedescriptor)
bool write(const vector6d_t *positions, const ControlMode control_mode=ControlMode::MODE_IDLE)
Writes needed information to the robot to be read by the URCaps program.
~ReverseInterface()
Disconnects possible clients so the reverse interface object can be safely destroyed.
Wrapper class for a TCP socket server.
Definition: tcp_server.h:59
static const int32_t MULT_JOINTSTATE
Definition: ur_driver.cpp:43
Set when no controller is currently active controlling the robot.
Set when servoj control is active.
size_t append(uint8_t *buffer, T &val)
void messageCallback(const int filedescriptor, char *buffer)
std::function< void(bool)> handle_program_state_
ControlMode
Control modes as interpreted from the script runnning on the robot.
When this is set, the program is expected to stop and exit.
Startup default until another mode is sent to the script.
#define URCL_LOG_WARN(...)
Definition: log.h:35
constexpr std::underlying_type< E >::type toUnderlying(const E e) noexcept
Converts an enum type to its underlying type.
Definition: types.h:58
void connectionCallback(const int filedescriptor)
The ReverseInterface class handles communication to the robot. It starts a server and waits for the r...
std::array< double, 6 > vector6d_t
Definition: types.h:30
ReverseInterface(uint32_t port, std::function< void(bool)> handle_program_state)
Creates a ReverseInterface object including a TCPServer.
#define URCL_LOG_INFO(...)
Definition: log.h:36


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Sun May 9 2021 02:16:26