OMPL学习笔记1

红色是ompl库中RRT搜索路径,蓝色是简化后的路径。

RigidBodyPlanning.cpp

#include <ompl/base/SpaceInformation.h>
#include <ompl/base/spaces/SE3StateSpace.h>
#include <ompl/geometric/planners/rrt/RRT.h>
#include <ompl/geometric/SimpleSetup.h>

#include <ompl/config.h>
#include <iostream>
#include <fstream>
#include <ostream>
#include "boost/bind.hpp"

using namespace std;

namespace ob = ompl::base;
namespace og = ompl::geometric;

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>(0);

  // extract the second component of the state and cast it to what we expect
  const ob::SO3StateSpace::StateType *rot = se3state->as<ob::SO3StateSpace::StateType>(1);

  // check validity of state defined by pos & rot

  // return a value that is always true but uses the two variables we define, so we avoid compiler
  // warnings
  return (const void *)rot != (const void *)pos;
}

void planWithSimpleSetup(void)
{
  // construct the state space we are planning in
  ob::StateSpacePtr space(new ob::SE3StateSpace());

  // set the bounds for the R^3 part of SE(3)
  ob::RealVectorBounds bounds(3);
  bounds.setLow(-1);
  bounds.setHigh(1);

  space->as<ob::SE3StateSpace>()->setBounds(bounds);

  // define a simple setup class
  og::SimpleSetup ss(space);

  // set state validity checking for this space
  ss.setStateValidityChecker(boost::bind(&isStateValid, _1));

  // create a random start state
  ob::ScopedState<> start(space);
  start.random();

  // create a random goal state
  ob::ScopedState<> goal(space);
  goal.random();

  // set the start and goal states
  ss.setStartAndGoalStates(start, goal);

  ob::PlannerPtr planner(new og::RRT(ss.getSpaceInformation()));
  ss.setPlanner(planner);

  // attempt to solve the problem within one second of planning time
  ob::PlannerStatus solved = ss.solve(1.0);

  if (solved) {
    std::cout << "Found solution:" << std::endl;
    // print the path to screen

    std::ofstream ofs0("../plot/path0.dat");
    ss.getSolutionPath().printAsMatrix(ofs0);

    ss.simplifySolution();
    ss.getSolutionPath().print(std::cout);

    std::ofstream ofs("../plot/path.dat");
    ss.getSolutionPath().printAsMatrix(ofs);
  } else
    std::cout << "No solution found" << std::endl;
}

int main(int, char **)
{
  std::cout << "OMPL version: " << OMPL_VERSION << std::endl;
  planWithSimpleSetup();

  return 0;
}

17-33行是检测当前状态是否可行。ob::SE3StateSpace::StateType *se3state是3维情况,ob::SE2StateSpace::StateType *se2state则表示二维。38-43设置边界范围。54-59为设置初始和目标随机位置。64-65设置用什么算法解决。可用算法参考可用算法。68期望解决的时间。

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

ADD_DEFINITIONS(
  -Wall
  -g
  -O2
  -std=c++11
)

project(RigidBodyPlanning)

find_package(OMPL REQUIRED)

include_directories(${OMPL_INCLUDE_DIRS})
link_directories(${OMPL_LIBRARY_DIRS})
add_definitions(${OMPL_DEFINITIONS})

FIND_PACKAGE(Boost COMPONENTS system REQUIRED)

add_executable (RigidBodyPlanning RigidBodyPlanning.cpp)
target_link_libraries (RigidBodyPlanning ${Boost_LIBRARIES} ${OMPL_LIBRARIES})

 

 

 

python画图

from mpl_toolkits.mplot3d import Axes3D
import numpy
import matplotlib.pyplot as plt
data = numpy.loadtxt('path.dat')
data1 = numpy.loadtxt('path0.dat')
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(data[:,0],data[:,1],data[:,2],'.-')
plt.hold('on')
plt.grid('on')
ax.plot(data1[:,0],data1[:,1],data1[:,2],'r-')
plt.show()

 

