手动建工程

来到总工作目录下, 如~/catkin_ws

新建工作目录

mkdir -p robot_test/src
cd src
catkin_init_workspace 初始化工作空间
cd .. 回到robot_test
catkin_make 编译工程

添加工作路径path

vim ~/.bashrc
在最下面加入工作路径
source ~/catkin_ws/robot_test/devel/setup.bash

编写模块,或下载模块

下载模块
git clone https://github.com/robopeak/rplidar_ros 如rplidar_ros模块
并移至robot/src路径下

下载模块

git clone https://github.com/robopeak/rplidar_ros 如rplidar_ros模块
并移至robot/src路径下

编写模块

cd robot_test/src
#一般用这种方式创建模块可以自动添加依赖
catkin_create_pkg amin roscpp rospy std_msgs(rosmsg)创建模块命名并同时添加依赖
cd hello
vim hello.cpp 写程序

#以下创建模块为手动添加依赖的方法
catkin_create_pkg hello 创建模块并命名
添加cpp文件支持
CMakeLists.txt 文件中
find_package(catkin REQUIRED COMPONENTS roscpp)
加上COMPONENTS roscpp, 表示支持cpp组件
在包的清单文件package.xml 中列出依赖库
# build_depend (编译依赖) run_depend(运行依赖)
<build_depend>roscpp</build_depend>
<run_depend>roscpp</run_depend>
# 有时会提示改成<exec_depend>
声明可执行文件
# CMakeLists.txt 中添加两行,来声明我们需要创建的可执行文件。
add_executable(executable-name source-files)
# 此处executable-name为执行文件的名字(hello),source-files为源文件的名字(hello.cpp)
target_link_libraries(executable-name ${catkin_LIBRARIES})
例hello.cpp文件(每写一个文件都要添加两行):
add_executable(hello hello.cpp)
target_link_libraries(hello ${catkin_LIBRARIES})
#直到以上都是手动添加依赖的方法

编译工作区
cd ~/catkin_ws/robot_test
catkin_make
catkin_make_isolated --install
编译时有可能会提示找不到ros.h,解决办法:
来到CMakeLists.txt, 改成如下 原本是有注释的, 把注释去掉
include_directories(
include
${catkin_INCLUDE_DIRS}
)

hello.cpp示例代码

#include <ros/ros.h>

int main ( int argc , char * argv[] )
{
ros::init ( argc , argv , "hello" ) ;
ros::NodeHandle nh ;
ROS_INFO_STREAM( " Hello , ROS! " ) ;
}

写好模块之后再次编译

cd ~/catkin_ws/robot_test
catkin_make_isolated --install
就是针对每个package独立进行cmake, make, make install ,这样做的好处是可以对单独的package进行调试。

运行

roslaunch运行
roslaunch rpildar_ros view_rplidar.launch 运行指定模块

rosrun运行
roscore
source devel/setup.bash
rosrun amin hello

远程传输

ssh连接远程(两边都要开启ssh服务)
ssh username@ip_address 主机名@IP地址
查看ssh服务状态
service ssh status
开启ssh服务
service ssh start

gazebo仿真

/usr/bin/env: "python\r": 没有那个文件或目录
解决方法:设置为unix格式
使用VIM打开文本
:set ff 然后回车,我这个文件显示为dos格式。
:set ff=unix 回车,设置为unix格式。
:wq

报错未能找到路径
Found the following, but they're either not files, or not executable:
         /home/peng/catkin_ws/src/xxxx/xxxx/xxxx.py
解决办法: 给予执行权限
来到对应文件路径
chmod +x hello.py