L2范数 squareNorm(),等价于计算vector的自身点积,norm()返回squareNorm的开方根。
这些操作应用于matrix,norm() 会返回Frobenius或Hilbert-Schmidt范数。
如果你想使用其他Lp范数,可以使用lpNorm< p >()方法。p可以取Infinity,表示L∞范数1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20int main()
{
VectorXf v(2);
MatrixXf m(2,2), n(2,2);
v << -1,
2;
m << 1,-2,
-3,4;
cout << "v.squaredNorm() = " << v.squaredNorm() << endl;
cout << "v.norm() = " << v.norm() << endl;
cout << "v.lpNorm<1>() = " << v.lpNorm<1>() << endl;
cout << "v.lpNorm<Infinity>() = " << v.lpNorm<Infinity>() << endl;
cout << endl;
cout << "m.squaredNorm() = " << m.squaredNorm() << endl;
cout << "m.norm() = " << m.norm() << endl;
cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << endl;
cout << "m.lpNorm<Infinity>() = " << m.lpNorm<Infinity>() << endl;
}
rostopic echo move_base/status
1 | rostopic pub /move_base_simple/goal geometry_msgs/PoseStamped "header: |
一句话:rostopic pub /move_base/cancel actionlib_msgs/GoalID -- {}
,即向话题/move_base/cancel
发空消息1
2
3
4ros::Publisher cancle_pub_ = nh_.advertise("move_base/cancel",1);
actionlib_msgs::GoalID first_goal;
cancle_pub_.publish(first_goal);
1 | install(TARGETS node |
最终的可执行文件的路径在install/lib/node
另一种情况,test_node
是可执行文件,test
是动态库,target_link_libraries
只是保证了devel/lib/test_node
中的可执行文件链接了test
,但不保证install/lib/test_node
中链接了test
,需要如下配置1
2
3
4
5
6install(
TARGETS test test_node
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
注意,如果修改了test
对应的源文件, 需要把编译后的libtest.so
也更新到远程机,否则test_node
没有变化
CMAKE_INSTALL_PREFIX
变量类似于configure脚本的–prefix
,常见的使用方法是在build文件夹里执行cmake -DCMAKE_INSTALL_PREFIX=/usr ..
INSTALL指令用于定义安装规则,安装的内容可以包括目标二进制、动态库、静态库以及
文件、目录、脚本等。
检查CMakelist文件发现路径是include_directories("/usr/include/eigen3")
检查eigen3的路径,发现我的是/usr/local/include/eigen3/Eigen
,可以链接对应的文件,即进行以下命令:1
sudo ln -s /usr/local/include/eigen3/Eigen /usr/include/Eigen
sudo ln -s 源文件 目标文件
是一个常用的linux命令,功能是为源文件在目标文件的位置建立一个同步的链接,当二者建立联系后即可在源文件中访问目标文件。
链接有两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link). 建立硬链接时,链接文件和被链接文件必须位于同一个文件系统中,并且不能建立指向目录的硬链接。默认情况下,ln产生硬链接。如果给ln命令加上- s选项,则建立符号链接。
把所有配置放到文件g2o.cmake
,然后在CMakeLists
里添加1
2
3
4
5
6
7
8set(ALL_TARGET_LIBRARIES "")
include(g2o.cmake)
# 最后添加
target_link_libraries(${PROJECT_NAME}
${catkin_LIBRARIES}
${ALL_TARGET_LIBRARIES}
)
无需再添加eigen的头文件目录
我目前用的还是g2o旧版本,如果程序对应的是新版本,可能出现这样的报错1
error: ‘make_unique’ is not a member of ‘g2o’
g2o的CMakeLists.txt
的OpenMP部分1
2
3
4
5
6
7
8
9
10
11
12# Eigen parallelise itself, OPENMP is experimental. We experienced some slowdown with it
set(G2O_USE_OPENMP OFF CACHE BOOL "Build g2o with OpenMP support (EXPERIMENTAL)")
if(G2O_USE_OPENMP)
find_package(OpenMP)
if(OPENMP_FOUND)
MESSAGE(" ----------------- Using OpenMP ! ----------------- ")
set (G2O_OPENMP 1)
set(g2o_C_FLAGS "${g2o_C_FLAGS} ${OpenMP_C_FLAGS}")
set(g2o_CXX_FLAGS "${g2o_CXX_FLAGS} -DEIGEN_DONT_PARALLELIZE ${OpenMP_CXX_FLAGS}")
message(STATUS "Compiling with OpenMP support")
endif(OPENMP_FOUND)
endif(G2O_USE_OPENMP)
使用OpenMP编译安装g2o,即set(G2O_USE_OPENMP ON)
,但是随即编译我的程序报错:1
2
3/usr/bin/ld: CMakeFiles/ls_slam_g2o.dir/src/main.cpp.o: undefined reference to symbol 'omp_set_lock@@OMP_3.0'
//usr/lib/x86_64-linux-gnu/libgomp.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
搜索发现函数void lock() { omp_set_lock(&_lock); }
定义在 openmp_mutex.h
在CMakeLists.txt
中添加:1
2
3
4
5
6
7
8FIND_PACKAGE(OpenMP)
target_link_libraries(node
${catkin_LIBRARIES}
${CSPARSE_LIBRARY}
${G2O_LIBS}
OpenMP::OpenMP_CXX
)
CMakeLists.txt
如下1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45cmake_minimum_required(VERSION 2.8.3)
project(node)
add_compile_options(-std=c++11)
set(CMAKE_BUILD_TYPE "Realease")
LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
# find g2o lib
find_package(G2O REQUIRED)
IF(G2O_FOUND)
include_directories(${G2O_INCLUDE_DIR})
message(" -------- G2O lib is found-------- : "${G2O_INCLUDE_DIR})
ENDIF(G2O_FOUND)
find_package(Eigen3 REQUIRED)
find_package(CSparse REQUIRED)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
# g2o的库文件也可能在 /usr/local/lib
LINK_DIRECTORIES(/opt/ros/melodic/lib)
SET(G2O_LIBS g2o_cli g2o_ext_freeglut_minimal g2o_simulator g2o_solver_slam2d_linear g2o_types_icp g2o_types_slam2d g2o_core g2o_interface g2o_solver_csparse g2o_solver_structure_only g2o_types_sba g2o_types_slam3d g2o_csparse_extension g2o_opengl_helper g2o_solver_dense g2o_stuff g2o_types_sclam2d g2o_parser g2o_solver_pcg g2o_types_data g2o_types_sim3 cxsparse )
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES ls_slam
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
)
include_directories(
include
${catkin_INCLUDE_DIRS}
${EIGEN_INCLUDE_DIRS}
${CSPARSE_INCLUDE_DIR}
)
add_executable(node src/main.cpp)
target_link_libraries(node ${catkin_LIBRARIES} ${CSPARSE_LIBRARY} ${G2O_LIBS})
编译g2o结果报错fatal error: Eigen/Core: No such file or directory
compilation terminated.
FindEigen3.cmake 使用以下代码段:1
2
3
4
5
6find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library
PATHS
${CMAKE_INSTALL_PREFIX}/include
${KDE4_INCLUDE_DIR}
PATH_SUFFIXES eigen3 eigen
)
所以它默认CMAKE_INSTALL_PREFIX
在Linux主机上查找/usr/local
打开可视化工具g2o_viewer
和一个示例,如果能使用,就正常了
manhattan3500 dataset
1 | class OdometryHelperRos { |
这是base_local_planner
局部规划器里起辅助功能的一个类,主要作用就是从/odom
的topic中获取机器人的速度状态(包括线速度(x,y)和角速度z)
Costmap2DROS
类有一个成员base_local_planner::OdometryHelperRos odom_helper_;
,提供给用户获取当前机器人速度的接口
要使用,首先#include <base_local_planner/odometry_helper_ros.h>
,
OdometryHelperRos()在初始化的时候,新建了NodeHandle并设置关注的topic的名称odom_topic;
并在回调函数odomCallback()中, 取出速度信息
类TrajectoryPlannerROS
中定义: base_local_planner::OdometryHelperRos odom_helper_;
move_base的yaml中,对局部路径规划配置:teb_local_planner/TebLocalPlannerROS
MoveBase中有一段:1
2
3private_nh.param("base_local_planner", local_planner, std::string("base_local_planner/TrajectoryPlannerROS"));
tc_ = blp_loader_.createInstance(local_planner);
tc_->initialize(blp_loader_.getName(local_planner), &tf_, controller_costmap_ros_);
1 | tf::Stamped<tf::Pose> robot_vel_tf; |
1 | bool stopped(const nav_msgs::Odometry& base_odom, |
CostmapModel
类继承了WorldModel
,能够获取点、连线、多边形轮廓的cost,是局部规划器与costmap间的一个桥梁。初始化在move_base的构造函数,一般如下使用:1
2
3
4//costmap_2d::Costmap2DROS* planner_costmap_ros_
world_model_ = new base_local_planner::CostmapModel(*(planner_costmap_ros_ ->getCostmap()) );
std::vector<geometry_msgs::Point> footprint = planner_costmap_ros_->getRobotFootprint();
double footprint_cost = world_model_->footprintCost(x_i, y_i, theta_i, footprint);CostmapModel
类帮助local planner在Costmap上进行计算, footprintCost
, lineCost
, pointCost
三个类方法分别能通过Costmap计算出机器人足迹范围、两个cell连线、单个cell的代价,并将值返回给局部规划器。
原型1
2
3
4
5double base_local_planner::CostmapModel::footprintCost(const geometry_msgs::Point & position,
const std::vector< geometry_msgs::Point > & footprint,
double inscribed_radius,
double circumscribed_radius
)
Checks if any obstacles in the costmap lie inside a convex footprint that is rasterized into the grid.
参数:
返回值:
Positive if all the points lie outside the footprint, negative otherwise: -1 if footprint covers at least a lethal obstacle cell, or -2 if footprint covers at least a no-information cell, or -3 if footprint is [partially] outside of the map
footprintCost
函数,先调用WorldModel类里面的footprintCost函数,用x,y,th把footprint_spec变成世界坐标下坐标,还得到原点在世界坐标系坐标,内切半径,外切半径。根据这些,调用CostmapModel::footprintCost
函数,转换到costmap坐标系下,用光栅化算法得到栅格点,由栅格点的代价值得到足迹点线段的代价值,再得到整个足迹点集的代价值就可以了。
footprintCost
函数为提高效率,它不计算足迹所有栅格的代价,认为只要计算N条边涉及到的栅格,就可计算出最大代价。首先尝试获取线段的地图坐标,然后利用lineCost
函数计算线段代价值,并用footprint_cost
参数保存最大的线段代价值,最终如果正常,则返回的也是footprint_cost。只要有1个线段的代价值小于0,则直接返回-1。
注意函数和栅格计算的代价值不同:返回值中,最大值是253, 即INSCRIBED_INFLATED_OBSTACLE
,因为比253大的 LETHAL_OBSTACLE (254)
, NO_INFORMATION (255)
会分别返回-1、-2,表示不可行。
返回值:
没有撞障碍时,返回值是 0
函数原型对第一个参数position
的解释是:The position of the robot in world coordinates。实际是 局部代价地图的全局坐标系 ,一般是 map 或 odom
以tf::Stamped<tf::Pose>
为输入类型1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27tf::Stamped<tf::Pose> input_pose;
input_pose.setOrigin(tf::Vector3(x, y, 0) );
input_pose.setRotation(tf::createQuaternionFromYaw(theta) );
input_pose.frame_id_ = "map";
input_pose.stamp_ = ros::Time::now();
tf::Stamped<tf::Pose> global_pose_in_local_costmap;
auto tf = context_->tf_;
try
{
tf->transformPose( local_planner_costmap_ros_->getGlobalFrameID(),
input_pose,
global_pose_in_local_costmap);
}
catch (tf::TransformException& ex)
{
ROS_INFO("Cannot transform in inCollision !");
return false;
}
double temp_x = global_pose_in_local_costmap.getOrigin().getX();
double temp_y = global_pose_in_local_costmap.getOrigin().getY();
double footprint_cost = world_model_->footprintCost(
temp_x,
temp_y,
tf::getYaw(global_pose_in_local_costmap.getRotation() ),
local_costmap_ros->getRobotFootprint() );
1 | double CostmapModel::footprintCost(const geometry_msgs::Point& position, |
搜索发现有3个lineCost
函数, TrajectoryPlanner::lineCost
, VoxelGridModel::lineCost
, CostmapModel::lineCost
。经过运行,发现调用的还是CostmapModel::lineCost
对于线段的代价计算函数lineCost,其实是对线段中的所有像素点(通过bresenham
算法得到,不必深入研究)进行代价计算,如果像素点的代价为LETHAL_OBSTACLE
或者NO_INFORMATION
,则该点代价为-1。如果某个点的代价为-1,则该线段的代价也为-1,否则取所有点中最大的代价作为线段的代价1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28// return a positive cost for a legal line 或者负值
double CostmapModel::lineCost(int x0, int x1, int y0, int y1) const
{
double line_cost = 0.0;
double point_cost = -1.0;
for( LineIterator line( x0, y0, x1, y1 ); line.isValid(); line.advance() )
{
point_cost = pointCost( line.getX(), line.getY() );
if(point_cost < 0)
return point_cost;
if(line_cost < point_cost)
line_cost = point_cost;
}
return line_cost;
}
double CostmapModel::pointCost(int x, int y) const
{
unsigned char cost = costmap_.getCost(x, y);
//if the cell is in an obstacle the path is invalid
if(cost == NO_INFORMATION)
return -2;
if(cost == LETHAL_OBSTACLE)
return -1;
return cost;
}
参考:
CostmapModel::footprintCost
ObstacleCostFunction类,继承自Trajectory类
目前所用的机器人模型是圆形, 在通用代价地图里定义:robot_radius: 0.26
, 而不是footprint
参数. 但是注意TEB里用的是Point类型.
指定footprint
的数组元素时, 点如果太多, 可以换行。 机器人的轮廓可能是不规则的polygon, 但它的运动中心永远是(0, 0), 顺时针和逆时针规范都支持。 这应该是局部坐标系决定的
现在分析源码中对footprint
参数的处理, Costmap2DROS
的构造函数里有一句: setUnpaddedRobotFootprint(makeFootprintFromParams(private_nh));
, 先看括号里的函数
1 | std::vector<geometry_msgs::Point> makeFootprintFromParams(ros::NodeHandle& nh) |
函数检查通用代价地图的yaml里是否定义了footprint
和robot_radius
参数。 前者优先, 如果footprint
已经定义了, 就把它做轮廓, 不再处理robot_radius
. 我目前用的是robot_radius
, 所以再看makeFootprintFromRadius
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16std::vector<geometry_msgs::Point> makeFootprintFromRadius(double radius)
{
std::vector<geometry_msgs::Point> points;
// Loop over 16 angles around a circle making a point each time
int N = 16;
geometry_msgs::Point pt;
for (int i = 0; i < N; ++i)
{
double angle = i * 2 * M_PI / N;
pt.x = cos(angle) * radius;
pt.y = sin(angle) * radius;
points.push_back(pt);
}
return points;
}
这其实是把圆形处理成了一个正十六边形, 把顶点的坐标都放到容器里. 这个容器最终就是setUnpaddedRobotFootprint
的参数。 打开rviz放大, 会看到它是一个十六边形。
所以对于圆形轮廓机器人, 内接圆和外接圆的半径是不同的, 因为正十六边形的外接圆半径是内接圆半径的1.01959倍
1 | void Costmap2DROS::setUnpaddedRobotFootprint(const std::vector<geometry_msgs::Point>& points) |
setUnpaddedRobotFootprint
主要是对padded_footprint_
赋值, 和setFootprint
函数
footprint_padding_
只用于reconfigureCB
, 可以不看. padFootprint
就是处理padding的情况, 但我们的footprint_padding
参数为0, 所以也不看了.
当话题上收到footprint时, 回调函数会将接收到的footprint根据参数footprint_padding_
的值进行膨胀, 得到膨胀后的 padded_footprint_
, 传递给各级地图。
调用了这个函数的还有setUnpaddedRobotFootprintRadius
回调函数 和 setUnpaddedRobotFootprintPolygon
回调函数, 前者对应footprint_radius
的参数值做的话题, 后者对应footprint_topic
的参数值做的话题
1 | void LayeredCostmap::setFootprint(const std::vector<geometry_msgs::Point>& footprint_spec) |
calculateMinAndMaxDistances
很重要, 它可求得内外圆的半径。footprint
无论是局部代价地图还是全局代价地图得到,两个半径的计算结果都一样。 对于固定的机器人,干脆将两个半径设置为固定值,省得计算。
这里的plugin就是全局和局部代价地图里定义的plugins
成员, 别忘了MoveBase里有两个代价地图, 对plugin的添加在Costmap2DROS
的构造函数里.
每一层都调用了onFootprintChanged
, 但是只有膨胀层覆盖了基类函数, 也就是InflationLayer::onFootprintChanged()
, 这个在另一篇文章分析. 其它层没有覆盖, 还是调用的Layer类的空函数virtual void onFootprintChanged() {}
机器人轮廓之所以能在导航时也行走, 原因在updateMap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24void Costmap2DROS::updateMap()
{
if (!stop_updates_)
{
// get pose in global frame of costmap
tf::Stamped < tf::Pose > pose;
if (getRobotPose (pose))
{
double x = pose.getOrigin().x(),
y = pose.getOrigin().y(),
yaw = tf::getYaw(pose.getRotation());
layered_costmap_->updateMap(x, y, yaw);
geometry_msgs::PolygonStamped footprint;
footprint.header.frame_id = global_frame_;
footprint.header.stamp = ros::Time::now();
transformFootprint(x, y, yaw, padded_footprint_, footprint);
footprint_pub_.publish(footprint);
initialized_ = true;
}
}
}
逻辑不复杂, padded_footprint_
就是上面的十六变形的Point容器, transformFootprint
是更新机器人行走时的轮廓, 原理和里程计解算类似.
footprint_pub_
发布的话题就是/move_base/local_costmap/footprint
footprint
, 类型geometry_msgs/Polygon
, 由move_base
订阅, 但没有发布者。
/move_base/global_costmap/footprint
和/move_base/local_costmap/footprint
话题是机器人轮廓, 可以直接获得。
Padding机制为机器人和障碍物之间提供了额外的距离, 打开rqt_reconfigure
调整全局代价地图的参数footprint_padding
, 膨胀层和障碍层会变化, 效果和调整参数inflation_radius
和cost_scaling_factor
相同。
调整局部代价地图的footprint_padding
, 机器人轮廓会成比例放大和缩小。 如果确实需要改变这个参数, 两个代价地图的都要改变。
这个和padding是不同的机制, 用话题的方式在线更改机器人的轮廓, 适用于机器人更换货架而导致轮廓改变的情况。
原因在源码中的这一段, footprint_radius
是自己加的, 注意全局和局部代价地图的相应话题都只发布一次
所以可以看到move_base
(实际在costmap_2d
) 订阅/footprint_radius
和 footprint
,但没有发布者
1 | private_nh.param(topic_param, topic, std::string("footprint")); |
两个回调函数都是调用函数setUnpaddedRobotFootprint
, 对于radius
的情况, 也就是圆形底盘, 还是把圆按正十六边形处理。
可以用命令测试,现实中由调度端实现:1
2
3
4
5rostopic pub -1 /move_base/local_costmap/footprint_radius std_msgs/Float32 '0.34'
rostopic pub -1 /move_base/global_costmap/footprint_radius std_msgs/Float32 '0.34'
rostopic pub -1 /move_base/local_costmap/polygon_footprint geometry_msgs/Polygon '[ [-0.3, 0.3], [0.3, 0.3], [0.3, -0.3], [-0.3, -0.3] ]'
rostopic pub -1 /move_base/global_costmap/polygon_footprint geometry_msgs/Polygon '[ [-0.3, 0.3], [0.3, 0.3], [0.3, -0.3], [-0.3, -0.3] ]'
footprint
是多边形, 每增加一条边, 就会增加TEB计算时间。设置和通用代价地图里相同1
2type: "polygon"
vertices: [[-0.26,0], [-0.240209, 0.099498], [-0.183848,0.240209], [0,0.26], [0.099498,0.240209], [0.5662,0.240209], [0.5662,-0.240209], [0.099498,-0.240209], [0,-0.26], [-0.183848,-0.240209], [-0.240209, -0.099498] ]
计算footprint
的内切圆半径和外切圆半径, 用到点到线段的距离, 计算方法是两个向量的点积等于一个向量在另一个向量的投影乘以另一个向量的模。看函数distanceToLine
点到线段的三种情况如下:
1 | /* 作用:计算点到线段的距离 |