重塑程序

This commit is contained in:
Cx330 committed 2025-07-01 22:38:49 +08:00
1 parent 84c38f206e
commit 85d0b7769f
135 files changed
+256 -4352

No files matched your search

@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.0.2)
project(position_demo)
project(Topic_Newsletter)
## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)
@@ -9,9 +9,10 @@ project(position_demo)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
geometry_msgs
nav_msgs
message_generation
rospy
std_msgs
visualization_msgs
)
## System dependencies are found with CMake's conventions
@@ -71,7 +72,7 @@ find_package(catkin REQUIRED COMPONENTS
## Generate added messages and services with any dependencies listed here
# generate_messages(
# DEPENDENCIES
# geometry_msgs# nav_msgs# std_msgs
# geometry_msgs# std_msgs# visualization_msgs
# )
################################################
@@ -105,8 +106,8 @@ find_package(catkin REQUIRED COMPONENTS
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES position_demo
# CATKIN_DEPENDS geometry_msgs nav_msgs rospy std_msgs
# LIBRARIES Topic_Newsletter
# CATKIN_DEPENDS geometry_msgs message_generation rospy std_msgs visualization_msgs
# DEPENDS system_lib
)
@@ -123,7 +124,7 @@ include_directories(
## Declare a C++ library
# add_library(${PROJECT_NAME}
# src/${PROJECT_NAME}/position_demo.cpp
# src/${PROJECT_NAME}/Topic_Newsletter.cpp
# )
## Add cmake target dependencies of the library
@@ -134,7 +135,7 @@ include_directories(
## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/position_demo_node.cpp)
# add_executable(${PROJECT_NAME}_node src/Topic_Newsletter_node.cpp)
## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
@@ -198,7 +199,7 @@ include_directories(
#############
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_position_demo.cpp)
# catkin_add_gtest(${PROJECT_NAME}-test test/test_Topic_Newsletter.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
@@ -1,8 +1,8 @@
<?xml version="1.0"?>
<package format="2">
<name>position_demo</name>
<name>Topic_Newsletter</name>
<version>0.0.0</version>
<description>The position_demo package</description>
<description>The Topic_Newsletter package</description>
<!-- One maintainer tag required, multiple allowed, one person per tag -->
<!-- Example: -->
@@ -19,7 +19,7 @@
<!-- Url tags are optional, but multiple are allowed, one per tag -->
<!-- Optional attribute type can be: website, bugtracker, or repository -->
<!-- Example: -->
<!-- <url type="website">http://wiki.ros.org/position_demo</url> -->
<!-- <url type="website">http://wiki.ros.org/Topic_Newsletter</url> -->
<!-- Author tags are optional, multiple are allowed, one per tag -->
@@ -50,17 +50,18 @@
<!-- <doc_depend>doxygen</doc_depend> -->
<buildtool_depend>catkin</buildtool_depend>
<build_depend>geometry_msgs</build_depend>
<build_depend>nav_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>visualization_msgs</build_depend>
<build_export_depend>geometry_msgs</build_export_depend>
<build_export_depend>nav_msgs</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<build_export_depend>visualization_msgs</build_export_depend>
<exec_depend>geometry_msgs</exec_depend>
<exec_depend>nav_msgs</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>visualization_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
@@ -0,0 +1,27 @@
#!/usr/bin/env python3
import rospy
import math
from geometry_msgs.msg import PointStamped
def main():
rospy.init_node('Publisher_S')
pub = rospy.Publisher('position', PointStamped, queue_size=10) # 向 position 话题发布 PointStamped 类型的消息
rate = rospy.Rate(10) # 每 0.1s 一条信息
k = 0
while not rospy.is_shutdown():
msg = PointStamped()
msg.header.stamp = rospy.Time.now()
msg.header.frame_id = "map" # 基于 map 的坐标系
msg.point.x = math.sin(0.05 * math.pi * k) # x 值
msg.point.y = math.cos(0.05 * math.pi * k) # y 值
msg.point.z = 0.0
pub.publish(msg) # 发布话题
rospy.loginfo("Published: x=%.3f, y=%.3f" % (msg.point.x, msg.point.y)) # 打印坐标信息
k += 1
rate.sleep()
if __name__ == '__main__':
main()
@@ -0,0 +1,91 @@
#!/usr/bin/env python3
import rospy
import random
from geometry_msgs.msg import PointStamped, Point
from visualization_msgs.msg import Marker, MarkerArray
from std_msgs.msg import ColorRGBA
MAX_POINTS = 100
original_trajectory = []
noisy_trajectory = []
noisy_pub = None
marker_pub = None
def position_callback(msg):
global original_trajectory, noisy_trajectory
global noisy_pub, marker_pub
noise_x = random.uniform(-0.1, 0.1)
noise_y = random.uniform(-0.1, 0.1)
noisy_msg = PointStamped()
noisy_msg.header = msg.header
noisy_msg.point.x = msg.point.x + noise_x # 对 x 添加噪声
noisy_msg.point.y = msg.point.y + noise_y # 对 y 添加噪声
noisy_msg.point.z = 0.0
original_trajectory.append(msg.point)
if len(original_trajectory) > MAX_POINTS:
original_trajectory.pop(0)
noisy_trajectory.append(noisy_msg.point)
if len(noisy_trajectory) > MAX_POINTS:
noisy_trajectory.pop(0)
marker_array = MarkerArray()
original_line = Marker()
original_line.header = msg.header
original_line.ns = "trajectory"
original_line.id = 0
original_line.type = Marker.LINE_STRIP
original_line.action = Marker.ADD
original_line.scale.x = 0.02
original_line.color.g = 1.0
original_line.color.a = 1.0
original_line.points = list(original_trajectory)
marker_array.markers.append(original_line)
noisy_line = Marker()
noisy_line.header = msg.header
noisy_line.ns = "trajectory"
noisy_line.id = 1
noisy_line.type = Marker.LINE_STRIP
noisy_line.action = Marker.ADD
noisy_line.scale.x = 0.02
noisy_line.color.r = 1.0
noisy_line.color.a = 1.0
noisy_line.points = list(noisy_trajectory)
marker_array.markers.append(noisy_line)
marker = Marker()
marker.header.frame_id = "map" # 设置坐标系
marker.header.stamp = rospy.Time.now()
marker.type = Marker.POINTS
marker.action = Marker.ADD
marker.scale.x = 0.025 # 点的直径
marker.scale.y = 0.025 # 点的直径
marker.color = ColorRGBA(1.0, 0.0, 0.0, 1.0) # 红色
points = []
for point in noisy_trajectory:
p = Point()
p.x = point.x
p.y = point.y
p.z = 0.0
points.append(p)
marker.points = points
marker_array.markers.append(marker)
marker_pub.publish(marker_array)
rospy.loginfo("Published noisy point: x=%.3f, y=%.3f | Noisy: x=%.3f, y=%.3f",msg.point.x, msg.point.y, noisy_msg.point.x, noisy_msg.point.y)
if __name__ == '__main__':
rospy.init_node('Subscriber_Client_S')
rospy.Subscriber('position', PointStamped, position_callback)
noisy_pub = rospy.Publisher('noisy_position', PointStamped, queue_size=10)
marker_pub = rospy.Publisher('visualization_marker_array', MarkerArray, queue_size=10)
rospy.loginfo("Node initialized, waiting for position data...")
rospy.spin()
-23
View File
@@ -1,23 +0,0 @@
#!/usr/bin/env python
import rospy
import numpy as np
from position_demo.srv import FftService, FftServiceResponse
# 服务回调函数,进行FFT变换
def handle_fft_request(req):
# 获取请求中的数据
data = req.data
# 对数据进行FFT变换
fft_result = np.fft.fft(data)
# 返回实部和虚部的实数部分作为结果
return FftServiceResponse(fft_result.real)
def fft_service():
rospy.init_node('fft_service')
# 定义服务
service = rospy.Service('fft_service', FftService, handle_fft_request)
rospy.loginfo("Ready to perform FFT on 128 data points.")
rospy.spin()
if __name__ == "__main__":
fft_service()
@@ -1,31 +0,0 @@
#!/usr/bin/env python3
import rospy
from geometry_msgs.msg import Point
import math
def publisher():
# 创建一个发布者,将坐标点发布到 /position_topic 主题
point_pub = rospy.Publisher('position_topic', Point, queue_size=10)
rospy.init_node('position_publisher', anonymous=True)
rate = rospy.Rate(10) # 10Hz (100ms)
k = 0
while not rospy.is_shutdown():
# 根据方程计算 x 和 y 坐标
x = math.sin(0.05 * k * math.pi)
y = math.cos(0.05 * k * math.pi)
# 创建一个 Point 消息并发布
point = Point(x=x, y=y, z=0)
point_pub.publish(point)
rospy.loginfo(f"Published Point: x={x:.4f}, y={y:.4f}")
k += 1
rate.sleep()
if __name__ == '__main__':
try:
publisher()
except rospy.ROSInterruptException:
pass
@@ -1,31 +0,0 @@
#!/usr/bin/env python3
import rospy
from geometry_msgs.msg import Point
import math
def publisher():
# 创建一个发布者,将坐标点发布到 /position_topic 主题
point_pub = rospy.Publisher('position_topic', Point, queue_size=10)
rospy.init_node('position_publisher', anonymous=True)
rate = rospy.Rate(10) # 10Hz (100ms)
k = 0
while not rospy.is_shutdown():
# 根据方程计算 x 和 y 坐标
x = math.sin(0.015625 * k * math.pi)
y = math.cos(0.015625 * k * math.pi)
# 创建一个 Point 消息并发布
point = Point(x=x, y=y, z=0)
point_pub.publish(point)
rospy.loginfo(f"Published Point: x={x:.4f}, y={y:.4f}")
k += 1
rate.sleep()
if __name__ == '__main__':
try:
publisher()
except rospy.ROSInterruptException:
pass
@@ -1,57 +0,0 @@
#!/usr/bin/env python3
import rospy
from geometry_msgs.msg import Point
from visualization_msgs.msg import Marker
from std_msgs.msg import ColorRGBA
# 保存接收到的所有点,更新轨迹
points = []
def callback(data):
global points
# 打印接收到的坐标点
rospy.loginfo(f"Received Point: x={data.x:.4f}, y={data.y:.4f}")
# 创建 Marker 消息
marker = Marker()
marker.header.frame_id = "world"
marker.header.stamp = rospy.Time.now()
# 设置 Marker 类型为点(而不是线条)
marker.type = Marker.POINTS
marker.action = Marker.ADD
# 设置点的大小,直径为 0.001
marker.scale.x = 0.025 # 点的直径(x 和 y 均为直径)
marker.scale.y = 0.025 # 点的直径
# 设置点的颜色
marker.color = ColorRGBA(1.0, 0.0, 0.0, 1.0) # 红色,透明度 1.0
# 添加接收到的坐标点
point = Point()
point.x = data.x
point.y = data.y
point.z = data.z
points.append(point)
# 将所有的点添加到 Marker 的点集合中
marker.points = points
# 发布 Marker
marker_pub.publish(marker)
def listener():
rospy.init_node('position_subscriber', anonymous=True)
# 创建 Marker 发布者
global marker_pub
marker_pub = rospy.Publisher('visualization_marker', Marker, queue_size=10)
rospy.Subscriber('position_topic', Point, callback)
rospy.spin()
if __name__ == '__main__':
listener()
@@ -1,70 +0,0 @@
#!/usr/bin/env python3
import rospy
from geometry_msgs.msg import Point
from visualization_msgs.msg import Marker
from std_msgs.msg import ColorRGBA
# 保存接收到的所有点,更新轨迹
points = []
x1 = 0
y1 = 0
def callback(data, n=[0]):
global points
global x1
global y1
if n[0] == 0:
x1 = data.x + 0.1
y1 = data.y + 0.1
n[0] = 1
else:
x1 = data.x - 0.1
y1 = data.y - 0.1
n[0] = 0
rospy.loginfo(f"Modified Point: x={x1:.4f}, y={y1:.4f}")
# 创建 Marker 消息
marker = Marker()
marker.header.frame_id = "world"
marker.header.stamp = rospy.Time.now()
# 设置 Marker 类型为点(而不是线条)
marker.type = Marker.POINTS
marker.action = Marker.ADD
# 设置点的大小,直径为 0.001
marker.scale.x = 0.025 # 点的直径(x 和 y 均为直径)
marker.scale.y = 0.025 # 点的直径
# 设置点的颜色
marker.color = ColorRGBA(1.0, 0.0, 0.0, 1.0) # 红色,透明度 1.0
# 添加接收到的坐标点
point = Point()
point.x = x1
point.y = y1
point.z = 0
points.append(point)
# 将所有的点添加到 Marker 的点集合中
marker.points = points
# 发布 Marker
marker_pub.publish(marker)
def listener():
rospy.init_node('position_subscriber', anonymous=True)
# 创建 Marker 发布者
global marker_pub
marker_pub = rospy.Publisher('visualization_marker', Marker, queue_size=10)
rospy.Subscriber('position_topic', Point, callback)
rospy.spin()
if __name__ == '__main__':
listener()
-3
View File
@@ -1,3 +0,0 @@
float64[128] data
---
float64[128] fft_result