Skip to content
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
33 changes: 33 additions & 0 deletions hz/src/hz_pkg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.5)
project(hz_pkg)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 查找依赖
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

# 信号发生器节点
add_executable(signal_generator src/signal_generator.cpp)
ament_target_dependencies(signal_generator rclcpp std_msgs)

# 信号处理器节点
add_executable(signal_processor src/signal_processor.cpp)
ament_target_dependencies(signal_processor rclcpp std_msgs)

# 安装可执行文件
install(TARGETS
signal_generator
signal_processor
DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()

21 changes: 21 additions & 0 deletions hz/src/hz_pkg/package.xml
Original file line number Diff line number Diff line change
@@ -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>hz_pkg</name> <!-- 包名,与 CMakeLists.txt 中的 project 一致 -->
<version>0.0.0</version>
<description>ROS 2 信号发生器和处理器示例</description>
<maintainer email="your@email.com">Your Name</maintainer>
<license>Apache-2.0</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>

64 changes: 64 additions & 0 deletions hz/src/hz_pkg/src/signal_generator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/float64.hpp"
#include <cmath>

using namespace std::chrono_literals;

class SignalGenerator : public rclcpp::Node
{
public:
SignalGenerator() : Node("signal_generator")
{
// 创建发布者,分别发布正弦波和方波
sine_publisher_ = this->create_publisher<std_msgs::msg::Float64>("sine_wave", 10);
square_publisher_ = this->create_publisher<std_msgs::msg::Float64>("square_wave", 10);

// 以1000Hz的频率发布信号
timer_ = this->create_wall_timer(
5ms, std::bind(&SignalGenerator::timer_callback, this));

// 初始化时间
start_time_ = this->now();
}

private:
void timer_callback()
{
double t = (this->now() - start_time_).seconds();

double sine_value = std::sin(2 * M_PI * 10 * t);

// 生成1Hz方波:
double square_value = (std::sin(2 * M_PI * 1 * t) >= 0) ? 1.0 : -1.0;

// 发布正弦波
auto sine_msg = std_msgs::msg::Float64();
sine_msg.data = sine_value;
sine_publisher_->publish(sine_msg);

// 发布方波
auto square_msg = std_msgs::msg::Float64();
square_msg.data = square_value;
square_publisher_->publish(square_msg);

if (count_ % 100 == 0) {
RCLCPP_INFO(this->get_logger(), "Sine: %.2f, Square: %.2f", sine_value, square_value);
}
count_++;
}

rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::Float64>::SharedPtr sine_publisher_;
rclcpp::Publisher<std_msgs::msg::Float64>::SharedPtr square_publisher_;
rclcpp::Time start_time_;
size_t count_ = 0;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SignalGenerator>());
rclcpp::shutdown();
return 0;
}

96 changes: 96 additions & 0 deletions hz/src/hz_pkg/src/signal_processor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/float64.hpp"
#include <mutex>

