script_command_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 ScriptCommandInterface::ScriptCommandInterface(uint32_t port) : ReverseInterface(port, [](bool foo) { return foo; })
36 {
37  client_connected_ = false;
38 }
39 
41 {
42  const int message_length = 1;
43  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
44  uint8_t* b_pos = buffer;
45  int32_t val = htobe32(toUnderlying(ScriptCommand::ZERO_FTSENSOR));
46  b_pos += append(b_pos, val);
47 
48  // writing zeros to allow usage with other script commands
49  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
50  {
51  val = htobe32(0);
52  b_pos += append(b_pos, val);
53  }
54  size_t written;
55 
56  return server_.write(client_fd_, buffer, sizeof(buffer), written);
57 }
58 
59 bool ScriptCommandInterface::setPayload(const double mass, const vector3d_t* cog)
60 {
61  const int message_length = 5;
62  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
63  uint8_t* b_pos = buffer;
64  int32_t val = htobe32(toUnderlying(ScriptCommand::SET_PAYLOAD));
65  b_pos += append(b_pos, val);
66 
67  val = htobe32(static_cast<int32_t>(mass * MULT_JOINTSTATE));
68  b_pos += append(b_pos, val);
69 
70  for (auto const& center_of_mass : *cog)
71  {
72  val = htobe32(static_cast<int32_t>(center_of_mass * MULT_JOINTSTATE));
73  b_pos += append(b_pos, val);
74  }
75 
76  // writing zeros to allow usage with other script commands
77  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
78  {
79  val = htobe32(0);
80  b_pos += append(b_pos, val);
81  }
82  size_t written;
83 
84  return server_.write(client_fd_, buffer, sizeof(buffer), written);
85 }
86 
88 {
89  const int message_length = 2;
90  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
91  uint8_t* b_pos = buffer;
92  int32_t val = htobe32(toUnderlying(ScriptCommand::SET_TOOL_VOLTAGE));
93  b_pos += append(b_pos, val);
94 
95  val = htobe32(toUnderlying(voltage) * MULT_JOINTSTATE);
96  b_pos += append(b_pos, val);
97 
98  // writing zeros to allow usage with other script commands
99  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
100  {
101  val = htobe32(0);
102  b_pos += append(b_pos, val);
103  }
104  size_t written;
105 
106  return server_.write(client_fd_, buffer, sizeof(buffer), written);
107 }
108 
109 bool ScriptCommandInterface::startForceMode(const vector6d_t* task_frame, const vector6uint32_t* selection_vector,
110  const vector6d_t* wrench, const unsigned int type, const vector6d_t* limits)
111 {
112  const int message_length = 26;
113  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
114  uint8_t* b_pos = buffer;
115 
116  int32_t val = htobe32(toUnderlying(ScriptCommand::START_FORCE_MODE));
117  b_pos += append(b_pos, val);
118 
119  for (auto const& frame : *task_frame)
120  {
121  val = htobe32(static_cast<int32_t>(frame * MULT_JOINTSTATE));
122  b_pos += append(b_pos, val);
123  }
124 
125  for (auto const& selection : *selection_vector)
126  {
127  val = htobe32(static_cast<int32_t>(selection * MULT_JOINTSTATE));
128  b_pos += append(b_pos, val);
129  }
130 
131  for (auto const& force_torque : *wrench)
132  {
133  val = htobe32(static_cast<int32_t>(force_torque * MULT_JOINTSTATE));
134  b_pos += append(b_pos, val);
135  }
136 
137  val = htobe32(static_cast<int32_t>(type * MULT_JOINTSTATE));
138  b_pos += append(b_pos, val);
139 
140  for (auto const& lim : *limits)
141  {
142  val = htobe32(static_cast<int32_t>(lim * MULT_JOINTSTATE));
143  b_pos += append(b_pos, val);
144  }
145 
146  // writing zeros to allow usage with other script commands
147  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
148  {
149  val = htobe32(0);
150  b_pos += append(b_pos, val);
151  }
152  size_t written;
153 
154  return server_.write(client_fd_, buffer, sizeof(buffer), written);
155 }
156 
158 {
159  const int message_length = 1;
160  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
161  uint8_t* b_pos = buffer;
162 
163  int32_t val = htobe32(toUnderlying(ScriptCommand::END_FORCE_MODE));
164  b_pos += append(b_pos, val);
165 
166  // writing zeros to allow usage with other script commands
167  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
168  {
169  val = htobe32(0);
170  b_pos += append(b_pos, val);
171  }
172  size_t written;
173 
174  return server_.write(client_fd_, buffer, sizeof(buffer), written);
175 }
176 
178 {
179  const int message_length = 1;
180  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
181  uint8_t* b_pos = buffer;
182 
183  int32_t val = htobe32(toUnderlying(ScriptCommand::START_TOOL_CONTACT));
184  b_pos += append(b_pos, val);
185 
186  // writing zeros to allow usage with other script commands
187  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
188  {
189  val = htobe32(0);
190  b_pos += append(b_pos, val);
191  }
192  size_t written;
193 
194  return server_.write(client_fd_, buffer, sizeof(buffer), written);
195 }
196 
198 {
199  const int message_length = 1;
200  uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
201  uint8_t* b_pos = buffer;
202 
203  int32_t val = htobe32(toUnderlying(ScriptCommand::END_TOOL_CONTACT));
204  b_pos += append(b_pos, val);
205 
206  // writing zeros to allow usage with other script commands
207  for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
208  {
209  val = htobe32(0);
210  b_pos += append(b_pos, val);
211  }
212  size_t written;
213 
214  return server_.write(client_fd_, buffer, sizeof(buffer), written);
215 }
216 
218 {
219  return client_connected_;
220 }
221 
222 void ScriptCommandInterface::connectionCallback(const int filedescriptor)
223 {
224  if (client_fd_ < 0)
225  {
226  URCL_LOG_DEBUG("Robot connected to ScriptCommandInterface.");
227  client_fd_ = filedescriptor;
228  client_connected_ = true;
229  }
230  else
231  {
232  URCL_LOG_ERROR("Connection request to ScriptCommandInterface received while connection already established. Only "
233  "one connection is allowed at a time. Ignoring this request.");
234  }
235 }
236 
237 void ScriptCommandInterface::disconnectionCallback(const int filedescriptor)
238 {
239  URCL_LOG_DEBUG("Connection to ScriptCommandInterface dropped.", filedescriptor);
240  client_fd_ = -1;
241  client_connected_ = false;
242 }
243 
244 void ScriptCommandInterface::messageCallback(const int filedescriptor, char* buffer, int nbytesrecv)
245 {
246  if (nbytesrecv == 4)
247  {
248  int32_t* status = reinterpret_cast<int*>(buffer);
249  URCL_LOG_DEBUG("Received message %d on Script command interface", be32toh(*status));
250 
252  {
253  handle_tool_contact_result_(static_cast<ToolContactResult>(be32toh(*status)));
254  }
255  else
256  {
257  URCL_LOG_DEBUG("Tool contact execution finished with result %d, but no callback was given.", be32toh(*status));
258  }
259  }
260  else
261  {
262  URCL_LOG_WARN("Received %d bytes on script command interface. Expecting 4 bytes, so ignoring this message",
263  nbytesrecv);
264  }
265 }
266 
267 } // namespace control
268 } // namespace urcl
bool startToolContact()
This will make the robot look for tool contact in the tcp directions that the robot is currently movi...
virtual void messageCallback(const int filedescriptor, char *buffer, int nbytesrecv) override
#define URCL_LOG_ERROR(...)
Definition: log.h:26
ToolVoltage
Possible values for the tool voltage.
static const int32_t MULT_JOINTSTATE
bool setPayload(const double mass, const vector3d_t *cog)
Set the active payload mass and center of gravity.
size_t append(uint8_t *buffer, T &val)
bool endForceMode()
Stop force mode and put the robot into normal operation mode.
bool setToolVoltage(const ToolVoltage voltage)
Set the tool voltage.
std::array< double, 3 > vector3d_t
Definition: types.h:29
virtual void disconnectionCallback(const int filedescriptor) override
bool clientConnected()
Returns whether a client/robot is connected to this server.
#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
bool endToolContact()
This will stop the robot from looking for a tool contact, it will also enable sending move commands t...
constexpr std::underlying_type< E >::type toUnderlying(const E e) noexcept
Converts an enum type to its underlying type.
Definition: types.h:58
virtual void connectionCallback(const int filedescriptor) override
std::array< uint32_t, 6 > vector6uint32_t
Definition: types.h:32
std::array< double, 6 > vector6d_t
Definition: types.h:30
bool zeroFTSensor()
Zero the force torque sensor.
bool startForceMode(const vector6d_t *task_frame, const vector6uint32_t *selection_vector, const vector6d_t *wrench, const unsigned int type, const vector6d_t *limits)
Set robot to be controlled in force mode.
The ReverseInterface class handles communication to the robot. It starts a server and waits for the r...
std::function< void(ToolContactResult)> handle_tool_contact_result_


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