forked from ROBOTIS-GIT/ros2xrcedds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ros2.hpp
executable file
·177 lines (138 loc) · 3.94 KB
/
ros2.hpp
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
/*
* ros2.hpp
*
* Created on: May 16, 2018
* Author: Kei
*/
#ifndef ROS2_HPP_
#define ROS2_HPP_
#include <string.h>
#include "publisher.hpp"
#include "subscriber.hpp"
#include "topic.hpp"
#include "msg_list.hpp"
#include <ros2_user_config.h>
#define ROS2_PUBLISHER_MAX USER_ROS2_PUBLISHER_MAX
#define ROS2_SUBSCRIBER_MAX USER_ROS2_SUBSCRIBER_MAX
namespace ros2 {
void runNodeSubUserCallback(uint16_t id, void* msgs, void* args);
class Node
{
public:
Node();
virtual ~Node(){};
void recreate();
void createWallTimer(uint32_t msec, CallbackFunc callback, void* callback_arg, PublisherHandle* pub);
void createWallFreq(uint32_t hz, CallbackFunc callback, void* callback_arg, PublisherHandle* pub);
void runPubCallback();
void runSubCallback(uint16_t reader_id, void* serialized_msg);
void deletePublisher(const char* name);
void deletePublisher(uint16_t writer_id);
void deleteSubscriber(const char* name);
void deleteSubscriber(uint16_t reader_id);
template <
typename MsgT>
Publisher<MsgT>* createPublisher(const char* name)
{
bool ret = false;
ros2::Publisher<MsgT> *p_pub = nullptr;
if (this->node_register_state_ == false)
{
return nullptr;
}
if (pub_cnt_ >= ROS2_PUBLISHER_MAX)
{
return nullptr;
}
// Register Topic
ret = this->registerTopic<MsgT>();
if (ret == false)
{
return nullptr;
}
p_pub = new ros2::Publisher<MsgT>(&this->xrcedds_publisher_, name);
if (p_pub->is_registered_ == false)
{
delete (p_pub);
return nullptr;
}
for (uint8_t i = 0; i < ROS2_PUBLISHER_MAX; i++)
{
if (pub_list_[i] == nullptr)
{
pub_list_[i] = p_pub;
pub_cnt_++;
break;
}
}
return p_pub;
}
template <
typename MsgT>
Subscriber<MsgT>* createSubscriber(const char* name, CallbackFunc callback, void* callback_arg)
{
bool ret = false;
ros2::Subscriber<MsgT> *p_sub = nullptr;
if(this->node_register_state_ == false)
{
return nullptr;
}
if(sub_cnt_ >= ROS2_SUBSCRIBER_MAX)
{
return nullptr;
}
// Register Topic
ret = this->registerTopic<MsgT>();
if (ret == false)
{
return nullptr;
}
p_sub = new ros2::Subscriber<MsgT>(&this->xrcedds_subscriber_, name, callback, callback_arg);
if(p_sub->is_registered_ == false)
{
delete(p_sub);
return nullptr;
}
for(uint8_t i = 0; i < ROS2_SUBSCRIBER_MAX; i++)
{
if(sub_list_[i] == nullptr)
{
sub_list_[i] = p_sub;
sub_cnt_++;
p_sub->subscribe();
break;
}
}
return p_sub;
}
private:
PublisherHandle* pub_list_[ROS2_PUBLISHER_MAX];
SubscriberHandle* sub_list_[ROS2_SUBSCRIBER_MAX];
bool node_register_state_;
xrcedds::Transport_t xrcedds_transport_;
xrcedds::Participant_t xrcedds_participant_;
xrcedds::Publisher_t xrcedds_publisher_;
xrcedds::Subscriber_t xrcedds_subscriber_;
uint8_t pub_cnt_;
uint8_t sub_cnt_;
template <
typename MsgT>
bool registerTopic()
{
bool ret;
MsgT topic;
if(this->node_register_state_ == false)
{
return false;
}
ret = xrcedds::registerTopic(&this->xrcedds_participant_, topic.name_, topic.type_, topic.id_);
return ret;
}
};
bool init(void* serial_dev);
bool init(void* comm_instance, const char* p_server_ip, uint16_t server_port, bool enable_tcp);
void spin(Node *node);
void syncTimeFromRemote(builtin_interfaces::Time* time);
builtin_interfaces::Time now();
} /* namespace ros2 */
#endif /* ROS2_H_ */