Compare commits

..

2 Commits

Author SHA1 Message Date
Bug 8aaae2f323 feat: add yolov8n.pt to git, link /opt/vendor/yolov8 for pip deps
- Include yolov8n.pt model in repo (6.5MB)
- Add symlink logic in run_all.sh: if /workspace/.site-packages missing
  and /opt/vendor/yolov8 exists (EFS), auto-link before setting PYTHONPATH
2026-05-07 14:49:40 +08:00
Bug 322ad4a4b2 feat: YOLO vision + Nav2 speedup + camera bridge
- Add vision_yolo ROS2 package for YOLOv8n object detection
- Add camera sensor to turtlebot3_waffle SDF model
- Add ROS-GZ camera bridge to launch file (/camera/image_raw)
- Speed up explore_lite: planner_frequency 0.2→1.0, min_frontier_size 0.5→0.3
- Add nav2_params_fast.yaml: max_vel 0.26→0.5 m/s
- Add --vision flag to autorun.sh / run_all.sh for auto-start YOLO
- Add PYTHONPATH for /workspace/.site-packages pip deps
- Add yolov8n.pt auto-download on first run
- Add requirements.txt and update_requirements.sh
- Update .gitignore for .site-packages, build, yolov8n.pt
2026-05-07 14:32:18 +08:00
20 changed files with 1029 additions and 354 deletions

15
.gitignore vendored
View File

@ -0,0 +1,15 @@
# Python dependencies (installed on-demand)
.site-packages/
# ROS2 build artifacts
build/
install/
log/
logs/
# Python cache
__pycache__/
*.pyc
*.pyo

View File

@ -3,4 +3,4 @@
# 浏览器使用镜像内 foundation 已在 /opt/gzweb8000起的页面本脚本仍起 gz sim、websocket.sdf(9002)、Nav2 等。
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
exec bash "${ROOT}/run_all.sh" mapping --gzweb --no-gzweb-vite
exec bash "${ROOT}/run_all.sh" mapping --explore --gzweb --no-gzweb-vite --vision

View File

@ -5,10 +5,10 @@
costmap_topic: map
costmap_updates_topic: map_updates
visualize: true
planner_frequency: 0.2 #0.15
planner_frequency: 1.0 #0.15
progress_timeout: 40.0
potential_scale: 1.0 #3.0
orientation_scale: 0.0
gain_scale: 3.0 #1.0
transform_tolerance: 0.3
min_frontier_size: 0.5 #0.75
min_frontier_size: 0.3 #0.75

350
nav2_params_fast.yaml Normal file
View File

