data_package.h
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 2019 FZI Forschungszentrum Informatik
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // -- END LICENSE BLOCK ------------------------------------------------
18 
19 //----------------------------------------------------------------------
26 //----------------------------------------------------------------------
27 
28 #ifndef UR_CLIENT_LIBRARY_DATA_PACKAGE_H_INCLUDED
29 #define UR_CLIENT_LIBRARY_DATA_PACKAGE_H_INCLUDED
30 
31 #include <unordered_map>
32 #include <vector>
33 
36 #include <boost/variant.hpp>
37 
38 namespace urcl
39 {
40 namespace rtde_interface
41 {
45 enum class RUNTIME_STATE : uint32_t
46 {
47  STOPPING = 0,
48  STOPPED = 1,
49  PLAYING = 2,
50  PAUSING = 3,
51  PAUSED = 4,
52  RESUMING = 5
53 };
54 
59 class DataPackage : public RTDEPackage
60 {
61 public:
62  using _rtde_type_variant = boost::variant<bool, uint8_t, uint32_t, uint64_t, int32_t, double, vector3d_t, vector6d_t,
64 
65  DataPackage() = delete;
66 
67  DataPackage(const DataPackage& other) : DataPackage(other.recipe_)
68  {
69  this->data_ = other.data_;
70  }
71 
77  DataPackage(const std::vector<std::string>& recipe) : RTDEPackage(PackageType::RTDE_DATA_PACKAGE), recipe_(recipe)
78  {
79  }
80  virtual ~DataPackage() = default;
81 
85  void initEmpty();
86 
95  virtual bool parseWith(comm::BinParser& bp);
101  virtual std::string toString() const;
102 
110  size_t serializePackage(uint8_t* buffer);
111 
123  template <typename T>
124  bool getData(const std::string& name, T& val)
125  {
126  if (data_.find(name) != data_.end())
127  {
128  val = boost::strict_get<T>(data_[name]);
129  }
130  else
131  {
132  return false;
133  }
134  return true;
135  }
136 
148  template <typename T, size_t N>
149  bool getData(const std::string& name, std::bitset<N>& val)
150  {
151  static_assert(sizeof(T) * 8 >= N, "Bitset is too large for underlying variable");
152 
153  if (data_.find(name) != data_.end())
154  {
155  val = std::bitset<N>(boost::strict_get<T>(data_[name]));
156  }
157  else
158  {
159  return false;
160  }
161  return true;
162  }
163 
174  template <typename T>
175  bool setData(const std::string& name, T& val)
176  {
177  if (data_.find(name) != data_.end())
178  {
179  data_[name] = val;
180  }
181  else
182  {
183  return false;
184  }
185  return true;
186  }
187 
193  void setRecipeID(const uint8_t& recipe_id)
194  {
195  recipe_id_ = recipe_id;
196  }
197 
198 private:
199  // Const would be better here
200  static std::unordered_map<std::string, _rtde_type_variant> g_type_list;
201  uint8_t recipe_id_;
202  std::unordered_map<std::string, _rtde_type_variant> data_;
203  std::vector<std::string> recipe_;
204 
205  struct ParseVisitor : public boost::static_visitor<>
206  {
207  template <typename T>
208  void operator()(T& d, comm::BinParser& bp) const
209  {
210  bp.parse(d);
211  }
212  };
213  struct StringVisitor : public boost::static_visitor<std::string>
214  {
215  template <typename T>
216  std::string operator()(T& d) const
217  {
218  std::stringstream ss;
219  ss << d;
220  return ss.str();
221  }
222  };
223  struct SizeVisitor : public boost::static_visitor<uint16_t>
224  {
225  template <typename T>
226  uint16_t operator()(T& d) const
227  {
228  return sizeof(d);
229  }
230  };
231  struct SerializeVisitor : public boost::static_visitor<size_t>
232  {
233  template <typename T>
234  size_t operator()(T& d, uint8_t* buffer) const
235  {
236  return comm::PackageSerializer::serialize(buffer, d);
237  }
238  };
239 };
240 
241 } // namespace rtde_interface
242 } // namespace urcl
243 
244 #endif // ifndef UR_CLIENT_LIBRARY_DATA_PACKAGE_H_INCLUDED
void operator()(T &d, comm::BinParser &bp) const
Definition: data_package.h:208
void parse(T &val)
Parses the next bytes as given type.
Definition: bin_parser.h:139
DataPackage(const DataPackage &other)
Definition: data_package.h:67
size_t operator()(T &d, uint8_t *buffer) const
Definition: data_package.h:234
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:124
std::unordered_map< std::string, _rtde_type_variant > data_
Definition: data_package.h:202
std::array< double, 3 > vector3d_t
Definition: types.h:29
PackageType
Possible package types.
RUNTIME_STATE
Possible values for the runtime state.
Definition: data_package.h:45
boost::variant< bool, uint8_t, uint32_t, uint64_t, int32_t, double, vector3d_t, vector6d_t, vector6int32_t, vector6uint32_t, std::string > _rtde_type_variant
Definition: data_package.h:63
DataPackage(const std::vector< std::string > &recipe)
Creates a new DataPackage object, based on a given recipe.
Definition: data_package.h:77
std::array< int32_t, 6 > vector6int32_t
Definition: types.h:31
bool getData(const std::string &name, std::bitset< N > &val)
Get a data field from the DataPackage as bitset.
Definition: data_package.h:149
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:193
static size_t serialize(uint8_t *buffer, T val)
A generalized serialization method for arbitrary datatypes.
std::array< uint32_t, 6 > vector6uint32_t
Definition: types.h:32
std::vector< std::string > recipe_
Definition: data_package.h:203
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:59
static std::unordered_map< std::string, _rtde_type_variant > g_type_list
Definition: data_package.h:200
bool setData(const std::string &name, T &val)
Set a data field in the DataPackage.
Definition: data_package.h:175


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Sun May 9 2021 02:16:26