Skip to content

Commit e9ac1df

Browse files
author
Alex Beh
committed
rename to ros2_example
1 parent d312d2b commit e9ac1df

File tree

6 files changed

+166
-76
lines changed

6 files changed

+166
-76
lines changed

CMakeLists.txt

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,3 @@ add_subdirectory(infinite_sense_core)
1414

1515
add_subdirectory(tools)
1616
add_subdirectory(example)
17-
18-
find_package(ZeroMQ QUIET)
19-
if (ZeroMQ_FOUND)
20-
message(STATUS "ZMQ found, Compiling ZMQ nodes")
21-
find_package(ZeroMQ REQUIRED)
22-
if (NOT ZeroMQ_LIBRARIES)
23-
message(WARNING "ZeroMQ_LIBRARIES is empty. Using manual linking fallback.")
24-
set(ZeroMQ_LIBRARIES zmq)
25-
endif()
26-
27-
find_package(Threads REQUIRED)
28-
29-
message(STATUS "ZeroMQ_LIBRARIES: ${ZeroMQ_LIBRARIES}")
30-
message(STATUS "ZeroMQ_INCLUDE_DIRS: ${ZeroMQ_INCLUDE_DIRS}")
31-
32-
add_executable(${PROJECT_NAME}_zmq_node
33-
example/ZMQ/zmq_main.cpp
34-
infinite_sense_core/src/messenger.cpp
35-
)
36-
target_include_directories(${PROJECT_NAME}_zmq_node PRIVATE
37-
${ZeroMQ_INCLUDE_DIRS}
38-
)
39-
target_link_libraries(${PROJECT_NAME}_zmq_node PRIVATE
40-
Threads::Threads
41-
${ZeroMQ_LIBRARIES}
42-
infinite_sense_core
43-
)
44-
endif ()
45-
46-
# Conditionally add ROS 2 components
47-
find_package(rclcpp QUIET)
48-
find_package(rclcpp_components QUIET)
49-
if(rclcpp_FOUND AND rclcpp_components_FOUND)
50-
message(STATUS "ROS 2 detected, adding example/ros2_components components...")
51-
add_subdirectory(example/ros2_components)
52-
else()
53-
message(STATUS "ROS 2 not found. Skipping ROS 2 components.")
54-
endif()

example/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ cmake_minimum_required(VERSION 3.10)
22
project(example)
33

44
add_subdirectory(GigeCam)
5-
add_subdirectory(CustomCam)
5+
add_subdirectory(CustomCam)
6+
add_subdirectory(ros2_example)