@ -0,0 +1,350 @@
amcl:
ros__parameters:
use_sim_time: True
alpha1: 0.2
alpha2: 0.2
alpha3: 0.2
alpha4: 0.2
alpha5: 0.2
base_frame_id: "base_footprint"
beam_skip_distance: 0.5
beam_skip_error_threshold: 0.9
beam_skip_threshold: 0.3
do_beamskip: false
global_frame_id: "map"
lambda_short: 0.1
laser_likelihood_max_dist: 2.0
laser_max_range: 100.0
laser_min_range: -1.0
laser_model_type: "likelihood_field"
max_beams: 60
max_particles: 2000
min_particles: 500
odom_frame_id: "odom"
pf_err: 0.05
pf_z: 0.99
recovery_alpha_fast: 0.0
recovery_alpha_slow: 0.0
resample_interval: 1
robot_model_type: "nav2_amcl::DifferentialMotionModel"
save_pose_rate: 0.5
sigma_hit: 0.2
tf_broadcast: true
transform_tolerance: 1.0
update_min_a: 0.2
update_min_d: 0.25
z_hit: 0.5
z_max: 0.05
z_rand: 0.5
z_short: 0.05
scan_topic: scan
bt_navigator:
ros__parameters:
use_sim_time: True
global_frame: map
robot_base_frame: base_link
odom_topic: /odom
bt_loop_duration: 10
default_server_timeout: 20
wait_for_service_timeout: 1000
# 'default_nav_through_poses_bt_xml' and 'default_nav_to_pose_bt_xml' are use defaults:
# nav2_bt_navigator/navigate_to_pose_w_replanning_and_recovery.xml
# nav2_bt_navigator/navigate_through_poses_w_replanning_and_recovery.xml
# They can be set here or via a RewrittenYaml remap from a parent launch file to Nav2.
plugin_lib_names:
- nav2_compute_path_to_pose_action_bt_node
- nav2_compute_path_through_poses_action_bt_node
- nav2_smooth_path_action_bt_node
- nav2_follow_path_action_bt_node
- nav2_spin_action_bt_node
- nav2_wait_action_bt_node
- nav2_assisted_teleop_action_bt_node
- nav2_back_up_action_bt_node
- nav2_drive_on_heading_bt_node
- nav2_clear_costmap_service_bt_node
- nav2_is_stuck_condition_bt_node
- nav2_goal_reached_condition_bt_node
- nav2_goal_updated_condition_bt_node
- nav2_globally_updated_goal_condition_bt_node
- nav2_is_path_valid_condition_bt_node
- nav2_initial_pose_received_condition_bt_node
- nav2_reinitialize_global_localization_service_bt_node
- nav2_rate_controller_bt_node
- nav2_distance_controller_bt_node
- nav2_speed_controller_bt_node
- nav2_truncate_path_action_bt_node
- nav2_truncate_path_local_action_bt_node
- nav2_goal_updater_node_bt_node
- nav2_recovery_node_bt_node
- nav2_pipeline_sequence_bt_node
- nav2_round_robin_node_bt_node
- nav2_transform_available_condition_bt_node
- nav2_time_expired_condition_bt_node
- nav2_path_expiring_timer_condition
- nav2_distance_traveled_condition_bt_node
- nav2_single_trigger_bt_node
- nav2_goal_updated_controller_bt_node
- nav2_is_battery_low_condition_bt_node
- nav2_navigate_through_poses_action_bt_node
- nav2_navigate_to_pose_action_bt_node
- nav2_remove_passed_goals_action_bt_node
- nav2_planner_selector_bt_node
- nav2_controller_selector_bt_node
- nav2_goal_checker_selector_bt_node
- nav2_controller_cancel_bt_node
- nav2_path_longer_on_approach_bt_node
- nav2_wait_cancel_bt_node
- nav2_spin_cancel_bt_node
- nav2_back_up_cancel_bt_node
- nav2_assisted_teleop_cancel_bt_node
- nav2_drive_on_heading_cancel_bt_node
- nav2_is_battery_charging_condition_bt_node
bt_navigator_navigate_through_poses_rclcpp_node:
ros__parameters:
use_sim_time: True
bt_navigator_navigate_to_pose_rclcpp_node:
ros__parameters:
use_sim_time: True
controller_server:
ros__parameters:
use_sim_time: True
controller_frequency: 20.0
min_x_velocity_threshold: 0.001
min_y_velocity_threshold: 0.5
min_theta_velocity_threshold: 0.001
failure_tolerance: 0.3
progress_checker_plugin: "progress_checker"
goal_checker_plugins: ["general_goal_checker"] # "precise_goal_checker"
controller_plugins: ["FollowPath"]
# Progress checker parameters
progress_checker:
plugin: "nav2_controller::SimpleProgressChecker"
required_movement_radius: 0.5
movement_time_allowance: 10.0
# Goal checker parameters
#precise_goal_checker:
# plugin: "nav2_controller::SimpleGoalChecker"
# xy_goal_tolerance: 0.25
# yaw_goal_tolerance: 0.25
# stateful: True
general_goal_checker:
stateful: True
plugin: "nav2_controller::SimpleGoalChecker"
xy_goal_tolerance: 0.25
yaw_goal_tolerance: 0.25
# DWB parameters
FollowPath:
plugin: "dwb_core::DWBLocalPlanner"
debug_trajectory_details: True
min_vel_x: 0.0
min_vel_y: 0.0
max_vel_x: 0.5
max_vel_y: 0.0
max_vel_theta: 1.0
min_speed_xy: 0.0
max_speed_xy: 0.5
min_speed_theta: 0.0
# Add high threshold velocity for turtlebot 3 issue.
# https://github.com/ROBOTIS-GIT/turtlebot3_simulations/issues/75
acc_lim_x: 2.5
acc_lim_y: 0.0
acc_lim_theta: 3.2
decel_lim_x: -2.5
decel_lim_y: 0.0
decel_lim_theta: -3.2
vx_samples: 20
vy_samples: 5
vtheta_samples: 20
sim_time: 1.7
linear_granularity: 0.05
angular_granularity: 0.025
transform_tolerance: 0.2
xy_goal_tolerance: 0.25
trans_stopped_velocity: 0.25
short_circuit_trajectory_evaluation: True
stateful: True
critics: ["RotateToGoal", "Oscillation", "BaseObstacle", "GoalAlign", "PathAlign", "PathDist", "GoalDist"]
BaseObstacle.scale: 0.02
PathAlign.scale: 32.0
PathAlign.forward_point_distance: 0.1
GoalAlign.scale: 24.0
GoalAlign.forward_point_distance: 0.1
PathDist.scale: 32.0
GoalDist.scale: 24.0
RotateToGoal.scale: 32.0
RotateToGoal.slowing_factor: 5.0
RotateToGoal.lookahead_time: -1.0
local_costmap:
local_costmap:
ros__parameters:
update_frequency: 5.0
publish_frequency: 2.0
global_frame: odom
robot_base_frame: base_link
use_sim_time: True
rolling_window: true
width: 3
height: 3
resolution: 0.05
robot_radius: 0.22
plugins: ["voxel_layer", "inflation_layer"]
inflation_layer:
plugin: "nav2_costmap_2d::InflationLayer"
cost_scaling_factor: 3.0
inflation_radius: 0.55
voxel_layer:
plugin: "nav2_costmap_2d::VoxelLayer"
enabled: True
publish_voxel_map: True
origin_z: 0.0
z_resolution: 0.05
z_voxels: 16
max_obstacle_height: 2.0
mark_threshold: 0
observation_sources: scan
scan:
topic: /scan
max_obstacle_height: 2.0
clearing: True
marking: True
data_type: "LaserScan"
raytrace_max_range: 3.0
raytrace_min_range: 0.0
obstacle_max_range: 2.5
obstacle_min_range: 0.0
static_layer:
plugin: "nav2_costmap_2d::StaticLayer"
map_subscribe_transient_local: True
always_send_full_costmap: True
global_costmap:
global_costmap:
ros__parameters:
update_frequency: 1.0
publish_frequency: 1.0
global_frame: map
robot_base_frame: base_link
use_sim_time: True
robot_radius: 0.22
resolution: 0.05
track_unknown_space: true
plugins: ["static_layer", "obstacle_layer", "inflation_layer"]
obstacle_layer:
plugin: "nav2_costmap_2d::ObstacleLayer"
enabled: True
observation_sources: scan
scan:
topic: /scan
max_obstacle_height: 2.0
clearing: True
marking: True
data_type: "LaserScan"
raytrace_max_range: 3.0
raytrace_min_range: 0.0
obstacle_max_range: 2.5
obstacle_min_range: 0.0
static_layer:
plugin: "nav2_costmap_2d::StaticLayer"
map_subscribe_transient_local: True
inflation_layer:
plugin: "nav2_costmap_2d::InflationLayer"
cost_scaling_factor: 3.0
inflation_radius: 0.55
always_send_full_costmap: True
map_server:
ros__parameters:
use_sim_time: True
# Overridden in launch by the "map" launch configuration or provided default value.
# To use in yaml, remove the default "map" value in the tb3_simulation_launch.py file & provide full path to map below.
yaml_filename: ""
map_saver:
ros__parameters:
use_sim_time: True
save_map_timeout: 5.0
free_thresh_default: 0.25
occupied_thresh_default: 0.65
map_subscribe_transient_local: True
planner_server:
ros__parameters:
expected_planner_frequency: 20.0
use_sim_time: True
planner_plugins: ["GridBased"]
GridBased:
plugin: "nav2_navfn_planner/NavfnPlanner"
tolerance: 0.5
use_astar: false
allow_unknown: true
smoother_server:
ros__parameters:
use_sim_time: True
smoother_plugins: ["simple_smoother"]
simple_smoother:
plugin: "nav2_smoother::SimpleSmoother"
tolerance: 1.0e-10
max_its: 1000
do_refinement: True
behavior_server:
ros__parameters:
costmap_topic: local_costmap/costmap_raw
footprint_topic: local_costmap/published_footprint
cycle_frequency: 10.0
behavior_plugins: ["spin", "backup", "drive_on_heading", "assisted_teleop", "wait"]
spin:
plugin: "nav2_behaviors/Spin"
backup:
plugin: "nav2_behaviors/BackUp"
drive_on_heading:
plugin: "nav2_behaviors/DriveOnHeading"
wait:
plugin: "nav2_behaviors/Wait"
assisted_teleop:
plugin: "nav2_behaviors/AssistedTeleop"
global_frame: odom
robot_base_frame: base_link
transform_tolerance: 0.1
use_sim_time: true
simulate_ahead_time: 2.0
max_rotational_vel: 1.0
min_rotational_vel: 0.4
rotational_acc_lim: 3.2
robot_state_publisher:
ros__parameters:
use_sim_time: True
waypoint_follower:
ros__parameters:
use_sim_time: True
loop_rate: 20
stop_on_failure: false
waypoint_task_executor_plugin: "wait_at_waypoint"
wait_at_waypoint:
plugin: "nav2_waypoint_follower::WaitAtWaypoint"
enabled: True
waypoint_pause_duration: 200
velocity_smoother:
ros__parameters:
use_sim_time: True
smoothing_frequency: 20.0
scale_velocities: False
feedback: "OPEN_LOOP"
max_velocity: [0.5, 0.0, 1.0]
min_velocity: [-0.5, 0.0, -1.0]
max_accel: [2.5, 0.0, 3.2]
max_decel: [-2.5, 0.0, -3.2]
odom_topic: "odom"
odom_duration: 0.1
deadband_velocity: [0.0, 0.0, 0.0]
velocity_timeout: 1.0

