Skip to content

ROS example including some advanced topics and my tricks about ROS development.

Notifications You must be signed in to change notification settings

yanliang-wang/ros_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Commitizen friendly

ROS example

ROS是一个功能很强大的工具,这个仓库包含了ROS的一些常用功能的使用demo,还有一些进阶功能的使用demo,便于快速进行ROS开发。除此之外,还包括一些ROS工具的介绍便于进行ROS工程的调试,enjoy it!

ROS1

Installation

git clone https://github.com/yanliang-wang/ros_example.git 
cd ros_example
rosdep install --from-paths src --ignore-src --rosdistro=melodic
catkin_make

Adcanced

1. roscpp Timer(定时器)

  • roscpp的定时器会计划以某一频率执行自定义的回调操作。
  • 计时器不是实时线程/内核替代品,而是对没有硬实时要求的事物很有用。

创建定时器:

  • 一般用法:

    ros::Timer ros::NodeHandle::createTimer(ros::Duration period, <callback>, bool oneshot = false);
    • period ,这是调用定时器回调函数时间间隔。例如,ros::Duration(0.1),即每十分之一秒执行一次
    • callback,回调函数,支持所有boost::bind类型可以是函数,类方法,函数对象,输入参数要是ros::TimerEvent类型。
    • oneshot ,表示是否只执行一次,如果已经执行过,还可以通过stop()、setPeriod(ros::Duration)和start()来再执行一次。

回调函数用法

  • 用法:

    void callback(const ros::TimerEvent&);// ros::TimerEvent结构体作为参数传入,它提供时间的相关信息,对于调试和配置非常有用
    • ros::TimerEvent结构体说明:
      • ros::Time last_expected 上次回调期望发生的时间
      • ros::Time last_real 上次回调实际发生的时间
      • ros::Time current_expected 本次回调期待发生的时间
      • ros::Time current_real 本次回调实际发生的时间
      • ros::WallTime profile.last_duration 上次回调的时间间隔(结束时间-开始时间),是wall-clock时间。

参考:https://www.ncnynl.com/archives/201702/1296.html

2. Action Demo

Action是ROS中一种双向异步的通信方法,这种机制应该是通过topic的形式来实现的(毕竟topic是单向异步的,双向的topic就可以实现双向异步,而且在action server和client启动时,会产生5个和状态相关的topic,这也验证了这种说法)

a. 常规使用1

  ac.sendGoal(goal);  
//wait for the action to return
  bool finished_before_timeout = ac.waitForResult(ros::Duration(30.0));
  if (finished_before_timeout) ...

在ac发送goal之后,可以指定等待多久时间,而不会像service那种一直阻塞等待结果的返回

这个例子在common_tutorials/actionlib_tutorials里

b. 常规使用2

ac.sendGoal(goal, &doneCB, &activeCB, &feedbackCB);
ros::spin();

这种使用方法在send goal时指定了server开始的回调函数,feedback的回调函数,done的回调函数,可以试试掌握server的状态

Tricks

  1. 减少topic带宽的方式

    • 没有订阅时不发布

      if (topic_name.getNumSubscribers() < 1) return;
  2. rviz显示点(位置,frontier等等)

    可以使用点云pcl::PointXYZI的来表示一个点,另外intensity可以表示想给的颜色,在rviz中的intensity显示模式可以自动的进行颜色的映射,可视化效果很好。

  3. 快速部署ROS包依赖环境

    • 官方依赖(可以通过apt install安装的):在ROS包的package.xml里写出这个包的依赖,然后通过rosdep install来安装一个包或者多个包的依赖

      rosdep install PACKAGE # 安装一个包的依赖
      rosdep install --from-paths src --ignore-src -r # 安装src下所有ROS包的依赖
    • 非官方依赖(通过git下载的):参考rosinstall file format写XXX.rosinstall文件,然后通过rosinstall来安装

      rosinstall DEPENDENCY_LOCATION XXX.rosinstall
  4. 编译选项

    屏蔽一些package的编译:在不想被编译的package目录下新建CATKIN_IGNORE的空文件即可,mata-package同样支持。

    编译指定的package:catkin_make -DCATKIN_WHITELIST_PACKAGES="pkg1, pkg2.."

Convenient ROS packages

  1. topic_tools

    • topic_tools/throttle可以用来方便地限制话题的发布

      • 限制频率到指定频率

        rosrun topic_tools throttle messages <intopic> <msgs_per_sec> [outtopic]

        前面的rosrun topic_tools throttle messages 是指使用的是throttle下面的messages模式,其中 intopic是指你想要改变频率的那个topic msgs_per_sec是指你想要它发布的频率,而outtopic是指改变发布频率后的topic的名称,可以省略,如果省略则自动在原来topic的名字上后缀throttle

      • 限制字节

        rosrun topic_tools throttle bytes <intopic> <bytes_per_sec> <window> [outtopic]

      除了上面的限制方法,还可以添加lazy参数,来减少不必要的消息发布.

    • topic_tools/relay可以用来重映射话题的名称

      e.g. rename base_scan to my_base_scan:

      rosrun topic_tools relay base_scan my_base_scan
      

    topic_tools除了throttle,relay之外,还有一些其他的节点提供了其他功能,参考http://wiki.ros.org/topic_tools

Attention List

  1. move_base规划的path,最后一个pose的值为inf

ROS2

参考官方example教程

About

ROS example including some advanced topics and my tricks about ROS development.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages