test_rtde_data_package.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // Copyright 2020 FZI Forschungszentrum Informatik
5 // Created on behalf of Universal Robots A/S
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // -- END LICENSE BLOCK ------------------------------------------------
19 
20 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 
29 #include <gtest/gtest.h>
30 
32 
33 using namespace urcl;
34 
35 TEST(rtde_data_package, serialize_pkg)
36 {
37  std::vector<std::string> recipe{ "speed_slider_mask" };
38  rtde_interface::DataPackage package(recipe);
39  package.initEmpty();
40 
41  uint32_t value = 1;
42  package.setData("speed_slider_mask", value);
43 
44  uint8_t buffer[4096];
45  package.setRecipeID(1);
46  size_t size = package.serializePackage(buffer);
47 
48  EXPECT_EQ(size, 8);
49 
50  uint8_t expected[] = { 0x0, 0x08, 0x55, 0x01, 0x00, 0x00, 0x00, 0x01 };
51 
52  for (size_t i = 0; i < size; ++i)
53  {
54  EXPECT_EQ(buffer[i], expected[i]);
55  }
56 }
57 
58 TEST(rtde_data_package, parse_pkg_protocolv2)
59 {
60  std::vector<std::string> recipe{ "timestamp", "actual_q" };
61  rtde_interface::DataPackage package(recipe);
62  package.initEmpty();
63 
64  uint8_t data_package[] = { 0x01, 0x40, 0xd0, 0x75, 0x8c, 0x49, 0xba, 0x5e, 0x35, 0xbf, 0xf9, 0x9c, 0x77, 0xd1, 0x10,
65  0xb4, 0x60, 0xbf, 0xfb, 0xa2, 0x33, 0xd1, 0x10, 0xb4, 0x60, 0xc0, 0x01, 0x9f, 0xbe, 0x68,
66  0x88, 0x5a, 0x30, 0xbf, 0xe9, 0xdb, 0x22, 0xa2, 0x21, 0x68, 0xc0, 0x3f, 0xf9, 0x85, 0x87,
67  0xa0, 0x00, 0x00, 0x00, 0xbf, 0x9f, 0xbe, 0x74, 0x44, 0x2d, 0x18, 0x00 };
68 
69  comm::BinParser bp(data_package, sizeof(data_package));
70 
71  EXPECT_TRUE(package.parseWith(bp));
72 
73  vector6d_t expected_q = { -1.6007, -1.7271, -2.203, -0.808, 1.5951, -0.031 };
74  vector6d_t actual_q;
75  package.getData("actual_q", actual_q);
76 
77  double abs = 1e-4;
78  EXPECT_NEAR(expected_q[0], actual_q[0], abs);
79  EXPECT_NEAR(expected_q[1], actual_q[1], abs);
80  EXPECT_NEAR(expected_q[2], actual_q[2], abs);
81  EXPECT_NEAR(expected_q[3], actual_q[3], abs);
82  EXPECT_NEAR(expected_q[4], actual_q[4], abs);
83  EXPECT_NEAR(expected_q[5], actual_q[5], abs);
84 
85  double expected_timestamp = 16854.1919;
86  double actual_timestamp;
87  package.getData("timestamp", actual_timestamp);
88 
89  EXPECT_NEAR(expected_timestamp, actual_timestamp, abs);
90 }
91 
92 TEST(rtde_data_package, parse_pkg_protocolv1)
93 {
94  std::vector<std::string> recipe{ "timestamp", "actual_q" };
95  rtde_interface::DataPackage package(recipe, 1);
96  package.initEmpty();
97 
98  uint8_t data_package[] = { 0x40, 0xd0, 0x75, 0x8c, 0x49, 0xba, 0x5e, 0x35, 0xbf, 0xf9, 0x9c, 0x77, 0xd1, 0x10,
99  0xb4, 0x60, 0xbf, 0xfb, 0xa2, 0x33, 0xd1, 0x10, 0xb4, 0x60, 0xc0, 0x01, 0x9f, 0xbe,
100  0x68, 0x88, 0x5a, 0x30, 0xbf, 0xe9, 0xdb, 0x22, 0xa2, 0x21, 0x68, 0xc0, 0x3f, 0xf9,
101  0x85, 0x87, 0xa0, 0x00, 0x00, 0x00, 0xbf, 0x9f, 0xbe, 0x74, 0x44, 0x2d, 0x18, 0x00 };
102  comm::BinParser bp(data_package, sizeof(data_package));
103 
104  EXPECT_TRUE(package.parseWith(bp));
105 
106  vector6d_t expected_q = { -1.6007, -1.7271, -2.203, -0.808, 1.5951, -0.031 };
107  vector6d_t actual_q;
108  package.getData("actual_q", actual_q);
109 
110  double abs = 1e-4;
111  EXPECT_NEAR(expected_q[0], actual_q[0], abs);
112  EXPECT_NEAR(expected_q[1], actual_q[1], abs);
113  EXPECT_NEAR(expected_q[2], actual_q[2], abs);
114  EXPECT_NEAR(expected_q[3], actual_q[3], abs);
115  EXPECT_NEAR(expected_q[4], actual_q[4], abs);
116  EXPECT_NEAR(expected_q[5], actual_q[5], abs);
117 
118  double expected_timestamp = 16854.1919;
119  double actual_timestamp;
120  package.getData("timestamp", actual_timestamp);
121 
122  EXPECT_NEAR(expected_timestamp, actual_timestamp, abs);
123 }
124 
125 TEST(rtde_data_package, get_data_not_part_of_recipe)
126 {
127  std::vector<std::string> recipe{ "timestamp", "actual_q" };
128  rtde_interface::DataPackage package(recipe);
129  package.initEmpty();
130 
131  uint32_t speed_slider_mask;
132  EXPECT_FALSE(package.getData("speed_slider_mask", speed_slider_mask));
133 }
134 
135 TEST(rtde_data_package, set_data_not_part_of_recipe)
136 {
137  std::vector<std::string> recipe{ "timestamp", "actual_q" };
138  rtde_interface::DataPackage package(recipe);
139  package.initEmpty();
140 
141  uint32_t speed_slider_mask = 1;
142  EXPECT_FALSE(package.setData("speed_slider_mask", speed_slider_mask));
143 }
144 
145 TEST(rtde_data_package, parse_and_get_bitset_data)
146 {
147  std::vector<std::string> recipe{ "robot_status_bits" };
148  rtde_interface::DataPackage package(recipe);
149  package.initEmpty();
150 
151  uint8_t data_package[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x40, 0xb2, 0x3d, 0xa9, 0xfb, 0xe7, 0x6c, 0x8b };
152  comm::BinParser bp(data_package, sizeof(data_package));
153 
154  EXPECT_TRUE(package.parseWith(bp));
155 
156  std::bitset<4> expected_robot_status_bits = 0000;
157  std::bitset<4> actual_robot_status_bits;
158  package.getData<uint32_t>("robot_status_bits", actual_robot_status_bits);
159 
160  EXPECT_EQ(expected_robot_status_bits, actual_robot_status_bits);
161 }
162 
163 int main(int argc, char* argv[])
164 {
165  ::testing::InitGoogleTest(&argc, argv);
166 
167  return RUN_ALL_TESTS();
168 }
TEST(rtde_data_package, serialize_pkg)
The BinParser class handles a byte buffer and functionality to iteratively parse the content...
Definition: bin_parser.h:44
bool getData(const std::string &name, T &val)
Get a data field from the DataPackage.
Definition: data_package.h:128
int main(int argc, char *argv[])
virtual bool parseWith(comm::BinParser &bp)
Sets the attributes of the package by parsing a serialized representation of the package.
size_t serializePackage(uint8_t *buffer)
Serializes the package.
void setRecipeID(const uint8_t &recipe_id)
Setter of the recipe id value used to identify the used recipe to the robot.
Definition: data_package.h:196
std::array< double, 6 > vector6d_t
Definition: types.h:30
The DataPackage class handles communication in the form of RTDE data packages both to and from the ro...
Definition: data_package.h:60
void initEmpty()
Initializes to contained list with empty values based on the recipe.
bool setData(const std::string &name, T &val)
Set a data field in the DataPackage.
Definition: data_package.h:178


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