os1_node.cpp
Go to the documentation of this file.
1 
17 #include <chrono>
18 #include <functional>
19 #include <iostream>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
25 #include <ros/console.h>
26 #include <ros/ros.h>
27 #include <sensor_msgs/Imu.h>
28 #include <sensor_msgs/PointCloud2.h>
29 
30 #include "ouster_driver/PacketMsg.h"
31 #include "ouster_driver/os1_ros.h"
32 
33 using ns = std::chrono::nanoseconds;
35 
36 int main(int argc, char** argv) {
37  ros::init(argc, argv, "os1_node");
38  ros::NodeHandle nh("~");
39 
40  auto scan_dur = ns(nh.param("scan_dur_ns", 100000000));
41  auto os1_hostname = nh.param("os1_lidar_address", std::string("localhost"));
42  auto os1_udp_dest = nh.param("pc_address", std::string("192.168.1.1"));
43  auto os1_lidar_port = nh.param("os1_lidar_port", -1);
44  auto os1_imu_port = nh.param("os1_imu_port", -1);
45  auto replay_mode = nh.param("replay", true);
46  auto points_topic_name = nh.param("points_topic_name", std::string("/points_raw"));
47  auto imu_topic_name = nh.param("imu_topic_name", std::string("/imu_raw"));
48  auto lidar_frame_name = nh.param("lidar_frame_name", std::string("/os1"));
49  auto imu_frame_name = nh.param("imu_frame_name", std::string("/os1_imu"));
50  auto pointcloud_mode = nh.param("pointcloud_mode", std::string("NATIVE"));
51  auto operation_mode_str = nh.param("operation_mode", std::string("1024x10"));
52  auto pulse_mode_str = nh.param("pulse_mode", std::string("STANDARD"));
53  auto window_rejection = nh.param("window_rejection", true);
54 
58  //defines the pointcloud mode
59  ouster_driver::OS1::set_point_mode(pointcloud_mode);
60  //----------------
64  //defines the advanced parameters
65  ouster::OS1::set_advanced_params(operation_mode_str, pulse_mode_str, window_rejection);
66  auto queue_size = 10;
67  if (operation_mode_str == std::string("512x20") || operation_mode_str == std::string("1024x20")) {
68  queue_size = 20;
69  scan_dur = scan_dur / 2; //scan duration should be smaller at faster frame rates
70  }
71  //----------------
72 
73  auto lidar_pub = nh.advertise<sensor_msgs::PointCloud2>(points_topic_name, queue_size);
74  auto imu_pub = nh.advertise<sensor_msgs::Imu>(imu_topic_name, queue_size);
75 
76  auto lidar_handler = ouster_driver::OS1::batch_packets(
77  scan_dur, [&](ns scan_ts, const ouster_driver::OS1::CloudOS1& cloud) {
78  lidar_pub.publish(
79  ouster_driver::OS1::cloud_to_cloud_msg(cloud, scan_ts, lidar_frame_name));
80  });
81 
82  auto imu_handler = [&](const PacketMsg& p) {
83  imu_pub.publish(ouster_driver::OS1::packet_to_imu_msg(p, imu_frame_name));
84  };
85 
86  if (replay_mode) {
87  auto lidar_packet_sub = nh.subscribe<PacketMsg, const PacketMsg&>(
88  "lidar_packets", 500, lidar_handler);
89  auto imu_packet_sub = nh.subscribe<PacketMsg, const PacketMsg&>(
90  "imu_packets", 500, imu_handler);
91  ros::spin();
92  } else {
93  auto lidar_packet_pub = nh.advertise<PacketMsg>("lidar_packets", 500);
94  auto imu_packet_pub = nh.advertise<PacketMsg>("imu_packets", 500);
95 
96  auto cli = ouster::OS1::init_client(os1_hostname, os1_udp_dest,
97  os1_lidar_port, os1_imu_port);
98  if (!cli) {
99  ROS_ERROR("Failed to initialize sensor at: %s", os1_hostname.c_str());
100  return 1;
101  }
102 
104  [&](const PacketMsg& pm) {
105  lidar_packet_pub.publish(pm);
106  lidar_handler(pm);
107  },
108  [&](const PacketMsg& pm) {
109  imu_packet_pub.publish(pm);
110  imu_handler(pm);
111  });
112  }
113  return 0;
114 }
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
std::shared_ptr< client > init_client(const std::string &hostname, const std::string &udp_dest_host, int lidar_port, int imu_port)
Definition: os1.cpp:114
pcl::PointCloud< PointOS1 > CloudOS1
Definition: os1_ros.h:23
ROSCPP_DECL void spin(Spinner &spinner)
void set_point_mode(std::string mode_xyzir)
Definition: os1_ros.cpp:207
ouster_driver::PacketMsg PacketMsg
Definition: os1_node.cpp:34
void set_advanced_params(std::string operation_mode_str, std::string pulse_mode_str, bool window_rejection)
Definition: os1.cpp:323
int main(int argc, char **argv)
Definition: os1_node.cpp:36
void spin(const ouster::OS1::client &cli, const std::function< void(const PacketMsg &pm)> &lidar_handler, const std::function< void(const PacketMsg &pm)> &imu_handler)
Definition: os1_ros.cpp:153
std::function< void(const PacketMsg &)> batch_packets(ns scan_dur, const std::function< void(ns, const CloudOS1 &)> &f)
Definition: os1_ros.h:120
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
sensor_msgs::Imu packet_to_imu_msg(const PacketMsg &pm, const std::string &frame="os1_imu")
Definition: os1_ros.cpp:32
std::chrono::nanoseconds ns
Definition: os1_node.cpp:33
#define ROS_ERROR(...)
sensor_msgs::PointCloud2 cloud_to_cloud_msg(const CloudOS1 &cloud, ns timestamp, const std::string &frame="os1")
Definition: os1_ros.cpp:70


ouster
Author(s): ouster developers
autogenerated on Mon Jun 10 2019 14:16:21