Skip to content

Commit b9b7d3d

Browse files
committed
Granular includes and update documentation
Signed-off-by: Alejandro Hernandez Cordero <ahcorde@gmail.com>
1 parent d5aa18a commit b9b7d3d

4 files changed

Lines changed: 58 additions & 20 deletions

File tree

README.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,24 @@ $ colcon build --merge-install --event-handlers console_direct+
3535
## Code of the Publisher
3636
Take a look at my_publisher.cpp
3737
```cpp
38-
#include <point_cloud_transport/point_cloud_transport.hpp>
38+
#include <chrono>
39+
#include <iostream>
40+
#include <filesystem>
41+
#include <memory>
42+
#include <string>
3943

4044
// for reading rosbag
4145
#include <ament_index_cpp/get_package_share_path.hpp>
46+
47+
#include <point_cloud_transport/point_cloud_transport.hpp>
48+
#include <rclcpp/executors/single_threaded_executor.hpp>
49+
#include <rclcpp/node.hpp>
4250
#include <rclcpp/serialization.hpp>
43-
#include <rclcpp/rclcpp.hpp>
51+
#include <rclcpp/serialized_message.hpp>
52+
#include <rclcpp/utilities.hpp>
4453
#include <rosbag2_cpp/reader.hpp>
45-
#include <rosbag2_cpp/storage_options.hpp>
46-
#include <rosbag2_cpp/converter_interfaces/serialization_format_converter.hpp>
54+
#include <rosbag2_storage/storage_options.hpp>
55+
#include <rosbag2_cpp/converter_options.hpp>
4756
#include <sensor_msgs/msg/point_cloud2.hpp>
4857

4958
int main(int argc, char ** argv)
@@ -55,17 +64,28 @@ int main(int argc, char ** argv)
5564
rclcpp::executors::SingleThreadedExecutor executor;
5665
executor.add_node(node);
5766

58-
point_cloud_transport::PointCloudTransport pct(node);
67+
point_cloud_transport::PointCloudTransport pct(*node);
5968
point_cloud_transport::Publisher pub = pct.advertise("pct/point_cloud", 100);
6069

6170
const std::string bagged_cloud_topic = "/point_cloud";
62-
const std::string shared_directory = ament_index_cpp::get_package_share_path(
63-
"point_cloud_transport_tutorial");
64-
const std::string bag_file = shared_directory + "/resources/rosbag2_2023_08_05-16_08_51";
71+
std::filesystem::path bag_file =
72+
ament_index_cpp::get_package_share_path("point_cloud_transport_tutorial") / "resources" /
73+
"rosbag2_2023_08_05-16_08_51";
74+
75+
if (argc > 1) {
76+
bag_file = std::filesystem::path(argv[1]);
77+
}
78+
79+
if (!std::filesystem::exists(bag_file)) {
80+
std::cout << "Not able to open file [" << bag_file.string() << "]" << '\n';
81+
return -1;
82+
}
83+
84+
std::cout << "Reading [" << bag_file.string() << "] bagfile" << '\n';
6585

6686
// boiler-plate to tell rosbag2 how to read our bag
6787
rosbag2_storage::StorageOptions storage_options;
68-
storage_options.uri = bag_file;
88+
storage_options.uri = bag_file.string();
6989
storage_options.storage_id = "mcap";
7090
rosbag2_cpp::ConverterOptions converter_options;
7191
converter_options.input_serialization_format = "cdr";
@@ -82,7 +102,7 @@ int main(int argc, char ** argv)
82102
auto serialized_message = reader.read_next();
83103
rclcpp::SerializedMessage extracted_serialized_msg(*serialized_message->serialized_data);
84104
if (serialized_message->topic_name == bagged_cloud_topic) {
85-
// deserialize and convert to message
105+
// deserialize and convert to ros2 message
86106
cloud_serialization.deserialize_message(&extracted_serialized_msg, &cloud_msg);
87107
// publish the message
88108
pub.publish(cloud_msg);
@@ -108,7 +128,7 @@ Header for including [<point_cloud_transport>](https://github.com/ros-perception
108128
Creates *PointCloudTransport* instance and initializes it with our *Node* shared pointer. Methods of *PointCloudTransport* can later be used to create point cloud publishers and subscribers similar to how methods of *Node* are used to create generic publishers and subscribers.
109129

110130
```cpp
111-
point_cloud_transport::PointCloudTransport pct(node);
131+
point_cloud_transport::PointCloudTransport pct(*node);
112132
```
113133
114134
Uses *PointCloudTransport* method to create a publisher on base topic *"pct/point_cloud"*. Depending on whether more plugins are built, additional (per-plugin) topics derived from the base topic may also be advertised. The second argument is the size of our publishing queue.
@@ -143,16 +163,21 @@ In this section, we'll see how to create a subscriber node, which receives `Poin
143163
Take a look at [my_subscriber.cpp](src/my_subscriber.cpp):
144164

