-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmros2.cpp
278 lines (224 loc) · 6.33 KB
/
mros2.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "mros2.h"
#include <rtps/rtps.h>
#ifdef __MBED__
#include "mbed.h"
#else /* __MBED__ */
#include "mros2_config.h"
#include "cmsis_os.h"
#endif /* __MBED__ */
namespace mros2
{
rtps::Domain *domain_ptr = NULL;
rtps::Participant *part_ptr = NULL; //TODO: detele this
rtps::Writer *pub_ptr = NULL;
#define SUB_MSG_SIZE 4 // addr size
osMessageQueueId_t subscriber_msg_queue_id;
bool completeNodeInit = false;
uint8_t endpointId = 0;
uint32_t subCbArray[10];
uint8_t buf[255];
uint8_t buf_index = 4;
/* Callback function to set the boolean to true upon a match */
void setTrue(void* args)
{
*static_cast<volatile bool*>(args) = true;
}
bool subMatched = false;
bool pubMatched = false;
void pubMatch(void* args)
{
MROS2_DEBUG("[MROS2LIB] publisher matched with remote subscriber");
}
void subMatch(void* args)
{
MROS2_DEBUG("[MROS2LIB] subscriber matched with remote publisher");
}
/*
* Initialization of mROS 2 environment
*/
#ifdef __MBED__
Thread *mros2_init_thread;
#endif /* __MBED__ */
void init(int argc, char * argv[])
{
buf[0] = 0;
buf[1] = 1;
buf[2] = 0;
buf[3] = 0;
#ifdef __MBED__
mros2_init_thread = new Thread(osPriorityAboveNormal, 5000, nullptr, "mROS2Thread");
mros2_init_thread->start(callback(mros2_init, (void *)NULL));
#else /* __MBED__ */
osThreadAttr_t attributes;
attributes.name = "mROS2Thread",
attributes.stack_size = 5000,
attributes.priority = (osPriority_t)24,
osThreadNew(mros2_init, NULL, (const osThreadAttr_t*)&attributes);
#endif /* __MBED__ */
}
void mros2_init(void *args)
{
osStatus_t ret;
MROS2_DEBUG("[MROS2LIB] mros2_init task start");
#ifndef __MBED__
MX_LWIP_Init();
MROS2_DEBUG("[MROS2LIB] Initilizing lwIP complete");
#endif /* __MBED__ */
static rtps::Domain domain;
domain_ptr = &domain;
#ifndef __MBED__
subscriber_msg_queue_id = osMessageQueueNew(SUB_MSG_COUNT, SUB_MSG_SIZE, NULL);
if (subscriber_msg_queue_id == NULL) {
MROS2_ERROR("[MROS2LIB] ERROR: mROS2 init failed");
return;
}
#endif /* __MBED__ */
/* wait until participant(node) is created */
while(!completeNodeInit) {
osDelay(100);
}
domain.completeInit();
MROS2_DEBUG("[MROS2LIB] Initilizing Domain complete");
while(!subMatched && !pubMatched) {
osDelay(1000);
}
MROS2_DEBUG("[MROS2LIB] mros2_init task end");
ret = osThreadTerminate(NULL);
if (ret != osOK) {
MROS2_ERROR("[MROS2LIB] ERROR: mros2_init() task terminate error %d", ret);
}
}
/*
* Node functions
*/
Node Node::create_node(std::string node_name)
{
MROS2_DEBUG("[MROS2LIB] create_node");
MROS2_DEBUG("[MROS2LIB] start creating participant");
while(domain_ptr == NULL) {
osDelay(100);
}
Node node;
node.part = domain_ptr->createParticipant();
/* TODO: utilize node name */
node.node_name = node_name;
part_ptr = node.part;
if(node.part == nullptr) {
MROS2_ERROR("[MROS2LIB] ERROR: create_node() failed");
while(true) {}
}
completeNodeInit = true;
MROS2_DEBUG("[MROS2LIB] successfully created participant");
return node;
}
/*
* Publisher functions
*/
template <class T>
Publisher Node::create_publisher(std::string topic_name, int qos)
{
rtps::Writer* writer = domain_ptr->createWriter(*part_ptr, ("rt/"+topic_name).c_str(), message_traits::TypeName<T*>().value(), false);
if(writer == nullptr) {
MROS2_ERROR("[MROS2LIB] ERROR: failed to create writer in create_publisher()");
while(true) {}
}
Publisher pub;
pub_ptr = writer;
pub.topic_name = topic_name;
/* Register callback to ensure that a publisher is matched to the writer before sending messages */
part_ptr->registerOnNewSubscriberMatchedCallback(pubMatch, &subMatched);
MROS2_DEBUG("[MROS2LIB] create_publisher complete.");
return pub;
}
template <class T>
void Publisher::publish(T& msg)
{
msg.copyToBuf(&buf[4]);
msg.memAlign(&buf[4]);
pub_ptr->newChange(rtps::ChangeKind_t::ALIVE, buf, msg.getTotalSize() + 4);
}
/*
* Subscriber functions
*/
typedef struct {
void (*cb_fp)(intptr_t);
intptr_t argp;
} SubscribeDataType;
template <class T>
Subscriber Node::create_subscription(std::string topic_name, int qos, void(*fp)(T*))
{
rtps::Reader* reader = domain_ptr->createReader(*(this->part), ("rt/"+topic_name).c_str(), message_traits::TypeName<T*>().value(), false);
if(reader == nullptr) {
MROS2_ERROR("[MROS2LIB] ERROR: failed to create reader in create_subscription()");
while(true) {}
}
Subscriber sub;
sub.topic_name = topic_name;
sub.cb_fp = (void (*)(intptr_t))fp;
SubscribeDataType *data_p;
data_p = new SubscribeDataType;
data_p->cb_fp = (void (*)(intptr_t))fp;
data_p->argp = (intptr_t)NULL;
reader->registerCallback(sub.callback_handler<T>, (void *)data_p);
/* Register callback to ensure that a subscriber is matched to the reader before receiving messages */
part_ptr->registerOnNewPublisherMatchedCallback(subMatch, &pubMatched);
MROS2_DEBUG("[MROS2LIB] create_subscription complete.");
return sub;
}
template <class T>
void Subscriber::callback_handler(void *callee, const rtps::ReaderCacheChange &cacheChange)
{
T msg;
const uint8_t *cacheData = cacheChange.getData();
msg.copyFromBuf(&cacheData[4]);
SubscribeDataType *sub = (SubscribeDataType *)callee;
void (*fp)(intptr_t) = sub->cb_fp;
fp((intptr_t)&msg);
}
/*
* Other utility functions
*/
void spin()
{
while(true) {
#ifndef __MBED__
osStatus_t ret;
SubscribeDataType* msg;
ret = osMessageQueueGet(subscriber_msg_queue_id, &msg, NULL, osWaitForever);
if (ret != osOK) {
MROS2_ERROR("[MROS2LIB] ERROR: mROS2 spin() wait error %d", ret);
}
#else /* __MBED__ */
// The queue above seems not to be pushed anywhere. So just sleep.
ThisThread::sleep_for(1000);
#endif /* __MBED__ */
}
}
} /* namespace mros2 */
/*
* Declaration for embeddedRTPS participants
*/
void *networkSubDriverPtr;
void *networkPubDriverPtr;
void (*hbPubFuncPtr)(void *);
void (*hbSubFuncPtr)(void *);
extern "C" void callHbPubFunc(void *arg)
{
if(hbPubFuncPtr != NULL && networkPubDriverPtr != NULL) {
(*hbPubFuncPtr)(networkPubDriverPtr);
}
}
extern "C" void callHbSubFunc(void *arg)
{
if(hbSubFuncPtr != NULL && networkSubDriverPtr != NULL) {
(*hbSubFuncPtr)(networkSubDriverPtr);
}
}
void setTrue(void* args)
{
*static_cast<volatile bool*>(args) = true;
}
/*
* specialize template functions described in platform's workspace
*/
#include "templates.hpp"