test_producer.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
2 // Copyright 2022 Universal Robots A/S
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of the {copyright_holder} nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 // -- END LICENSE BLOCK ------------------------------------------------
30 
31 #include <gtest/gtest.h>
32 #include <condition_variable>
33 
38 
39 using namespace urcl;
40 
41 class ProducerTest : public ::testing::Test
42 {
43 protected:
44  void SetUp()
45  {
46  server_.reset(new comm::TCPServer(60002));
47  server_->setConnectCallback(std::bind(&ProducerTest::connectionCallback, this, std::placeholders::_1));
48  server_->start();
49  }
50 
51  void Teardown()
52  {
53  // Clean up
54  server_.reset();
55  }
56 
57  void connectionCallback(const int filedescriptor)
58  {
59  std::lock_guard<std::mutex> lk(connect_mutex_);
60  client_fd_ = filedescriptor;
61  connect_cv_.notify_one();
62  connection_callback_ = true;
63  }
64 
65  bool waitForConnectionCallback(int milliseconds = 100)
66  {
67  std::unique_lock<std::mutex> lk(connect_mutex_);
68  if (connect_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
69  connection_callback_ == true)
70  {
71  connection_callback_ = false;
72  return true;
73  }
74  return false;
75  }
76 
77  std::unique_ptr<comm::TCPServer> server_;
79 
80 private:
81  std::condition_variable connect_cv_;
82  std::mutex connect_mutex_;
83 
84  bool connection_callback_ = false;
85 };
86 
87 TEST_F(ProducerTest, get_data_package)
88 {
89  comm::URStream<rtde_interface::RTDEPackage> stream("127.0.0.1", 60002);
90  std::vector<std::string> recipe = { "timestamp" };
91  rtde_interface::RTDEParser parser(recipe);
92  parser.setProtocolVersion(2);
93  comm::URProducer<rtde_interface::RTDEPackage> producer(stream, parser);
94 
95  producer.setupProducer();
96  waitForConnectionCallback();
97  producer.startProducer();
98 
99  // RTDE package with timestamp
100  uint8_t data_package[] = { 0x00, 0x0c, 0x55, 0x01, 0x40, 0xbb, 0xbf, 0xdb, 0xa5, 0xe3, 0x53, 0xf7 };
101  size_t written;
102  server_->write(client_fd_, data_package, sizeof(data_package), written);
103 
104  std::vector<std::unique_ptr<rtde_interface::RTDEPackage>> products;
105  EXPECT_EQ(producer.tryGet(products), true);
106 
107  if (rtde_interface::DataPackage* data = dynamic_cast<rtde_interface::DataPackage*>(products[0].get()))
108  {
109  double timestamp;
110  data->getData("timestamp", timestamp);
111  EXPECT_FLOAT_EQ(timestamp, 7103.86);
112  }
113  else
114  {
115  std::cout << "Failed to get data package" << std::endl;
116  GTEST_FAIL();
117  }
118 
119  producer.stopProducer();
120 }
121 
122 int main(int argc, char* argv[])
123 {
124  ::testing::InitGoogleTest(&argc, argv);
125 
126  return RUN_ALL_TESTS();
127 }
void setProtocolVersion(uint16_t protocol_version)
Definition: rtde_parser.h:119
void setupProducer() override
Triggers the stream to connect to the robot.
Definition: producer.h:63
The stream is an abstraction of the TCPSocket that offers reading a full UR data package out of the s...
Definition: stream.h:42
TEST_F(ProducerTest, get_data_package)
Wrapper class for a TCP socket server.
Definition: tcp_server.h:59
bool waitForConnectionCallback(int milliseconds=100)
void connectionCallback(const int filedescriptor)
int main(int argc, char *argv[])
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
The RTDE specific parser. Interprets a given byte stream as serialized RTDE packages and parses it ac...
Definition: rtde_parser.h:45
void startProducer() override
Definition: producer.h:89
void stopProducer() override
Stops the producer. Currently no functionality needed.
Definition: producer.h:84
std::condition_variable connect_cv_
std::unique_ptr< comm::TCPServer > server_
std::mutex connect_mutex_
The DataPackage class handles communication in the form of RTDE data packages both to and from the ro...
Definition: data_package.h:60
A general producer for URPackages. Implements funcionality to produce packages by reading and parsing...
Definition: producer.h:40


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Tue Jul 4 2023 02:09:47