Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New environment variable to change easily the publication mode #470

Merged
merged 10 commits into from
Nov 12, 2020
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ You have two ways of telling you ROS 2 application which XML to use:
1. Placing your XML file in the running directory under the name `DEFAULT_FASTRTPS_PROFILES.xml`.
2. Setting environment variable `FASTRTPS_DEFAULT_PROFILES_FILE` to your XML file.

### Change publication mode

Another way to change easily the publication mode is to use the environment variable `ROS_PUBLICATION_MODE`.
hidmic marked this conversation as resolved.
Show resolved Hide resolved
The admissible values are:
* `ASYNCHRONOUS`: asynchronous publication mode
* `SYNCHRONOUS`: synchronous publication mode
* `AUTO`: let the rmw implementation select the publication mode.

By default, `rmw_fastrtps` assumes this variable to be `ASYNCHRONOUS`.
Also, if you set the variable to `AUTO`, `rmw_fastrtps` will use the publication mode set in the XML file or, failing that, the default value set in Fast DDS (currently set to `SYNCHRONOUS`).

## Example

The following example configures Fast DDS to publish synchronously, and to have a pre-allocated history that can be expanded whenever it gets filled.
Expand Down
6 changes: 5 additions & 1 deletion rmw_fastrtps_cpp/src/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ rmw_fastrtps_cpp::create_publisher(
}

if (!participant_info->leave_middleware_default_qos) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
publisherParam.historyMemoryPolicy =
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
} else if (participant_info->publishing_mode == publishing_mode_t::SYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::SYNCHRONOUS_PUBLISH_MODE;
}
}

publisherParam.topic.topicKind =
Expand Down
6 changes: 5 additions & 1 deletion rmw_fastrtps_cpp/src/rmw_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ rmw_create_client(
qos_policies, ros_service_response_prefix, service_name, "Reply");

if (!participant_info->leave_middleware_default_qos) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
publisherParam.historyMemoryPolicy =
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
} else if (participant_info->publishing_mode == publishing_mode_t::SYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::SYNCHRONOUS_PUBLISH_MODE;
}
}

publisherParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
Expand Down
6 changes: 5 additions & 1 deletion rmw_fastrtps_cpp/src/rmw_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,13 @@ rmw_create_service(
qos_policies, ros_service_requester_prefix, service_name, "Request");

if (!impl->leave_middleware_default_qos) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
publisherParam.historyMemoryPolicy =
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
if (impl->publishing_mode == publishing_mode_t::ASYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
} else if (impl->publishing_mode == publishing_mode_t::SYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::SYNCHRONOUS_PUBLISH_MODE;
}
}

publisherParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
Expand Down
6 changes: 5 additions & 1 deletion rmw_fastrtps_dynamic_cpp/src/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ rmw_fastrtps_dynamic_cpp::create_publisher(
}

if (!participant_info->leave_middleware_default_qos) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
publisherParam.historyMemoryPolicy =
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
} else if (participant_info->publishing_mode == publishing_mode_t::SYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::SYNCHRONOUS_PUBLISH_MODE;
}
}

publisherParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
Expand Down
6 changes: 5 additions & 1 deletion rmw_fastrtps_dynamic_cpp/src/rmw_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,13 @@ rmw_create_client(
qos_policies, ros_service_response_prefix, service_name, "Reply");

if (!participant_info->leave_middleware_default_qos) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
publisherParam.historyMemoryPolicy =
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
if (participant_info->publishing_mode == publishing_mode_t::ASYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
} else if (participant_info->publishing_mode == publishing_mode_t::SYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::SYNCHRONOUS_PUBLISH_MODE;
}
}

publisherParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
Expand Down
6 changes: 5 additions & 1 deletion rmw_fastrtps_dynamic_cpp/src/rmw_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,13 @@ rmw_create_service(
qos_policies, ros_service_requester_prefix, service_name, "Request");

if (!impl->leave_middleware_default_qos) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
publisherParam.historyMemoryPolicy =
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
if (impl->publishing_mode == publishing_mode_t::ASYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::ASYNCHRONOUS_PUBLISH_MODE;
} else if (impl->publishing_mode == publishing_mode_t::SYNCHRONOUS) {
publisherParam.qos.m_publishMode.kind = eprosima::fastrtps::SYNCHRONOUS_PUBLISH_MODE;
}
}

publisherParam.topic.topicKind = eprosima::fastrtps::rtps::NO_KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ using rmw_dds_common::operator<<;

class ParticipantListener;

enum class publishing_mode_t
{
ASYNCHRONOUS, // Asynchronous publishing mode
SYNCHRONOUS, // Synchronous publishing mode
AUTO // Use publishing mode set in XML file or Fast DDS default
};

typedef struct CustomParticipantInfo
{
eprosima::fastrtps::Participant * participant;
Expand All @@ -53,6 +60,7 @@ typedef struct CustomParticipantInfo
// their settings are going to be overwritten by code
// with the default configuration.
bool leave_middleware_default_qos;
publishing_mode_t publishing_mode;
} CustomParticipantInfo;

class ParticipantListener : public eprosima::fastrtps::ParticipantListener
Expand Down
18 changes: 18 additions & 0 deletions rmw_fastrtps_shared_cpp/src/participant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ __create_participant(
const char * identifier,
ParticipantAttributes participantAttrs,
bool leave_middleware_default_qos,
publishing_mode_t publishing_mode,
rmw_dds_common::Context * common_context)
{
// Declare everything before beginning to create things.
Expand Down Expand Up @@ -122,6 +123,7 @@ __create_participant(
goto fail;
}
participant_info->leave_middleware_default_qos = leave_middleware_default_qos;
participant_info->publishing_mode = publishing_mode;

participant_info->participant = participant;
participant_info->listener = listener;
Expand Down Expand Up @@ -185,6 +187,7 @@ rmw_fastrtps_shared_cpp::create_participant(
participantAttrs.rtps.setName(enclave);

bool leave_middleware_default_qos = false;
publishing_mode_t publishing_mode = publishing_mode_t::ASYNCHRONOUS;
const char * env_value;
const char * error_str;
error_str = rcutils_get_env("RMW_FASTRTPS_USE_QOS_FROM_XML", &env_value);
Expand All @@ -194,6 +197,20 @@ rmw_fastrtps_shared_cpp::create_participant(
}
if (env_value != nullptr) {
leave_middleware_default_qos = strcmp(env_value, "1") == 0;
} else if (env_value == nullptr || !leave_middleware_default_qos) {
error_str = rcutils_get_env("ROS_PUBLICATION_MODE", &env_value);
if (error_str != NULL) {
RCUTILS_LOG_DEBUG_NAMED("rmw_fastrtps_shared_cpp", "Error getting env var: %s\n", error_str);
hidmic marked this conversation as resolved.
Show resolved Hide resolved
return nullptr;
}
if (env_value != nullptr) {
// Synchronous publishing
if (strcmp(env_value, "SYNCHRONOUS") == 0) {
publishing_mode = publishing_mode_t::SYNCHRONOUS;
} else if (strcmp(env_value, "AUTO") == 0) {
publishing_mode = publishing_mode_t::AUTO;
}
JLBuenoLopez marked this conversation as resolved.
Show resolved Hide resolved
}
}
// allow reallocation to support discovery messages bigger than 5000 bytes
if (!leave_middleware_default_qos) {
Expand Down Expand Up @@ -257,6 +274,7 @@ rmw_fastrtps_shared_cpp::create_participant(
identifier,
participantAttrs,
leave_middleware_default_qos,
publishing_mode,
common_context);
}

Expand Down