primary_parser.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019, FZI Forschungszentrum Informatik (refactor)
3  *
4  * Copyright 2017, 2018 Simon Rasmussen (refactor)
5  *
6  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 #include <vector>
34 
35 namespace urcl
36 {
37 namespace primary_interface
38 {
43 class PrimaryParser : public comm::Parser<PrimaryPackage>
44 {
45 public:
46  PrimaryParser() = default;
47  virtual ~PrimaryParser() = default;
48 
58  bool parse(comm::BinParser& bp, std::vector<std::unique_ptr<PrimaryPackage>>& results)
59  {
60  int32_t packet_size;
61  RobotPackageType type;
62  bp.parse(packet_size);
63  bp.parse(type);
64 
65  switch (type)
66  {
68  {
69  while (!bp.empty())
70  {
71  if (!bp.checkSize(sizeof(uint32_t)))
72  {
73  URCL_LOG_ERROR("Failed to read sub-package length, there's likely a parsing error");
74  return false;
75  }
76  uint32_t sub_size = bp.peek<uint32_t>();
77  if (!bp.checkSize(static_cast<size_t>(sub_size)))
78  {
79  URCL_LOG_WARN("Invalid sub-package size of %" PRIu32 " received!", sub_size);
80  return false;
81  }
82 
83  // deconstruction of a sub parser will increment the position of the parent parser
84  comm::BinParser sbp(bp, sub_size);
85  sbp.consume(sizeof(sub_size));
86  RobotStateType type;
87  sbp.parse(type);
88 
89  std::unique_ptr<PrimaryPackage> packet(stateFromType(type));
90 
91  if (packet == nullptr)
92  {
93  sbp.consume();
94 
95  // TODO: create robot state type here
96  continue;
97  }
98 
99  if (!packet->parseWith(sbp))
100  {
101  URCL_LOG_ERROR("Sub-package parsing of type %d failed!", static_cast<int>(type));
102  return false;
103  }
104 
105  results.push_back(std::move(packet));
106 
107  if (!sbp.empty())
108  {
109  URCL_LOG_ERROR("Sub-package of type %d was not parsed completely!", static_cast<int>(type));
110  sbp.debug();
111  return false;
112  }
113  }
114 
115  break;
116  }
117 
119  {
120  uint64_t timestamp;
121  uint8_t source;
122  RobotMessagePackageType message_type;
123 
124  bp.parse(timestamp);
125  bp.parse(source);
126  bp.parse(message_type);
127 
128  std::unique_ptr<PrimaryPackage> packet(messageFromType(message_type, timestamp, source));
129  if (!packet->parseWith(bp))
130  {
131  URCL_LOG_ERROR("Package parsing of type %d failed!", static_cast<int>(message_type));
132  return false;
133  }
134 
135  results.push_back(std::move(packet));
136  return true;
137  break;
138  }
139 
140  default:
141  {
142  URCL_LOG_DEBUG("Invalid robot package type recieved: %u", static_cast<uint8_t>(type));
143  bp.consume();
144  return true;
145  }
146  }
147  return true;
148  }
149 
150 private:
152  {
153  switch (type)
154  {
156  return new RobotModeData(type);
157 
158  // return new rmd;
159  // case robot_state_type::MASTERBOARD_DATA:
160  // return new MBD;*/
162  return new KinematicsInfo(type);
164  return new ConfigurationData(type);
165  default:
166  return new RobotState(type);
167  }
168  }
169 
170  RobotMessage* messageFromType(RobotMessagePackageType type, uint64_t timestamp, uint8_t source)
171  {
172  switch (type)
173  {
174  /*case robot_state_type::ROBOT_MODE_DATA:
175  // SharedRobotModeData* rmd = new SharedRobotModeData();
176 
177  //return new rmd;
178  case robot_state_type::MASTERBOARD_DATA:
179  return new MBD;*/
181  return new VersionMessage(timestamp, source);
183  return new ErrorCodeMessage(timestamp, source);
184  default:
185  return new RobotMessage(timestamp, source);
186  }
187  }
188 };
189 
190 } // namespace primary_interface
191 } // namespace urcl
urcl::primary_interface::RobotMessagePackageType::ROBOT_MESSAGE_ERROR_CODE
@ ROBOT_MESSAGE_ERROR_CODE
urcl::primary_interface::PrimaryParser::messageFromType
RobotMessage * messageFromType(RobotMessagePackageType type, uint64_t timestamp, uint8_t source)
Definition: primary_parser.h:170
robot_mode_data.h
pipeline.h
bin_parser.h
urcl::comm::BinParser::peek
T peek()
Parses the next bytes as given type without moving the buffer pointer.
Definition: bin_parser.h:121
urcl::primary_interface::ErrorCodeMessage
The ErrorCodeMessage class handles the error code messages sent via the primary UR interface.
Definition: error_code_message.h:66
urcl::primary_interface::RobotModeData
This messages contains data about the mode of the robot.
Definition: robot_mode_data.h:45
robot_message.h
urcl::primary_interface::RobotMessagePackageType
RobotMessagePackageType
Possible RobotMessage types.
Definition: robot_message.h:41
urcl::primary_interface::PrimaryParser::~PrimaryParser
virtual ~PrimaryParser()=default
package_header.h
urcl
Definition: bin_parser.h:36
urcl::primary_interface::RobotMessagePackageType::ROBOT_MESSAGE_VERSION
@ ROBOT_MESSAGE_VERSION
error_code_message.h
URCL_LOG_ERROR
#define URCL_LOG_ERROR(...)
Definition: log.h:26
urcl::primary_interface::RobotMessage
The RobotMessage class is a parent class for the different received robot messages.
Definition: robot_message.h:57
urcl::comm::Parser
The parser is a general paser. The namsepace rtde_interface and primary_interface both iclude classes...
Definition: parser.h:36
URCL_LOG_DEBUG
#define URCL_LOG_DEBUG(...)
Definition: log.h:23
urcl::primary_interface::PrimaryParser
The primary specific parser. Interprets a given byte stream as serialized primary packages and parses...
Definition: primary_parser.h:43
urcl::primary_interface::ConfigurationData
The ConfigurationData class handles the configuration data sent via the primary UR interface.
Definition: configuration_data.h:44
version_message.h
urcl::primary_interface::RobotStateType::KINEMATICS_INFO
@ KINEMATICS_INFO
configuration_data.h
urcl::comm::BinParser::empty
bool empty()
Checks if no unparsed bytes remain in the buffer.
Definition: bin_parser.h:362
urcl::primary_interface::RobotStateType::CONFIGURATION_DATA
@ CONFIGURATION_DATA
kinematics_info.h
urcl::comm::BinParser::consume
void consume()
Sets the current buffer position to the end of the buffer, finishing parsing.
Definition: bin_parser.h:318
urcl::primary_interface::PrimaryParser::parse
bool parse(comm::BinParser &bp, std::vector< std::unique_ptr< PrimaryPackage >> &results)
Uses the given BinParser to create package objects from the contained serialization.
Definition: primary_parser.h:58
urcl::comm::BinParser::checkSize
bool checkSize(size_t bytes)
Checks if at least a given number of bytes is still remaining unparsed in the buffer.
Definition: bin_parser.h:339
parser.h
urcl::primary_interface::RobotStateType::ROBOT_MODE_DATA
@ ROBOT_MODE_DATA
urcl::primary_interface::VersionMessage
The VersionMessage class handles the version messages sent via the primary UR interface.
Definition: version_message.h:41
urcl::primary_interface::RobotPackageType
RobotPackageType
Possible RobotPackage types.
Definition: primary/package_header.h:47
urcl::primary_interface::RobotStateType
RobotStateType
Possible RobotState types.
Definition: robot_state.h:42
urcl::primary_interface::RobotPackageType::ROBOT_STATE
@ ROBOT_STATE
urcl::primary_interface::PrimaryParser::PrimaryParser
PrimaryParser()=default
urcl::primary_interface::PrimaryParser::stateFromType
RobotState * stateFromType(RobotStateType type)
Definition: primary_parser.h:151
URCL_LOG_WARN
#define URCL_LOG_WARN(...)
Definition: log.h:24
urcl::primary_interface::RobotState
Base class for a RobotState data packages will be used directly.
Definition: robot_state.h:59
urcl::primary_interface::RobotPackageType::ROBOT_MESSAGE
@ ROBOT_MESSAGE
urcl::comm::BinParser::parse
void parse(T &val)
Parses the next bytes as given type.
Definition: bin_parser.h:139
urcl::comm::BinParser
The BinParser class handles a byte buffer and functionality to iteratively parse the content.
Definition: bin_parser.h:44
urcl::primary_interface::KinematicsInfo
This messages contains information about the robot's calibration. The DH parameters are a combination...
Definition: kinematics_info.h:43
robot_state.h
urcl::comm::BinParser::debug
void debug()
Logs debugging information about the BinParser object.
Definition: bin_parser.h:370


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58