<think>好的,用户想了解如何在OMPL的tests中的2denvs.cpp中使用2D地图基准测试RRT算法。首先,我需要回忆一下OMPL的结构和相关测试用例。OMPL(Open Motion Planning Library)是一个用于运动规划的开源库,提供了多种规划算法,包括RRT、PRM等。 首先,用户提到的2denvs.cpp应该位于OMPL的测试目录中。这个文件可能定义了一些2D环境,用于测试不同的规划算法。我需要确定这个文件的作用,是否提供了不同的二维地图场景,比如障碍物布局,供算法进行测试。 接下来,用户想测试RRT算法。RRT(快速探索随机树)是一种基于采样的运动规划算法,适用于高维空间。在2D环境中,RRT会随机采样点,并扩展树结构以探索空间,避开障碍物,最终找到起点到目标点的路径。 要使用2denvs.cpp中的2D地图测试RRT,可能需要以下步骤: 1. **环境设置**:确认2denvs.cpp中已经定义了不同的2D地图场景,比如通过障碍物的坐标来划分可行区域和障碍区域。可能需要查看该文件中的具体代码,看看如何加载或生成这些地图。 2. **算法配置**:在OMPL中,选择RRT算法作为规划器。需要创建一个状态空间(比如SE2,表示二维空间中的位置和方向),然后设置规划器的类型为RRT。 3. **问题定义**:设置起点和终点的状态,确保它们位于可行区域,即不被障碍物阻挡。这可能需要根据2D地图的具体布局来确定合适的起点和终点。 4. **路径规划与验证**:运行规划器,生成路径,并检查路径的有效性,确保路径避开了所有障碍物。同时,可能需要调整RRT的参数,比如步长、目标偏置概率等,以优化规划效果。 5. **结果分析**:评估RRT在特定2D地图中的性能,比如规划时间、路径长度、节点数量等指标。这可能涉及到多次运行测试,统计平均结果。 可能存在的一些问题包括环境是否正确加载、碰撞检测的实现是否准确,以及参数设置是否合理。例如,如果步长太大,可能会导致路径碰撞;如果目标偏置太低,可能影响收敛速度。 另外,需要查看OMPL的文档或测试用例的具体实现,确认如何调用2denvs.cpp中的环境。可能需要在测试代码中指定使用哪个地图,并关联到RRT规划器上。可能需要编写或修改测试代码,设置规划场景,然后运行测试。 总结步骤大致如下: - 确认2denvs.cpp的环境结构 - 配置RRT规划器及参数 - 设置起点和终点 - 运行测试并收集数据 - 分析结果,调整参数优化 可能需要参考OMPL的官方示例,如RigidBodyPlanning等,看看如何整合环境和规划器。同时,确保编译和运行环境正确,能够执行测试用例。</think>在OMPL中使用`tests/2denvs.cpp`中的2D地图基准测试RRT算法,需遵循以下步骤: --- ### 一、理解文件结构及功能 1. **`2denvs.cpp`作用** 该文件定义了多个预置的2D栅格地图环境(如迷宫、障碍物布局等),通过`ompl::app::RigidBodyGeometry`类实现几何碰撞检测,支持算法在复杂环境中的路径规划测试。 2. **RRT算法集成** OMPL中RRT的实现类为`ompl::geometric::RRT`,需将其与2D环境的状态空间(如`SE2`)绑定,并设置碰撞检测函数。 --- ### 二、关键代码实现步骤 #### 1. 初始化2D环境 ```cpp #include <omplapp/config.h> #include <omplapp/apps/SE2RigidBodyPlanning.h> #include <ompl/geometric/planners/rrt/RRT.h> auto space = std::make_shared<ompl::base::SE2StateSpace>(); // 二维状态空间 ompl::base::RealVectorBounds bounds(2); bounds.setLow(0); // 地图坐标下限 bounds.setHigh(10); // 地图坐标上限(假设地图尺寸10x10) space->setBounds(bounds); auto ss = std::make_shared<ompl::geometric::SimpleSetup>(space); ss->setStateValidityChecker( // 设置碰撞检测 [&](const ompl::base::State* state) { const auto* se2 = state->as<ompl::base::SE2StateSpace::StateType>(); double x = se2->getX(), y = se2->getY(); return /* 根据2denvs.cpp中的地图判断(x,y)是否可行 */; }); ``` #### 2. 加载2D地图障碍物 需从`2denvs.cpp`中提取地图数据(如多边形顶点),例如: ```cpp // 示例:定义一个矩形障碍物 std::vector<Polygon> obstacles; Polygon rect; rect.vertices = {{2,2}, {2,5}, {5,5}, {5,2}}; obstacles.push_back(rect); ``` #### 3. 配置RRT规划器 ```cpp auto planner = std::make_shared<ompl::geometric::RRT>(ss->getSpaceInformation()); planner->setRange(0.5); // 设置RRT步长 ss->setPlanner(planner); ``` #### 4. 设置起点与终点 ```cpp ompl::base::ScopedState<> start(space), goal(space); start[0] = 1.0; start[1] = 1.0; // 起点坐标 goal[0] = 9.0; goal[1] = 9.0; // 终点坐标 ss->setStartAndGoalStates(start, goal); ``` #### 5. 运行规划并验证 ```cpp ompl::base::PlannerStatus solved = ss->solve(10.0); // 超时10秒 if (solved) { ss->simplifySolution(); // 路径简化 ompl::geometric::PathGeometric path = ss->getSolutionPath(); path.print(std::cout); // 输出路径点 } ``` --- ### 三、参数调优与性能评估 1. **关键参数** - **`setRange()`**: 步长影响探索速度与路径平滑度 - **`setGoalBias()`**: 目标偏向概率(默认5%) - **`setIntermediateStates()`**: 是否记录中间状态 2. **性能指标** ```cpp ompl::tools::Benchmark b(*ss, "RRT_test"); b.addExperimentParameter("max_step", "REAL", "0.5"); // 记录参数 b.run(10, 1.0); // 运行10次,每次1秒 b.saveResultsToFile("rrt_2dbenchmark.log"); ``` --- ### 四、常见问题及解决 1. **碰撞检测失败** 检查`StateValidityChecker`中的几何判断逻辑,确保与`2denvs.cpp`中障碍物定义一致。 2. **规划超时** - 增大`setRange()`值加速探索 - 调整`setGoalBias()`至10%~20% 3. **路径不光滑** 调用`ss->simplifySolution()`或使用RRT*等优化版本。 --- ### 五、扩展:与其他算法对比 在`2denvs.cpp`中可替换规划器,例如PRM: ```cpp #include <ompl/geometric/planners/prm/PRM.h> auto planner = std::make_shared<ompl::geometric::PRM>(ss->getSpaceInformation()); ``` 通过修改环境与参数,可系统评估RRT在狭窄通道、高障碍密度等场景下的表现。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值