Skip to content

Commit

Permalink
micrortps bridge: clean templates; add backwards compatibility; add R…
Browse files Browse the repository at this point in the history
…OS2 compatibility
  • Loading branch information
TSC21 committed Oct 4, 2019
1 parent 278d2fd commit 1970ef8
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 113 deletions.
37 changes: 25 additions & 12 deletions msg/templates/urtps/Publisher.cpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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>
Expand All @@ -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"

Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions msg/templates/urtps/Publisher.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -54,7 +54,7 @@ topic = alias if alias else spec.short_name
* @@file @(topic)_Publisher.h
* This header file contains the declaration of the publisher functions.
*
* This file was generated by the tool fastcdrgen.
* This file was adapted from the fastcdrgen tool.
*/


Expand All @@ -64,7 +64,7 @@ topic = alias if alias else spec.short_name
#include <fastrtps/fastrtps_fwd.h>
#include <fastrtps/publisher/PublisherListener.h>

@[if float(fastrtps_version) < 1.9]@
@[if 1.5 < fastrtpsgen_version <= 1.7]@
#include "@(topic)_PubSubTypes.h"
@[else]@
#include "@(topic)PubSubTypes.h"
Expand All @@ -80,7 +80,7 @@ public:
virtual ~@(topic)_Publisher();
bool init();
void run();
@[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 publish(@(topic)_* st);
@[else]@
void publish(@(topic)* st);
Expand All @@ -97,7 +97,7 @@ private:
void onPublicationMatched(Publisher* pub, MatchingInfo& info);
int n_matched;
} m_listener;
@[if float(fastrtps_version) < 1.9]@
@[if 1.5 < fastrtpsgen_version <= 1.7]@
@(topic)_PubSubType myType;
@[else]@
@(topic)PubSubType myType;
Expand Down
6 changes: 3 additions & 3 deletions msg/templates/urtps/RtpsTopics.cpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ recv_topics = [(alias[idx] if alias[idx] else s.short_name) for idx, s in enumer
/****************************************************************************
*
* 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:
Expand Down Expand Up @@ -92,7 +92,7 @@ void RtpsTopics::publish(uint8_t topic_ID, char data_buffer[], size_t len)
@[for topic in send_topics]@
case @(rtps_message_id(ids, topic)): // @(topic)
{
@[ 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;
Expand Down Expand Up @@ -149,7 +149,7 @@ bool RtpsTopics::getMsg(const uint8_t topic_ID, eprosima::fastcdr::Cdr &scdr)
case @(rtps_message_id(ids, topic)): // @(topic)
if (_@(topic)_sub.hasMsg())
{
@[ 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)_ msg = _@(topic)_sub.getMsg();
@[ else]@
@(topic) msg = _@(topic)_sub.getMsg();
Expand Down
2 changes: 1 addition & 1 deletion msg/templates/urtps/RtpsTopics.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ recv_topics = [(alias[idx] if alias[idx] else s.short_name) for idx, s in enumer
/****************************************************************************
*
* 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:
Expand Down
32 changes: 20 additions & 12 deletions msg/templates/urtps/Subscriber.cpp.em
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -54,7 +54,7 @@ topic = alias if alias else spec.short_name
* @@file @(topic)_Subscriber.cpp
* This file contains the implementation of the subscriber functions.
*
* This file was generated by the tool fastcdrgen.
* This file was adapted from the fastcdrgen tool.
*/

#include <fastrtps/participant/Participant.h>
Expand All @@ -66,41 +66,50 @@ topic = alias if alias else spec.short_name

#include "@(topic)_Subscriber.h"


@(topic)_Subscriber::@(topic)_Subscriber() : mp_participant(nullptr), mp_subscriber(nullptr) {}

@(topic)_Subscriber::~@(topic)_Subscriber() { Domain::removeParticipant(mp_participant);}

bool @(topic)_Subscriber::init()
{
// Create RTPSParticipant

ParticipantAttributes PParam;
PParam.rtps.builtin.domainId = 0; //MUST BE THE SAME AS IN THE PUBLISHER
PParam.rtps.builtin.domainId = 0; // MUST BE THE SAME AS IN THE PUBLISHER
@[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)_subscriber"); //You can put the name you want
mp_participant = Domain::createParticipant(PParam);
if(mp_participant == nullptr)
return false;

//Register the type

Domain::registerType(mp_participant, (TopicDataType*) &myType);
Domain::registerType(mp_participant, static_cast<TopicDataType*>(&myType));

// Create Subscriber

SubscriberAttributes Rparam;
Rparam.topic.topicKind = NO_KEY;
Rparam.topic.topicDataType = myType.getName(); //Must be registered before the creation of the subscriber
@[if 1.5 < fastrtpsgen_version <= 1.7]@
Rparam.topic.topicName = "@(topic)_PubSubTopic";
mp_subscriber = Domain::createSubscriber(mp_participant, Rparam, (SubscriberListener*) &m_listener);
@[else]@
Rparam.topic.topicName = "@(topic)PubSubTopic";
@[end if]@
@[if ros2_distro and ros2_distro != "ardent"]@
Rparam.topic.topicName = "rt/" + Wparam.topic.topicName;
@[end if]@
mp_subscriber = Domain::createSubscriber(mp_participant, Rparam, static_cast<SubscriberListener*>(&m_listener));
if(mp_subscriber == nullptr)
return false;
return true;
}

void @(topic)_Subscriber::SubListener::onSubscriptionMatched(Subscriber* sub, MatchingInfo& info)
{
(void)sub;

if (info.status == MATCHED_MATCHING)
{
n_matched++;
Expand All @@ -116,8 +125,7 @@ void @(topic)_Subscriber::SubListener::onSubscriptionMatched(Subscriber* sub, Ma
void @(topic)_Subscriber::SubListener::onNewDataMessage(Subscriber* sub)
{
// Take data
if(sub->takeNextData(&msg, &m_info))
@[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;
Expand Down Expand Up @@ -148,7 +156,7 @@ bool @(topic)_Subscriber::hasMsg()
return m_listener.has_msg;
}

@[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)_ @(topic)_Subscriber::getMsg()
@[else]@
@(topic) @(topic)_Subscriber::getMsg()
Expand Down
12 changes: 6 additions & 6 deletions msg/templates/urtps/Subscriber.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -54,7 +54,7 @@ topic = alias if alias else spec.short_name
* @@file @(topic)_Subscriber.h
* This header file contains the declaration of the subscriber functions.
*
* This file was generated by the tool fastcdrgen.
* This file was adapted from the fastcdrgen tool.
*/


Expand All @@ -64,7 +64,7 @@ topic = alias if alias else spec.short_name
#include <fastrtps/fastrtps_fwd.h>
#include <fastrtps/subscriber/SubscriberListener.h>
#include <fastrtps/subscriber/SampleInfo.h>
@[if float(fastrtps_version) < 1.9]@
@[if 1.5 < fastrtpsgen_version <= 1.7]@
#include "@(topic)_PubSubTypes.h"
@[else]@
#include "@(topic)PubSubTypes.h"
Expand All @@ -81,7 +81,7 @@ public:
bool init();
void run();
bool hasMsg();
@[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)_ getMsg();
@[else]@
@(topic) getMsg();
Expand All @@ -100,15 +100,15 @@ private:
SampleInfo_t m_info;
int n_matched;
int n_msg;
@[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)_ msg;
@[else]@
@(topic) msg;
@[end if]@
bool has_msg = false;

} m_listener;
@[if float(fastrtps_version) < 1.9]@
@[if 1.5 < fastrtpsgen_version <= 1.7]@
@(topic)_PubSubType myType;
@[else]@
@(topic)PubSubType myType;
Expand Down
2 changes: 1 addition & 1 deletion msg/templates/urtps/microRTPS_agent_CMakeLists.txt.em
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
################################################################################
#
# 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:
Expand Down
2 changes: 1 addition & 1 deletion msg/templates/urtps/microRTPS_transport.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/****************************************************************************
*
* 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:
Expand Down
Loading

0 comments on commit 1970ef8

Please sign in to comment.