-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathdata_package.h
213 lines (192 loc) · 6.01 KB
/
data_package.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
// -- BEGIN LICENSE BLOCK ----------------------------------------------
// Copyright 2019 FZI Forschungszentrum Informatik
// Created on behalf of Universal Robots A/S
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -- END LICENSE BLOCK ------------------------------------------------
//----------------------------------------------------------------------
/*!\file
*
* \author Lea Steffen [email protected]
* \date 2019-04-01
*
*/
//----------------------------------------------------------------------
#ifndef UR_CLIENT_LIBRARY_DATA_PACKAGE_H_INCLUDED
#define UR_CLIENT_LIBRARY_DATA_PACKAGE_H_INCLUDED
#include <unordered_map>
#include <variant>
#include <vector>
#include "ur_client_library/types.h"
#include "ur_client_library/rtde/rtde_package.h"
namespace urcl
{
namespace rtde_interface
{
/*!
* \brief Possible values for the runtime state
*/
enum class RUNTIME_STATE : uint32_t
{
STOPPING = 0,
STOPPED = 1,
PLAYING = 2,
PAUSING = 3,
PAUSED = 4,
RESUMING = 5
};
/*!
* \brief The DataPackage class handles communication in the form of RTDE data packages both to and
* from the robot. It contains functionality to parse and serialize packages for arbitrary recipes.
*/
class DataPackage : public RTDEPackage
{
public:
using _rtde_type_variant = std::variant<bool, uint8_t, uint32_t, uint64_t, int32_t, double, vector3d_t, vector6d_t,
vector6int32_t, vector6uint32_t, std::string>;
DataPackage() = delete;
DataPackage(const DataPackage& other) : DataPackage(other.recipe_)
{
this->data_ = other.data_;
this->protocol_version_ = other.protocol_version_;
}
/*!
* \brief Creates a new DataPackage object, based on a given recipe.
*
* \param recipe The used recipe
*
* \param protocol_version Protocol version used for the RTDE communication
*/
DataPackage(const std::vector<std::string>& recipe, const uint16_t& protocol_version = 2)
: RTDEPackage(PackageType::RTDE_DATA_PACKAGE), recipe_(recipe), protocol_version_(protocol_version)
{
}
virtual ~DataPackage() = default;
/*!
* \brief Initializes to contained list with empty values based on the recipe.
*/
void initEmpty();
/*!
* \brief Sets the attributes of the package by parsing a serialized representation of the
* package.
*
* \param bp A parser containing a serialized version of the package
*
* \returns True, if the package was parsed successfully, false otherwise
*/
virtual bool parseWith(comm::BinParser& bp);
/*!
* \brief Produces a human readable representation of the package object.
*
* \returns A string representing the object
*/
virtual std::string toString() const;
/*!
* \brief Serializes the package.
*
* \param buffer Buffer to fill with the serialization
*
* \returns The total size of the serialized package
*/
size_t serializePackage(uint8_t* buffer);
/*!
* \brief Get a data field from the DataPackage.
*
* The data package contains a lot of different data fields, depending on the recipe.
*
* \param name The string identifier for the data field as used in the documentation.
* \param val Target variable. Make sure, it's the correct type.
*
* \returns True on success, false if the field cannot be found inside the package.
*/
template <typename T>
bool getData(const std::string& name, T& val)
{
if (data_.find(name) != data_.end())
{
val = std::get<T>(data_[name]);
}
else
{
return false;
}
return true;
}
/*!
* \brief Get a data field from the DataPackage as bitset
*
* The data package contains a lot of different data fields, depending on the recipe.
*
* \param name The string identifier for the data field as used in the documentation.
* \param val Target variable. Make sure, it's the correct type.
*
* \returns True on success, false if the field cannot be found inside the package.
*/
template <typename T, size_t N>
bool getData(const std::string& name, std::bitset<N>& val)
{
static_assert(sizeof(T) * 8 >= N, "Bitset is too large for underlying variable");
if (data_.find(name) != data_.end())
{
val = std::bitset<N>(std::get<T>(data_[name]));
}
else
{
return false;
}
return true;
}
/*!
* \brief Set a data field in the DataPackage.
*
* The data package contains a lot of different data fields, depending on the recipe.
*
* \param name The string identifier for the data field as used in the documentation.
* \param val Value to set. Make sure, it's the correct type.
*
* \returns True on success, false if the field cannot be found inside the package.
*/
template <typename T>
bool setData(const std::string& name, T& val)
{
if (data_.find(name) != data_.end())
{
data_[name] = val;
}
else
{
return false;
}
return true;
}
/*!
* \brief Setter of the recipe id value used to identify the used recipe to the robot.
*
* \param recipe_id The new value
*/
void setRecipeID(const uint8_t& recipe_id)
{
recipe_id_ = recipe_id;
}
private:
// Const would be better here
static std::unordered_map<std::string, _rtde_type_variant> g_type_list;
uint8_t recipe_id_;
std::unordered_map<std::string, _rtde_type_variant> data_;
std::vector<std::string> recipe_;
uint16_t protocol_version_;
};
} // namespace rtde_interface
} // namespace urcl
#endif // ifndef UR_CLIENT_LIBRARY_DATA_PACKAGE_H_INCLUDED