mirror of
https://gitlab.com/obbart/universal_robots_ros_driver.git
synced 2026-04-10 10:00:48 +02:00
Major refactor
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <chrono>
|
||||
#include "ur_modern_driver/pipeline.h"
|
||||
#include "ur_modern_driver/ur/parser.h"
|
||||
#include "ur_modern_driver/ur/stream.h"
|
||||
@@ -9,9 +10,10 @@ class URProducer : public IProducer<T>
|
||||
private:
|
||||
URStream& stream_;
|
||||
URParser<T>& parser_;
|
||||
std::chrono::seconds timeout_;
|
||||
|
||||
public:
|
||||
URProducer(URStream& stream, URParser<T>& parser) : stream_(stream), parser_(parser)
|
||||
URProducer(URStream& stream, URParser<T>& parser) : stream_(stream), parser_(parser), timeout_(1)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -32,24 +34,29 @@ public:
|
||||
{
|
||||
// 4KB should be enough to hold any packet received from UR
|
||||
uint8_t buf[4096];
|
||||
|
||||
// blocking call
|
||||
ssize_t len = stream_.receive(buf, sizeof(buf));
|
||||
|
||||
// LOG_DEBUG("Read %d bytes from stream", len);
|
||||
|
||||
if (len == 0)
|
||||
size_t read = 0;
|
||||
//expoential backoff reconnects
|
||||
while(true)
|
||||
{
|
||||
LOG_WARN("Read nothing from stream");
|
||||
return false;
|
||||
}
|
||||
else if (len < 0)
|
||||
{
|
||||
LOG_WARN("Stream closed");
|
||||
return false;
|
||||
if(stream_.read(buf, sizeof(buf), read))
|
||||
{
|
||||
//reset sleep amount
|
||||
timeout_ = std::chrono::seconds(1);
|
||||
break;
|
||||
}
|
||||
|
||||
if(stream_.closed())
|
||||
return false;
|
||||
|
||||
LOG_WARN("Failed to read from stream, reconnecting in %ld seconds...", timeout_.count());
|
||||
std::this_thread::sleep_for(timeout_);
|
||||
auto next = timeout_ * 2;
|
||||
if(next <= std::chrono::seconds(120))
|
||||
timeout_ = next;
|
||||
}
|
||||
|
||||
BinParser bp(buf, static_cast<size_t>(len));
|
||||
|
||||
BinParser bp(buf, read);
|
||||
return parser_.parse(bp, products);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user