Skip to content

Commit 3da6bfb

Browse files
committed
init
0 parents  commit 3da6bfb

File tree

497 files changed

+73214
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

497 files changed

+73214
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/opt/ros/melodic/share/catkin/cmake/toplevel.cmake

planner/bspline_opt/CMakeLists.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(bspline_opt)
3+
4+
set(CMAKE_BUILD_TYPE "Release")
5+
ADD_COMPILE_OPTIONS(-std=c++11 )
6+
ADD_COMPILE_OPTIONS(-std=c++14 )
7+
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g")
8+
9+
find_package(Eigen3 REQUIRED)
10+
find_package(PCL 1.7 REQUIRED)
11+
12+
find_package(catkin REQUIRED COMPONENTS
13+
roscpp
14+
rospy
15+
std_msgs
16+
visualization_msgs
17+
plan_env
18+
cv_bridge
19+
path_searching
20+
)
21+
22+
catkin_package(
23+
INCLUDE_DIRS include
24+
LIBRARIES bspline_opt
25+
CATKIN_DEPENDS plan_env
26+
# DEPENDS system_lib
27+
)
28+
29+
include_directories(
30+
SYSTEM
31+
include
32+
${catkin_INCLUDE_DIRS}
33+
${Eigen3_INCLUDE_DIRS}
34+
${PCL_INCLUDE_DIRS}
35+
)
36+
37+
add_library( bspline_opt
38+
src/uniform_bspline.cpp
39+
src/bspline_optimizer.cpp
40+
src/gradient_descent_optimizer.cpp
41+
)
42+
target_link_libraries( bspline_opt
43+
${catkin_LIBRARIES}
44+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#ifndef _BSPLINE_OPTIMIZER_H_
2+
#define _BSPLINE_OPTIMIZER_H_
3+
4+
#include <Eigen/Eigen>
5+
#include <path_searching/dyn_a_star.h>
6+
#include <bspline_opt/uniform_bspline.h>
7+
#include <plan_env/grid_map.h>
8+
#include <ros/ros.h>
9+
#include "bspline_opt/lbfgs.hpp"
10+
11+
// Gradient and elasitc band optimization
12+
13+
// Input: a signed distance field and a sequence of points
14+
// Output: the optimized sequence of points
15+
// The format of points: N x 3 matrix, each row is a point
16+
namespace ego_planner
17+
{
18+
19+
class ControlPoints
20+
{
21+
public:
22+
double clearance;
23+
int size;
24+
Eigen::MatrixXd points;
25+
std::vector<std::vector<Eigen::Vector3d>> base_point; // The point at the statrt of the direction vector (collision point)
26+
std::vector<std::vector<Eigen::Vector3d>> direction; // Direction vector, must be normalized.
27+
std::vector<bool> flag_temp; // A flag that used in many places. Initialize it everytime before using it.
28+
// std::vector<bool> occupancy;
29+
30+
void resize(const int size_set)
31+
{
32+
size = size_set;
33+
34+
base_point.clear();
35+
direction.clear();
36+
flag_temp.clear();
37+
// occupancy.clear();
38+
39+
points.resize(3, size_set);
40+
base_point.resize(size);
41+
direction.resize(size);
42+
flag_temp.resize(size);
43+
// occupancy.resize(size);
44+
}
45+
};
46+
47+
class BsplineOptimizer
48+
{
49+
50+
public:
51+
BsplineOptimizer() {}
52+
~BsplineOptimizer() {}
53+
54+
/* main API */
55+
void setEnvironment(const GridMap::Ptr &env);
56+
void setParam(ros::NodeHandle &nh);
57+
Eigen::MatrixXd BsplineOptimizeTraj(const Eigen::MatrixXd &points, const double &ts,
58+
const int &cost_function, int max_num_id, int max_time_id);
59+
60+
/* helper function */
61+
62+
// required inputs
63+
void setControlPoints(const Eigen::MatrixXd &points);
64+
void setBsplineInterval(const double &ts);
65+
void setCostFunction(const int &cost_function);
66+
void setTerminateCond(const int &max_num_id, const int &max_time_id);
67+
68+
// optional inputs
69+
void setGuidePath(const vector<Eigen::Vector3d> &guide_pt);
70+
void setWaypoints(const vector<Eigen::Vector3d> &waypts,
71+
const vector<int> &waypt_idx); // N-2 constraints at most
72+
73+
void optimize();
74+
75+
Eigen::MatrixXd getControlPoints();
76+
77+
AStar::Ptr a_star_;
78+
std::vector<Eigen::Vector3d> ref_pts_;
79+
80+
std::vector<std::vector<Eigen::Vector3d>> initControlPoints(Eigen::MatrixXd &init_points, bool flag_first_init = true);
81+
bool BsplineOptimizeTrajRebound(Eigen::MatrixXd &optimal_points, double ts); // must be called after initControlPoints()
82+
bool BsplineOptimizeTrajRefine(const Eigen::MatrixXd &init_points, const double ts, Eigen::MatrixXd &optimal_points);
83+
84+
inline int getOrder(void) { return order_; }
85+
86+
private:
87+
GridMap::Ptr grid_map_;
88+
89+
enum FORCE_STOP_OPTIMIZE_TYPE
90+
{
91+
DONT_STOP,
92+
STOP_FOR_REBOUND,
93+
STOP_FOR_ERROR
94+
} force_stop_type_;
95+
96+
// main input
97+
// Eigen::MatrixXd control_points_; // B-spline control points, N x dim
98+
double bspline_interval_; // B-spline knot span
99+
Eigen::Vector3d end_pt_; // end of the trajectory
100+
// int dim_; // dimension of the B-spline
101+
//
102+
vector<Eigen::Vector3d> guide_pts_; // geometric guiding path points, N-6
103+
vector<Eigen::Vector3d> waypoints_; // waypts constraints
104+
vector<int> waypt_idx_; // waypts constraints index
105+
//
106+
int max_num_id_, max_time_id_; // stopping criteria
107+
int cost_function_; // used to determine objective function
108+
double start_time_; // global time for moving obstacles
109+
110+
/* optimization parameters */
111+
int order_; // bspline degree
112+
double lambda1_; // jerk smoothness weight
113+
double lambda2_, new_lambda2_; // distance weight
114+
double lambda3_; // feasibility weight
115+
double lambda4_; // curve fitting
116+
117+
int a;
118+
//
119+
double dist0_; // safe distance
120+
double max_vel_, max_acc_; // dynamic limits
121+
122+
int variable_num_; // optimization variables
123+
int iter_num_; // iteration of the solver
124+
Eigen::VectorXd best_variable_; //
125+
double min_cost_; //
126+
127+
ControlPoints cps_;
128+
129+
/* cost function */
130+
/* calculate each part of cost function with control points q as input */
131+
132+
static double costFunction(const std::vector<double> &x, std::vector<double> &grad, void *func_data);
133+
void combineCost(const std::vector<double> &x, vector<double> &grad, double &cost);
134+
135+
// q contains all control points
136+
void calcSmoothnessCost(const Eigen::MatrixXd &q, double &cost,
137+
Eigen::MatrixXd &gradient, bool falg_use_jerk = true);
138+
void calcFeasibilityCost(const Eigen::MatrixXd &q, double &cost,
139+
Eigen::MatrixXd &gradient);
140+
void calcDistanceCostRebound(const Eigen::MatrixXd &q, double &cost, Eigen::MatrixXd &gradient, int iter_num, double smoothness_cost);
141+
void calcFitnessCost(const Eigen::MatrixXd &q, double &cost, Eigen::MatrixXd &gradient);
142+
bool check_collision_and_rebound(void);
143+
144+
static int earlyExit(void *func_data, const double *x, const double *g, const double fx, const double xnorm, const double gnorm, const double step, int n, int k, int ls);
145+
static double costFunctionRebound(void *func_data, const double *x, double *grad, const int n);
146+
static double costFunctionRefine(void *func_data, const double *x, double *grad, const int n);
147+
148+
bool rebound_optimize();
149+
bool refine_optimize();
150+
void combineCostRebound(const double *x, double *grad, double &f_combine, const int n);
151+
void combineCostRefine(const double *x, double *grad, double &f_combine, const int n);
152+
153+
/* for benckmark evaluation only */
154+
public:
155+
typedef unique_ptr<BsplineOptimizer> Ptr;
156+
157+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
158+
};
159+
160+
} // namespace ego_planner
161+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef _GRADIENT_DESCENT_OPT_H_
2+
#define _GRADIENT_DESCENT_OPT_H_
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <Eigen/Eigen>
7+
8+
using namespace std;
9+
10+
class GradientDescentOptimizer
11+
{
12+
13+
public:
14+
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
15+
16+
typedef double (*objfunDef)(const Eigen::VectorXd &x, Eigen::VectorXd &grad, bool &force_return, void *data);
17+
enum RESULT
18+
{
19+
FIND_MIN,
20+
FAILED,
21+
RETURN_BY_ORDER,
22+
REACH_MAX_ITERATION
23+
};
24+
25+
GradientDescentOptimizer(int v_num, objfunDef objf, void *f_data)
26+
{
27+
variable_num_ = v_num;
28+
objfun_ = objf;
29+
f_data_ = f_data;
30+
};
31+
32+
void set_maxiter(int limit) { iter_limit_ = limit; }
33+
void set_maxeval(int limit) { invoke_limit_ = limit; }
34+
void set_xtol_rel(double xtol_rel) { xtol_rel_ = xtol_rel; }
35+
void set_xtol_abs(double xtol_abs) { xtol_abs_ = xtol_abs; }
36+
void set_min_grad(double min_grad) { min_grad_ = min_grad; }
37+
38+
RESULT optimize(Eigen::VectorXd &x_init_optimal, double &opt_f);
39+
40+
private:
41+
int variable_num_{0};
42+
int iter_limit_{1e10};
43+
int invoke_limit_{1e10};
44+
double xtol_rel_;
45+
double xtol_abs_;
46+
double min_grad_;
47+
double time_limit_;
48+
void *f_data_;
49+
objfunDef objfun_;
50+
};
51+
52+
#endif

0 commit comments

Comments
 (0)