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 
63  void setupProducer() override
64  {
65  timeval tv;
66  tv.tv_sec = 1;
67  tv.tv_usec = 0;
68  stream_.setReceiveTimeout(tv);
69  if (!stream_.connect())
70  {
71  throw UrException("Failed to connect to robot. Please check if the robot is booted and connected.");
72  }
73  }
77  void teardownProducer() override
78  {
79  stopProducer();
80  }
84  void stopProducer() override
85  {
86  running_ = false;
87  }
88 
89  void startProducer() override
90  {
91  running_ = true;
92  }
93 
101  bool tryGet(std::vector<std::unique_ptr<T>>& products) override
102  {
103  // TODO This function has become really ugly! That should be refactored!
104 
105  // 4KB should be enough to hold any packet received from UR
106  uint8_t buf[4096];
107  size_t read = 0;
108  // expoential backoff reconnects
109  while (true)
110  {
111  if (stream_.read(buf, sizeof(buf), read))
112  {
113  // reset sleep amount
114  timeout_ = std::chrono::seconds(1);
115  BinParser bp(buf, read);
116  return parser_.parse(bp, products);
117  }
118 
119  if (!running_)
120  return true;
121 
122  if (stream_.closed())
123  return false;
124 
125  URCL_LOG_WARN("Failed to read from stream, reconnecting in %ld seconds...", timeout_.count());
126  std::this_thread::sleep_for(timeout_);
127 
128  if (stream_.connect())
129  continue;
130 
131  auto next = timeout_ * 2;
132  if (next <= std::chrono::seconds(120))
133  timeout_ = next;
134  }
135 
136  return false;
137  }
138 };
139 } // namespace comm
140 } // namespace urcl
void teardownProducer() override
Tears down the producer. Currently no special handling needed.
Definition: producer.h:77
std::chrono::seconds timeout_
Definition: producer.h:45
bool read(uint8_t *buf, const size_t buf_len, size_t &read)
Reads a full UR package out of a socket. For this, it looks into the package and reads the byte lengt...
Definition: stream.h:127
void setupProducer() override
Triggers the stream to connect to the robot.
Definition: producer.h:63
URProducer(URStream< T > &stream, Parser< T > &parser)
Creates a URProducer object, registering a stream and a parser.
Definition: producer.h:56
The BinParser class handles a byte buffer and functionality to iteratively parse the content...
Definition: bin_parser.h:44
virtual bool parse(BinParser &bp, std::vector< std::unique_ptr< T >> &results)=0
Declares the parse function.
The stream is an abstraction of the TCPSocket that offers reading a full UR data package out of the s...
Definition: stream.h:42
bool closed()
Returns whether the underlying socket is currently closed.
Definition: stream.h:78
The parser is a general paser. The namsepace rtde_interface and primary_interface both iclude classes...
Definition: parser.h:36
bool connect()
Connects to the configured socket.
Definition: stream.h:61
URStream< T > & stream_
Definition: producer.h:43
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:101
void startProducer() override
Definition: producer.h:89
void stopProducer() override
Stops the producer. Currently no functionality needed.
Definition: producer.h:84
#define URCL_LOG_WARN(...)
Definition: log.h:35
Parent class for arbitrary producers of packages.
Definition: pipeline.h:169
Parser< T > & parser_
Definition: producer.h:44
void setReceiveTimeout(const timeval &timeout)
Setup Receive timeout used for this socket.
Definition: tcp_socket.cpp:197
A general producer for URPackages. Implements funcionality to produce packages by reading and parsing...
Definition: producer.h:40
Our base class for exceptions. Specialized exceptions should inherit from those.
Definition: exceptions.h:40


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