145165
```cpp
166+
#include <memory>
167+
146168
#include <point_cloud_transport/point_cloud_transport.hpp>
147-
#include <rclcpp/rclcpp.hpp>
169+
#include <rclcpp/executors.hpp>
170+
#include <rclcpp/logging.hpp>
171+
#include <rclcpp/node.hpp>
172+
#include <rclcpp/utilities.hpp>
148173
#include <sensor_msgs/msg/point_cloud2.hpp>
149174

150175
int main(int argc, char ** argv)
151176
{
152177
rclcpp::init(argc, argv);
153178
auto node = std::make_shared<rclcpp::Node>("point_cloud_subscriber");
154179

155-
point_cloud_transport::PointCloudTransport pct(node);
180+
point_cloud_transport::PointCloudTransport pct(*node);
156181
point_cloud_transport::Subscriber pct_sub = pct.subscribe(
157182
"pct/point_cloud", 100,
158183
[node](const sensor_msgs::msg::PointCloud2::ConstSharedPtr & msg)
@@ -191,7 +216,7 @@ auto node = rclcpp::Node::make_shared("point_cloud_subscriber");
191216
Creates *PointCloudTransport* instance and initializes it with our *Node*. Methods of *PointCloudTransport* can later be used to create point cloud publishers and subscribers similar to how methods of *NodeHandle* are used to create generic publishers and subscribers.
192217
193218
```cpp
194-
point_cloud_transport::PointCloudTransport pct(node);
219+
point_cloud_transport::PointCloudTransport pct(*node);
195220
```
196221

197222
Uses *PointCloudTransport* method to create a subscriber on base topic *"pct/point_cloud"*. The second argument is the size of our subscribing queue. The third argument tells the subscriber to execute lambda function whenever a message is received.

src/my_encoder.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@
2727
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2828
// POSSIBILITY OF SUCH DAMAGE.
2929

30+
#include <filesystem>
31+
#include <string>
32+
3033
#include <ament_index_cpp/get_package_share_path.hpp>
34+
#include <rclcpp/logger.hpp>
35+
#include <rclcpp/logging.hpp>
3136
#include <rclcpp/serialization.hpp>
32-
#include <rclcpp/rclcpp.hpp>
37+
#include <rclcpp/serialized_message.hpp>
3338
#include <rosbag2_cpp/reader.hpp>
3439
#include <rosbag2_storage/storage_options.hpp>
3540
#include <rosbag2_cpp/converter_options.hpp>
3641
#include <rosbag2_cpp/converter_interfaces/serialization_format_converter.hpp>
42+
#include <sensor_msgs/msg/point_cloud2.hpp>
3743

3844
#include <point_cloud_transport/point_cloud_codec.hpp>
3945

src/my_publisher.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
// POSSIBILITY OF SUCH DAMAGE.
2929

3030

31+
#include <chrono>
3132
#include <iostream>
3233
#include <filesystem>
3334
#include <memory>
@@ -37,12 +38,14 @@
3738
#include <ament_index_cpp/get_package_share_path.hpp>
3839

3940
#include <point_cloud_transport/point_cloud_transport.hpp>
41+
#include <rclcpp/executors/single_threaded_executor.hpp>
42+
#include <rclcpp/node.hpp>
4043
#include <rclcpp/serialization.hpp>
41-
#include <rclcpp/rclcpp.hpp>
42-
#include <rcpputils/filesystem_helper.hpp>
44+
#include <rclcpp/serialized_message.hpp>
45+
#include <rclcpp/utilities.hpp>
4346
#include <rosbag2_cpp/reader.hpp>
4447
#include <rosbag2_storage/storage_options.hpp>
45-
#include <rosbag2_cpp/converter_interfaces/serialization_format_converter.hpp>
48+
#include <rosbag2_cpp/converter_options.hpp>
4649
#include <sensor_msgs/msg/point_cloud2.hpp>
4750

4851
int main(int argc, char ** argv)
@@ -58,7 +61,6 @@ int main(int argc, char ** argv)
5861
point_cloud_transport::Publisher pub = pct.advertise("pct/point_cloud", 100);
5962

6063
const std::string bagged_cloud_topic = "/point_cloud";
61-
std::filesystem::path shared_directory;
6264
std::filesystem::path bag_file =
6365
ament_index_cpp::get_package_share_path("point_cloud_transport_tutorial") / "resources" /
6466
"rosbag2_2023_08_05-16_08_51";

src/my_subscriber.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@
2828
// POSSIBILITY OF SUCH DAMAGE.
2929

3030

31+
#include <memory>
32+
3133
#include <point_cloud_transport/point_cloud_transport.hpp>
32-
#include <rclcpp/rclcpp.hpp>
34+
#include <rclcpp/executors.hpp>
35+
#include <rclcpp/logging.hpp>
36+
#include <rclcpp/node.hpp>
37+
#include <rclcpp/utilities.hpp>
3338
#include <sensor_msgs/msg/point_cloud2.hpp>
3439

3540
int main(int argc, char ** argv)

0 commit comments

Comments
 (0)