Skip to content

Commit 7c88a8e

Browse files
cagueroiche033arjo129
authored
Add comms infrastructure (#1416)
This PR adds some infrastructure for being able to create comms systems. A comms system lets you send and receive data under the constrains of a given comms model. The comms model specifies when and how messages should be delivered to its destinations. Subscribers create a regular callback via Ignition Transport and send a request to a centralized broker to bind an address, the robot attached to the address and the topic name used as a callback. Publishers fill a Dataframe message specifying the addresses of the sender and destination and set the payload. Then, publishers need to send via Ignition Transport the message to the centralized broker. Besides the general infrastructure, this PR contains two systems: PerfectComms and CommsEndpoint. PerfectComms is an example of a comms system. As required, it implements the ICommsModel interface and it always delivers the messages to their destinations. CommsEndpoint is a helper system that can be optionally attached to a model (see examples/worlds/comms.sdf) and lets you assign an address to a robot and the subscription topic. The system automatically connects with the broker and bind this address/robot for you. You could do this process manually if needed but I think it's convenient for most of the users. Signed-off-by: Carlos Agüero <[email protected]> Co-authored-by: Ian Chen <[email protected]> Co-authored-by: Arjo Chakravarty <[email protected]>
1 parent b825c27 commit 7c88a8e

21 files changed

+2132
-0
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
2+
3+
project(ign-gazebo-comms)
4+
5+
find_package(ignition-transport11 QUIET REQUIRED)
6+
set(IGN_TRANSPORT_VER ${ignition-transport11_VERSION_MAJOR})
7+
8+
add_executable(publisher publisher.cc)
9+
target_link_libraries(publisher
10+
ignition-transport${IGN_TRANSPORT_VER}::core)
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (C) 2022 Open Source Robotics Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
// Example: ./publisher addr1
19+
20+
#include <atomic>
21+
#include <chrono>
22+
#include <csignal>
23+
#include <iostream>
24+
#include <string>
25+
#include <thread>
26+
27+
#include <ignition/msgs.hh>
28+
#include <ignition/transport.hh>
29+
30+
/// \brief Flag used to break the publisher loop and terminate the program.
31+
static std::atomic<bool> g_terminatePub(false);
32+
33+
//////////////////////////////////////////////////
34+
/// \brief Usage function.
35+
void usage()
36+
{
37+
std::cerr << "./publisher <dst_address>" << std::endl;
38+
}
39+
40+
//////////////////////////////////////////////////
41+
/// \brief Function callback executed when a SIGINT or SIGTERM signals are
42+
/// captured. This is used to break the infinite loop that publishes messages
43+
/// and exit the program smoothly.
44+
void signal_handler(int _signal)
45+
{
46+
if (_signal == SIGINT || _signal == SIGTERM)
47+
g_terminatePub = true;
48+
}
49+
50+
//////////////////////////////////////////////////
51+
int main(int argc, char **argv)
52+
{
53+
if (argc != 2)
54+
{
55+
usage();
56+
return -1;
57+
}
58+
59+
// Install a signal handler for SIGINT and SIGTERM.
60+
std::signal(SIGINT, signal_handler);
61+
std::signal(SIGTERM, signal_handler);
62+
63+
// Create a transport node and advertise a topic.
64+
ignition::transport::Node node;
65+
std::string topic = "/broker/msgs";
66+
67+
auto pub = node.Advertise<ignition::msgs::Dataframe>(topic);
68+
if (!pub)
69+
{
70+
std::cerr << "Error advertising topic [" << topic << "]" << std::endl;
71+
return -1;
72+
}
73+
74+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
75+
76+
// Prepare the message.
77+
ignition::msgs::Dataframe msg;
78+
msg.set_src_address("addr1");
79+
msg.set_dst_address(argv[1]);
80+
81+
// Publish messages at 1Hz.
82+
int counter = 0;
83+
while (!g_terminatePub)
84+
{
85+
// Prepare the payload.
86+
ignition::msgs::StringMsg payload;
87+
payload.set_data("hello world " + std::to_string(counter));
88+
std::string serializedData;
89+
if (!payload.SerializeToString(&serializedData))
90+
{
91+
std::cerr << "Error serializing message\n"
92+
<< payload.DebugString() << std::endl;
93+
}
94+
msg.set_data(serializedData);
95+
96+
if (!pub.Publish(msg))
97+
break;
98+
99+
++counter;
100+
101+
std::cout << "Publishing hello on topic [" << topic << "]" << std::endl;
102+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
103+
}
104+
105+
return 0;
106+
}

examples/worlds/perfect_comms.sdf

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?xml version="1.0" ?>
2+
<!--
3+
Ignition Gazebo comms plugin demo.
4+
5+
Compile the comms publisher:
6+
cd ign-gazebo/examples/standalone/comms
7+
mkdir build
8+
cd build
9+
cmake ..
10+
make
11+
12+
Try launching a comms subscriber:
13+
ign topic -e -t addr1/rx
14+
15+
Try launching a comms publisher:
16+
./publisher addr1
17+
-->
18+
<sdf version="1.6">
19+
<world name="perfect_comms">
20+
21+
<physics name="1ms" type="ignored">
22+
<max_step_size>0.001</max_step_size>
23+
<real_time_factor>1.0</real_time_factor>
24+
</physics>
25+
<plugin
26+
filename="ignition-gazebo-physics-system"
27+
name="ignition::gazebo::systems::Physics">
28+
</plugin>
29+
<plugin
30+
filename="ignition-gazebo-user-commands-system"
31+
name="ignition::gazebo::systems::UserCommands">
32+
</plugin>
33+
<plugin
34+
filename="ignition-gazebo-scene-broadcaster-system"
35+
name="ignition::gazebo::systems::SceneBroadcaster">
36+
</plugin>
37+
<plugin
38+
filename="ignition-gazebo-perfect-comms-system"
39+
name="ignition::gazebo::systems::PerfectComms">
40+
</plugin>
41+
42+
<light type="directional" name="sun">
43+
<cast_shadows>true</cast_shadows>
44+
<pose>0 0 10 0 0 0</pose>
45+
<diffuse>1 1 1 1</diffuse>
46+
<specular>0.5 0.5 0.5 1</specular>
47+
<attenuation>
48+
<range>1000</range>
49+
<constant>0.9</constant>
50+
<linear>0.01</linear>
51+
<quadratic>0.001</quadratic>
52+
</attenuation>
53+
<direction>-0.5 0.1 -0.9</direction>
54+
</light>
55+
56+
<model name="ground_plane">
57+
<static>true</static>
58+
<link name="link">
59+
<collision name="collision">
60+
<geometry>
61+
<plane>
62+
<normal>0 0 1</normal>
63+
<size>100 100</size>
64+
</plane>
65+
</geometry>
66+
</collision>
67+
<visual name="visual">
68+
<geometry>
69+
<plane>
70+
<normal>0 0 1</normal>
71+
<size>100 100</size>
72+
</plane>
73+
</geometry>
74+
<material>
75+
<ambient>0.8 0.8 0.8 1</ambient>
76+
<diffuse>0.8 0.8 0.8 1</diffuse>
77+
<specular>0.8 0.8 0.8 1</specular>
78+
</material>
79+
</visual>
80+
</link>
81+
</model>
82+
83+
<model name="box1">
84+
<pose>2 0 0.5 0 0 0</pose>
85+
<link name="box_link">
86+
<inertial>
87+
<inertia>
88+
<ixx>0.16666</ixx>
89+
<ixy>0</ixy>
90+
<ixz>0</ixz>
91+
<iyy>0.16666</iyy>
92+
<iyz>0</iyz>
93+
<izz>0.16666</izz>
94+
</inertia>
95+
<mass>1.0</mass>
96+
</inertial>
97+
<collision name="box_collision">
98+
<geometry>
99+
<box>
100+
<size>1 1 1</size>
101+
</box>
102+
</geometry>
103+
</collision>
104+
105+
<visual name="box_visual">
106+
<geometry>
107+
<box>
108+
<size>1 1 1</size>
109+
</box>
110+
</geometry>
111+
<material>
112+
<ambient>1 0 0 1</ambient>
113+
<diffuse>1 0 0 1</diffuse>
114+
<specular>1 0 0 1</specular>
115+
</material>
116+
</visual>
117+
</link>
118+
119+
<plugin
120+
filename="ignition-gazebo-comms-endpoint-system"
121+
name="ignition::gazebo::systems::CommsEndpoint">
122+
<address>addr1</address>
123+
<topic>addr1/rx</topic>
124+
</plugin>
125+
</model>
126+
127+
<model name="box2">
128+
<pose>-2 0 0.5 0 0 0</pose>
129+
<link name="box_link">
130+
<inertial>
131+
<inertia>
132+
<ixx>0.16666</ixx>
133+
<ixy>0</ixy>
134+
<ixz>0</ixz>
135+
<iyy>0.16666</iyy>
136+
<iyz>0</iyz>
137+
<izz>0.16666</izz>
138+
</inertia>
139+
<mass>1.0</mass>
140+
</inertial>
141+
<collision name="box_collision">
142+
<geometry>
143+
<box>
144+
<size>1 1 1</size>
145+
</box>
146+
</geometry>
147+
</collision>
148+
149+
<visual name="box_visual">
150+
<geometry>
151+
<box>
152+
<size>1 1 1</size>
153+
</box>
154+
</geometry>
155+
<material>
156+
<ambient>0 0 1 1</ambient>
157+
<diffuse>0 0 1 1</diffuse>
158+
<specular>0 0 1 1</specular>
159+
</material>
160+
</visual>
161+
</link>
162+
163+
<plugin
164+
filename="ignition-gazebo-comms-endpoint-system"
165+
name="ignition::gazebo::systems::CommsEndpoint">
166+
<address>addr2</address>
167+
<topic>addr2/rx</topic>
168+
<broker>
169+
<bind_service>/broker/bind</bind_service>
170+
<unbind_service>/broker/unbind</unbind_service>
171+
</broker>
172+
</plugin>
173+
174+
</model>
175+
176+
</world>
177+
</sdf>

0 commit comments

Comments
 (0)