Skip to content

Commit 870d314

Browse files
author
Felix Exner
authored
Merge pull request #166 from UniversalRobots/packaget
Use the package type and not the header type as template parameter for communication
2 parents ad3cb7c + 248babc commit 870d314

33 files changed

+275
-312
lines changed

ur_calibration/include/ur_calibration/calibration_consumer.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@
3535

3636
namespace ur_calibration
3737
{
38-
class CalibrationConsumer
39-
: public ur_driver::comm::IConsumer<ur_driver::comm::URPackage<ur_driver::primary_interface::PackageHeader>>
38+
class CalibrationConsumer : public ur_driver::comm::IConsumer<ur_driver::primary_interface::PrimaryPackage>
4039
{
4140
public:
4241
CalibrationConsumer();
4342
virtual ~CalibrationConsumer() = default;
4443

45-
virtual bool
46-
consume(std::shared_ptr<ur_driver::comm::URPackage<ur_driver::primary_interface::PackageHeader>> product);
44+
virtual bool consume(std::shared_ptr<ur_driver::primary_interface::PrimaryPackage> product);
4745

4846
bool isCalibrated() const
4947
{

ur_calibration/src/calibration_consumer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ CalibrationConsumer::CalibrationConsumer() : calibrated_(false)
3333
{
3434
}
3535

36-
bool CalibrationConsumer::consume(
37-
std::shared_ptr<ur_driver::comm::URPackage<ur_driver::primary_interface::PackageHeader>> product)
36+
bool CalibrationConsumer::consume(std::shared_ptr<ur_driver::primary_interface::PrimaryPackage> product)
3837
{
3938
auto kin_info = std::dynamic_pointer_cast<ur_driver::primary_interface::KinematicsInfo>(product);
4039
if (kin_info != nullptr)

ur_calibration/src/calibration_correction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ class CalibrationCorrection
8080

8181
void run()
8282
{
83-
comm::URStream<PackageHeader> stream(robot_ip_, UR_PRIMARY_PORT);
83+
comm::URStream<PrimaryPackage> stream(robot_ip_, UR_PRIMARY_PORT);
8484
primary_interface::PrimaryParser parser;
85-
comm::URProducer<PackageHeader> prod(stream, parser);
85+
comm::URProducer<PrimaryPackage> prod(stream, parser);
8686
CalibrationConsumer consumer;
8787

8888
comm::INotifier notifier;
8989

90-
comm::Pipeline<PackageHeader> pipeline(prod, consumer, "Pipeline", notifier);
90+
comm::Pipeline<PrimaryPackage> pipeline(prod, &consumer, "Pipeline", notifier);
9191
pipeline.run();
9292
while (!consumer.isCalibrated())
9393
{

ur_robot_driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ add_library(ur_robot_driver
8989
#src/ur/robot_mode.cpp
9090
src/primary/primary_package.cpp
9191
src/primary/robot_message.cpp
92+
src/primary/robot_state.cpp
9293
src/primary/robot_message/version_message.cpp
9394
src/primary/robot_state/kinematics_info.cpp
9495
src/rtde/control_package_pause.cpp

ur_robot_driver/include/ur_robot_driver/comm/package.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class URPackage
6666
*/
6767
virtual std::string toString() const = 0;
6868

69+
using HeaderType = HeaderT;
70+
6971
private:
7072
HeaderT header_;
7173
};

ur_robot_driver/include/ur_robot_driver/comm/parser.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace comm
3232
* iclude classes which inherit from it (rtdeParser and primaryParser).
3333
* The parser functionality also embodies a factory function taking in an uint8.
3434
*/
35-
template <typename HeaderT>
35+
template <typename T>
3636
class Parser
3737

3838
{
@@ -46,11 +46,10 @@ class Parser
4646
* \param bp Instance of class binaryParser
4747
* \param results A unique pointer
4848
*/
49-
virtual bool parse(BinParser& bp, std::vector<std::unique_ptr<URPackage<HeaderT>>>& results) = 0;
50-
using _header_type = HeaderT;
49+
virtual bool parse(BinParser& bp, std::vector<std::unique_ptr<T>>& results) = 0;
5150

5251
private:
53-
HeaderT header_;
52+
typename T::HeaderType header_;
5453
// URProducer producer_;
5554
};
5655

ur_robot_driver/include/ur_robot_driver/comm/pipeline.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ class MultiConsumer : public IConsumer<T>
163163
/*!
164164
* \brief Parent class for arbitrary producers of packages.
165165
*
166-
* @tparam HeaderT Header type of the produced packages
166+
* @tparam T Type of the produced products
167167
*/
168-
template <typename HeaderT>
168+
template <typename T>
169169
class IProducer
170170
{
171171
public:
@@ -200,7 +200,7 @@ class IProducer
200200
*
201201
* \returns Success of the package production.
202202
*/
203-
virtual bool tryGet(std::vector<std::unique_ptr<URPackage<HeaderT>>>& products) = 0;
203+
virtual bool tryGet(std::vector<std::unique_ptr<T>>& products) = 0;
204204
};
205205

206206
/*!
@@ -228,15 +228,14 @@ class INotifier
228228
* the producer is called and returned packages are saved in a queue. This queue is then either also
229229
* cyclically utilized by the registered consumer or can be externally used.
230230
*
231-
* @tparam HeaderT Header type of the managed packages
231+
* @tparam T Type of the managed packages
232232
*/
233-
template <typename HeaderT>
233+
template <typename T>
234234
class Pipeline
235235
{
236236
public:
237237
typedef std::chrono::high_resolution_clock Clock;
238238
typedef Clock::time_point Time;
239-
using _package_type = URPackage<HeaderT>;
240239
/*!
241240
* \brief Creates a new Pipeline object, registering producer, consumer and notifier.
242241
* Additionally, an empty queue is initialized.
@@ -246,8 +245,8 @@ class Pipeline
246245
* \param name The pipeline's name
247246
* \param notifier The notifier to use
248247
*/
249-
Pipeline(IProducer<HeaderT>& producer, IConsumer<_package_type>& consumer, std::string name, INotifier& notifier)
250-
: producer_(producer), consumer_(&consumer), name_(name), notifier_(notifier), queue_{ 32 }, running_{ false }
248+
Pipeline(IProducer<T>& producer, IConsumer<T>* consumer, std::string name, INotifier& notifier)
249+
: producer_(producer), consumer_(consumer), name_(name), notifier_(notifier), queue_{ 32 }, running_{ false }
251250
{
252251
}
253252
/*!
@@ -258,7 +257,7 @@ class Pipeline
258257
* \param name The pipeline's name
259258
* \param notifier The notifier to use
260259
*/
261-
Pipeline(IProducer<HeaderT>& producer, std::string name, INotifier& notifier)
260+
Pipeline(IProducer<T>& producer, std::string name, INotifier& notifier)
262261
: producer_(producer), consumer_(nullptr), name_(name), notifier_(notifier), queue_{ 32 }, running_{ false }
263262
{
264263
}
@@ -327,17 +326,17 @@ class Pipeline
327326
*
328327
* \returns
329328
*/
330-
bool getLatestProduct(std::unique_ptr<URPackage<HeaderT>>& product, std::chrono::milliseconds timeout)
329+
bool getLatestProduct(std::unique_ptr<T>& product, std::chrono::milliseconds timeout)
331330
{
332331
return queue_.waitDequeTimed(product, timeout);
333332
}
334333

335334
private:
336-
IProducer<HeaderT>& producer_;
337-
IConsumer<_package_type>* consumer_;
335+
IProducer<T>& producer_;
336+
IConsumer<T>* consumer_;
338337
std::string name_;
339338
INotifier& notifier_;
340-
moodycamel::BlockingReaderWriterQueue<std::unique_ptr<_package_type>> queue_;
339+
moodycamel::BlockingReaderWriterQueue<std::unique_ptr<T>> queue_;
341340
std::atomic<bool> running_;
342341
std::thread pThread_, cThread_;
343342

@@ -396,7 +395,7 @@ class Pipeline
396395
{
397396
LOG_WARN("No realtime capabilities found. Consider using a realtime system for better performance");
398397
}
399-
std::vector<std::unique_ptr<_package_type>> products;
398+
std::vector<std::unique_ptr<T>> products;
400399
while (running_)
401400
{
402401
if (!producer_.tryGet(products))
@@ -422,7 +421,7 @@ class Pipeline
422421

423422
void runConsumer()
424423
{
425-
std::unique_ptr<_package_type> product;
424+
std::unique_ptr<T> product;
426425
while (running_)
427426
{
428427
// timeout was chosen because we should receive messages

ur_robot_driver/include/ur_robot_driver/comm/producer.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ namespace comm
3636
*
3737
* @tparam HeaderT Header type of packages to produce.
3838
*/
39-
template <typename HeaderT>
40-
class URProducer : public IProducer<HeaderT>
39+
template <typename T>
40+
class URProducer : public IProducer<T>
4141
{
4242
private:
43-
URStream<HeaderT>& stream_;
44-
Parser<HeaderT>& parser_;
43+
URStream<T>& stream_;
44+
Parser<T>& parser_;
4545
std::chrono::seconds timeout_;
4646

4747
bool running_;
@@ -53,8 +53,7 @@ class URProducer : public IProducer<HeaderT>
5353
* \param stream The stream to read from
5454
* \param parser The parser to use to interpret received byte information
5555
*/
56-
URProducer(URStream<HeaderT>& stream, Parser<HeaderT>& parser)
57-
: stream_(stream), parser_(parser), timeout_(1), running_(false)
56+
URProducer(URStream<T>& stream, Parser<T>& parser) : stream_(stream), parser_(parser), timeout_(1), running_(false)
5857
{
5958
}
6059

@@ -99,7 +98,7 @@ class URProducer : public IProducer<HeaderT>
9998
*
10099
* \returns Success of reading and parsing the package
101100
*/
102-
bool tryGet(std::vector<std::unique_ptr<URPackage<HeaderT>>>& products) override
101+
bool tryGet(std::vector<std::unique_ptr<T>>& products) override
103102
{
104103
// TODO This function has become really ugly! That should be refactored!
105104

ur_robot_driver/include/ur_robot_driver/comm/shell_consumer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ namespace comm
4242
*
4343
* @tparam HeaderT Header type of the packages to consume
4444
*/
45-
template <typename HeaderT>
46-
class ShellConsumer : public IConsumer<URPackage<HeaderT>>
45+
template <typename T>
46+
class ShellConsumer : public IConsumer<T>
4747
{
4848
public:
4949
ShellConsumer() = default;
@@ -56,7 +56,7 @@ class ShellConsumer : public IConsumer<URPackage<HeaderT>>
5656
*
5757
* \returns True if the output was successful
5858
*/
59-
virtual bool consume(std::shared_ptr<URPackage<HeaderT>> product)
59+
virtual bool consume(std::shared_ptr<T> product)
6060
{
6161
LOG_INFO("%s", product->toString().c_str());
6262
return true;

ur_robot_driver/include/ur_robot_driver/comm/stream.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace comm
3838
* peek at the field defining the package length. This is why it is templated with the package
3939
* header type.
4040
*/
41-
template <typename HeaderT>
41+
template <typename T>
4242
class URStream : public TCPSocket
4343
{
4444
public:
@@ -116,30 +116,30 @@ class URStream : public TCPSocket
116116
std::mutex write_mutex_, read_mutex_;
117117
};
118118

119-
template <typename HeaderT>
120-
bool URStream<HeaderT>::write(const uint8_t* buf, const size_t buf_len, size_t& written)
119+
template <typename T>
120+
bool URStream<T>::write(const uint8_t* buf, const size_t buf_len, size_t& written)
121121
{
122122
std::lock_guard<std::mutex> lock(write_mutex_);
123123
return TCPSocket::write(buf, buf_len, written);
124124
}
125125

126-
template <typename HeaderT>
127-
bool URStream<HeaderT>::read(uint8_t* buf, const size_t buf_len, size_t& total)
126+
template <typename T>
127+
bool URStream<T>::read(uint8_t* buf, const size_t buf_len, size_t& total)
128128
{
129129
std::lock_guard<std::mutex> lock(read_mutex_);
130130

131131
bool initial = true;
132132
uint8_t* buf_pos = buf;
133-
size_t remainder = sizeof(typename HeaderT::_package_size_type);
133+
size_t remainder = sizeof(typename T::HeaderType::_package_size_type);
134134
size_t read = 0;
135135

136136
while (remainder > 0 && TCPSocket::read(buf_pos, remainder, read))
137137
{
138138
TCPSocket::setOptions(getSocketFD());
139139
if (initial)
140140
{
141-
remainder = HeaderT::getPackageLength(buf);
142-
if (remainder >= (buf_len - sizeof(typename HeaderT::_package_size_type)))
141+
remainder = T::HeaderType::getPackageLength(buf);
142+
if (remainder >= (buf_len - sizeof(typename T::HeaderType::_package_size_type)))
143143
{
144144
LOG_ERROR("Packet size %zd is larger than buffer %zu, discarding.", remainder, buf_len);
145145
return false;

0 commit comments

Comments
 (0)