6
requirements.txt Normal file
View File

@ -0,0 +1,6 @@
# YOLO vision 依赖(安装到 /workspace/.site-packages 以持久化)
# 使用方法pip install -r requirements.txt -t /workspace/.site-packages
ultralytics
opencv-python
numpy<2

24
restart_nav2_explore.sh Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
source /opt/ros/humble/setup.bash
source /workspace/install/setup.bash
echo "[restart] Killing old processes..."
pkill -f "explore_lite/lib/explore_lite" 2>/dev/null || true
pkill -f "nav2_smoother/smoother_server" 2>/dev/null || true
pkill -f "nav2_behaviors/behavior_server" 2>/dev/null || true
pkill -f "nav2_waypoint_follower/waypoint_follower" 2>/dev/null || true
pkill -f "nav2_velocity_smoother/velocity_smoother" 2>/dev/null || true
pkill -f "nav2_controller/controller_server" 2>/dev/null || true
pkill -f "nav2_planner/planner_server" 2>/dev/null || true
pkill -f "nav2_bt_navigator/bt_navigator" 2>/dev/null || true
pkill -f "nav2_lifecycle_manager/lifecycle_manager" 2>/dev/null || true
sleep 2
echo "[restart] Starting Nav2 with fast params..."
nohup ros2 launch nav2_bringup navigation_launch.py use_sim_time:=true params_file:=/workspace/nav2_params_fast.yaml > /tmp/nav2.log 2>&1 &
echo "[restart] Starting explore_lite with fast params..."
nohup ros2 launch explore_lite explore.launch.py use_sim_time:=true > /tmp/explore.log 2>&1 &
echo "[restart] Done. Logs: /tmp/nav2.log /tmp/explore.log"
echo "[restart] Verify in 5s with: ros2 node list | grep -E \"explore|nav2\""

