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 等。 # 浏览器使用镜像内 foundation 已在 /opt/gzweb8000起的页面本脚本仍起 gz sim、websocket.sdf(9002)、Nav2 等。
set -euo pipefail set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 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_topic: map
costmap_updates_topic: map_updates costmap_updates_topic: map_updates
visualize: true visualize: true
planner_frequency: 0.2 #0.15 planner_frequency: 1.0 #0.15
progress_timeout: 40.0 progress_timeout: 40.0
potential_scale: 1.0 #3.0 potential_scale: 1.0 #3.0
orientation_scale: 0.0 orientation_scale: 0.0
gain_scale: 3.0 #1.0 gain_scale: 3.0 #1.0
transform_tolerance: 0.3 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,347 +1,368 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
export DISPLAY=:1 # Link pip deps from EFS vendor if /workspace/.site-packages is missing
if [[ ! -d "${ROOT}/.site-packages" && -d "/opt/vendor/yolov8" ]]; then
# Unified launcher for TurtleBot3 office workflow: ln -sf /opt/vendor/yolov8 "${ROOT}/.site-packages"
# - Terminal A: simulation / bridge / (optional SLAM) / RViz fi
# - Terminal B: Nav2
# - Optional C: explore_lite export PYTHONPATH="${ROOT}/.site-packages:$PYTHONPATH"
# - Optional websocket + gzweb-demo frontend export DISPLAY=:1
#
# Modes: # Auto-download YOLO model if not present
# mapping : online SLAM mapping if [[ ! -f "\${ROOT}/yolov8n.pt" ]]; then
# localization : map-based localization/navigation 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
# Mapping mode interactive commands: fi
# s : save map and switch to localization mode
# q : stop all
# # Unified launcher for TurtleBot3 office workflow:
# Examples: # - Terminal A: simulation / bridge / (optional SLAM) / RViz
# ./run_all.sh mapping --explore --gzweb # - Terminal B: Nav2
# ./run_all.sh localization --gzweb --map /abs/path/office_map.yaml # - Optional C: explore_lite
# - Optional websocket + gzweb-demo frontend
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" #
MODE="${1:-mapping}" # Modes:
shift || true # mapping : online SLAM mapping
# localization : map-based localization/navigation
ROBOT_MODEL="${ROBOT_MODEL:-waffle}" #
USE_SIM_TIME="${USE_SIM_TIME:-true}" # Mapping mode interactive commands:
GZ_PARTITION="${GZ_PARTITION:-tb3_office_gz}" # s : save map and switch to localization mode
WORLD_PATH_DEFAULT="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/worlds/office_gz_dartsim.sdf" # q : stop all
MAP_YAML_DEFAULT="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/map/office_map.yaml" #
MAP_BASENAME_DEFAULT="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/map/office_map" # Examples:
SLEEP_NAV2="${SLEEP_NAV2:-18}" # ./run_all.sh mapping --explore --gzweb
EXPLORE_WAIT_TIMEOUT="${EXPLORE_WAIT_TIMEOUT:-420}" # ./run_all.sh localization --gzweb --map /abs/path/office_map.yaml
WORLD_PATH="${WORLD_PATH:-$WORLD_PATH_DEFAULT}" ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MAP_YAML="${NAV2_MAP_PATH:-$MAP_YAML_DEFAULT}" MODE="${1:-mapping}"
MAP_BASENAME="${MAP_BASENAME:-$MAP_BASENAME_DEFAULT}" shift || true
WITH_EXPLORE=false ROBOT_MODEL="${ROBOT_MODEL:-waffle}"
WITH_GZWE=false USE_SIM_TIME="${USE_SIM_TIME:-true}"
NO_RVIZ=false GZ_PARTITION="${GZ_PARTITION:-tb3_office_gz}"
# 与 --gzweb 同用:只起 gz websocket(9002) 等资源,不起 office 内 npm run dev页面用镜像内 /opt/gzwebfoundation WORLD_PATH_DEFAULT="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/worlds/office_gz_dartsim.sdf"
NO_GZWEB_VITE=false MAP_YAML_DEFAULT="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/map/office_map.yaml"
MAP_BASENAME_DEFAULT="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/map/office_map"
SLEEP_NAV2="${SLEEP_NAV2:-18}"
while [[ $# -gt 0 ]]; do EXPLORE_WAIT_TIMEOUT="${EXPLORE_WAIT_TIMEOUT:-420}"
case "$1" in
--explore) WITH_EXPLORE=true; shift ;; WORLD_PATH="${WORLD_PATH:-$WORLD_PATH_DEFAULT}"
--gzweb) WITH_GZWEB=true; shift ;; MAP_YAML="${NAV2_MAP_PATH:-$MAP_YAML_DEFAULT}"
--no-gzweb-vite) NO_GZWEB_VITE=true; shift ;; MAP_BASENAME="${MAP_BASENAME:-$MAP_BASENAME_DEFAULT}"
--no-rviz) NO_RVIZ=true; shift ;;
--map) MAP_YAML="$2"; shift 2 ;; WITH_EXPLORE=false
--world) WORLD_PATH="$2"; shift 2 ;; WITH_VISION=false
--help|-h) WITH_GZWE=false
cat <<'EOF' NO_RVIZ=false
Usage: # 与 --gzweb 同用:只起 gz websocket(9002) 等资源,不起 office 内 npm run dev页面用镜像内 /opt/gzwebfoundation
./run_all.sh mapping [--explore] [--gzweb] [--no-gzweb-vite] [--no-rviz] [--world PATH] NO_GZWEB_VITE=false
./run_all.sh localization [--gzweb] [--no-gzweb-vite] [--no-rviz] [--map PATH] [--world PATH]
while [[ $# -gt 0 ]]; do
--no-gzweb-vite 需与 --gzweb 同时使用:不启动 workspace 内 gzweb-demo 的 Vite浏览器用 /opt/gzweb8000 case "$1" in
EOF --explore) WITH_EXPLORE=true; shift ;;
exit 0 --vision) WITH_VISION=true; shift ;;
;; --gzweb) WITH_GZWEB=true; shift ;;
*) --no-gzweb-vite) NO_GZWEB_VITE=true; shift ;;
echo "[run_all] unknown argument: $1" >&2 --no-rviz) NO_RVIZ=true; shift ;;
exit 1 --map) MAP_YAML="$2"; shift 2 ;;
;; --world) WORLD_PATH="$2"; shift 2 ;;
esac --help|-h)
done cat <<'EOF'
Usage:
if [[ "$MODE" != "mapping" && "$MODE" != "localization" ]]; then ./run_all.sh mapping [--explore] [--gzweb] [--no-gzweb-vite] [--no-rviz] [--world PATH]
echo "[run_all] mode must be mapping or localization" >&2 ./run_all.sh localization [--gzweb] [--no-gzweb-vite] [--no-rviz] [--map PATH] [--world PATH]
exit 1
fi --no-gzweb-vite 需与 --gzweb 同时使用:不启动 workspace 内 gzweb-demo 的 Vite浏览器用 /opt/gzweb8000
EOF
if [[ ! -f "$WORLD_PATH" ]]; then exit 0
echo "[run_all] world not found: $WORLD_PATH" >&2 ;;
exit 1 *)
fi echo "[run_all] unknown argument: $1" >&2
exit 1
if [[ "$MODE" == "localization" && ! -f "$MAP_YAML" ]]; then ;;
echo "[run_all] map yaml not found: $MAP_YAML" >&2 esac
exit 1 done
fi
if [[ "$MODE" != "mapping" && "$MODE" != "localization" ]]; then
set +u echo "[run_all] mode must be mapping or localization" >&2
source /opt/ros/humble/setup.bash exit 1
fi
# 仅在 install 目录缺失时构建(避免每次启动都重复构建,同时修复未 source ROS 环境导致的 ament_cmake 找不到)
if [[ ! -d "${ROOT}/install" ]]; then if [[ ! -f "$WORLD_PATH" ]]; then
colcon build --symlink-install echo "[run_all] world not found: $WORLD_PATH" >&2
fi exit 1
fi
source "${ROOT}/install/setup.bash"
set -u if [[ "$MODE" == "localization" && ! -f "$MAP_YAML" ]]; then
export TURTLEBOT3_MODEL="$ROBOT_MODEL" echo "[run_all] map yaml not found: $MAP_YAML" >&2
exit 1
LOG_DIR="${ROOT}/logs/run_all_$(date +%Y%m%d_%H%M%S)" fi
mkdir -p "$LOG_DIR"
mkdir -p "$(dirname "$MAP_BASENAME")" set +u
source /opt/ros/humble/setup.bash
PIDS=()
# 仅在 install 目录缺失时构建(避免每次启动都重复构建,同时修复未 source ROS 环境导致的 ament_cmake 找不到)
start_bg() { if [[ ! -d "${ROOT}/install" ]]; then
local name="$1"; shift colcon build --symlink-install
local logfile="${LOG_DIR}/${name}.log" fi
echo "[run_all] starting ${name} ..."
"$@" >"$logfile" 2>&1 & source "${ROOT}/install/setup.bash"
local pid=$! set -u
PIDS+=("$pid") export TURTLEBOT3_MODEL="$ROBOT_MODEL"
echo "[run_all] ${name} pid=${pid}, log=${logfile}"
} LOG_DIR="${ROOT}/logs/run_all_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$LOG_DIR"
cleanup() { mkdir -p "$(dirname "$MAP_BASENAME")"
echo "[run_all] stopping all processes..."
for pid in "${PIDS[@]:-}"; do PIDS=()
kill "$pid" 2>/dev/null || true
done start_bg() {
} local name="$1"; shift
trap cleanup EXIT INT TERM local logfile="${LOG_DIR}/${name}.log"
echo "[run_all] starting ${name} ..."
wait_for_topic() { "$@" >"$logfile" 2>&1 &
local topic="$1" local pid=$!
local timeout="${2:-60}" PIDS+=("$pid")
local i=0 echo "[run_all] ${name} pid=${pid}, log=${logfile}"
while (( i < timeout )); do }
if ros2 topic list 2>/dev/null | grep -Fxq "${topic}"; then
return 0 cleanup() {
fi echo "[run_all] stopping all processes..."
sleep 1 for pid in "${PIDS[@]:-}"; do
((i+=1)) kill "$pid" 2>/dev/null || true
done done
return 1 }
} trap cleanup EXIT INT TERM
wait_for_port() { wait_for_topic() {
local port="$1" local topic="$1"
local timeout="${2:-30}" local timeout="${2:-60}"
local i=0 local i=0
while (( i < timeout )); do while (( i < timeout )); do
if ss -ltn 2>/dev/null | grep -q ":${port} "; then if ros2 topic list 2>/dev/null | grep -Fxq "${topic}"; then
return 0 return 0
fi fi
sleep 1 sleep 1
((i+=1)) ((i+=1))
done done
return 1 return 1
} }
wait_for_explore_stop_log() { wait_for_port() {
local logfile="$1" local port="$1"
local timeout="${2:-900}" local timeout="${2:-30}"
local i=0 local i=0
while (( i < timeout )); do while (( i < timeout )); do
if [[ -f "$logfile" ]] && grep -Eq "All frontiers traversed/tried out, stopping\\.|Exploration stopped\\." "$logfile"; then if ss -ltn 2>/dev/null | grep -q ":${port} "; then
return 0 return 0
fi fi
sleep 1 sleep 1
((i+=1)) ((i+=1))
done done
return 1 return 1
} }
wait_for_explore_stop_log_with_timer() { wait_for_explore_stop_log() {
local logfile="$1" local logfile="$1"
local timeout="${2:-900}" local timeout="${2:-900}"
local start_ts local i=0
start_ts="$(date +%s)" while (( i < timeout )); do
local i=0 if [[ -f "$logfile" ]] && grep -Eq "All frontiers traversed/tried out, stopping\\.|Exploration stopped\\." "$logfile"; then
while (( i < timeout )); do return 0
if [[ -f "$logfile" ]] && grep -Eq "All frontiers traversed/tried out, stopping\\.|Exploration stopped\\." "$logfile"; then fi
return 0 sleep 1
fi ((i+=1))
if (( i % 10 == 0 )); then done
local now_ts elapsed return 1
now_ts="$(date +%s)" }
elapsed=$(( now_ts - start_ts ))
echo "[run_all] explore elapsed: ${elapsed}s" wait_for_explore_stop_log_with_timer() {
fi local logfile="$1"
sleep 1 local timeout="${2:-900}"
((i+=1)) local start_ts
done start_ts="$(date +%s)"
return 1 local i=0
} while (( i < timeout )); do
if [[ -f "$logfile" ]] && grep -Eq "All frontiers traversed/tried out, stopping\\.|Exploration stopped\\." "$logfile"; then
kill_port_listeners() { return 0
local port="$1" fi
local pids if (( i % 10 == 0 )); then
pids="$(ss -ltnp 2>/dev/null | grep ":${port} " | sed -E 's/.*pid=([0-9]+).*/\1/' | sort -u || true)" local now_ts elapsed
if [[ -n "${pids}" ]]; then now_ts="$(date +%s)"
echo "[run_all] port ${port} already in use, killing: ${pids}" elapsed=$(( now_ts - start_ts ))
kill ${pids} 2>/dev/null || true echo "[run_all] explore elapsed: ${elapsed}s"
sleep 1 fi
fi sleep 1
} ((i+=1))
done
show_rviz_arg="show_rviz:=true" return 1
if [[ "$NO_RVIZ" == true ]]; then }
show_rviz_arg="show_rviz:=false"
fi kill_port_listeners() {
local port="$1"
if [[ "$MODE" == "mapping" ]]; then local pids
start_bg "A_sim" \ pids="$(ss -ltnp 2>/dev/null | grep ":${port} " | sed -E 's/.*pid=([0-9]+).*/\1/' | sort -u || true)"
ros2 launch turtlebot3_gazebo turtlebot3_office_gz.launch.py \ if [[ -n "${pids}" ]]; then
robot_model:="$ROBOT_MODEL" \ echo "[run_all] port ${port} already in use, killing: ${pids}"
use_sim_time:="$USE_SIM_TIME" \ kill ${pids} 2>/dev/null || true
start_slam:=true \ sleep 1
"$show_rviz_arg" \ fi
gz_partition:="$GZ_PARTITION" \ }
gz_world_name:=default \
world:="$WORLD_PATH" show_rviz_arg="show_rviz:=true"
else if [[ "$NO_RVIZ" == true ]]; then
start_bg "A_sim" \ show_rviz_arg="show_rviz:=false"
ros2 launch turtlebot3_gazebo turtlebot3_office_gz.launch.py \ fi
robot_model:="$ROBOT_MODEL" \
use_sim_time:="$USE_SIM_TIME" \ if [[ "$MODE" == "mapping" ]]; then
start_slam:=false \ start_bg "A_sim" \
"$show_rviz_arg" \ ros2 launch turtlebot3_gazebo turtlebot3_office_gz.launch.py \
gz_partition:="$GZ_PARTITION" \ robot_model:="$ROBOT_MODEL" \
gz_world_name:=default \ use_sim_time:="$USE_SIM_TIME" \
world:="$WORLD_PATH" start_slam:=true \
fi "$show_rviz_arg" \
gz_partition:="$GZ_PARTITION" \
# --gzwebgz launch(9002) + 可选 office 内 gzweb-demo 的 Vite5173 gz_world_name:=default \
# --gzweb --no-gzweb-vite不起 office Vite页面用 foundation 已在 /opt/gzweb:8000 起的实例(见 run_all_fix.sh world:="$WORLD_PATH"
if [[ "$WITH_GZWEB" == true ]]; then else
export GZ_PARTITION start_bg "A_sim" \
OFFICE_MODEL_ROOT="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/models/turtlebot3_office" ros2 launch turtlebot3_gazebo turtlebot3_office_gz.launch.py \
RESOURCE_PATHS=( robot_model:="$ROBOT_MODEL" \
"${HOME}/.gazebo/models" use_sim_time:="$USE_SIM_TIME" \
"${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/models" start_slam:=false \
"${OFFICE_MODEL_ROOT}" "$show_rviz_arg" \
) gz_partition:="$GZ_PARTITION" \
for d in "${OFFICE_MODEL_ROOT}"/*/materials/scripts "${OFFICE_MODEL_ROOT}"/*/materials/textures; do gz_world_name:=default \
[[ -d "${d}" ]] && RESOURCE_PATHS+=("${d}") world:="$WORLD_PATH"
done fi
RESOURCE_PATH_JOINED="$(IFS=:; echo "${RESOURCE_PATHS[*]}")"
export GZ_SIM_RESOURCE_PATH="${RESOURCE_PATH_JOINED}" # --gzwebgz launch(9002) + 可选 office 内 gzweb-demo 的 Vite5173
# Keep compatibility with components resolving textures via legacy lookup vars. # --gzweb --no-gzweb-vite不起 office Vite页面用 foundation 已在 /opt/gzweb:8000 起的实例(见 run_all_fix.sh
export GZ_FILE_PATH="${RESOURCE_PATH_JOINED}" if [[ "$WITH_GZWEB" == true ]]; then
export GAZEBO_RESOURCE_PATH="${RESOURCE_PATH_JOINED}" export GZ_PARTITION
OFFICE_MODEL_ROOT="${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/models/turtlebot3_office"
if [[ "$NO_GZWEB_VITE" != true ]]; then RESOURCE_PATHS=(
if [[ ! -d "${ROOT}/gzweb-demo/gzweb-demo/node_modules/gzweb" ]]; then "${HOME}/.gazebo/models"
echo "[run_all] gzweb demo dependencies missing, running npm install once..." "${ROOT}/turtlebot3_simulations/turtlebot3_gazebo/models"
bash -lc "cd '${ROOT}/gzweb-demo/gzweb-demo' && npm install" "${OFFICE_MODEL_ROOT}"
fi )
else for d in "${OFFICE_MODEL_ROOT}"/*/materials/scripts "${OFFICE_MODEL_ROOT}"/*/materials/textures; do
echo "[run_all] --no-gzweb-vite: skipping office gzweb-demo npm install; use /opt/gzweb (port 8000) for UI." [[ -d "${d}" ]] && RESOURCE_PATHS+=("${d}")
fi done
RESOURCE_PATH_JOINED="$(IFS=:; echo "${RESOURCE_PATHS[*]}")"
kill_port_listeners 9002 export GZ_SIM_RESOURCE_PATH="${RESOURCE_PATH_JOINED}"
kill_port_listeners 5173 # Keep compatibility with components resolving textures via legacy lookup vars.
export GZ_FILE_PATH="${RESOURCE_PATH_JOINED}"
echo "[run_all] waiting for /clock before websocket..." export GAZEBO_RESOURCE_PATH="${RESOURCE_PATH_JOINED}"
if wait_for_topic "/clock" 25; then
start_bg "D_websocket" gz launch "${ROOT}/websocket.sdf" if [[ "$NO_GZWEB_VITE" != true ]]; then
if [[ "$NO_GZWEB_VITE" != true ]]; then if [[ ! -d "${ROOT}/gzweb-demo/gzweb-demo/node_modules/gzweb" ]]; then
start_bg "E_gzweb_frontend" bash -lc "cd '${ROOT}/gzweb-demo/gzweb-demo' && npm run dev:host" echo "[run_all] gzweb demo dependencies missing, running npm install once..."
echo "[run_all] office gzweb-demo Vite: http://localhost:5173" bash -lc "cd '${ROOT}/gzweb-demo/gzweb-demo' && npm install"
fi fi
else else
echo "[run_all] WARN: /clock not detected in time; starting websocket (and optional frontend) anyway." echo "[run_all] --no-gzweb-vite: skipping office gzweb-demo npm install; use /opt/gzweb (port 8000) for UI."
start_bg "D_websocket" gz launch "${ROOT}/websocket.sdf" fi
if [[ "$NO_GZWEB_VITE" != true ]]; then
start_bg "E_gzweb_frontend" bash -lc "cd '${ROOT}/gzweb-demo/gzweb-demo' && npm run dev:host" kill_port_listeners 9002
fi kill_port_listeners 5173
fi
echo "[run_all] waiting for /clock before websocket..."
if wait_for_port 9002 12; then if wait_for_topic "/clock" 25; then
echo "[run_all] websocket server ready on ws://localhost:9002" start_bg "D_websocket" gz launch "${ROOT}/websocket.sdf"
else if [[ "$NO_GZWEB_VITE" != true ]]; then
echo "[run_all] ERROR: websocket server failed on 9002; check ${LOG_DIR}/D_websocket.log" start_bg "E_gzweb_frontend" bash -lc "cd '${ROOT}/gzweb-demo/gzweb-demo' && npm run dev:host"
fi echo "[run_all] office gzweb-demo Vite: http://localhost:5173"
fi fi
else
echo "[run_all] waiting ${SLEEP_NAV2}s before starting Nav2..." echo "[run_all] WARN: /clock not detected in time; starting websocket (and optional frontend) anyway."
sleep "$SLEEP_NAV2" start_bg "D_websocket" gz launch "${ROOT}/websocket.sdf"
if [[ "$NO_GZWEB_VITE" != true ]]; then
if [[ "$MODE" == "mapping" ]]; then start_bg "E_gzweb_frontend" bash -lc "cd '${ROOT}/gzweb-demo/gzweb-demo' && npm run dev:host"
start_bg "B_nav2" ros2 launch nav2_bringup navigation_launch.py use_sim_time:="$USE_SIM_TIME" fi
else fi
start_bg "B_nav2" \
ros2 launch nav2_bringup bringup_launch.py \ if wait_for_port 9002 12; then
use_sim_time:="$USE_SIM_TIME" \ echo "[run_all] websocket server ready on ws://localhost:9002"
map:="$MAP_YAML" \ else
slam:=false echo "[run_all] ERROR: websocket server failed on 9002; check ${LOG_DIR}/D_websocket.log"
fi fi
fi
if [[ "$WITH_EXPLORE" == true && "$MODE" == "mapping" ]]; then
start_bg "C_explore" ros2 launch explore_lite explore.launch.py use_sim_time:="$USE_SIM_TIME" echo "[run_all] waiting ${SLEEP_NAV2}s before starting Nav2..."
last_index=$(( ${#PIDS[@]} - 1 )) sleep "$SLEEP_NAV2"
EXPLORE_PID="${PIDS[$last_index]}"
EXPLORE_LOG="${LOG_DIR}/C_explore.log" if [[ "$MODE" == "mapping" ]]; then
fi 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
echo start_bg "B_nav2" \
echo "[run_all] started. Mode: ${MODE}" ros2 launch nav2_bringup bringup_launch.py params_file:=/workspace/nav2_params_fast.yaml \
echo "[run_all] logs: ${LOG_DIR}" use_sim_time:="$USE_SIM_TIME" \
echo map:="$MAP_YAML" \
slam:=false
if [[ "$MODE" == "mapping" ]]; then fi
if [[ "$WITH_GZWEB" == true ]]; then
if [[ "$NO_GZWEB_VITE" == true ]]; then if [[ "$WITH_EXPLORE" == true && "$MODE" == "mapping" ]]; then
echo "[run_all] UI: foundation /opt/gzweb on port 8000 (no office Vite)." start_bg "C_explore" ros2 launch explore_lite explore.launch.py use_sim_time:="$USE_SIM_TIME"
elif wait_for_port 5173 25; then last_index=$(( ${#PIDS[@]} - 1 ))
echo "[run_all] office gzweb-demo ready: http://localhost:5173" EXPLORE_PID="${PIDS[$last_index]}"
else EXPLORE_LOG="${LOG_DIR}/C_explore.log"
echo "[run_all] WARN: office gzweb frontend not ready yet, check ${LOG_DIR}/E_gzweb_frontend.log" fi
fi
fi echo
if [[ "$WITH_VISION" == true ]]; then
if [[ "${WITH_EXPLORE}" == true && -n "${EXPLORE_PID:-}" ]]; then start_bg "D_vision_yolo" ros2 run vision_yolo detector
echo "[run_all] exploration is running (pid=${EXPLORE_PID}), waiting for stop signal (timeout=${EXPLORE_WAIT_TIMEOUT}s)..." last_index=$(( ${#PIDS[@]} - 1 ))
if wait_for_explore_stop_log_with_timer "${EXPLORE_LOG}" "${EXPLORE_WAIT_TIMEOUT}"; then VISION_PID="${PIDS[$last_index]}"
echo "[run_all] detected exploration stop from log, finalizing explore process..." VISION_LOG="${LOG_DIR}/D_vision_yolo.log"
else fi
echo "[run_all] WARN: explore stop not detected within timeout; forcing stop and saving current map."
fi echo "[run_all] started. Mode: ${MODE}"
kill "${EXPLORE_PID}" 2>/dev/null || true echo "[run_all] logs: ${LOG_DIR}"
wait "${EXPLORE_PID}" 2>/dev/null || true echo
echo "[run_all] explore_lite stopped, now saving map."
fi if [[ "$MODE" == "mapping" ]]; then
if [[ "$WITH_GZWEB" == true ]]; then
if [[ "${WITH_EXPLORE}" == true ]]; then if [[ "$NO_GZWEB_VITE" == true ]]; then
echo "[run_all] auto-saving map to ${MAP_BASENAME}.yaml/.pgm ..." echo "[run_all] UI: foundation /opt/gzweb on port 8000 (no office Vite)."
ros2 run nav2_map_server map_saver_cli -f "$MAP_BASENAME" --ros-args -p use_sim_time:="$USE_SIM_TIME" elif wait_for_port 5173 25; then
echo "[run_all] map saved successfully:" echo "[run_all] office gzweb-demo ready: http://localhost:5173"
echo " - ${MAP_BASENAME}.yaml" else
echo " - ${MAP_BASENAME}.pgm" echo "[run_all] WARN: office gzweb frontend not ready yet, check ${LOG_DIR}/E_gzweb_frontend.log"
echo "[run_all] mapping workflow completed, exiting now." fi
exit 0 fi
fi
if [[ "${WITH_EXPLORE}" == true && -n "${EXPLORE_PID:-}" ]]; then
cat <<EOF echo "[run_all] exploration is running (pid=${EXPLORE_PID}), waiting for stop signal (timeout=${EXPLORE_WAIT_TIMEOUT}s)..."
[run_all] Mapping mode notes: if wait_for_explore_stop_log_with_timer "${EXPLORE_LOG}" "${EXPLORE_WAIT_TIMEOUT}"; then
- RViz 'Localization: inactive' is normal during SLAM mapping. echo "[run_all] detected exploration stop from log, finalizing explore process..."
- You did not enable --explore, so auto-save/auto-exit is disabled. else
- Press Ctrl+C when you want to stop all. echo "[run_all] WARN: explore stop not detected within timeout; forcing stop and saving current map."
EOF fi
wait kill "${EXPLORE_PID}" 2>/dev/null || true
else wait "${EXPLORE_PID}" 2>/dev/null || true
echo "[run_all] Localization mode: RViz Localization should become active after initial pose." echo "[run_all] explore_lite stopped, now saving map."
echo "[run_all] Keep this terminal running; Ctrl+C to stop all." fi
wait
fi if [[ "${WITH_EXPLORE}" == true ]]; then
echo "[run_all] auto-saving map to ${MAP_BASENAME}.yaml/.pgm ..."
ros2 run nav2_map_server map_saver_cli -f "$MAP_BASENAME" --ros-args -p use_sim_time:="$USE_SIM_TIME"
echo "[run_all] map saved successfully:"
echo " - ${MAP_BASENAME}.yaml"
echo " - ${MAP_BASENAME}.pgm"
echo "[run_all] mapping workflow completed, exiting now."
exit 0
fi
cat <<EOF
[run_all] Mapping mode notes:
- RViz 'Localization: inactive' is normal during SLAM mapping.
- You did not enable --explore, so auto-save/auto-exit is disabled.
- Press Ctrl+C when you want to stop all.
EOF
wait
else
echo "[run_all] Localization mode: RViz Localization should become active after initial pose."
echo "[run_all] Keep this terminal running; Ctrl+C to stop all."
wait
fi

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'{joint_gz}@sensor_msgs/msg/JointState[gz.msgs.Model',
f'{tf_topic}@tf2_msgs/msg/TFMessage[gz.msgs.Pose_V', f'{tf_topic}@tf2_msgs/msg/TFMessage[gz.msgs.Pose_V',
'/scan@sensor_msgs/msg/LaserScan@gz.msgs.LaserScan', '/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=[ remappings=[
(joint_gz, '/joint_states'), (joint_gz, '/joint_states'),
(tf_topic, '/tf'), (tf_topic, '/tf'),
(cmd_topic, '/cmd_vel'), (cmd_topic, '/cmd_vel'),
(odom_topic, '/odom'), (odom_topic, '/odom'),
('/camera', '/camera/image_raw'),
('/camera_info', '/camera/camera_info'),
], ],
parameters=[{ parameters=[{
f'qos_overrides.{cmd_topic}.subscriber.reliability': 'reliable', f'qos_overrides.{cmd_topic}.subscriber.reliability': 'reliable',

View File

@ -432,10 +432,6 @@
</axis> </axis>
</joint> </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"> <plugin filename="gz-sim-diff-drive-system" name="gz::sim::systems::DiffDrive">
<left_joint>wheel_left_joint</left_joint> <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 filename="gz-sim-joint-state-publisher-system" name="gz::sim::systems::JointStatePublisher">
</plugin> </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> </model>
</sdf> </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.