习题2
课程要点:
- ROS包结构 - 使用Eclipse进行集成化编程 - ROS C++客户端库(roscpp) - ROS订阅者和发布者 - ROS参数服务器
- RViz可视化
练习内容:
在这个练习中,你将创建第一个ROS包。 该软件包最终应能够订阅来自Husky 机器人的激光扫描消息并处理传入的数据。 这个节点将成为下一个练习的基础。 使用Eclipse编辑包(第2讲幻灯片10-13)。
1. 可选(困难模式):从头开始创建包husky_highlevel_controller(查看ROS模板以供参考https://github.com/ethz-asl/ros_best_practices)。 使用该命令catkin_create_pkg创建一个新的包,其依赖关系为roscpp和sensor_msgs。
~~: catkin_create_pkg package_name {dependencies}
2. 或者(简单模式):从课程网站下载包含预备文件的Zip文件husky_highlevel_controller。
~~: 直接下载,但推荐步骤1。
3. 查看CMakelists.txt和package.xml文件。 (讲座2幻灯片5-7页),如下:
----CMakelists.txt
cmake_minimum_required(VERSION 2.8.3)
project(husky_highlevel_controller)
## Use C++11
add_definitions(--std=c++11)
## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
roscpp
sensor_msgs
)
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if you package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
INCLUDE_DIRS
include
# LIBRARIES
CATKIN_DEPENDS
roscpp
sensor_msgs
# DEPENDS
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
include
${catkin_INCLUDE_DIRS}
)
## Declare a C++ executable
add_executable(${PROJECT_NAME}
src/${PROJECT_NAME}_node.cpp
src/HuskyHighlevelController.cpp
)
## Specify libraries to link a library or executable target against
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
)
----package.xml
<?xml version="1.0"?>
<package format="2">
<name>husky_highlevel_controller</name>
<version>0.1.0</version>
<description>The husky_highlevel_controller package</description>
<maintainer email="dominic.jud@mavt.ethz.ch">Dominic Jud</maintainer>
<license>BSD</license>
<author email="dominic.jud@mavt.ethz.ch">Dominic Jud</author>
<buildtool_depend>catkin</buildtool_depend>
<depend>roscpp</depend>
<depend>sensor_msgs</depend>
</package>
----
4. 为/scan主题创建一个订阅者。 (讲座2幻灯片17/19)
//----Subscribers--subscriber to /scan topic
ros::Subscriber scan_sub_;
5. 订阅/scan并带主题名称和队列大小的参数文件。 (讲座2幻灯片20/21)
scan_sub_topic: /scan
scan_sub_queue_size: 200
6. 为该用户创建一个回调方法,用于输出激光扫描仪到终端的最小距离测量结果。 查看消息类型链接:http://docs.ros.org/kinetic/api/sensor_msgs/html/msg/LaserScan.html。这里需要注意(rosrun
itself doesn't load parameters. rosparam
is the tool for that. Basically the command is the same like in the lauch file, you'd only need to take care of pushing things into the node's namespace, e.g.)
// subscribers
scan_sub_ = nodeHandle_.subscribe(subscriberTopic_, queue_size , &HuskyHighlevelController::scanCallback, this);
void HuskyHighlevelController::scanCallback(const sensor_msgs::LaserScan &scan_msg)
{
float smallest_distance = 100;
// the angle corresponding to the minimum distance
//number of the elements in ranges array
int arr_size = floor((scan_msg.angle_max-scan_msg.angle_min)/scan_msg.angle_increment);
for (int i=0 ; i< arr_size ;i++)
{
if (scan_msg.ranges[i] < smallest_distance)
{
smallest_distance = scan_msg.ranges[i];
}
}
}
cout<<"cout Minimum laser distance(m): "<<smallest_distance<<"\n";
ROS_INFO_STREAM("ROS_INFO_STREAM Minimum laser distance(m): %lf"<<smallest_distance);
ROS_INFO("ROS_INFO Minimum laser distance(m): %lf", smallest_distance);
找出上述三种差异,指出一个小bug?
7. 从练习1添加您的启动文件并另外添加:
○ 运行husky_highlevel_controller节点。
○ 加载参数文件。
<node pkg="husky_highlevel_controller" type="husky_highlevel_controller" name="husky_highlevel_controller"
output="screen" launch-prefix="gnome-terminal --command">
<rosparam command="load" file="$(find husky_highlevel_controller)/config/config.yaml" />
</node>
8. 从启动文件launch传递参数laser_enabled到husky_empty_world.launch文件,其值为true。
<arg name="laser_enabled" default="true"/>
9. 在RViz中显示激光扫描并将RViz添加到启动文件launch中。 确保将odom设置为固定坐标(位于全局选项下)并调整激光扫描点的大小。 (讲座2幻灯片22-24)
<node pkg="rviz" type="rviz" name="rviz" />
评分标准:
❏启动launch文件开始husky仿真。 终端中可以看到激光传感器的输出(最小值等)。[40%]
门柱4.19m和书柜1.03m距离不同在终端显示结果如下:
❏检查节点是否按照作业模板的要求完成。[30%]
❏是否使用了参数文件?[15%]
❏激光扫描数据是否如图所示在RViz中可视化?[15%]
英文原版作业参考之前汇总博客。
~End~