-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathndt_node.cpp
92 lines (78 loc) · 3.25 KB
/
ndt_node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cstdio>
#include <memory>
#include <string>
#include "rclcpp/rclcpp.hpp"
#include "rcutils/cmdline_parser.h"
#include "std_msgs/msg/string.hpp"
#include <sensor_msgs/msg/point_cloud2.hpp>
void print_usage()
{
printf("Usage for listener app:\n");
printf("listener [-t topic_name] [-h]\n");
printf("options:\n");
printf("-h : Print this help function.\n");
printf("-t topic_name : Specify the topic on which to subscribe. Defaults to chatter.\n");
}
// Create a Listener class that subclasses the generic rclcpp::Node base class.
// The main function below will instantiate the class as a ROS node.
class Listener : public rclcpp::Node
{
public:
explicit Listener(const std::string & topic_name, const std::string & topic_name2 = "map")
: Node("listener")
{
// Create a callback function for when messages are received.
// Variations of this function also exist using, for example UniquePtr for zero-copy transport.
auto callback =
[this](const sensor_msgs::msg::PointCloud2::SharedPtr msg) -> void
{
RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg->header.frame_id.c_str());
//TODO:
// here you call NdtLib function and pass in the msg as input
// return a pose message and publish it as https://github.com/ros2/common_interfaces/blob/master/geometry_msgs/msg/PoseStamped.msg
};
auto callback2 =
[this](const sensor_msgs::msg::PointCloud2::SharedPtr msg) -> void
{
RCLCPP_INFO(this->get_logger(), "I heard: [%s]", msg->header.frame_id.c_str());
//TODO: here you get your map point cloud (one time only)
};
// Create a subscription to the topic which can be matched with one or more compatible ROS
// publishers.
// Note that not all publishers on the same topic with the same type will be compatible:
// they must have compatible Quality of Service policies.
sub_ = create_subscription<sensor_msgs::msg::PointCloud2>(topic_name, callback);
sub2_ = create_subscription<sensor_msgs::msg::PointCloud2>(topic_name2, callback2);
// TODO: create a pose publisher, see for reference
// https://github.com/ros2/demos/blob/master/demo_nodes_cpp/src/topics/talker.cpp
}
private:
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr sub_;
rclcpp::Subscription<sensor_msgs::msg::PointCloud2>::SharedPtr sub2_;
};
int main(int argc, char * argv[])
{
// Force flush of the stdout buffer.
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
if (rcutils_cli_option_exist(argv, argv + argc, "-h")) {
print_usage();
return 0;
}
// Initialize any global resources needed by the middleware and the client library.
// You must call this before using any other part of the ROS system.
// This should be called once per process.
rclcpp::init(argc, argv);
// Parse the command line options.
auto topic = std::string("points_raw");
char * cli_option = rcutils_cli_get_option(argv, argv + argc, "-t");
if (nullptr != cli_option) {
topic = std::string(cli_option);
}
// Create a node.
auto node = std::make_shared<Listener>(topic);
// spin will block until work comes in, execute work as it becomes available, and keep blocking.
// It will only be interrupted by Ctrl-C.
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}