mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-24 06:43:33 +08:00
micrortps bridge: clean templates; add backwards compatibility; add ROS2 compatibility
This commit is contained in:
@@ -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)_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>
|
||||
@@ -66,7 +66,6 @@ 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);}
|
||||
@@ -74,26 +73,34 @@ topic = alias if alias else spec.short_name
|
||||
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;
|
||||
@@ -101,6 +108,8 @@ bool @(topic)_Subscriber::init()
|
||||
|
||||
void @(topic)_Subscriber::SubListener::onSubscriptionMatched(Subscriber* sub, MatchingInfo& info)
|
||||
{
|
||||
(void)sub;
|
||||
|
||||
if (info.status == MATCHED_MATCHING)
|
||||
{
|
||||
n_matched++;
|
||||
@@ -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;
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user