staubli_driver_ros2 main
ROS2 control driver for Staubli robots
Loading...
Searching...
No Matches
protocol.hpp
Go to the documentation of this file.
1// Copyright 2025 ICUBE Laboratory, University of Strasbourg
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Author: Thibault Poignonec (thibault.poignonec@gmail.fr)
16
17#ifndef STAUBLI_ROBOT_DRIVER__COMMUNICATION__PROTOCOL_HPP_
18#define STAUBLI_ROBOT_DRIVER__COMMUNICATION__PROTOCOL_HPP_
19
20#include <array>
21#include <cstdint>
22#include <limits>
23#include <string>
24#include <vector>
25
26#include "rclcpp/time.hpp"
27
29
30namespace staubli_robot_driver {
31
35constexpr uint16_t MAGIC_NUMBER = 0xABCD;
36
40constexpr uint8_t PROTOCOL_VERSION = 1;
41
45constexpr uint16_t DEFAULT_CONTROL_PORT = 8080;
46
50constexpr uint16_t DEFAULT_DIAGNOSTICS_PORT = 8081;
51
55constexpr size_t MAX_FRAME_SEQUENCE_NUMBER = std::numeric_limits<uint16_t>::max();
56
66 uint8_t message_type = static_cast<uint8_t>(MessageType::INVALID);
68 uint16_t sequence_number = 0;
70 uint16_t payload_size = 0;
71
73 bool serialize(uint8_t* buffer, size_t buffer_size) const;
74
76 bool deserialize(uint8_t* buffer, size_t buffer_size);
77
79 static size_t get_serialized_size() { return 8; }
80};
81
104class Message {
105public:
106 Message() = default;
107 virtual ~Message() = default;
108
110 virtual size_t get_serialized_size() const = 0;
111
113 virtual bool serialize(uint8_t* buffer, size_t buffer_size) const = 0;
114
116 virtual bool deserialize(uint8_t* buffer, size_t buffer_size) = 0;
117
118 // Convenience methods for std::vector
119
129 bool serialize(std::vector<uint8_t>& data) const;
130
139 bool deserialize(std::vector<uint8_t>& data);
140
141public:
144
146 rclcpp::Time reception_timestamp = rclcpp::Time(0);
147};
148
149} // namespace staubli_robot_driver
150
151#endif // STAUBLI_ROBOT_DRIVER__COMMUNICATION__PROTOCOL_HPP_
Generic message structure.
Definition protocol.hpp:104
virtual bool serialize(uint8_t *buffer, size_t buffer_size) const =0
Serialize message to bytes.
FrameHeader header
Message header.
Definition protocol.hpp:143
virtual ~Message()=default
bool serialize(std::vector< uint8_t > &data) const
Serialize message to vector.
virtual bool deserialize(uint8_t *buffer, size_t buffer_size)=0
Deserialize bytes to message.
bool deserialize(std::vector< uint8_t > &data)
Deserialize message from vector.
virtual size_t get_serialized_size() const =0
Size of the entire message in bytes.
rclcpp::Time reception_timestamp
Timestamp of the message reception (local clock)
Definition protocol.hpp:146
Definition messages.hpp:23
constexpr size_t MAX_FRAME_SEQUENCE_NUMBER
Maximal sequence number before wrap-around.
Definition protocol.hpp:55
constexpr uint16_t DEFAULT_CONTROL_PORT
Default port for the control socket.
Definition protocol.hpp:45
constexpr uint8_t PROTOCOL_VERSION
Protocol version.
Definition protocol.hpp:40
constexpr uint16_t MAGIC_NUMBER
Magic number to identify the protocol.
Definition protocol.hpp:35
constexpr uint16_t DEFAULT_DIAGNOSTICS_PORT
Default port for the diagnostics socket.
Definition protocol.hpp:50
Frame header structure.
Definition protocol.hpp:60
uint16_t payload_size
Size of the payload in bytes.
Definition protocol.hpp:70
bool deserialize(uint8_t *buffer, size_t buffer_size)
Deserialize bytes to header.
uint8_t protocol_version
Protocol version (should be PROTOCOL_VERSION)
Definition protocol.hpp:64
uint16_t sequence_number
Sequence number for tracking messages.
Definition protocol.hpp:68
static size_t get_serialized_size()
Size of the header in bytes.
Definition protocol.hpp:79
bool serialize(uint8_t *buffer, size_t buffer_size) const
Serialize header to bytes.
uint16_t magic_number
Magic number for identifying the protocol.
Definition protocol.hpp:62
uint8_t message_type
Type of message.
Definition protocol.hpp:66