Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drawing Point Cloud retrieve from Velodyne VLP-16
cmake_minimum_required( VERSION 2.8 )
# Create Project
project( solution )
add_executable( project main.cpp )
# Set StartUp Project (Option)
# (This setting is able to enable by using CMake 3.6.0 RC1 or later.)
set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "project" )
# Find Packages
find_package( PCL 1.8 REQUIRED )
if( PCL_FOUND )
# [C/C++]>[General]>[Additional Include Directories]
include_directories( ${PCL_INCLUDE_DIRS} )
# [C/C++]>[Preprocessor]>[Preprocessor Definitions]
add_definitions( ${PCL_DEFINITIONS} )
# For Use Not PreCompiled Features
#add_definitions( -DPCL_NO_PRECOMPILE )
# [Linker]>[General]>[Additional Library Directories]
link_directories( ${PCL_LIBRARY_DIRS} )
# [Linker]>[Input]>[Additional Dependencies]
target_link_libraries( project ${PCL_LIBRARIES} )
endif()
#include <iostream>
#include <string>
#include <vector>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/vlp_grabber.h>
#include <pcl/console/parse.h>
#include <pcl/visualization/pcl_visualizer.h>
// Point Type
// pcl::PointXYZ, pcl::PointXYZI, pcl::PointXYZRGBA
typedef pcl::PointXYZI PointType;
int main( int argc, char *argv[] )
{
// Command-Line Argument Parsing
if( pcl::console::find_switch( argc, argv, "-help" ) ){
std::cout << "usage: " << argv[0]
<< " [-ipaddress <192.168.1.70>]"
<< " [-port <2368>]"
<< " [-pcap <*.pcap>]"
<< " [-help]"
<< std::endl;
return 0;
}
std::string ipaddress( "192.168.1.70" );
std::string port( "2368" );
std::string pcap;
pcl::console::parse_argument( argc, argv, "-ipaddress", ipaddress );
pcl::console::parse_argument( argc, argv, "-port", port );
pcl::console::parse_argument( argc, argv, "-pcap", pcap );
std::cout << "-ipadress : " << ipaddress << std::endl;
std::cout << "-port : " << port << std::endl;
std::cout << "-pcap : " << pcap << std::endl;
// Point Cloud
pcl::PointCloud<PointType>::ConstPtr cloud;
// PCL Visualizer
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer( new pcl::visualization::PCLVisualizer( "Velodyne Viewer" ) );
viewer->addCoordinateSystem( 3.0, "coordinate" );
viewer->setBackgroundColor( 0.0, 0.0, 0.0, 0 );
viewer->initCameraParameters();
viewer->setCameraPosition( 0.0, 0.0, 30.0, 0.0, 1.0, 0.0, 0 );
// Point Cloud Color Hndler
pcl::visualization::PointCloudColorHandler<PointType>::Ptr handler;
const std::type_info& type = typeid( PointType );
if( type == typeid( pcl::PointXYZ ) ){
std::vector<double> color = { 255.0, 255.0, 255.0 };
boost::shared_ptr<pcl::visualization::PointCloudColorHandlerCustom<PointType>> color_handler( new pcl::visualization::PointCloudColorHandlerCustom<PointType>( color[0], color[1], color[2] ) );
handler = color_handler;
}
else if( type == typeid( pcl::PointXYZI ) ){
boost::shared_ptr<pcl::visualization::PointCloudColorHandlerGenericField<PointType>> color_handler( new pcl::visualization::PointCloudColorHandlerGenericField<PointType>( "intensity" ) );
handler = color_handler;
}
else if( type == typeid( pcl::PointXYZRGBA ) ){
boost::shared_ptr<pcl::visualization::PointCloudColorHandlerRGBField<PointType>> color_handler( new pcl::visualization::PointCloudColorHandlerRGBField<PointType>() );
handler = color_handler;
}
else{
throw std::runtime_error( "This PointType is unsupported." );
}
// Retrieved Point Cloud Callback Function
boost::mutex mutex;
boost::function<void( const pcl::PointCloud<PointType>::ConstPtr& )> function =
[ &cloud, &mutex ]( const pcl::PointCloud<PointType>::ConstPtr& ptr ){
boost::mutex::scoped_lock lock( mutex );
/* Point Cloud Processing */
cloud = ptr;
};
// VLP Grabber
boost::shared_ptr<pcl::VLPGrabber> grabber;
if( !pcap.empty() ){
std::cout << "Capture from PCAP..." << std::endl;
grabber = boost::shared_ptr<pcl::VLPGrabber>( new pcl::VLPGrabber( pcap ) );
}
else if( !ipaddress.empty() && !port.empty() ){
std::cout << "Capture from Sensor..." << std::endl;
grabber = boost::shared_ptr<pcl::VLPGrabber>( new pcl::VLPGrabber( boost::asio::ip::address::from_string( ipaddress ), boost::lexical_cast<unsigned short>( port ) ) );
}
// Register Callback Function
boost::signals2::connection connection = grabber->registerCallback( function );
// Start Grabber
grabber->start();
while( !viewer->wasStopped() ){
// Update Viewer
viewer->spinOnce();
boost::mutex::scoped_try_lock lock( mutex );
if( lock.owns_lock() && cloud ){
// Update Point Cloud
handler->setInputCloud( cloud );
if( !viewer->updatePointCloud( cloud, *handler, "cloud" ) ){
viewer->addPointCloud( cloud, *handler, "cloud" );
}
}
}
// Stop Grabber
grabber->stop();
// Disconnect Callback Function
if( connection.connected() ){
connection.disconnect();
}
return 0;
}
@kalai86
Copy link

kalai86 commented Jul 17, 2019

hi,
I got same issue
when I am running this code and giving input saved pcap file, then one window with 3 axis getting opening and there is no point cloud data

so please help me
thanks
kalai

@kalai86
Copy link

kalai86 commented Jul 18, 2019

I'm also having the same problem as @shaikhibrahim951 and @LiShuaixin. Adding set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11") or set(CMAKE_CXX_STANDARD 11) did not help, nor trying with full path to .pcap file. Stream from lidar works perfectly.

hi,
got same issue, i directly given full path only
when I am running this code and giving input saved pcap file, then one window with 3 axis getting opening and there is no point cloud data

so please help me
thanks
kalai

@kalai86
Copy link

kalai86 commented Jul 29, 2019

My system is Ubuntu 16.04 and pcl 1.8.
I have compiled the codes under /build with cmake .. and make.
I got the same issue as @LiShuaixin. There is no pointcloud streaming from the pcap file. Can anyone help with this?

test

hi songwenhuang,

you can install libpcap with in the PCL folder .
open pcl folder using terminal and give below command

sudo apt-get install libpcap-dev

then open the your build folder then cmake and make after that it will work

thanks
kalai

@jangbyoungjun
Copy link

jangbyoungjun commented May 5, 2021

Hi, I'm visualizing the point cloud with your code and using it well.
Can anyone know how to distinguish dynamic and static objects?
스크린샷, 2021-05-05 21-37-42

@BhagyarajDharmana
Copy link

BhagyarajDharmana commented Jun 8, 2022

Hello all,

When I am trying to retrieve the data from pcap files I am getting the segmentation fault (core dumped). Did anyone experienced the same error.
Any leads will be helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment