Skip to content

Commit

Permalink
Move viewer and examples (stella-cv#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymd-stella committed Jul 14, 2023
1 parent 18b976d commit 3543951
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 82 deletions.
26 changes: 7 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,13 @@ include_directories(${popl_INCLUDE_DIR})
set(json_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/3rd/json/include)
include_directories(${json_INCLUDE_DIR})

# pangolin viewer
set(USE_PANGOLIN_VIEWER OFF CACHE BOOL "Enable Pangolin Viewer")
if(USE_PANGOLIN_VIEWER)
find_library(Pangolin_Viewer_LIB pangolin_viewer)
find_package(Pangolin REQUIRED)
find_package(pangolin_viewer QUIET)
if(pangolin_viewer_FOUND)
message(STATUS "Viewer for examples: PangolinViewer")
endif()

# socket publisher
set(USE_SOCKET_PUBLISHER OFF CACHE BOOL "Enable Socket Publisher")
if(USE_SOCKET_PUBLISHER)
find_package(sioclient REQUIRED)
find_package(Protobuf REQUIRED)
find_library(Socket_Publisher_LIB socket_publisher)
if(NOT PROTOBUF_PROTOC_EXECUTABLE)
message(FATAL_ERROR "Could not find protoc executable (PROTOBUF_PROTOC_EXECUTABLE)")
endif()
message(STATUS "Found protoc executable: ${PROTOBUF_PROTOC_EXECUTABLE}")
include_directories(${SIOCLIENT_INCLUDE_DIR} ${PROTOBUF_INCLUDE_DIRS})
find_package(socket_publisher QUIET)
if(socket_publisher_FOUND)
message(STATUS "Viewer for examples: SocketPublisher")
endif()

#################################
Expand All @@ -202,11 +191,10 @@ catkin_package(CATKIN_DEPENDS

include_directories(
${catkin_INCLUDE_DIRS}
${stella_vslam_INCLUDE_DIRS}
src)

link_libraries(
${catkin_LIBRARIES}
${stella_vslam_LIBS})
stella_vslam::stella_vslam)

add_subdirectory(src)
27 changes: 19 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,30 @@ RUN set -x && \
git submodule update -i --recursive && \
mkdir -p build && \
cd build && \
CMAKE_PREFIX_PATH=/opt/ros/${ROS_DISTRO}/lib/cmake cmake \
-DUSE_PANGOLIN_VIEWER=ON \
-DUSE_SOCKET_PUBLISHER=OFF \
-DINSTALL_PANGOLIN_VIEWER=ON \
-DUSE_STACK_TRACE_LOGGER=ON \
.. && \
CMAKE_PREFIX_PATH=/opt/ros/${ROS_DISTRO}/lib/cmake cmake .. && \
make -j${NUM_THREADS} && \
make install && \
rm -rf CMakeCache.txt CMakeFiles Makefile cmake_install.cmake example src && \
chmod -R 777 ./*

ARG PANGOLIN_VIEWER_COMMIT=f3564675d420bcdbe6380b29b5e2f2b623987dd0
WORKDIR /tmp
RUN set -x && \
git clone https://github.com/stella-cv/pangolin_viewer.git && \
cd pangolin_viewer && \
git checkout -q ${PANGOLIN_VIEWER_COMMIT} && \
git submodule update --init --recursive && \
mkdir -p build && \
cd build && \
CMAKE_PREFIX_PATH=/opt/ros/${ROS_DISTRO}/lib/cmake cmake \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} \
.. && \
make -j${NUM_THREADS} && \
make install && \
cd /tmp && \
rm -rf *

# ROS
RUN set -x && \
apt-get update -y -qq && \
Expand All @@ -131,8 +144,6 @@ RUN set -x && \
: "build ROS packages" && \
bash -c "source /opt/ros/${ROS_DISTRO}/setup.bash; \
catkin_make -j${NUM_THREADS} \
-DUSE_PANGOLIN_VIEWER=ON \
-DUSE_SOCKET_PUBLISHER=OFF \
-DUSE_STACK_TRACE_LOGGER=ON"

RUN set -x && \
Expand Down
26 changes: 18 additions & 8 deletions Dockerfile.socket
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,29 @@ RUN set -x && \
git submodule update -i --recursive && \
mkdir -p build && \
cd build && \
CMAKE_PREFIX_PATH=/opt/ros/${ROS_DISTRO}/lib/cmake cmake \
-DUSE_PANGOLIN_VIEWER=OFF \
-DUSE_SOCKET_PUBLISHER=ON \
-DINSTALL_PANGOLIN_VIEWER=ON \
-DUSE_STACK_TRACE_LOGGER=ON \
.. && \
CMAKE_PREFIX_PATH=/opt/ros/${ROS_DISTRO}/lib/cmake cmake .. && \
make -j${NUM_THREADS} && \
make install && \
rm -rf CMakeCache.txt CMakeFiles Makefile cmake_install.cmake example src && \
chmod -R 777 ./*

ARG SOCKET_PUBLISHER_COMMIT=e2aaf66b27a6477b478bc8e86992907f09f29188
WORKDIR /tmp
RUN set -x && \
git clone https://github.com/stella-cv/socket_publisher.git && \
cd socket_publisher && \
git checkout ${SOCKET_PUBLISHER_COMMIT} && \
mkdir -p build && \
cd build && \
CMAKE_PREFIX_PATH=/opt/ros/${ROS_DISTRO}/lib/cmake cmake \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} \
.. && \
make -j${NUM_THREADS} && \
make install && \
cd /tmp && \
rm -rf *

# ROS
RUN set -x && \
apt-get update -y -qq && \
Expand All @@ -130,8 +142,6 @@ RUN set -x && \
: "build ROS packages" && \
bash -c "source /opt/ros/${ROS_DISTRO}/setup.bash; \
catkin_make -j${NUM_THREADS} \
-DUSE_PANGOLIN_VIEWER=OFF \
-DUSE_SOCKET_PUBLISHER=ON \
-DUSE_STACK_TRACE_LOGGER=ON"

RUN set -x && \
Expand Down
20 changes: 10 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
if(USE_PANGOLIN_VIEWER)
if(pangolin_viewer_FOUND)
message(STATUS "Viewer for examples: pangolin viewer")
elseif(USE_SOCKET_PUBLISHER)
endif()
if(socket_publisher_FOUND)
message(STATUS "Viewer for examples: socket publisher")
else()
message(STATUS "Viewer for examples: none")
endif()

set(EXECUTABLE_TARGETS "")
Expand All @@ -15,20 +14,21 @@ foreach(EXECUTABLE_TARGET IN LISTS EXECUTABLE_TARGETS)
add_dependencies(${EXECUTABLE_TARGET} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

# pangolin_viewer is used on a priority basis
if(USE_PANGOLIN_VIEWER)
if(pangolin_viewer_FOUND)
# set macro flag
target_compile_definitions(${EXECUTABLE_TARGET}
PRIVATE -DUSE_PANGOLIN_VIEWER)
PRIVATE HAVE_PANGOLIN_VIEWER)
# link viewer
target_link_libraries(${EXECUTABLE_TARGET} PRIVATE
${Pangolin_Viewer_LIB} ${Pangolin_LIBRARIES})
elseif(USE_SOCKET_PUBLISHER)
pangolin_viewer::pangolin_viewer)
endif()
if(socket_publisher_FOUND)
# set macro flag
target_compile_definitions(${EXECUTABLE_TARGET}
PRIVATE -DUSE_SOCKET_PUBLISHER)
PRIVATE HAVE_SOCKET_PUBLISHER)
# link viewer
target_link_libraries(${EXECUTABLE_TARGET} PRIVATE
${Socket_Publisher_LIB} ${SIOCLIENT_LIBRARY} ${PROTOBUF_LIBRARIES})
socket_publisher::socket_publisher ${SIOCLIENT_LIBRARY} ${PROTOBUF_LIBRARIES})
endif()

# setup stack trace logger
Expand Down
132 changes: 95 additions & 37 deletions src/run_slam.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifdef USE_PANGOLIN_VIEWER
#include <pangolin_viewer/viewer.h>
#elif USE_SOCKET_PUBLISHER
#include <socket_publisher/publisher.h>
#ifdef HAVE_PANGOLIN_VIEWER
#include "pangolin_viewer/viewer.h"
#endif
#ifdef HAVE_SOCKET_PUBLISHER
#include "socket_publisher/publisher.h"
#endif

#include <stella_vslam/system.h>
Expand Down Expand Up @@ -31,41 +32,56 @@ namespace fs = ghc::filesystem;
void tracking(const std::shared_ptr<stella_vslam_ros::system>& slam_ros,
const std::shared_ptr<stella_vslam::config>& cfg,
const bool eval_log,
const std::string& map_db_path) {
const std::string& map_db_path,
const std::string& viewer_string) {
auto& SLAM = slam_ros->slam_;

// create a viewer object
// and pass the frame_publisher and the map_publisher
#ifdef USE_PANGOLIN_VIEWER
pangolin_viewer::viewer viewer(stella_vslam::util::yaml_optional_ref(cfg->yaml_node_, "PangolinViewer"), SLAM, SLAM->get_frame_publisher(), SLAM->get_map_publisher());
#elif USE_SOCKET_PUBLISHER
socket_publisher::publisher publisher(stella_vslam::util::yaml_optional_ref(cfg->yaml_node_, "SocketPublisher"), SLAM, SLAM->get_frame_publisher(), SLAM->get_map_publisher());
#ifdef HAVE_PANGOLIN_VIEWER
std::shared_ptr<pangolin_viewer::viewer> viewer;
if (viewer_string == "pangolin_viewer") {
viewer = std::make_shared<pangolin_viewer::viewer>(
stella_vslam::util::yaml_optional_ref(cfg->yaml_node_, "PangolinViewer"),
SLAM,
SLAM->get_frame_publisher(),
SLAM->get_map_publisher());
}
#endif
#ifdef HAVE_SOCKET_PUBLISHER
std::shared_ptr<socket_publisher::publisher> publisher;
if (viewer_string == "socket_publisher") {
publisher = std::make_shared<socket_publisher::publisher>(
stella_vslam::util::yaml_optional_ref(cfg->yaml_node_, "SocketPublisher"),
SLAM,
SLAM->get_frame_publisher(),
SLAM->get_map_publisher());
}
#endif

// run the viewer in another thread
#ifdef USE_PANGOLIN_VIEWER
std::thread thread([&]() {
viewer.run();
if (SLAM->terminate_is_requested()) {
// wait until the loop BA is finished
while (SLAM->loop_BA_is_running()) {
std::this_thread::sleep_for(std::chrono::microseconds(5000));
}
ros::shutdown();
}
});
#elif USE_SOCKET_PUBLISHER
std::thread thread([&]() {
publisher.run();
if (SLAM->terminate_is_requested()) {
// wait until the loop BA is finished
while (SLAM->loop_BA_is_running()) {
std::this_thread::sleep_for(std::chrono::microseconds(5000));
std::shared_ptr<std::thread> viewer_thread;
if (viewer_string != "none") {
viewer_thread = std::make_shared<std::thread>([&]() {
if (viewer_string == "pangolin_viewer") {
#ifdef HAVE_PANGOLIN_VIEWER
viewer->run();
#endif
}
ros::shutdown();
}
});
if (viewer_string == "socket_publisher") {
#ifdef HAVE_SOCKET_PUBLISHER
publisher->run();
#endif
}
if (SLAM->terminate_is_requested()) {
// wait until the loop BA is finished
while (SLAM->loop_BA_is_running()) {
std::this_thread::sleep_for(std::chrono::microseconds(5000));
}
ros::shutdown();
}
});
}

ros::Rate rate(50);
while (ros::ok()) {
Expand All @@ -74,13 +90,19 @@ void tracking(const std::shared_ptr<stella_vslam_ros::system>& slam_ros,
}

// automatically close the viewer
#ifdef USE_PANGOLIN_VIEWER
viewer.request_terminate();
thread.join();
#elif USE_SOCKET_PUBLISHER
publisher.request_terminate();
thread.join();
if (viewer_string == "pangolin_viewer") {
#ifdef HAVE_PANGOLIN_VIEWER
viewer->request_terminate();
#endif
}
if (viewer_string == "socket_publisher") {
#ifdef HAVE_SOCKET_PUBLISHER
publisher->request_terminate();
#endif
}
if (viewer_string != "none") {
viewer_thread->join();
}

// shutdown the SLAM process
SLAM->shutdown();
Expand Down Expand Up @@ -133,6 +155,7 @@ int main(int argc, char* argv[]) {
auto disable_mapping = op.add<popl::Switch>("", "disable-mapping", "disable mapping");
auto temporal_mapping = op.add<popl::Switch>("", "temporal-mapping", "enable temporal mapping");
auto rectify = op.add<popl::Switch>("r", "rectify", "rectify stereo image");
auto viewer = op.add<popl::Value<std::string>>("", "viewer", "viewer [pangolin_viewer, socket_publisher, none]");
try {
op.parse(argc, argv);
}
Expand All @@ -155,6 +178,41 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE;
}

// viewer
std::string viewer_string;
if (viewer->is_set()) {
viewer_string = viewer->value();
if (viewer_string != "pangolin_viewer" && viewer_string != "socket_publisher" && viewer_string != "none") {
std::cerr << "invalid arguments (--viewer)" << std::endl
<< std::endl
<< op << std::endl;
return EXIT_FAILURE;
}
#ifndef HAVE_PANGOLIN_VIEWER
if (viewer_string == "pangolin_viewer") {
std::cerr << "pangolin_viewer not linked" << std::endl
<< std::endl
<< op << std::endl;
return EXIT_FAILURE;
}
#endif
#ifndef HAVE_SOCKET_PUBLISHER
if (viewer_string == "socket_publisher") {
std::cerr << "socket_publisher not linked" << std::endl
<< std::endl
<< op << std::endl;
return EXIT_FAILURE;
}
#endif
}
else {
#ifdef HAVE_PANGOLIN_VIEWER
viewer_string = "pangolin_viewer";
#elif defined(HAVE_SOCKET_PUBLISHER)
viewer_string = "socket_publisher";
#endif
}

// setup logger
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] %^[%L] %v%$");
spdlog::set_level(spdlog::level::from_str(log_level->value()));
Expand Down Expand Up @@ -214,7 +272,7 @@ int main(int argc, char* argv[]) {
}

// run tracking
tracking(slam_ros, cfg, eval_log->is_set(), map_db_path_out->value());
tracking(slam_ros, cfg, eval_log->is_set(), map_db_path_out->value(), viewer_string);

#ifdef USE_GOOGLE_PERFTOOLS
ProfilerStop();
Expand Down

0 comments on commit 3543951

Please sign in to comment.