View File

@ -1,7 +1,20 @@
#!/usr/bin/env bash
set -euo pipefail
# Link pip deps from EFS vendor if /workspace/.site-packages is missing
if [[ ! -d "${ROOT}/.site-packages" && -d "/opt/vendor/yolov8" ]]; then
ln -sf /opt/vendor/yolov8 "${ROOT}/.site-packages"
fi
export PYTHONPATH="${ROOT}/.site-packages:$PYTHONPATH"
export DISPLAY=:1
# Auto-download YOLO model if not present
if [[ ! -f "\${ROOT}/yolov8n.pt" ]]; then
echo "[run_all] Downloading yolov8n.pt..."
wget -q -O "\${ROOT}/yolov8n.pt" https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n.pt || \ curl -sL -o "\${ROOT}/yolov8n.pt" https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n.pt
fi
# Unified launcher for TurtleBot3 office workflow:
# - Terminal A: simulation / bridge / (optional SLAM) / RViz
# - Terminal B: Nav2
@ -38,15 +51,16 @@ MAP_YAML="${NAV2_MAP_PATH:-$MAP_YAML_DEFAULT}"
MAP_BASENAME="${MAP_BASENAME:-$MAP_BASENAME_DEFAULT}"
WITH_EXPLORE=false
WITH_VISION=false
WITH_GZWE=false
NO_RVIZ=false
# 与 --gzweb 同用:只起 gz websocket(9002) 等资源,不起 office 内 npm run dev页面用镜像内 /opt/gzwebfoundation
NO_GZWEB_VITE=false
while [[ $# -gt 0 ]]; do
case "$1" in
--explore) WITH_EXPLORE=true; shift ;;
--vision) WITH_VISION=true; shift ;;
--gzweb) WITH_GZWEB=true; shift ;;
--no-gzweb-vite) NO_GZWEB_VITE=true; shift ;;
--no-rviz) NO_RVIZ=true; shift ;;
@ -279,10 +293,10 @@ echo "[run_all] waiting ${SLEEP_NAV2}s before starting Nav2..."
sleep "$SLEEP_NAV2"
if [[ "$MODE" == "mapping" ]]; then
start_bg "B_nav2" ros2 launch nav2_bringup navigation_launch.py use_sim_time:="$USE_SIM_TIME"
start_bg "B_nav2" ros2 launch nav2_bringup navigation_launch.py use_sim_time:="$USE_SIM_TIME" params_file:=/workspace/nav2_params_fast.yaml
else
start_bg "B_nav2" \
ros2 launch nav2_bringup bringup_launch.py \
ros2 launch nav2_bringup bringup_launch.py params_file:=/workspace/nav2_params_fast.yaml \
use_sim_time:="$USE_SIM_TIME" \
map:="$MAP_YAML" \
slam:=false
@ -296,6 +310,13 @@ if [[ "$WITH_EXPLORE" == true && "$MODE" == "mapping" ]]; then
fi
echo
if [[ "$WITH_VISION" == true ]]; then
start_bg "D_vision_yolo" ros2 run vision_yolo detector
last_index=$(( ${#PIDS[@]} - 1 ))
VISION_PID="${PIDS[$last_index]}"
VISION_LOG="${LOG_DIR}/D_vision_yolo.log"
fi
echo "[run_all] started. Mode: ${MODE}"
echo "[run_all] logs: ${LOG_DIR}"
echo

View File

@ -0,0 +1,23 @@
<?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>vision_yolo</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="devuser@todo.todo">devuser</maintainer>
<license>TODO: License declaration</license>
<depend>rclpy</depend>
<depend>sensor_msgs</depend>
<depend>vision_msgs</depend>
<depend>std_msgs</depend>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>

View File

View File

@ -0,0 +1,4 @@
[develop]
script_dir=$base/lib/vision_yolo
[install]
install_scripts=$base/lib/vision_yolo

30
src/vision_yolo/setup.py Normal file
View File

@ -0,0 +1,30 @@
from setuptools import find_packages, setup
package_name = 'vision_yolo'
setup(
name=package_name,
version='0.0.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='devuser',
maintainer_email='devuser@todo.todo',
description='YOLOv8 vision detector for TurtleBot3',
license='MIT',
extras_require={
'test': [
'pytest',
],
},
entry_points={
'console_scripts': [
'detector = vision_yolo.detector:main',
],
},
)

View File

@ -0,0 +1,25 @@
# Copyright 2015 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ament_copyright.main import main
import pytest
# Remove the `skip` decorator once the source file(s) have a copyright header
@pytest.mark.skip(reason='No copyright header has been placed in the generated source file.')
@pytest.mark.copyright
@pytest.mark.linter
def test_copyright():
rc = main(argv=['.', 'test'])
assert rc == 0, 'Found errors'

View File

@ -0,0 +1,25 @@
# Copyright 2017 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ament_flake8.main import main_with_errors
import pytest
@pytest.mark.flake8
@pytest.mark.linter
def test_flake8():
rc, errors = main_with_errors(argv=[])
assert rc == 0, \
'Found %d code style errors / warnings:\n' % len(errors) + \
'\n'.join(errors)

View File

@ -0,0 +1,23 @@
# Copyright 2015 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ament_pep257.main import main
import pytest
@pytest.mark.linter
@pytest.mark.pep257
def test_pep257():
rc = main(argv=['.', 'test'])
assert rc == 0, 'Found code style errors / warnings'

View File

View File

@ -0,0 +1,83 @@
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image
from vision_msgs.msg import Detection2DArray, Detection2D, ObjectHypothesisWithPose
from cv_bridge import CvBridge
from ultralytics import YOLO
import cv2
class YoloDetector(Node):
def __init__(self):
super().__init__('yolo_detector')
self.bridge = CvBridge()
self.get_logger().info('Loading YOLOv8n model...')
self.model = YOLO('yolov8n.pt')
self.get_logger().info('YOLOv8n model loaded')
self.sub = self.create_subscription(
Image, '/camera/image_raw', self.on_image, 10)
self.pub = self.create_publisher(
Detection2DArray, '/vision/detections', 10)
self.pub_vis = self.create_publisher(
Image, '/vision/image_annotated', 10)
self.get_logger().info('YOLO detector node started, subscribing to /camera/image_raw')
def on_image(self, msg):
try:
cv_img = self.bridge.imgmsg_to_cv2(msg, 'bgr8')
# CPU 降采样加速
cv_img = cv2.resize(cv_img, (320, 240))
except Exception as e:
self.get_logger().error(f'cv_bridge failed: {e}')
return
results = self.model(cv_img, verbose=False, imgsz=320)
dets = Detection2DArray()
dets.header = msg.header
for r in results:
for box in r.boxes:
cls_id = int(box.cls[0])
conf = float(box.conf[0])
label = self.model.names[cls_id]
x1, y1, x2, y2 = map(float, box.xyxy[0])
det = Detection2D()
det.bbox.center.position.x = (x1 + x2) / 2.0
det.bbox.center.position.y = (y1 + y2) / 2.0
det.bbox.center.theta = 0.0
det.bbox.size_x = x2 - x1
det.bbox.size_y = y2 - y1
hyp = ObjectHypothesisWithPose()
hyp.hypothesis.class_id = label
hyp.hypothesis.score = conf
det.results.append(hyp)
dets.detections.append(det)
cv2.rectangle(cv_img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
cv2.putText(cv_img, f'{label} {conf:.2f}', (int(x1), int(y1) - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
self.pub.publish(dets)
if dets.detections:
self.get_logger().info(f'Detected: {[d.results[0].hypothesis.class_id for d in dets.detections]}')
vis_msg = self.bridge.cv2_to_imgmsg(cv_img, 'bgr8')
vis_msg.header = msg.header
self.pub_vis.publish(vis_msg)
def main(args=None):
rclpy.init(args=args)
node = YoloDetector()
try:
rclpy.spin(node)
except KeyboardInterrupt:
pass
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()

View File

@ -113,12 +113,16 @@ def _launch_setup(context, *args, **kwargs):
f'{joint_gz}@sensor_msgs/msg/JointState[gz.msgs.Model',
f'{tf_topic}@tf2_msgs/msg/TFMessage[gz.msgs.Pose_V',
'/scan@sensor_msgs/msg/LaserScan@gz.msgs.LaserScan',
'/camera@sensor_msgs/msg/Image@gz.msgs.Image',
'/camera_info@sensor_msgs/msg/CameraInfo@gz.msgs.CameraInfo',
],
remappings=[
(joint_gz, '/joint_states'),
(tf_topic, '/tf'),
(cmd_topic, '/cmd_vel'),
(odom_topic, '/odom'),
('/camera', '/camera/image_raw'),
('/camera_info', '/camera/camera_info'),
],
parameters=[{
f'qos_overrides.{cmd_topic}.subscriber.reliability': 'reliable',

View File

@ -432,10 +432,6 @@
</axis>
</joint>
<joint name="camera_joint" type="fixed">
<parent>base_link</parent>
<child>realsense_link</child>
</joint>
<plugin filename="gz-sim-diff-drive-system" name="gz::sim::systems::DiffDrive">
<left_joint>wheel_left_joint</left_joint>
@ -458,6 +454,37 @@
<plugin filename="gz-sim-joint-state-publisher-system" name="gz::sim::systems::JointStatePublisher">
</plugin>
<link name="camera_link">
<pose relative_to="base_link">0.064 -0.065 0.094 0 0 0</pose>
<inertial>
<mass>0.01</mass>
<inertia>
<ixx>0.001</ixx><iyy>0.001</iyy><izz>0.001</izz>
</inertia>
</inertial>
<sensor name="camera" type="camera">
<camera>
<horizontal_fov>1.047</horizontal_fov>
<image>
<width>640</width>
<height>480</height>
</image>
<clip>
<near>0.1</near>
<far>100</far>
</clip>
</camera>
<always_on>1</always_on>
<update_rate>30</update_rate>
<visualize>true</visualize>
<topic>camera</topic>
</sensor>
</link>
<joint name="camera_joint" type="fixed">
<parent>base_link</parent>
<child>camera_link</child>
</joint>
</model>
</sdf>

15
update_requirements.sh Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
# 安装 vision_yolo 依赖到 /workspace/.site-packages
# 容器重启后系统盘重置,但 /workspace 是 EFS 持久化存储
set -euo pipefail
TARGET="/workspace/.site-packages"
mkdir -p "$TARGET"
pip install ultralytics opencv-python "numpy<2" \
-t "$TARGET" \
-i https://pypi.tuna.tsinghua.edu.cn/simple \
--no-cache-dir
echo "Dependencies installed to $TARGET"
echo "PYTHONPATH is already set in run_all.sh"

BIN
yolov8n.pt Normal file

Binary file not shown.