class SignalProcessor : public rclcpp::Node
{
public:
SignalProcessor() : Node("signal_processor")
{
// 订阅正弦波信号
sine_subscription_ = this->create_subscription<std_msgs::msg::Float64>(
"sine_wave", 10,
std::bind(&SignalProcessor::sine_callback, this, std::placeholders::_1));

// 订阅方波信号
square_subscription_ = this->create_subscription<std_msgs::msg::Float64>(
"square_wave", 10,
std::bind(&SignalProcessor::square_callback, this, std::placeholders::_1));

// 创建处理后信号的发布者
processed_publisher_ = this->create_publisher<std_msgs::msg::Float64>("processed_signal", 10);

latest_sine_ = 0.0;
latest_square_ = 0.0;
has_sine_ = false;
has_square_ = false;
}

private:
// 处理正弦波回调
void sine_callback(const std_msgs::msg::Float64::SharedPtr msg)
{
std::lock_guard<std::mutex> lock(data_mutex_);
latest_sine_ = msg->data;
has_sine_ = true;
process_signals();
}

// 处理方波回调
void square_callback(const std_msgs::msg::Float64::SharedPtr msg)
{
std::lock_guard<std::mutex> lock(data_mutex_);
latest_square_ = msg->data;
has_square_ = true;
process_signals();
}

void process_signals()
{
// 确保信号收到
if (!has_sine_ || !has_square_) {
return;
}

double processed_value;
if ((latest_sine_ >= 0 && latest_square_ >= 0) ||
(latest_sine_ < 0 && latest_square_ < 0)) {
processed_value = latest_sine_;
} else {
processed_value = 0.0;
}

// 发布处理后的信号
auto msg = std_msgs::msg::Float64();
msg.data = processed_value;
processed_publisher_->publish(msg);

// 每100次处理打印一次状态
if (count_ % 100 == 0) {
RCLCPP_INFO(this->get_logger(), "Sine: %.2f, Square: %.2f, Processed: %.2f",
latest_sine_, latest_square_, processed_value);
}
count_++;
}

rclcpp::Subscription<std_msgs::msg::Float64>::SharedPtr sine_subscription_;
rclcpp::Subscription<std_msgs::msg::Float64>::SharedPtr square_subscription_;
rclcpp::Publisher<std_msgs::msg::Float64>::SharedPtr processed_publisher_;

// 存储最新的信号值
double latest_sine_;
double latest_square_;
bool has_sine_;
bool has_square_;
std::mutex data_mutex_;
size_t count_ = 0;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<SignalProcessor>());
rclcpp::shutdown();
return 0;
}

33 changes: 33 additions & 0 deletions ros2_ws/src/my_ros2_pkg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.5)
project(my_ros2_pkg)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 查找依赖
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)

# 声明发布者节点
add_executable(publisher_node src/publisher_node.cpp)
ament_target_dependencies(publisher_node rclcpp std_msgs)

# 声明订阅者节点
add_executable(subscriber_node src/subscriber_node.cpp)
ament_target_dependencies(subscriber_node rclcpp std_msgs)

# 安装可执行文件
install(TARGETS
publisher_node
subscriber_node
DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()

21 changes: 21 additions & 0 deletions ros2_ws/src/my_ros2_pkg/package.xml
Original file line number Diff line number Diff line change
@@ -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>my_ros2_pkg</name>
<version>0.0.0</version>
<description>ROS 2 C++ 发布者和订阅者示例</description>
<maintainer email="your@email.com">Your Name</maintainer>
<license>Apache-2.0</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>

42 changes: 42 additions & 0 deletions ros2_ws/src/my_ros2_pkg/src/publisher_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <chrono>
#include <functional>
#include <memory>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;


class MinimalPublisher : public rclcpp::Node
{
public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
timer_ = this->create_wall_timer(
500ms, std::bind(&MinimalPublisher::timer_callback, this));
}

private:
void timer_callback()
{
auto message = std_msgs::msg::String();
message.data = "Hello, world! " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
size_t count_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}
31 changes: 31 additions & 0 deletions ros2_ws/src/my_ros2_pkg/src/subscriber_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <memory>

#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;

class MinimalSubscriber : public rclcpp::Node
{
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
subscription_ = this->create_subscription<std_msgs::msg::String>(
"topic", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}

private:
void topic_callback(const std_msgs::msg::String::SharedPtr msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg->data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};

int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriber>());
rclcpp::shutdown();
return 0;
}
29 changes: 29 additions & 0 deletions work1/src/filter_pkg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cmake_minimum_required(VERSION 3.5)
project(filter_visualization)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)

add_executable(data_publisher src/data_publisher.cpp)
ament_target_dependencies(data_publisher rclcpp std_msgs sensor_msgs)

add_executable(filter_node src/filter_node.cpp)
ament_target_dependencies(filter_node rclcpp std_msgs sensor_msgs)

install(TARGETS
data_publisher
filter_node
DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_package()
Loading