rtde_writer.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 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 
29 
30 namespace urcl
31 {
32 namespace rtde_interface
33 {
34 RTDEWriter::RTDEWriter(comm::URStream<RTDEPackage>* stream, const std::vector<std::string>& recipe)
35  : stream_(stream), recipe_(recipe), queue_{ 32 }, running_(false), package_(recipe_)
36 {
37 }
38 
39 void RTDEWriter::init(uint8_t recipe_id)
40 {
41  recipe_id_ = recipe_id;
43  running_ = true;
44  writer_thread_ = std::thread(&RTDEWriter::run, this);
45 }
46 
48 {
49  uint8_t buffer[4096];
50  size_t size;
51  size_t written;
52  std::unique_ptr<DataPackage> package;
53  while (running_)
54  {
55  if (queue_.waitDequeTimed(package, 1000000))
56  {
57  package->setRecipeID(recipe_id_);
58  size = package->serializePackage(buffer);
59  stream_->write(buffer, size, written);
60  }
61  }
62  URCL_LOG_DEBUG("Write thread ended.");
63 }
64 
65 bool RTDEWriter::sendSpeedSlider(double speed_slider_fraction)
66 {
67  std::lock_guard<std::mutex> guard(package_mutex_);
68  uint32_t mask = 1;
69  bool success = true;
70  success = package_.setData("speed_slider_mask", mask);
71  success = success && package_.setData("speed_slider_fraction", speed_slider_fraction);
72 
73  if (success)
74  {
75  if (!queue_.tryEnqueue(std::unique_ptr<DataPackage>(new DataPackage(package_))))
76  {
77  return false;
78  }
79  }
80  mask = 0;
81  success = package_.setData("speed_slider_mask", mask);
82  return success;
83 }
84 
85 bool RTDEWriter::sendStandardDigitalOutput(uint8_t output_pin, bool value)
86 {
87  std::lock_guard<std::mutex> guard(package_mutex_);
88  uint8_t mask = pinToMask(output_pin);
89  bool success = true;
90  uint8_t digital_output;
91  if (value)
92  {
93  digital_output = 255;
94  }
95  else
96  {
97  digital_output = 0;
98  }
99  success = package_.setData("standard_digital_output_mask", mask);
100  success = success && package_.setData("standard_digital_output", digital_output);
101 
102  if (success)
103  {
104  if (!queue_.tryEnqueue(std::unique_ptr<DataPackage>(new DataPackage(package_))))
105  {
106  return false;
107  }
108  }
109  mask = 0;
110  success = package_.setData("standard_digital_output_mask", mask);
111  return success;
112 }
113 
114 bool RTDEWriter::sendConfigurableDigitalOutput(uint8_t output_pin, bool value)
115 {
116  std::lock_guard<std::mutex> guard(package_mutex_);
117  uint8_t mask = pinToMask(output_pin);
118  bool success = true;
119  uint8_t digital_output;
120  if (value)
121  {
122  digital_output = 255;
123  }
124  else
125  {
126  digital_output = 0;
127  }
128  success = package_.setData("configurable_digital_output_mask", mask);
129  success = success && package_.setData("configurable_digital_output", digital_output);
130 
131  if (success)
132  {
133  if (!queue_.tryEnqueue(std::unique_ptr<DataPackage>(new DataPackage(package_))))
134  {
135  return false;
136  }
137  }
138  mask = 0;
139  success = package_.setData("configurable_digital_output_mask", mask);
140  return success;
141 }
142 
143 bool RTDEWriter::sendToolDigitalOutput(uint8_t output_pin, bool value)
144 {
145  std::lock_guard<std::mutex> guard(package_mutex_);
146  uint8_t mask = pinToMask(output_pin);
147  bool success = true;
148  uint8_t digital_output;
149  if (value)
150  {
151  digital_output = 255;
152  }
153  else
154  {
155  digital_output = 0;
156  }
157  success = package_.setData("tool_digital_output_mask", mask);
158  success = success && package_.setData("tool_digital_output", digital_output);
159 
160  if (success)
161  {
162  if (!queue_.tryEnqueue(std::unique_ptr<DataPackage>(new DataPackage(package_))))
163  {
164  return false;
165  }
166  }
167  mask = 0;
168  success = package_.setData("tool_digital_output_mask", mask);
169  return success;
170 }
171 
172 bool RTDEWriter::sendStandardAnalogOutput(uint8_t output_pin, double value)
173 {
174  std::lock_guard<std::mutex> guard(package_mutex_);
175  uint8_t mask = pinToMask(output_pin);
176  // default to current for now, as no functionality to choose included in set io service
177  uint8_t output_type = 0;
178  bool success = true;
179  success = package_.setData("standard_analog_output_mask", mask);
180  success = success && package_.setData("standard_analog_output_type", output_type);
181  success = success && package_.setData("standard_analog_output_0", value);
182  success = success && package_.setData("standard_analog_output_1", value);
183 
184  if (success)
185  {
186  if (!queue_.tryEnqueue(std::unique_ptr<DataPackage>(new DataPackage(package_))))
187  {
188  return false;
189  }
190  }
191  mask = 0;
192  success = package_.setData("standard_analog_output_mask", mask);
193  return success;
194 }
195 
196 uint8_t RTDEWriter::pinToMask(uint8_t pin)
197 {
198  if (pin > 7)
199  {
200  return 0;
201  }
202 
203  return 1 << pin;
204 }
205 
206 bool RTDEWriter::sendInputBitRegister(uint32_t register_id, bool value)
207 {
208  std::lock_guard<std::mutex> guard(package_mutex_);
209  std::stringstream ss;
210  ss << "input_bit_register_" << register_id;
211 
212  bool success = package_.setData(ss.str(), value);
213 
214  if (success)
215  {
216  if (!queue_.tryEnqueue(std::unique_ptr<DataPackage>(new DataPackage(package_))))
217  {
218  return false;
219  }
220  }
221  return success;
222 }
223 
224 bool RTDEWriter::sendInputIntRegister(uint32_t register_id, int32_t value)
225 {
226  std::lock_guard<std::mutex> guard(package_mutex_);
227  std::stringstream ss;
228  ss << "input_int_register_" << register_id;
229 
230  bool success = package_.setData(ss.str(), value);
231 
232  if (success)
233  {
234  if (!queue_.tryEnqueue(std::unique_ptr<DataPackage>(new DataPackage(package_))))
235  {
236  return false;
237  }
238  }
239  return success;
240 }
241 
242 bool RTDEWriter::sendInputDoubleRegister(uint32_t register_id, double value)
243 {
244  std::lock_guard<std::mutex> guard(package_mutex_);
245  std::stringstream ss;
246  ss << "input_double_register_" << register_id;
247 
248  bool success = package_.setData(ss.str(), value);
249 
250  if (success)
251  {
252  if (!queue_.tryEnqueue(std::unique_ptr<DataPackage>(new DataPackage(package_))))
253  {
254  return false;
255  }
256  }
257  return success;
258 }
259 
260 } // namespace rtde_interface
261 } // namespace urcl
bool sendStandardDigitalOutput(uint8_t output_pin, bool value)
Creates a package to request setting a new value for one of the standard digital output pins...
Definition: rtde_writer.cpp:85
void init(uint8_t recipe_id)
Starts the writer thread, which periodically clears the queue to write packages to the robot...
Definition: rtde_writer.cpp:39
bool sendInputIntRegister(uint32_t register_id, int32_t value)
Creates a package to request setting a new value for an input_int_register.
The stream is an abstraction of the TCPSocket that offers reading a full UR data package out of the s...
Definition: stream.h:42
comm::URStream< RTDEPackage > * stream_
Definition: rtde_writer.h:158
bool sendConfigurableDigitalOutput(uint8_t output_pin, bool value)
Creates a package to request setting a new value for one of the configurable digital output pins...
bool sendToolDigitalOutput(uint8_t output_pin, bool value)
Creates a package to request setting a new value for one of the tool output pins. ...
#define URCL_LOG_DEBUG(...)
Definition: log.h:34
uint8_t pinToMask(uint8_t pin)
std::vector< std::string > recipe_
Definition: rtde_writer.h:159
void run()
The writer thread loop, continually serializing and sending packages to the robot.
Definition: rtde_writer.cpp:47
bool sendStandardAnalogOutput(uint8_t output_pin, double value)
Creates a package to request setting a new value for one of the standard analog output pins...
bool sendInputBitRegister(uint32_t register_id, bool value)
Creates a package to request setting a new value for an input_bit_register.
bool sendInputDoubleRegister(uint32_t register_id, double value)
Creates a package to request setting a new value for an input_double_register.
The DataPackage class handles communication in the form of RTDE data packages both to and from the ro...
Definition: data_package.h:59
void initEmpty()
Initializes to contained list with empty values based on the recipe.
moodycamel::BlockingReaderWriterQueue< std::unique_ptr< DataPackage > > queue_
Definition: rtde_writer.h:161
bool setData(const std::string &name, T &val)
Set a data field in the DataPackage.
Definition: data_package.h:175
bool sendSpeedSlider(double speed_slider_fraction)
Creates a package to request setting a new value for the speed slider.
Definition: rtde_writer.cpp:65


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