Skip to content

Commit 050ae4b

Browse files
authored
EVALG-77: Add C++ example to Application Design tutorial (#695)
1 parent 4403d4d commit 050ae4b

File tree

8 files changed

+628
-49
lines changed

8 files changed

+628
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,65 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<?xml-model href="https://community.rti.com/schema/7.3.0/rti_dds_profiles.xsd" type="application/xml" schematypens="http://www.w3.org/2001/XMLSchema"?>
3-
<dds>
2+
<dds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://community.rti.com/schema/current/rti_dds_profiles.xsd">
44
<types>
5-
<const name="VIN_LENGTH" type="uint8" value="17"/>
6-
<typedef name="VIN" type="string" stringMaxLength="VIN_LENGTH"/>
7-
5+
<const name="VIN_LENGTH" type="uint8" value="17" />
6+
<typedef name="VIN" type="string" stringMaxLength="VIN_LENGTH" />
87
<struct name="Coord" extensibility="final" nested="true">
9-
<member name="lat" type="float64"/>
10-
<member name="lon" type="float64"/>
8+
<member name="lat" type="float64" />
9+
<member name="lon" type="float64" />
1110
</struct>
11+
<const name="VehicleTransitTopic" type="string" value="&quot;VehicleTransit&quot;" />
1212
<struct name="VehicleTransit" extensibility="appendable">
13-
<member name="vehicle_vin"
14-
type="nonBasic"
15-
nonBasicTypeName="VIN"
16-
key="true" />
17-
<member name="current_position"
18-
type="nonBasic"
19-
nonBasicTypeName="Coord" />
20-
<member name="current_route"
21-
type="nonBasic"
22-
nonBasicTypeName="Coord"
23-
sequenceMaxLength="-1"
24-
optional="true" /> <!-- 'no route' == standby -->
13+
<member name="vehicle_vin" type="nonBasic" nonBasicTypeName="VIN" key="true" />
14+
<member name="current_position" type="nonBasic" nonBasicTypeName="Coord" />
15+
<member name="current_route" type="nonBasic" nonBasicTypeName="Coord"
16+
sequenceMaxLength="-1" optional="true" />
2517
</struct>
26-
27-
<typedef name="Percentage" type="float64" min="0.0" max="100.0"/>
18+
<typedef name="Percentage" type="float64" min="0.0" max="100.0" />
19+
<const name="VehicleMetricsTopic" type="string" value="&quot;VehicleMetrics&quot;" />
2820
<struct name="VehicleMetrics" extensibility="appendable">
29-
<member name="vehicle_vin"
30-
type="nonBasic"
31-
nonBasicTypeName="VIN"
32-
key="true"/>
33-
<member name="fuel_level"
34-
type="nonBasic"
35-
nonBasicTypeName="Percentage"/>
21+
<member name="vehicle_vin" type="nonBasic" nonBasicTypeName="VIN" key="true" />
22+
<member name="fuel_level" type="nonBasic" nonBasicTypeName="Percentage" />
3623
</struct>
3724
</types>
3825

3926
<qos_library name="VehicleModeling_Library">
4027
<qos_profile name="VehicleMetrics_Profile">
41-
<datawriter_qos base_name="BuiltinQosLib::Generic.BestEffort">
28+
<datareader_qos base_name="BuiltinQosLib::Generic.BestEffort">
4229
<deadline>
4330
<period>
44-
<sec>10</sec>
31+
<sec>15</sec>
4532
<nanosec>0</nanosec>
4633
</period>
4734
</deadline>
48-
</datawriter_qos>
49-
<datareader_qos base_name="BuiltinQosLib::Generic.BestEffort">
35+
</datareader_qos>
36+
<datawriter_qos base_name="BuiltinQosLib::Generic.BestEffort">
5037
<deadline>
5138
<period>
52-
<sec>15</sec>
39+
<sec>10</sec>
5340
<nanosec>0</nanosec>
5441
</period>
5542
</deadline>
56-
</datareader_qos>
43+
</datawriter_qos>
5744
</qos_profile>
5845
<qos_profile name="VehicleTransit_Profile">
59-
<datawriter_qos base_name="BuiltinQosLib::Generic.StrictReliable">
46+
<datareader_qos base_name="BuiltinQosLib::Generic.KeepLastReliable">
6047
<durability>
6148
<kind>TRANSIENT_LOCAL_DURABILITY_QOS</kind>
6249
</durability>
63-
</datawriter_qos>
64-
<datareader_qos base_name="BuiltinQosLib::Generic.KeepLastReliable">
50+
</datareader_qos>
51+
<datawriter_qos base_name="BuiltinQosLib::Generic.StrictReliable">
6552
<durability>
6653
<kind>TRANSIENT_LOCAL_DURABILITY_QOS</kind>
6754
</durability>
68-
</datareader_qos>
55+
</datawriter_qos>
6956
</qos_profile>
7057
</qos_library>
7158

7259
<domain_library name="DomainLibrary">
7360
<domain name="VehicleDomain" domain_id="0">
61+
<register_type name="VehicleMetrics" />
62+
<register_type name="VehicleTransit" />
7463
<topic name="VehicleMetricsTopic" register_type_ref="VehicleMetrics" />
7564
<topic name="VehicleTransitTopic" register_type_ref="VehicleTransit" />
7665
</domain>
@@ -80,24 +69,26 @@
8069
<domain_participant name="PublisherApp" domain_ref="DomainLibrary::VehicleDomain">
8170
<publisher name="Publisher">
8271
<data_writer name="MetricsWriter" topic_ref="VehicleMetricsTopic">
83-
<datawriter_qos base_name="VehicleModeling_Library::VehicleMetrics_Profile" />
72+
<datawriter_qos base_name="VehicleModeling_Library::VehicleMetrics_Profile">
73+
</datawriter_qos>
8474
</data_writer>
85-
8675
<data_writer name="TransitWriter" topic_ref="VehicleTransitTopic">
87-
<datawriter_qos base_name="VehicleModeling_Library::VehicleTransit_Profile" />
88-
</data_writer>
89-
</publisher>
76+
<datawriter_qos base_name="VehicleModeling_Library::VehicleTransit_Profile">
77+
</datawriter_qos>
78+
</data_writer>
79+
</publisher>
9080
</domain_participant>
91-
9281
<domain_participant name="SubscriberApp" domain_ref="DomainLibrary::VehicleDomain">
9382
<subscriber name="Subscriber">
9483
<data_reader name="MetricsReader" topic_ref="VehicleMetricsTopic">
95-
<datareader_qos base_name="VehicleModeling_Library::VehicleMetrics_Profile" />
84+
<datareader_qos base_name="VehicleModeling_Library::VehicleMetrics_Profile">
85+
</datareader_qos>
9686
</data_reader>
9787
<data_reader name="TransitReader" topic_ref="VehicleTransitTopic">
98-
<datareader_qos base_name="VehicleModeling_Library::VehicleTransit_Profile" />
88+
<datareader_qos base_name="VehicleModeling_Library::VehicleTransit_Profile">
89+
</datareader_qos>
9990
</data_reader>
100-
</subscriber>
91+
</subscriber>
10192
</domain_participant>
10293
</domain_participant_library>
10394
</dds>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
#
4+
# RTI grants Licensee a license to use, modify, compile, and create derivative
5+
# works of the Software. Licensee has the right to distribute object form
6+
# only for use with RTI products. The Software is provided "as is", with no
7+
# warranty of any type, including any warranty for fitness for any purpose.
8+
# RTI is under no obligation to maintain or support the Software. RTI shall
9+
# not be liable for any incidental or consequential damages arising out of the
10+
# use or inability to use the software.
11+
#
12+
13+
cmake_minimum_required(VERSION 3.11)
14+
project(connext-tutorial-application-design)
15+
list(APPEND CMAKE_MODULE_PATH
16+
"${CMAKE_CURRENT_SOURCE_DIR}/../../../resources/cmake/Modules"
17+
)
18+
include(ConnextDdsConfigureCmakeUtils)
19+
connextdds_configure_cmake_utils()
20+
21+
# Include ConnextDdsAddExample.cmake from resources/cmake
22+
include(ConnextDdsAddExample)
23+
24+
connextdds_call_codegen(
25+
TYPE_PATH "../VehicleModeling.xml"
26+
LANG "C++11"
27+
PREFIX "VehicleModeling"
28+
)
29+
30+
connextdds_add_application(
31+
TARGET VehicleModeling_publisher
32+
LANG "C++11"
33+
OUTPUT_NAME "publisher"
34+
SOURCES
35+
$<TARGET_OBJECTS:VehicleModeling_CXX11_obj>
36+
"${CMAKE_CURRENT_SOURCE_DIR}/publisher.cxx"
37+
DEPENDENCIES
38+
NO_REQUIRE_QOS
39+
${_CONNEXT_DEPENDENCIES}
40+
)
41+
42+
connextdds_add_application(
43+
TARGET VehicleModeling_subscriber
44+
LANG "C++11"
45+
OUTPUT_NAME "subscriber"
46+
SOURCES
47+
$<TARGET_OBJECTS:VehicleModeling_CXX11_obj>
48+
"${CMAKE_CURRENT_SOURCE_DIR}/subscriber.cxx"
49+
DEPENDENCIES
50+
NO_REQUIRE_QOS
51+
${_CONNEXT_DEPENDENCIES}
52+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Tutorial: Application Design
2+
3+
This code is part of the Connext Application Design tutorial.
4+
5+
## Building the Example :wrench:
6+
7+
You can build the example following the instructions in the tutorial, or you can
8+
build it using CMake as follows.
9+
10+
1. Generate the build files:
11+
12+
```sh
13+
mkdir build
14+
cd build
15+
cmake ..
16+
```
17+
18+
This command will try to find the location of your Connext installation. If it
19+
can't find it, specify it with the ``-DCONNEXTDDS_DIR`` option, for example:
20+
21+
```sh
22+
cmake -DCONNEXTDDS_DIR=/home/rti/rti_connext_dds-x.y.z ..
23+
```
24+
25+
If you are compiling for windows you may also need to specify the
26+
[generator](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html),
27+
and platform.
28+
29+
```sh
30+
cmake .. -G "Visual Studio 17 2022" -A x64
31+
```
32+
33+
2. Build the applications:
34+
35+
```sh
36+
cmake --build .
37+
```
38+
39+
If you are using a multi-configuration generator, such as Visual Studio
40+
solutions, you can specify the configuration mode to build as follows:
41+
42+
```sh
43+
cmake --build . --config Release|Debug
44+
```
45+
46+
## Running the Example :rocket:
47+
48+
See the tutorial for instructions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// (c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
//
4+
// RTI grants Licensee a license to use, modify, compile, and create derivative
5+
// works of the Software. Licensee has the right to distribute object form
6+
// only for use with RTI products. The Software is provided "as is", with no
7+
// warranty of any type, including any warranty for fitness for any purpose.
8+
// RTI is under no obligation to maintain or support the Software. RTI shall
9+
// not be liable for any incidental or consequential damages arising out of the
10+
// use or inability to use the software.
11+
12+
#ifndef COMMON_HPP
13+
#define COMMON_HPP
14+
15+
#include <vector>
16+
#include <sstream>
17+
#include <random>
18+
19+
#include "VehicleModeling.hpp"
20+
21+
using CoordSequence = rti::core::bounded_sequence<Coord, 100>;
22+
23+
namespace { // Coord namespace
24+
25+
std::string to_string(const Coord &coord)
26+
{
27+
std::ostringstream ss;
28+
ss << "Coord(lat: " << coord.lat() << ", lon: " << coord.lon() << ")";
29+
return ss.str();
30+
}
31+
32+
} // namespace
33+
34+
namespace rti::core { // bounded_sequence namespace
35+
36+
std::string to_string(const CoordSequence &route)
37+
{
38+
using ::to_string;
39+
40+
std::ostringstream ss;
41+
ss << "Route(";
42+
for (auto it = route.begin(); it != route.end();) {
43+
ss << to_string(*it);
44+
if (++it != route.end()) {
45+
ss << ", ";
46+
}
47+
}
48+
ss << ")";
49+
return ss.str();
50+
}
51+
52+
} // namespace rti::core
53+
54+
namespace utils {
55+
56+
namespace details {
57+
58+
static std::random_device rd;
59+
static std::mt19937 gen { rd() };
60+
61+
}; // namespace details
62+
63+
void set_random_seed(unsigned seed)
64+
{
65+
details::gen.seed(seed);
66+
}
67+
68+
double random_range(double min, double max)
69+
{
70+
return std::uniform_real_distribution<>(min, max)(details::gen);
71+
}
72+
73+
double random_stduniform()
74+
{
75+
return random_range(0.0, 1.0);
76+
}
77+
78+
std::string new_vin()
79+
{
80+
static const std::string choices("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
81+
82+
std::ostringstream ss;
83+
for (int i = 0; i < VIN_LENGTH; ++i) {
84+
ss << choices[random_range(0, choices.size())];
85+
}
86+
return ss.str();
87+
}
88+
89+
90+
CoordSequence new_route(int n = 5)
91+
{
92+
CoordSequence route;
93+
for (int i = 0; i < n; ++i) {
94+
double r1 = random_stduniform() * 100.0;
95+
double r2 = random_stduniform() * 100.0;
96+
route.push_back(Coord { r1, r2 });
97+
}
98+
return route;
99+
}
100+
101+
} // namespace utils
102+
103+
#endif

0 commit comments

Comments
 (0)