producer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019, FZI Forschungszentrum Informatik (templating)
3  *
4  * Copyright 2017, 2018 Simon Rasmussen (refactor)
5  *
6  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 #include <chrono>
28 
29 namespace urcl
30 {
31 namespace comm
32 {
39 template <typename T>
40 class URProducer : public IProducer<T>
41 {
42 private:
45  std::chrono::seconds timeout_;
46 
47  bool running_;
48 
49 public:
56  URProducer(URStream<T>& stream, Parser<T>& parser) : stream_(stream), parser_(parser), timeout_(1), running_(false)
57  {
58  }
59 
67  void setupProducer(const size_t max_num_tries = 0,
68  const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10)) override
69  {
70  timeval tv;
71  tv.tv_sec = 1;
72  tv.tv_usec = 0;
73  stream_.setReceiveTimeout(tv);
74  if (!stream_.connect(max_num_tries, reconnection_time))
75  {
76  throw UrException("Failed to connect to robot. Please check if the robot is booted and connected.");
77  }
78  }
82  void teardownProducer() override
83  {
84  stopProducer();
85  }
89  void stopProducer() override
90  {
91  running_ = false;
92  }
93 
94  void startProducer() override
95  {
96  running_ = true;
97  }
98 
106  bool tryGet(std::vector<std::unique_ptr<T>>& products) override
107  {
108  // TODO This function has become really ugly! That should be refactored!
109 
110  // 4KB should be enough to hold any packet received from UR
111  uint8_t buf[4096];
112  size_t read = 0;
113  // expoential backoff reconnects
114  while (true)
115  {
116  if (stream_.read(buf, sizeof(buf), read))
117  {
118  // reset sleep amount
119  timeout_ = std::chrono::seconds(1);
120  BinParser bp(buf, read);
121  return parser_.parse(bp, products);
122  }
123 
124  if (!running_)
125  return true;
126 
127  if (stream_.closed())
128  return false;
129 
130  URCL_LOG_WARN("Failed to read from stream, reconnecting in %ld seconds...", timeout_.count());
131  std::this_thread::sleep_for(timeout_);
132 
133  if (stream_.connect())
134  continue;
135 
136  auto next = timeout_ * 2;
137  if (next <= std::chrono::seconds(120))
138  timeout_ = next;
139  }
140 
141  return false;
142  }
143 };
144 } // namespace comm
145 } // namespace urcl
pipeline.h
exceptions.h
urcl::comm::URStream
The stream is an abstraction of the TCPSocket that offers reading a full UR data package out of the s...
Definition: stream.h:40
urcl
Definition: bin_parser.h:36
urcl::comm::URProducer::setupProducer
void setupProducer(const size_t max_num_tries=0, const std::chrono::milliseconds reconnection_time=std::chrono::seconds(10)) override
Triggers the stream to connect to the robot.
Definition: producer.h:67
urcl::comm::Parser
The parser is a general paser. The namsepace rtde_interface and primary_interface both iclude classes...
Definition: parser.h:36
urcl::comm::URProducer::teardownProducer
void teardownProducer() override
Tears down the producer. Currently no special handling needed.
Definition: producer.h:82
urcl::comm::URProducer
A general producer for URPackages. Implements funcionality to produce packages by reading and parsing...
Definition: producer.h:40
urcl::comm::IProducer
Parent class for arbitrary producers of packages.
Definition: pipeline.h:206
urcl::UrException
Our base class for exceptions. Specialized exceptions should inherit from those.
Definition: exceptions.h:53
urcl::comm::URProducer::startProducer
void startProducer() override
Definition: producer.h:94
stream.h
urcl::comm::URProducer::stream_
URStream< T > & stream_
Definition: producer.h:43
urcl::comm::URProducer::timeout_
std::chrono::seconds timeout_
Definition: producer.h:45
urcl::comm::URProducer::running_
bool running_
Definition: producer.h:47
urcl::comm::URProducer::tryGet
bool tryGet(std::vector< std::unique_ptr< T >> &products) override
Attempts to read byte stream from the robot and parse it as a URPackage.
Definition: producer.h:106
parser.h
urcl::comm::URProducer::parser_
Parser< T > & parser_
Definition: producer.h:44
urcl::comm::URProducer::stopProducer
void stopProducer() override
Stops the producer. Currently no functionality needed.
Definition: producer.h:89
URCL_LOG_WARN
#define URCL_LOG_WARN(...)
Definition: log.h:24
package.h
urcl::comm::BinParser
The BinParser class handles a byte buffer and functionality to iteratively parse the content.
Definition: bin_parser.h:44
urcl::comm::URProducer::URProducer
URProducer(URStream< T > &stream, Parser< T > &parser)
Creates a URProducer object, registering a stream and a parser.
Definition: producer.h:56


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58