Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish pointcloud messages in ROS #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
FROM ubuntu:18.04
FROM ros:latest

RUN apt-get update
RUN apt-get upgrade -y

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get install -y wget cmake build-essential vim libeigen3-dev libflann-dev \
libpcap0.8-dev libvtk6-dev libboost-all-dev less
RUN apt-get upgrade -y && apt-get install -y wget cmake build-essential vim libeigen3-dev libflann-dev \
libpcap0.8-dev libvtk6-dev libboost-all-dev ros-melodic-pcl-conversions less && rm -rf /var/lib/apt/lists/*

RUN wget -qO - https://deb.nodesource.com/setup_11.x | bash - \
&& apt-get -y install nodejs
Expand Down Expand Up @@ -49,4 +48,8 @@ WORKDIR /usr/src/server
RUN npm run compile
EXPOSE 3002

CMD [ "npm", "start" ]
RUN source /opt/ros/melodic/setup.bash && roscore >> /tmp/roscorelog &
COPY docker_entrypoint.sh .

#CMD [ "npm", "start" ]
CMD ./docker_entrypoint.sh
6 changes: 6 additions & 0 deletions web/docker_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
source /opt/ros/melodic/setup.bash
roscore &
sleep 60
npm start

11 changes: 11 additions & 0 deletions web/server/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@
"include_dirs": [
"/usr/include/eigen3",
"/usr/local/include/pcl-1.9",
"/opt/ros/melodic/include",
"<!@(node -p \"require('node-addon-api').include\")"
],
"libraries": [
"-Wl,-rpath,/usr/local/lib",
"-Wl,-rpath,/opt/ros/melodic/lib",
"-L/opt/ros/melodic/lib",
"-lroscpp",
"-lrosconsole",
"-lrostime",
"-lroscpp_serialization",
"-lboost_system",
"-lboost_thread",
"-pthread",
"-lactionlib",
"-lpcl_common",
"-lpcl_features",
"-lpcl_filters",
Expand Down
30 changes: 24 additions & 6 deletions web/server/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#include <iostream>
#include <napi.h>
#include <pcl/io/pcd_io.h>
#include <pcl/conversions.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <unordered_map>

std::unordered_map<std::string, int> pointcloud_counts;
std::unordered_map<std::string, pcl::PointCloud<pcl::PointXYZ>::Ptr> clouds;

#include "ros/ros.h"
#include <pcl_conversions/pcl_conversions.h>
#include "sensor_msgs/PointCloud2.h"

std::unordered_map<std::string, ros::Publisher> pointcloud_publishers;
std::unordered_map<std::string, pcl::PointCloud<pcl::PointXYZ>::Ptr> clouds;
std::shared_ptr<ros::NodeHandle> nodehandle;

void processPointCloud(const Napi::CallbackInfo& info )
{
Expand All @@ -18,27 +24,39 @@ void processPointCloud(const Napi::CallbackInfo& info )
std::string lidarname = static_cast<std::string>(lidarId);
float* data = originalData.Data();

const auto& itr = pointcloud_counts.find(lidarname);
if (itr == pointcloud_counts.end()) {
pointcloud_counts.emplace(lidarname, 0);
const auto& itr = pointcloud_publishers.find(lidarname);
if (itr == pointcloud_publishers.end()) {
pointcloud_publishers.emplace(lidarname,
nodehandle->advertise<sensor_msgs::PointCloud2>(lidarname, 3));
}
pointcloud_counts[lidarname]++;


pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
for (unsigned int i = 0; i < originalData.Length(); i += 3) {
cloud->push_back(pcl::PointXYZ(data[i], data[i + 1], data[i + 2]));
}

clouds.emplace(lidarname, cloud);
sensor_msgs::PointCloud2 s_cloud;
pcl::toROSMsg(*cloud, s_cloud);

pointcloud_publishers[lidarname].publish(s_cloud);

pcl::io::savePCDFileASCII("tempcloud.pcd", *cloud);
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
const ros::M_string topic_remappings;

ros::init(topic_remappings, "pointcloud_cpp");

nodehandle = std::make_shared<ros::NodeHandle>();

exports.Set(
"processPointCloud", Napi::Function::New(env, processPointCloud)
);
return exports;
}

NODE_API_MODULE(addon, Init)