热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

octomap中3drrt路径规划

在octomap中制定起止点,目标点,使用rrt规划一条路径出来,没有运动学,动力学的限制,只要能避开障碍物。效果如下(绿线是规划的路线,红线是B样条优化的曲线):

在octomap中制定起止点,目标点,使用rrt规划一条路径出来,没有运动学,动力学的限制,只要能避开障碍物。

效果如下(绿线是规划的路线,红线是B样条优化的曲线):

#include ros/ros.h
#include octomap_msgs/Octomap.h
#include octomap_msgs/conversions.h
#include octomap_ros/conversions.h
#include octomap/octomap.h
#include message_filters/subscriber.h
#include visualization_msgs/Marker.h
#include trajectory_msgs/MultiDOFJointTrajectory.h
#include nav_msgs/Odometry.h
#include geometry_msgs/Pose.h
#include nav_msgs/Path.h
#include geometry_msgs/PoseStamped.h
#include ompl/base/spaces/SE3StateSpace.h
#include ompl/base/spaces/SE3StateSpace.h
#include ompl/base/OptimizationObjective.h
#include ompl/base/objectives/PathLengthOptimizationObjective.h
// #include ompl/geometric/planners/rrt/RRTstar.h
#include ompl/geometric/planners/rrt/InformedRRTstar.h
#include ompl/geometric/SimpleSetup.h
#include ompl/config.h
#include iostream
#include fcl/config.h
#include fcl/octree.h
#include fcl/traversal/traversal_node_octree.h
#include fcl/collision.h
#include fcl/broadphase/broadphase.h
#include fcl/math/transform.h
namespace ob = ompl::base;
namespace og = ompl::geometric;
// Declear some global variables
//ROS publishers
ros::Publisher vis_pub;
ros::Publisher traj_pub;
class planner {
public:
void setStart(double x, double y, double z)
{
ob::ScopedState ob::SE3StateSpace start(space);
start- setXYZ(x,y,z);
start- as ob::SO3StateSpace::StateType (1)- setIdentity();
pdef- clearStartStates();
pdef- addStartState(start);
}
void setGoal(double x, double y, double z)
{
ob::ScopedState ob::SE3StateSpace goal(space);
goal- setXYZ(x,y,z);
goal- as ob::SO3StateSpace::StateType (1)- setIdentity();
pdef- clearGoal();
pdef- setGoalState(goal);
std::cout goal set to: x y z std::endl;
}
void updateMap(std::shared_ptr fcl::CollisionGeometry map)
{
tree_obj = map;
}
// Constructor
planner(void)
{
//四旋翼的障碍物几何形状
Quadcopter = std::shared_ptr fcl::CollisionGeometry (new fcl::Box(0.8, 0.8, 0.3));
//分辨率参数设置
fcl::OcTree* tree = new fcl::OcTree(std::shared_ptr const octomap::OcTree (new octomap::OcTree(0.15)));
tree_obj = std::shared_ptr fcl::CollisionGeometry (tree);
//解的状态空间
space = ob::StateSpacePtr(new ob::SE3StateSpace());
// create a start state
ob::ScopedState ob::SE3StateSpace start(space);
// create a goal state
ob::ScopedState ob::SE3StateSpace goal(space);
// set the bounds for the R^3 part of SE(3)
// 搜索的三维范围设置
ob::RealVectorBounds bounds(3);
bounds.setLow(0,-5);
bounds.setHigh(0,5);
bounds.setLow(1,-5);
bounds.setHigh(1,5);
bounds.setLow(2,0);
bounds.setHigh(2,3);
space- as ob::SE3StateSpace ()- setBounds(bounds);
// construct an instance of space information from this state space
si = ob::SpaceInformationPtr(new ob::SpaceInformation(space));
start- setXYZ(0,0,0);
start- as ob::SO3StateSpace::StateType (1)- setIdentity();
// start.random();
goal- setXYZ(0,0,0);
goal- as ob::SO3StateSpace::StateType (1)- setIdentity();
// goal.random();
// set state validity checking for this space
si- setStateValidityChecker(std::bind( planner::isStateValid, this, std::placeholders::_1 ));
// create a problem instance
pdef = ob::ProblemDefinitionPtr(new ob::ProblemDefinition(si));
// set the start and goal states
pdef- setStartAndGoalStates(start, goal);
// set Optimizattion objective
pdef- setOptimizationObjective(planner::getPathLengthObjWithCostToGo(si));
std::cout Initialized: std::endl;
}
// Destructor
~planner()
{
}
void replan(void)
std::cout Total Points: path_smooth- getStateCount () std::endl;
if(path_smooth- getStateCount () = 2)
plan();
else
{
for (std::size_t idx = 0; idx path_smooth- getStateCount (); idx++)
{
if(!replan_flag)
replan_flag = !isStateValid(path_smooth- getState(idx));
else
break;
}
if(replan_flag)
plan();
else
std::cout Replanning not required std::endl;
}
void plan(void)
// create a planner for the defined space
og::InformedRRTstar* rrt = new og::InformedRRTstar(si);
//设置rrt的参数range
rrt- setRange(0.2);
ob::PlannerPtr plan(rrt);
// set the problem we are trying to solve for the planner
plan- setProblemDefinition(pdef);
// perform setup steps for the planner
plan- setup();
// print the settings for this space
si- printSettings(std::cout);
std::cout problem setting\n
// print the problem settings
pdef- print(std::cout);
// attempt to solve the problem within one second of planning time
ob::PlannerStatus solved = plan- solve(1);
if (solved)
{
// get the goal representation from the problem definition (not the same as the goal state)
// and inquire about the found path
std::cout Found solution: std::endl;
ob::PathPtr path = pdef- getSolutionPath();
og::PathGeometric* pth = pdef- getSolutionPath()- as og::PathGeometric
pth- printAsMatrix(std::cout);
// print the path to screen
// path- print(std::cout);
nav_msgs::Path msg;
msg.header.stamp = ros::Time::now();
msg.header.frame_id = map
for (std::size_t path_idx = 0; path_idx pth- getStateCount (); path_idx++)
{
const ob::SE3StateSpace::StateType *se3state = pth- getState(path_idx)- as ob::SE3StateSpace::StateType
// extract the first component of the state and cast it to what we expect
const ob::RealVectorStateSpace::StateType *pos = se3state- as ob::RealVectorStateSpace::StateType
// extract the second component of the state and cast it to what we expect
const ob::SO3StateSpace::StateType *rot = se3state- as ob::SO3StateSpace::StateType
geometry_msgs::PoseStamped pose;
// pose.header.frame_id = /world
pose.pose.position.x = pos- values[0];
pose.pose.position.y = pos- values[1];
pose.pose.position.z = pos- values[2];
pose.pose.orientation.x = rot-
pose.pose.orientation.y = rot-
pose.pose.orientation.z = rot-
pose.pose.orientation.w = rot-
msg.poses.push_back(pose);
}
traj_pub.publish(msg);
//Path smoothing using bspline
//B样条曲线优化
og::PathSimplifier* pathBSpline = new og::PathSimplifier(si);
path_smooth = new og::PathGeometric(dynamic_cast const og::PathGeometric (*pdef- getSolutionPath()));
pathBSpline- smoothBSpline(*path_smooth,3);
// std::cout Smoothed Path std::endl;
// path_smooth.print(std::cout);
//Publish path as markers
nav_msgs::Path smooth_msg;
smooth_msg.header.stamp = ros::Time::now();
smooth_msg.header.frame_id = map
for (std::size_t idx = 0; idx path_smooth- getStateCount (); idx++)
{
// cast the abstract state type to the type we expect
const ob::SE3StateSpace::StateType *se3state = path_smooth- getState(idx)- as ob::SE3StateSpace::StateType
// extract the first component of the state and cast it to what we expect
const ob::RealVectorStateSpace::StateType *pos = se3state- as ob::RealVectorStateSpace::StateType
// extract the second component of the state and cast it to what we expect
const ob::SO3StateSpace::StateType *rot = se3state- as ob::SO3StateSpace::StateType
geometry_msgs::PoseStamped point;
// pose.header.frame_id = /world
point.pose.position.x = pos- values[0];
point.pose.position.y = pos- values[1];
point.pose.position.z = pos- values[2];
point.pose.orientation.x = rot-
point.pose.orientation.y = rot-
point.pose.orientation.z = rot-
point.pose.orientation.w = rot-
smooth_msg.poses.push_back(point);
std::cout Published marker: idx std::endl;
vis_pub.publish(smooth_msg);
// ros::Duration(0.1).sleep();
// Clear memory
pdef- clearSolutionPaths();
replan_flag = false;
}
else
std::cout No solution found std::endl;
}
private:
// construct the state space we are planning in
ob::StateSpacePtr space;
// construct an instance of space information from this state space
ob::SpaceInformationPtr si;
// create a problem instance
ob::ProblemDefinitionPtr pdef;
og::PathGeometric* path_smooth;
bool replan_flag = false;
std::shared_ptr fcl::CollisionGeometry Quadcopter;
std::shared_ptr fcl::CollisionGeometry tree_obj;
bool isStateValid(const ob::State *state)
{
// cast the abstract state type to the type we expect
const ob::SE3StateSpace::StateType *se3state = state- as ob::SE3StateSpace::StateType
// extract the first component of the state and cast it to what we expect
const ob::RealVectorStateSpace::StateType *pos = se3state- as ob::RealVectorStateSpace::StateType
// extract the second component of the state and cast it to what we expect
const ob::SO3StateSpace::StateType *rot = se3state- as ob::SO3StateSpace::StateType
fcl::CollisionObject treeObj((tree_obj));
fcl::CollisionObject aircraftObject(Quadcopter);
// check validity of state defined by pos rot
fcl::Vec3f translation(pos- values[0],pos- values[1],pos- values[2]);
fcl::Quaternion3f rotation(rot- w, rot- x, rot- y, rot-
aircraftObject.setTransform(rotation, translation);
fcl::CollisionRequest requestType(1,false,1,false);
fcl::CollisionResult collisionResult;
fcl::collide( aircraftObject, treeObj, requestType, collisionResult);
return(!collisionResult.isCollision());
// Returns a structure representing the optimization objective to use
// for optimal motion planning. This method returns an objective which
// attempts to minimize the length in configuration space of computed
// paths.
ob::OptimizationObjectivePtr getThresholdPathLengthObj(const ob::SpaceInformationPtr si)
{
ob::OptimizationObjectivePtr obj(new ob::PathLengthOptimizationObjective(si));
// obj- setCostThreshold(ob::Cost(1.51));
return obj;
ob::OptimizationObjectivePtr getPathLengthObjWithCostToGo(const ob::SpaceInformationPtr si)
{
ob::OptimizationObjectivePtr obj(new ob::PathLengthOptimizationObjective(si));
obj- setCostToGoHeuristic( ob::goalRegionCostToGo);
return obj;
void octomapCallback(const octomap_msgs::Octomap::ConstPtr msg, planner* planner_ptr)
{
//loading octree from binary
// const std::string filename = /home/xiaopeng/dense.bt
// octomap::OcTree temp_tree(0.1);
// temp_tree.readBinary(filename);
// fcl::OcTree* tree = new fcl::OcTree(std::shared_ptr const octomap::OcTree ( temp_tree));
// convert octree to collision object
octomap::OcTree* tree_oct = dynamic_cast octomap::OcTree* (octomap_msgs::msgToMap(*msg));
fcl::OcTree* tree = new fcl::OcTree(std::shared_ptr const octomap::OcTree (tree_oct));
// Update the octree used for collision checking
planner_ptr- updateMap(std::shared_ptr fcl::CollisionGeometry (tree));
planner_ptr- replan();
void odomCb(const nav_msgs::Odometry::ConstPtr msg, planner* planner_ptr)
{
planner_ptr- setStart(msg- pose.pose.position.x, msg- pose.pose.position.y, msg- pose.pose.position.z);
void startCb(const geometry_msgs::PointStamped::ConstPtr msg, planner* planner_ptr)
{
planner_ptr- setStart(msg- point.x, msg- point.y, msg- point.z);
void goalCb(const geometry_msgs::PointStamped::ConstPtr msg, planner* planner_ptr)
{
planner_ptr- setGoal(msg- point.x, msg- point.y, msg- point.z);
planner_ptr- plan();
int main(int argc, char **argv)
{
ros::init(argc, argv, octomap_planner
ros::NodeHandle n;
planner planner_object;
ros::Subscriber octree_sub = n.subscribe octomap_msgs::Octomap ( /octomap_binary , 1, boost::bind( octomapCallback, _1, planner_object));
// ros::Subscriber odom_sub = n.subscribe nav_msgs::Odometry ( /rovio/odometry , 1, boost::bind( odomCb, _1, planner_object));
ros::Subscriber goal_sub = n.subscribe geometry_msgs::PointStamped ( /goal/clicked_point , 1, boost::bind( goalCb, _1, planner_object));
ros::Subscriber start_sub = n.subscribe geometry_msgs::PointStamped ( /start/clicked_point , 1, boost::bind( startCb, _1, planner_object));
// vis_pub = n.advertise visualization_msgs::Marker ( visualization_marker , 0 );
vis_pub = n.advertise nav_msgs::Path ( visualization_marker , 0 );
// traj_pub = n.advertise trajectory_msgs::MultiDOFJointTrajectory ( waypoints ,1);
traj_pub = n.advertise nav_msgs::Path ( waypoints ,1);
std::cout OMPL version: OMPL_VERSION std::endl;
ros::spin();
return 0;
}


推荐阅读
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 本文介绍了如何清除Eclipse中SVN用户的设置。首先需要查看使用的SVN接口,然后根据接口类型找到相应的目录并删除相关文件。最后使用SVN更新或提交来应用更改。 ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • 解决github访问慢的问题的方法集锦
    本文总结了国内用户在访问github网站时可能遇到的加载慢的问题,并提供了解决方法,其中包括修改hosts文件来加速访问。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • React项目中运用React技巧解决实际问题的总结
    本文总结了在React项目中如何运用React技巧解决一些实际问题,包括取消请求和页面卸载的关联,利用useEffect和AbortController等技术实现请求的取消。文章中的代码是简化后的例子,但思想是相通的。 ... [详细]
  • Linux的uucico命令使用方法及工作模式介绍
    本文介绍了Linux的uucico命令的使用方法和工作模式,包括主动模式和附属模式。uucico是用来处理uucp或uux送到队列的文件传输工具,具有操作简单快捷、实用性强的特点。文章还介绍了uucico命令的参数及其说明,包括-c或--quiet、-C或--ifwork、-D或--nodetach、-e或--loop、-f或--force、-i或--stdin、-I--config、-l或--prompt等。通过本文的学习,读者可以更好地掌握Linux的uucico命令的使用方法。 ... [详细]
  • HTML5网页模板怎么加百度统计?
    本文介绍了如何在HTML5网页模板中加入百度统计,并对模板文件、css样式表、js插件库等内容进行了说明。同时还解答了关于HTML5网页模板的使用方法、表单提交、域名和空间的问题,并介绍了如何使用Visual Studio 2010创建HTML5模板。此外,还提到了使用Jquery编写美好的HTML5前端框架模板的方法,以及制作企业HTML5网站模板和支持HTML5的CMS。 ... [详细]
author-avatar
偏偏喜欢你_Jerry_207
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有