New Chat
Ctrl
K
  • Websites
  • Docs
  • Slides
  • Sheets
  • Deep Research
Kimi Code Kimi Claw
Chat History
  • Mobile App
  • About Us
    • Visit Moonshot AI
    • Open Platform
    • Features
    • Terms of Service
    • Privacy Policy
  • Language
  • User Feedback

ROS2 启动问题及解决方案

wangyu@wangyu-LEGION-REN7000K-26IRB:~/projects/ros2_tutorials/pub_sub$ tree . ├── CMakeLists.txt ├── include │   └── pub_sub ├── launch ├── LICENSE ├── package.xml ├── README.md └── src 4 directories, 4 files wangyu@wangyu-LEGION-REN7000K-26IRB:~/ros2_ws/src/pub_sub$ tree . ├── CMakeLists.txt ├── include │   └── pub_sub ├── launch │   └── turtle_circle.launch.py ├── LICENSE ├── log │   ├── COLCON_IGNORE │   ├── latest -> latest_list │   ├── latest_list -> list_2025-10-05_18-55-21 │   └── list_2025-10-05_18-55-21 │   └── logger_all.log ├── package.xml ├── README.md └── src ├── turtle_publisher.cpp └── turtle_subscriber.py 8 directories, 9 files projects下是我的github项目,我该怎么把工作空间的项目同步过来,然后要不要修改什么地方,可以提交到github上。
一句话思路:
把 ros2_ws/src/pub_sub 变成 Git 仓库 ros2_tutorials/pub_sub 的“工作区”,然后只提交“源码 + 构建描述”,不提交 build/, install/, log/ 等临时文件。
下面给你最简、最安全的 5 步流程,你照抄即可。

✅ 第 1 步:一次性准备 .gitignore

进入你的 GitHub 仓库目录,让以后永远不会误提交编译产物:
bash
Copy
cd ~/projects/ros2_tutorials
cat > .gitignore <<'EOF'
# ROS 2
build/
install/
log/
COLCON_IGNORE

# IDE
.vscode/
.idea/
*.swp
*~

# Python
__pycache__/
*.pyc
EOF
git add .gitignore
git commit -m "chore: add ROS 2 .gitignore"

✅ 第 2 步:清空旧空壳,把真正源码搬进来

bash
Copy
# 删掉刚才的空包(只留空壳)
rm -rf pub_sub

# 把整个功能包复制过来
cp -r ~/ros2_ws/src/pub_sub ./

# 立即提交一次“初始导入”
git add pub_sub
git commit -m "feat: import pub_sub package (C++ pub + Python sub)"
此时目录结构变成:
plain
Copy
ros2_tutorials/
├── .git/
├── .gitignore
└── pub_sub/
    ├── CMakeLists.txt
    ├── package.xml
    ├── launch/
    ├── src/
    └── README.md

✅ 第 3 步:以后只在 ros2_ws 里“链接”使用

不要再把源码拷来拷去,用 符号链接 让工作空间指向 Git 仓库即可:
bash
Copy
# 先把原来的功能包挪走备份(可随后删除)
mv ~/ros2_ws/src/pub_sub ~/ros2_ws/src/pub_sub.bak

# 创建软链接(Linux 下最方便)
ln -s ~/projects/ros2_tutorials/pub_sub ~/ros2_ws/src/pub_sub
以后你:
  • 在 ~/ros2_ws/src/pub_sub 里改代码 → 实际改的是 Git 仓库里的文件。
  • 构建、运行照常用 colcon build,产物仍在 ros2_ws/build/install,不会污染 Git 仓库。

✅ 第 4 步:日常开发流程

bash
Copy
# 1. 写代码
code ~/ros2_ws/src/pub_sub/...

# 2. 构建测试
cd ~/ros2_ws
colcon build --packages-select pub_sub
source install/setup.bash
ros2 launch pub_sub turtle_circle.launch.py

# 3. 提交
cd ~/projects/ros2_tutorials
git add pub_sub
git commit -m "feat: 让海龟根据颜色改变方向"
git push origin main

✅ 第 5 步:换电脑 / 队友拉取后怎么用?

bash
Copy
git clone https://github.com/yourname/ros2_tutorials.git
cd ros2_tutorials
# 把软链接再建一次即可
ln -s $(pwd)/pub_sub ~/your_ros2_ws/src/pub_sub

小结(记住 3 句话)

  1. 源码放 Git 仓库,构建产物留在工作空间。
  2. 用 软链接 把“工作空间里的包”指向 Git 仓库,一劳永逸。
  3. 仓库根目录放 .gitignore,别让 build/install/log 混进去。
照做后,你就可以正常 git push,别人也能直接复用你的包!
This model only extracts text from images, switch to K2.5 for full visual understanding.