feat: update examples and README with streaming support and uint32 force types
- Change CForce3D fx/fy/fz from int16 to uint32 to match hardware - Add independent C++ example with command and streaming modes - Rewrite Python example with threaded streaming (read_loop + consumer pattern) - Add ROS2 C++ publisher/subscriber examples - Update README with streaming APIs, ROS2 docs, data type definitions, and full FFI table - Add CHANGELOG
This commit is contained in:
52
example/ros-cpp/CMakeLists.txt
Normal file
52
example/ros-cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,52 @@
|
||||
cmake_minimum_required(VERSION 3.8)
|
||||
project(eskin_ros2_demo)
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
# Force use of system Python (avoid Conda Python interfering with ROS 2)
|
||||
set(Python3_EXECUTABLE /usr/bin/python3 CACHE FILEPATH "System Python3" FORCE)
|
||||
|
||||
# Ensure ROS 2 Python packages are findable even without ROS 2 sourced (e.g. VS Code CMake Tools)
|
||||
set(ENV{PYTHONPATH} "/opt/ros/jazzy/lib/python3.12/site-packages:$ENV{PYTHONPATH}")
|
||||
|
||||
# find dependencies
|
||||
find_package(ament_cmake REQUIRED)
|
||||
find_package(rclcpp REQUIRED)
|
||||
find_package(std_msgs REQUIRED)
|
||||
|
||||
|
||||
if(BUILD_TESTING)
|
||||
find_package(ament_lint_auto REQUIRED)
|
||||
# the following line skips the linter which checks for copyrights
|
||||
# comment the line when a copyright and license is added to all source files
|
||||
set(ament_cmake_copyright_FOUND TRUE)
|
||||
# the following line skips cpplint (only works in a git repo)
|
||||
# comment the line when this package is in a git repo and when
|
||||
# a copyright and license is added to all source files
|
||||
set(ament_cmake_cpplint_FOUND TRUE)
|
||||
ament_lint_auto_find_test_dependencies()
|
||||
endif()
|
||||
|
||||
# Eskin SDK library
|
||||
set(ESKIN_SDK_DIR "/home/lenn/Workspace/eskin-finger-sdk" CACHE PATH "Path to eskin-finger-sdk")
|
||||
add_library(eskin_finger_sdk SHARED IMPORTED)
|
||||
set_target_properties(eskin_finger_sdk PROPERTIES
|
||||
IMPORTED_LOCATION "${ESKIN_SDK_DIR}/target/release/libeskin_finger_sdk.so"
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include"
|
||||
)
|
||||
|
||||
add_executable(eskin_publisher src/eskin_publisher.cpp)
|
||||
ament_target_dependencies(eskin_publisher rclcpp std_msgs)
|
||||
target_link_libraries(eskin_publisher eskin_finger_sdk pthread)
|
||||
|
||||
add_executable(eskin_subscriber src/eskin_subscriber.cpp)
|
||||
ament_target_dependencies(eskin_subscriber rclcpp std_msgs)
|
||||
|
||||
install(TARGETS
|
||||
eskin_publisher
|
||||
eskin_subscriber
|
||||
DESTINATION lib/${PROJECT_NAME}
|
||||
)
|
||||
ament_package()
|
||||
130
example/ros-cpp/eskin_publisher.cpp
Normal file
130
example/ros-cpp/eskin_publisher.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "std_msgs/msg/u_int32.hpp"
|
||||
|
||||
#include "../include/eskin_ffi.h"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
class EskinPublisher : public rclcpp::Node {
|
||||
public:
|
||||
EskinPublisher(const std::string & device_path)
|
||||
: Node("eskin_publisher"), running_(true)
|
||||
{
|
||||
// 打开设备
|
||||
device_ = eskin_open(device_path.c_str(), nullptr);
|
||||
if (!device_) {
|
||||
RCLCPP_FATAL(this->get_logger(), "Failed to open device: %s", device_path.c_str());
|
||||
rclcpp::shutdown();
|
||||
return;
|
||||
}
|
||||
RCLCPP_INFO(this->get_logger(), "Device opened: %s", device_path.c_str());
|
||||
|
||||
// 启动 streaming
|
||||
auto err = eskin_start_stream(device_);
|
||||
if (err != ESkinSuccess) {
|
||||
RCLCPP_FATAL(this->get_logger(), "Failed to start stream, error: %d", (int)err);
|
||||
eskin_close(device_);
|
||||
rclcpp::shutdown();
|
||||
return;
|
||||
}
|
||||
RCLCPP_INFO(this->get_logger(), "Streaming started");
|
||||
|
||||
// 创建 publisher
|
||||
publisher_ = this->create_publisher<std_msgs::msg::UInt32>("comb_force", 10);
|
||||
|
||||
// 启动独立读取线程
|
||||
read_thread_ = std::thread(&EskinPublisher::read_loop, this);
|
||||
|
||||
// 10ms 定时器:从队列取数据并发布
|
||||
timer_ = this->create_wall_timer(10ms, std::bind(&EskinPublisher::publish_callback, this));
|
||||
|
||||
RCLCPP_INFO(this->get_logger(), "EskinPublisher setup success");
|
||||
}
|
||||
|
||||
~EskinPublisher() {
|
||||
// 停止读取线程
|
||||
running_ = false;
|
||||
cv_.notify_all();
|
||||
if (read_thread_.joinable()) {
|
||||
read_thread_.join();
|
||||
}
|
||||
|
||||
// 停止 streaming 并关闭设备
|
||||
if (device_) {
|
||||
eskin_stop_stream(device_);
|
||||
eskin_close(device_);
|
||||
}
|
||||
RCLCPP_INFO(this->get_logger(), "EskinPublisher destroyed");
|
||||
}
|
||||
|
||||
private:
|
||||
// 独立读取线程:持续从设备读取 sample 放入队列
|
||||
void read_loop() {
|
||||
while (running_) {
|
||||
CFingerSample sample;
|
||||
memset(&sample, 0, sizeof(sample));
|
||||
EskinSdkErrorCode err = eskin_read_sample(device_, 50, &sample);
|
||||
if (err == ESkinSuccess) {
|
||||
std::lock_guard<std::mutex> lock(queue_mutex_);
|
||||
sample_queue_.push(sample);
|
||||
// 限制队列大小,防止堆积
|
||||
while (sample_queue_.size() > 100) {
|
||||
sample_queue_.pop();
|
||||
}
|
||||
} else if (err != ESkinTimeout) {
|
||||
RCLCPP_WARN_THROTTLE(this->get_logger(), *this->get_clock(), 1000,
|
||||
"eskin_read_sample error: %d", (int)err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 定时器回调:从队列取数据并发布到 ROS topic
|
||||
void publish_callback() {
|
||||
std::lock_guard<std::mutex> lock(queue_mutex_);
|
||||
while (!sample_queue_.empty()) {
|
||||
const auto & sample = sample_queue_.front();
|
||||
auto msg = std_msgs::msg::UInt32();
|
||||
// 使用 combined_force 中的 fz(法向力)作为发布值
|
||||
msg.data = static_cast<uint32_t>(sample.combined_force.force.fz);
|
||||
publisher_->publish(msg);
|
||||
sample_queue_.pop();
|
||||
}
|
||||
}
|
||||
|
||||
bool running_;
|
||||
EskinDeviceHandle device_;
|
||||
std::thread read_thread_;
|
||||
std::mutex queue_mutex_;
|
||||
std::queue<CFingerSample> sample_queue_;
|
||||
std::condition_variable cv_;
|
||||
|
||||
rclcpp::TimerBase::SharedPtr timer_;
|
||||
rclcpp::Publisher<std_msgs::msg::UInt32>::SharedPtr publisher_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
rclcpp::init(argc, argv);
|
||||
|
||||
// 设备路径可通过命令行参数传入,默认 /dev/ttyUSB0
|
||||
std::string device_path = "/dev/ttyUSB0";
|
||||
if (argc > 1) {
|
||||
device_path = argv[1];
|
||||
}
|
||||
|
||||
auto node = std::make_shared<EskinPublisher>(device_path);
|
||||
rclcpp::spin(node);
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
32
example/ros-cpp/eskin_subscriber.cpp
Normal file
32
example/ros-cpp/eskin_subscriber.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <memory>
|
||||
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "std_msgs/msg/u_int32.hpp"
|
||||
|
||||
using std::placeholders::_1;
|
||||
|
||||
class EskinSubscriber : public rclcpp::Node {
|
||||
public:
|
||||
EskinSubscriber()
|
||||
: Node("eskin_subscriber")
|
||||
{
|
||||
subscription_ = this->create_subscription<std_msgs::msg::UInt32>(
|
||||
"comb_force", 10,
|
||||
std::bind(&EskinSubscriber::topic_callback, this, _1));
|
||||
RCLCPP_INFO(this->get_logger(), "EskinSubscriber listening on /comb_force");
|
||||
}
|
||||
|
||||
private:
|
||||
void topic_callback(const std_msgs::msg::UInt32::SharedPtr msg) const {
|
||||
RCLCPP_INFO(this->get_logger(), "Received force: %u", msg->data);
|
||||
}
|
||||
|
||||
rclcpp::Subscription<std_msgs::msg::UInt32>::SharedPtr subscription_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
rclcpp::init(argc, argv);
|
||||
rclcpp::spin(std::make_shared<EskinSubscriber>());
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
21
example/ros-cpp/package.xml
Normal file
21
example/ros-cpp/package.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0"?>
|
||||
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||
<package format="3">
|
||||
<name>eskin_ros2_demo</name>
|
||||
<version>0.0.0</version>
|
||||
<description>TODO: Package description</description>
|
||||
<maintainer email="lenn@todo.todo">lenn</maintainer>
|
||||
<license>TODO: License declaration</license>
|
||||
|
||||
<buildtool_depend>ament_cmake</buildtool_depend>
|
||||
|
||||
<depend>rclcpp</depend>
|
||||
<depend>std_msgs</depend>
|
||||
|
||||
<test_depend>ament_lint_auto</test_depend>
|
||||
<test_depend>ament_lint_common</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_cmake</build_type>
|
||||
</export>
|
||||
</package>
|
||||
Reference in New Issue
Block a user