example/ros2_components/CMakeLists.txt

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(ros2_example VERSION 1.0)
4+
5+
find_package(ament_cmake REQUIRED)
6+
find_package(rclcpp REQUIRED)
7+
find_package(rclcpp_components REQUIRED)
8+
find_package(std_msgs REQUIRED)
9+
10+
# === InfiniteSense SDK ===
11+
set(INFINITE_SENSE_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/infinite_sense_core/include")
12+
set(INFINITE_SENSE_LIB_NAME infinite_sense_core)
13+
14+
# === Component (shared library) ===
15+
add_library(synchronizer_component SHARED
16+
synchronizer_component.cpp
17+
)
18+
19+
target_include_directories(synchronizer_component
20+
PUBLIC
21+
$<BUILD_INTERFACE:${INFINITE_SENSE_INCLUDE_DIR}>
22+
$<INSTALL_INTERFACE:include>
23+
)
24+
25+
target_link_libraries(synchronizer_component
26+
${INFINITE_SENSE_LIB_NAME}
27+
)
28+
29+
ament_target_dependencies(synchronizer_component
30+
rclcpp
31+
rclcpp_components
32+
std_msgs
33+
)
34+
35+
rclcpp_components_register_nodes(synchronizer_component SynchronizerComponent)
36+
37+
# === Standalone ROS 2 Node (executable) ===
38+
add_executable(synchronizer_node
39+
synchronizer_node.cpp
40+
)
41+
42+
target_include_directories(synchronizer_node
43+
PUBLIC
44+
${INFINITE_SENSE_INCLUDE_DIR}
45+
)
46+
47+
target_link_libraries(synchronizer_node
48+
${INFINITE_SENSE_LIB_NAME}
49+
)
50+
51+
ament_target_dependencies(synchronizer_node
52+
rclcpp
53+
std_msgs
54+
)
55+
56+
# === Installation ===
57+
install(TARGETS
58+
synchronizer_component
59+
synchronizer_node
60+
ARCHIVE DESTINATION lib
61+
LIBRARY DESTINATION lib
62+
RUNTIME DESTINATION lib/${PROJECT_NAME}
63+
)
64+
65+
# === Export ===
66+
ament_export_targets(synchronizer_component HAS_LIBRARY_TARGET)
67+
ament_export_dependencies(rclcpp rclcpp_components std_msgs)

example/ros2_components/synchronizer_component.cpp renamed to example/ros2_example/synchronizer_component.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ class SynchronizerComponent : public rclcpp::Node
1717
RCLCPP_INFO(get_logger(), "Initializing SynchronizerComponent...");
1818

1919
// Set up the serial connection
20-
synchronizer_.SetSerialLink("/dev/ttyACM0", 460800);
20+
// synchronizer_.SetUsbLink("/dev/ttyACM0", 460800);
2121

22-
// Optionally: SetNetLink or UseMvCam
23-
// synchronizer_.SetNetLink("192.168.1.188", 8888);
24-
// synchronizer_.UseMvCam();
22+
synchronizer_.SetNetLink("192.168.1.188", 8888);
2523

24+
// 2.配置同步接口
25+
// auto mv_cam = std::make_shared<CustomCam>();
26+
// mv_cam->SetParams({{"camera_1", CAM_1},{"camera_2", CAM_2},});
27+
// synchronizer_.UseSensor(mv_cam);
28+
29+
// 3.开启同步
2630
synchronizer_.Start();
27-
Synchronizer::PrintSummary();
2831

2932
// Setup a timer instead of while loop
3033
timer_ = this->create_wall_timer(1s, [this]() {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include "infinite_sense.h"
2+
#include "rclcpp/rclcpp.hpp"
3+
#include <chrono>
4+
#include <fstream>
5+
#include <thread>
6+
7+
namespace infinite_sense {
8+
class CustomCam final : public Sensor {
9+
public:
10+
~CustomCam() override { Stop(); }
11+
bool Initialization() override { return true; }
12+
void Stop() override {}
13+
void Start() override {}
14+
15+
private:
16+
void Receive(void *handle, const std::string &) override {
17+
// Dummy implementation to satisfy linker
18+
}
19+
};
20+
} // namespace infinite_sense
21+
22+
using namespace std::chrono_literals;
23+
using namespace infinite_sense;
24+
25+
class SynchronizerNode : public rclcpp::Node {
26+
public:
27+
SynchronizerNode() : Node("synchronizer_node") {
28+
RCLCPP_INFO(get_logger(), "Initializing SynchronizerNode...");
29+
30+
synchronizer_.SetUsbLink("/dev/ttyACM0", 921600);
31+
// synchronizer_.SetNetLink("192.168.1.188", 8888);
32+
33+
auto mv_cam = std::make_shared<CustomCam>();
34+
mv_cam->SetParams({
35+
{"basler_middle", CAM_1},
36+
{"basler_left", CAM_1},
37+
});
38+
synchronizer_.UseSensor(mv_cam);
39+
40+
synchronizer_.Start();
41+
RCLCPP_INFO(this->get_logger(), "Synchronizer started!");
42+
43+
log_file_.open("last_trigger_times.log", std::ios::out | std::ios::trunc);
44+
if (!log_file_.is_open()) {
45+
RCLCPP_ERROR(this->get_logger(), "Failed to open log file for trigger times!");
46+
} else {
47+
log_file_ << "Trigger times log:\n====================\n";
48+
}
49+
50+
timer_ = this->create_wall_timer(1s, [this]() {
51+
RCLCPP_INFO(this->get_logger(), "Logging trigger times...");
52+
log_file_ << "Trigger log at: " << this->get_clock()->now().nanoseconds() << " ns\n";
53+
54+
for (const auto &[name, cam_id] : std::map<std::string, TriggerDevice>{
55+
{"basler_middle", CAM_1},
56+
{"basler_left", CAM_1},
57+
}) {
58+
uint64_t trigger_time = 0;
59+
if (GET_LAST_TRIGGER_STATUS(cam_id, trigger_time)) {
60+
log_file_ << name << ": " << trigger_time << " us\n";
61+
} else {
62+
log_file_ << name << ": (no trigger yet)\n";
63+
}
64+
}
65+
});
66+
}
67+
68+
~SynchronizerNode() {
69+
RCLCPP_INFO(this->get_logger(), "Closing log file...");
70+
if (log_file_.is_open()) {
71+
log_file_.close();
72+
}
73+
RCLCPP_INFO(this->get_logger(), "Shutting down synchronizer...");
74+
synchronizer_.Stop();
75+
RCLCPP_INFO(this->get_logger(), "SynchronizerNode shutdown complete.");
76+
}
77+
78+
private:
79+
Synchronizer synchronizer_;
80+
rclcpp::TimerBase::SharedPtr timer_;
81+
std::ofstream log_file_;
82+
};
83+
84+
int main(int argc, char **argv) {
85+
rclcpp::init(argc, argv);
86+
rclcpp::spin(std::make_shared<SynchronizerNode>());
87+
rclcpp::shutdown();
88+
return 0;
89+
}

0 commit comments

Comments
 (0)