ros学习

一、初始化
1. 创建ROS工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/
catkin_make
source devel/setup.bash
2. 创建ROS Package
 cd ~/catkin_ws/src
 catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
 # catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
3. 编译
cd ~/catkin_ws
catkin_make
chmod 777 ~/catkin_ws/devel/setup.bash
~/catkin_ws/devel/setup.bash
二、ROS操作
1. ROS节点命令
roscore 					#在运行所有ROS程序前首先要运行的命令。
rosnode list				#看哪些节点在运行
rosnode info [node_name]	#返回某个指定节点的信息
rosrun [package_name] [node_name]	#用包名直接运行软件包内的节点
rosnode ping [node]			#测试节点是否正常
2. ROS话题命令
- rqt_graph用动态的图显示了系统中正在发生的事情.
sudo apt-get install ros-kinetic-rqt
sudo apt-get install ros-kinetic-rqt-common-plugins
rosrun rqt_graph rqt_graph


-rostopic命令工具能让你获取ROS话题的信息。
rostopic echo [topic] 	#显示在某个话题上发布的数据。
rostopic list			#列出当前已被订阅和发布的所有话题
rostopic type [topic] 	#查看所发布话题的消息类型
rosmsg show [type]		#查看type详细信息
rostopic pub [topic] [msg_type] [args] #数据发布
rostopic hz [topic]		#报告数据发布的速率
rostopic pub -1 /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'
-1: 这一选项会让rostopic只发布一条消息,然后退出.
/turtle1/cmd_vel: 这是要发布到的话题的名称
geometry_msgs/Twist: 这是发布到话题时要使用的消息的类型
--: 这一选项(两个破折号)用来告诉选项解析器,表明之后的参数都不是选项。
3. ROS服务
rosservice list         输出活跃服务的信息
rosservice call         用给定的参数调用服务
rosservice type         输出服务的类型
rosservice find         按服务的类型查找服务
rosservice uri          输出服务的ROSRPC uri
rossrv [type]			二级具体

rosparam set            设置参数
rosparam get            获取参数
rosparam load           从文件中加载参数
rosparam dump           向文件中转储参数
rosparam delete         删除参数
rosparam list           列出参数名	
4. ROS启动
roslaunch [package] [filename.launch]
5. 创建ROS消息和服务
#创建msg
$ roscd beginner_tutorials
$ mkdir msg
$ echo "int64 num" > msg/Num.msg

#复制一个srv过来
$ mkdir srv
$ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv

#打开package.xml, 确保它包含以下两行且没有被注释
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>

#打开CMakeLists.txt为已经存在里面的find_package调用添加message_generation依赖项
# 不要直接复制这一大段,只需将message_generation加在括号闭合前即可
find_package(catkin REQUIRED COMPONENTS
   roscpp
   rospy
   std_msgs
   message_generation
)

#还要确保导出消息的运行时依赖关系
catkin_package(
  ...
  CATKIN_DEPENDS message_runtime ...
  ...)
  
#修改字段
add_message_files(
  FILES
  Num.msg
)
add_service_files(
  FILES
  AddTwoInts.srv
)
  • 验证
	$ rosmsg show beginner_tutorials/Num
	$ rosmsg show Num
	
	$ rossrv show beginner_tutorials/AddTwoInts
  • 编译
# In your catkin workspace
$ roscd beginner_tutorials
$ cd ../..
$ catkin_make
$ cd -

C++消息的头文件将生成在~/catkin_ws/devel/include/beginner_tutorials/。
Python脚本将创建在~/catkin_ws/devel/lib/python2.7/dist-packages/beginner_tutorials/msg。
三、ROS编程(c++)
1.发布者和订阅者
$ roscd beginner_tutorials
$ cd src
$ vim talker.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"

#include <sstream>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;
  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
  ros::Rate loop_rate(10);

  int count = 0;
  while (ros::ok())
  {
    std_msgs::String msg;
    std::stringstream ss;
    ss << "hello world " << count;
    msg.data = ss.str();

    ROS_INFO("%s", msg.data.c_str());
    chatter_pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
  }
  return 0;
}

$ vim listener.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
  ros::spin();

  return 0;
}

$ roscd beginner_tutorials
$ vim CMakeLists.txt

#添加
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)

add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)

# 第一行:生成可执行文件,生成的可执行文件在~/catkin_ws/devel/lib/<package name>

  • 编译

      $ cd~/catkin_ws
      $ catkin_make
    

  • 验证
	$ roscore
	$ rosrun beginner_tutorials talker
	$ rosrun beginner_tutorials listener
2.服务端和客户端
$ vim add_two_ints_server.cpp

#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h"

bool add(beginner_tutorials::AddTwoInts::Request  &req,
         beginner_tutorials::AddTwoInts::Response &res)
{
  res.sum = req.a + req.b;
  ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
  ROS_INFO("sending back response: [%ld]", (long int)res.sum);
  return true;
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "add_two_ints_server");
  ros::NodeHandle n;

  ros::ServiceServer service = n.advertiseService("add_two_ints", add);
  ROS_INFO("Ready to add two ints.");
  ros::spin();

  return 0;
}

$ vim add_two_ints_client.cpp

#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h"
#include <cstdlib>

int main(int argc, char **argv)
{
  ros::init(argc, argv, "add_two_ints_client");
  if (argc != 3)
  {
    ROS_INFO("usage: add_two_ints_client X Y");
    return 1;
  }

  ros::NodeHandle n;
  ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
  beginner_tutorials::AddTwoInts srv;
  srv.request.a = atoll(argv[1]);
  srv.request.b = atoll(argv[2]);
  if (client.call(srv))
  {
    ROS_INFO("Sum: %ld", (long int)srv.response.sum);
  }
  else
  {
    ROS_ERROR("Failed to call service add_two_ints");
    return 1;
  }

  return 0;
}

$ vim CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)

## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)

## Declare ROS messages and services
add_message_files(FILES Num.msg)
add_service_files(FILES AddTwoInts.srv)

## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs)

## Declare a catkin package
catkin_package()

## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})

add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)

add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)

$ cd ~/catkin_ws
$ catkin_make

四、CMakelist.txt解析

https://www.jianshu.com/p/75fa7b1f3938

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值