micrortps bridge: clean templates; add backwards compatibility; add ROS2 compatibility

This commit is contained in:
TSC21
2019-10-02 10:40:37 +01:00
committed by Nuno Marques
parent 278d2fd21f
commit 1970ef875f
12 changed files with 147 additions and 113 deletions
+25 -12
View File
@@ -20,7 +20,7 @@ topic = alias if alias else spec.short_name
/****************************************************************************
*
* Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima).
* Copyright (C) 2018-2019 PX4 Pro Development Team. All rights reserved.
* Copyright (C) 2018-2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -54,7 +54,7 @@ topic = alias if alias else spec.short_name
* @@file @(topic)_Publisher.cpp
* This file contains the implementation of the publisher functions.
*
* This file was generated by the tool fastcdrgen.
* This file was adapted from the fastcdrgen tool.
*/
#include <fastrtps/participant/Participant.h>
@@ -64,7 +64,9 @@ topic = alias if alias else spec.short_name
#include <fastrtps/Domain.h>
@[if 1.5 < fastrtpsgen_version <= 1.7]@
#include <fastrtps/utils/eClock.h>
@[else]@
#include "@(topic)_Publisher.h"
@@ -76,26 +78,34 @@ topic = alias if alias else spec.short_name
bool @(topic)_Publisher::init()
{
// Create RTPSParticipant
ParticipantAttributes PParam;
PParam.rtps.builtin.domainId = 0;
@[if 1.5 < fastrtpsgen_version <= 1.7]@
PParam.rtps.builtin.leaseDuration = c_TimeInfinite;
@[else]@
PParam.rtps.builtin.discovery_config.leaseDuration = c_TimeInfinite;
@[end if]@
PParam.rtps.setName("@(topic)_publisher"); //You can put here the name you want
mp_participant = Domain::createParticipant(PParam);
if(mp_participant == nullptr)
return false;
//Register the type
Domain::registerType(mp_participant, (TopicDataType*) &myType);
// Register the type
Domain::registerType(mp_participant, static_cast<TopicDataType*>(&myType));
// Create Publisher
PublisherAttributes Wparam;
Wparam.topic.topicKind = NO_KEY;
Wparam.topic.topicDataType = myType.getName(); //This type MUST be registered
@[if 1.5 < fastrtpsgen_version <= 1.7]@
Wparam.topic.topicName = "@(topic)_PubSubTopic";
mp_publisher = Domain::createPublisher(mp_participant, Wparam, (PublisherListener*) &m_listener);
@[else]@
Wparam.topic.topicName = "@(topic)PubSubTopic";
@[end if]@
@[if ros2_distro and ros2_distro != "ardent"]@
Wparam.topic.topicName = "rt/" + Wparam.topic.topicName;
@[end if]@
mp_publisher = Domain::createPublisher(mp_participant, Wparam, static_cast<PublisherListener*>(&m_listener));
if(mp_publisher == nullptr)
return false;
//std::cout << "Publisher created, waiting for Subscribers." << std::endl;
@@ -120,12 +130,15 @@ void @(topic)_Publisher::run()
{
while(m_listener.n_matched == 0)
{
eClock::my_sleep(250); // Sleep 250 ms
@[if 1.5 < fastrtpsgen_version <= 1.7]@
eClock::my_sleep(250); // Sleep 250 ms;
@[else]@
std::this_thread::sleep_for(std::chrono::milliseconds(250)); // Sleep 250 ms
@[end if]@
}
// Publication code
@[if (ros2_distro == "ardent" or ros2_distro == "bouncy" or ros2_distro == "crystal") and float(fastrtps_version) < 1.9]@
@[if 1.5 < fastrtpsgen_version <= 1.7]@
@(topic)_ st;
@[else]@
@(topic) st;
@@ -154,7 +167,7 @@ void @(topic)_Publisher::run()
}while(std::cin >> ch);
}
@[if (ros2_distro == "ardent" or ros2_distro == "bouncy" or ros2_distro == "crystal") and float(fastrtps_version) < 1.9]@
@[if 1.5 < fastrtpsgen_version <= 1.7]@
void @(topic)_Publisher::publish(@(topic)_* st)
@[else]@
void @(topic)_Publisher::publish(@(topic)* st)