Loading...
Searching...
No Matches
VAMPPlanning.cpp
1// demos/vamp/VAMPPlanning.cpp
2#include <iostream>
3#include <vector>
4#include <array>
5#include <chrono>
6
7#include <boost/program_options.hpp>
8
9// OMPL headers
10#include <ompl/base/PlannerTerminationCondition.h>
11#include <ompl/base/ProblemDefinition.h>
12#include <ompl/base/SpaceInformation.h>
13#include <ompl/base/spaces/RealVectorStateSpace.h>
14#include <ompl/geometric/SimpleSetup.h>
15
16// Planners
17#include <ompl/geometric/planners/rrt/RRTConnect.h>
18#include <ompl/geometric/planners/rrt/RRT.h>
19#include <ompl/geometric/planners/kpiece/KPIECE1.h>
20#include <ompl/geometric/planners/kpiece/LBKPIECE1.h>
21
22// Benchmark
23#include <ompl/tools/benchmark/Benchmark.h>
24
25// VAMP integration headers
26#include <ompl/vamp/Utils.h>
27#include <ompl/vamp/VampStateValidityChecker.h>
28#include <ompl/vamp/VampMotionValidator.h>
29#include <ompl/vamp/VampStateSpace.h>
30
31#include <vamp/collision/factory.hh>
32#include <vamp/robots/panda.hh>
33
34namespace ob = ompl::base;
35namespace og = ompl::geometric;
36namespace ot = ompl::tools;
37namespace po = boost::program_options;
38
39using Robot = vamp::robots::Panda;
40using Environment = vamp::collision::Environment<vamp::FloatVector<vamp::FloatVectorWidth>>;
41
42// Sphere obstacles
43std::vector<std::array<float, 3>> obstacles = {
44 {0.55, 0, 0.25}, {0.35, 0.35, 0.25}, {0, 0.55, 0.25}, {-0.55, 0, 0.25}, {-0.35, -0.35, 0.25},
45 {0, -0.55, 0.25}, {0.35, -0.35, 0.25}, {0.35, 0.35, 0.8}, {0, 0.55, 0.8}, {-0.35, 0.35, 0.8},
46 {-0.55, 0, 0.8}, {-0.35, -0.35, 0.8}, {0, -0.55, 0.8}, {0.35, -0.35, 0.8},
47};
48
49void planOnce()
50{
51 // Build a simple sphere obstacle environment
52 vamp::collision::Environment<float> env_float;
53
54 constexpr float radius = 0.2f;
55 for (const auto &obs : obstacles)
56 {
57 env_float.spheres.emplace_back(vamp::collision::factory::sphere::array(obs, radius));
58 }
59 env_float.sort();
60
61 // Convert to vectorized environment for SIMD collision checking
62 Environment env(env_float);
63
64 // Create state space
65 auto space = std::make_shared<ompl::vamp::VampStateSpace<Robot>>();
66
67 // Prints state space bounds (joint limits)
68 std::cout << "Robot bounds:" << std::endl;
69 for (std::size_t i = 0; i < Robot::dimension; ++i)
70 {
71 std::cout << i << ": " << space->getBounds().low[i] << " to " << space->getBounds().high[i] << std::endl;
72 }
73
74 // Create simple setup
75 og::SimpleSetup ss(space);
76
77 auto si = ss.getSpaceInformation();
78
79 // Sets state validity checker and motion validator, using SIMD-accelerated methods from VAMP
80 si->setStateValidityChecker(std::make_shared<ompl::vamp::VampStateValidityChecker<Robot>>(si, env));
81 si->setMotionValidator(std::make_shared<ompl::vamp::VampMotionValidator<Robot>>(si, env));
82
83 // Define start and goal configurations (Panda 7 DOF)
84 ob::ScopedState<> start(space);
85 ob::ScopedState<> goal(space);
86
87 std::array<double, 7> start_config = {0., -0.785, 0., -2.356, 0., 1.571, 0.785};
88 std::array<double, 7> goal_config = {2.35, 1., 0., -0.8, 0., 2.5, 0.785};
89
90 for (std::size_t i = 0; i < Robot::dimension; ++i)
91 {
92 start[i] = start_config[i];
93 goal[i] = goal_config[i];
94 }
95
96 // Create RRTConnect planner (optional, a default will be automatically chosen otherwise)
97 auto planner = std::make_shared<og::RRTConnect>(si);
98 ss.setPlanner(planner);
99
100 // Setup problem definition
101 ss.setStartAndGoalStates(start, goal);
102
103 // Solve with 5 second timeout
104 auto start_time = std::chrono::steady_clock::now();
106 auto end_time = std::chrono::steady_clock::now();
107
108 auto duration_ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count();
109 auto duration_ms = duration_ns / 1e6;
110 std::cout << "Planning time: " << duration_ms << " ms" << std::endl;
111
113 {
114 std::cout << "Found solution!" << std::endl;
115 auto path = ss.getSolutionPath();
116 std::cout << "Path has " << path.getStateCount() << " states" << std::endl;
117 path.print(std::cout);
118 }
119 else
120 {
121 std::cout << "No solution found" << std::endl;
122 }
123}
124
125void planBenchmark(int runCount = 100)
126{
127 // Build a simple sphere obstacle environment
128 vamp::collision::Environment<float> env_float;
129
130 constexpr float radius = 0.2f;
131 for (const auto &obs : obstacles)
132 {
133 env_float.spheres.emplace_back(vamp::collision::factory::sphere::array(obs, radius));
134 }
135 env_float.sort();
136
137 // Convert to vectorized environment for SIMD collision checking
138 Environment env(env_float);
139
140 // create state space
141 auto space = std::make_shared<ompl::vamp::VampStateSpace<Robot>>();
142
143 std::cout << "Robot bounds:" << std::endl;
144 for (std::size_t i = 0; i < Robot::dimension; ++i)
145 {
146 std::cout << i << ": " << space->getBounds().low[i] << " to " << space->getBounds().high[i] << std::endl;
147 }
148
149 // Create simple setup
150 og::SimpleSetup ss(space);
151
152 auto si = ss.getSpaceInformation();
153
154 si->setStateValidityChecker(std::make_shared<ompl::vamp::VampStateValidityChecker<Robot>>(si, env));
155 si->setMotionValidator(std::make_shared<ompl::vamp::VampMotionValidator<Robot>>(si, env));
156
157 // Define start and goal configurations (Panda 7 DOF)
158 ob::ScopedState<> start(space);
159 ob::ScopedState<> goal(space);
160
161 std::array<double, 7> start_config = {0., -0.785, 0., -2.356, 0., 1.571, 0.785};
162 std::array<double, 7> goal_config = {2.35, 1., 0., -0.8, 0., 2.5, 0.785};
163
164 for (std::size_t i = 0; i < Robot::dimension; ++i)
165 {
166 start[i] = start_config[i];
167 goal[i] = goal_config[i];
168 }
169
170 // Setup problem definition
171 ss.setStartAndGoalStates(start, goal);
172
173 // setup benchmark
174 double memoryLimit = 4096;
175 double runtimeLimit = 5.0;
176 ot::Benchmark b(ss, "VAMP_Cage_Planning");
177 ot::Benchmark::Request request(runtimeLimit, memoryLimit, runCount);
178
179 b.addPlanner(std::make_shared<og::RRTConnect>(si));
180 b.addPlanner(std::make_shared<og::RRT>(si));
181 b.addPlanner(std::make_shared<og::KPIECE1>(si));
182 b.addPlanner(std::make_shared<og::LBKPIECE1>(si));
183
184 b.benchmark(request);
185 b.saveResultsToFile("vamp_cage_planning_benchmark_cpp.log");
186
187 // Use python script to transfer .log to .db
188 std::cout << "Results saved to vamp_cage_planning_benchmark_cpp.log" << std::endl;
189 std::cout << "Use python script 'ompl/scripts/ompl_benchmark_statistics.py' to transfer .log to .db" << std::endl;
190 // ompl/scripts/ompl_benchmark_statistics.py vamp_cage_planning_benchmark_cpp.log -d
191 // vamp_cage_planning_benchmark_cpp.db
192}
193
194int main(int argc, char **argv)
195{
196 po::options_description desc("Options");
197
198 int runCount = 0;
199 desc.add_options()("help", "show help message")("benchmark", po::value<int>(&runCount)->default_value(0),
200 "Benchmark Planners for this number of trials");
201 po::variables_map vm;
202 po::store(po::parse_command_line(argc, argv, desc), vm);
203 po::notify(vm);
204
205 if (vm.count("help"))
206 {
207 std::cout << desc << std::endl;
208 return 0;
209 }
210
211 if (runCount != 0U)
212 {
213 std::cout << "Running benchmark with " << runCount << " trials." << std::endl;
214 planBenchmark(runCount);
215 }
216 else
217 {
218 std::cout << "Running single planning instance." << std::endl;
219 planOnce();
220 }
221
222 return 0;
223}
Definition of a scoped state.
Definition ScopedState.h:57
Create the set of classes typically needed to solve a geometric problem.
Definition SimpleSetup.h:63
Benchmark a set of planners on a problem instance.
Definition Benchmark.h:49
This namespace contains sampling based planning routines shared by both planning under geometric cons...
PlannerTerminationCondition timedPlannerTerminationCondition(double duration)
Return a termination condition that will become true duration seconds in the future (wall-time).
This namespace contains code that is specific to planning under geometric constraints.
Includes various tools such as self config, benchmarking, etc.
A class to store the exit status of Planner::solve().
@ EXACT_SOLUTION
The planner found an exact solution.
Representation of a benchmark request.
Definition Benchmark.h:152