Last active
June 27, 2017 22:24
-
-
Save youtalk/ef14a9c6f899665ed2d2dec1e927446d to your computer and use it in GitHub Desktop.
freertps apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "freertps/freertps.h" | |
// コールバック関数 | |
void chatter_cb(const void *msg) | |
{ | |
// str_lenの型は32ビットなのに、forループの中では8ビット受け渡しなのが解せない | |
uint32_t str_len = *((uint32_t *)msg); | |
char buf[128] = {0}; | |
for (int i = 0; i < str_len && i < sizeof(buf)-1; i++) | |
// インデックスを4だけ進めているのは、std_msgs::Stringの"data"というキー名を除くため? | |
buf[i] = ((uint8_t *)msg)[4+i]; | |
printf("I heard: [%s]\n", buf); | |
} | |
int main(int argc, char **argv) | |
{ | |
printf("hello, world!\r\n"); | |
// ROSのソースコードとアルゴリズムが変わらないので、中身を知らなくても読める | |
freertps_system_init(); | |
// Subscriber宣言 | |
freertps_create_sub("chatter", "std_msgs::msg::dds_::String_", chatter_cb); | |
freertps_start(); | |
while (freertps_system_ok()) | |
{ | |
// "frudp_"という接頭語がつくんですね。"fr"eertpsの"UDP"通信という意味でしょう | |
frudp_listen(1000000); | |
frudp_disco_tick(); | |
} | |
frudp_fini(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "freertps/freertps.h" | |
#include "std_msgs/string.h" | |
int main(int argc, char **argv) | |
{ | |
freertps_system_init(); | |
// Publisher宣言 | |
frudp_pub_t *pub = freertps_create_pub("chatter", std_msgs__string__type.rtps_typename); | |
frudp_disco_start(); | |
// メッセージ型はstructにマッピングされる | |
struct std_msgs__string msg; | |
char data_buf[64] = {0}; | |
msg.data = data_buf; | |
uint8_t cdr[68] = {0}; | |
int pub_count = 0; | |
while (freertps_system_ok()) | |
{ | |
frudp_listen(500000); | |
frudp_disco_tick(); | |
snprintf(msg.data, sizeof(data_buf), "Hello, world! %d", pub_count++); | |
// このシリアライズ関数でRTPSのプロトコルに沿った形に変換されるのでしょう | |
int cdr_len = serialize_std_msgs__string(&msg, cdr, sizeof(cdr)); | |
freertps_publish(pub, cdr, cdr_len); | |
printf("sending: [%s]\r\n", data_buf); | |
} | |
frudp_fini(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment