Skip to content

Commit 73eacf2

Browse files
committed
Implements first version of MPPI for collision avoidance
1 parent dbc88d9 commit 73eacf2

17 files changed

Lines changed: 803 additions & 138 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/cmake-build-debug/
22
/cmake-build-release/
3-
/.idea/
3+
/.idea/
4+
CMakeLists.txt.user

CMakeLists.txt

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
cmake_minimum_required(VERSION 3.24)
1+
cmake_minimum_required(VERSION 3.20)
22
project(mppi_ca_cpp)
33

44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
66

7-
#add_definitions(-DHAVE_CBLAS=1)
8-
#add_definitions(-DUSE_CBLAS -DUSE_LAPACK)
97
add_compile_options(-lcblas)
108
add_compile_options(-lopenblas)
119

1210
find_package(Python REQUIRED COMPONENTS Interpreter Development NumPy)
1311
find_package(BLAS REQUIRED)
1412
find_package(LAPACK REQUIRED)
15-
#find_package(CBLAS REQUIRED)
13+
find_package(ortools CONFIG REQUIRED)
1614

1715
message(STATUS "BLAS VENDOR: " ${BLA_VENDOR})
1816
message(STATUS "BLAS LIBRARIES: " ${BLAS_LIBRARIES})
@@ -31,8 +29,9 @@ add_subdirectory(external/pybind11)
3129

3230
add_library(controller_library SHARED
3331
src/controller.cpp
34-
src/diff_drive.cpp
35-
src/geometry.cpp)
32+
src/dynamics.cpp
33+
src/geometry.cpp
34+
src/orca_lines.cpp)
3635

3736

3837
target_include_directories(controller_library
@@ -43,7 +42,6 @@ target_include_directories(controller_library
4342
target_compile_definitions(controller_library PUBLIC -DUSE_CBLAS)
4443

4544
target_link_libraries(controller_library ${LAPACK_LIBRARIES})
46-
#target_link_libraries(controller_library ${BLAS_LIBRARIES})
4745

4846
target_link_libraries(controller_library xtensor)
4947
target_link_libraries(controller_library xtensor-blas)
@@ -52,15 +50,10 @@ target_link_libraries(controller_library xsimd)
5250

5351
pybind11_add_module(mppi_ca_cpp python_bind/converters.hpp python_bind/converters.cpp python_bind/mppi_ca_cpp.cpp NO_EXTRAS)
5452
target_link_libraries(mppi_ca_cpp PRIVATE xtensor-python Python::NumPy)
55-
target_link_libraries(mppi_ca_cpp PRIVATE controller_library)
56-
57-
add_executable(mppi_test src/main.cpp)
58-
target_link_libraries(mppi_test PRIVATE controller_library)
53+
target_link_libraries(mppi_ca_cpp PRIVATE controller_library ortools::ortools)
5954

6055

6156
add_custom_target(mppi_all)
6257
add_dependencies(mppi_all
6358
controller_library
64-
mppi_test
65-
mppi_ca_cpp
66-
)
59+
mppi_ca_cpp)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Model Predictive Path Integral for Multi-Agent Collision Avoidance
2+
3+
A detailed description of the ***MPPI for collision avoidance*** algorithm,
4+
corresponding parameters and build will be given here soon.

include/controller.hpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111
#include <iostream>
1212
#include <ctime>
1313
#include <limits>
14+
#include <cmath>
15+
#include <memory>
16+
#include <random>
17+
18+
#include "ortools/linear_solver/linear_solver.h"
1419

1520
#include "consts.hpp"
16-
#include "diff_drive.hpp"
21+
#include "dynamics.hpp"
1722
#include "geometry.hpp"
23+
#include "orca_lines.hpp"
1824

1925
namespace mppica {
2026
using std::cout;
@@ -38,16 +44,9 @@ namespace mppica {
3844
double ma_dist_weight = 1000.0;
3945
double ma_collision_value = 10000.0;
4046
double ma_dist_threshold = 2.0;
41-
42-
size_t max_safe_distr_step; // TODO
43-
size_t max_bvc_cost_step; // TODO
44-
double bvc_weight; // TODO
45-
double bvc_w_base; // TODO
46-
double bvc_w_mean; // TODO
47-
double bvc_w_std; // TODO
48-
49-
bool importance_sampl_en; // TODO importance sampling
50-
double alpha; // TODO sampling from zero alpha % of batches
47+
int max_safe_distr_step = 0;
48+
bool car_like_dyn = false;
49+
double alpha; // TODO: Sampling with initial mean value in alpha% of batches
5150
};
5251

5352
struct AgentParams {
@@ -60,15 +59,13 @@ namespace mppica {
6059
double dd_w_min = -2.0;
6160
};
6261

63-
// xt::xtensor<double, 2> control_limits;
64-
6562

6663
class Controller {
6764
public:
6865
Controller();
6966

7067
auto nextStep(const xt::xtensor<double, 1> &curr_state, const xt::xtensor<double, 2> &neighbors_states,
71-
const xt::xtensor<double, 1> &goal_state) -> xt::xtensor<double, 1>;
68+
const xt::xtensor<double, 1> &goal_state) -> std::tuple<xt::xtensor<double, 1>, xt::xtensor<double, 3>>;
7269

7370
void setAgentParams(AgentParams ag_params);
7471

@@ -118,18 +115,31 @@ namespace mppica {
118115

119116
void multiAgentCost(size_t batch, size_t time_step, const xt::xtensor<double, 1> &goal_state);
120117

118+
void computeLinearConstraintsForState(const xt::xtensor<double, 1> &state,
119+
const xt::xtensor<double, 2> &neighbors_states);
120+
121+
void resampleInitialWithConstraints();
122+
123+
124+
std::tuple<bool, double, double> computeNewDistr(const xt::xtensor<double, 1> &state, const xt::xtensor<double, 1> &control);
125+
126+
std::tuple<xt::xtensor<double, 1>, xt::xtensor<double, 1>> translateConstraintsToControlBounds(const xt::xtensor<double, 1> &state);
121127

122128
bool computationShouldStop(); // TODO
123129
void updateStopComputationConditions(); // TODO
124130
void computeMainTrajectory(); // TODO
125131

126132

133+
size_t step; // For debug purpuses
134+
size_t controller_id; // For debug purpuses
135+
127136
bool ma_cost_en;
128137
size_t max_neighbors_num;
129138
size_t neighbors_num;
130139
double ma_dist_weight;
131140
double ma_collision_value;
132141
double ma_dist_threshold;
142+
int max_safe_distr_step;
133143

134144
size_t batch_size;
135145
size_t time_steps;
@@ -148,17 +158,26 @@ namespace mppica {
148158
xt::xtensor<double, 2> control_sequence;
149159
xt::xtensor<double, 3> generated_trajectories;
150160
xt::xtensor<double, 1> traj_costs;
151-
152161
xt::xtensor<double, 3> neighbors_traj;
162+
xt::xtensor<double, 2> linear_constraints;
163+
std::vector<ORCALine> orca_lines;
164+
bool orca_complete = true;
165+
166+
153167

154168
double common_run_cost_weight;
155169
double common_terminal_cost_weight;
156170

157171
double agent_size;
158172
double vis_radius;
173+
double rad_eps;
159174

175+
bool use_car_like_dyn;
160176

161177
DiffDrive dyn_model; //TODO Generalize for other models
178+
CarLike car_like_dyn;
179+
180+
162181
};
163182

164183
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@
1313
#define MPPI_CA_CPP_DIFF_DRIVE_HPP
1414

1515

16-
class DiffDrive
17-
{
16+
class DiffDrive {
1817
public:
1918
auto shift(const xt::xtensor<double, 1> &state, double dt) -> xt::xtensor<double, 1>;
20-
auto dynamic(const xt::xtensor<double, 1> &state, const xt::xtensor<double, 1> &control, double dt) -> xt::xtensor<double, 1>;
2119

20+
auto dynamic(const xt::xtensor<double, 1> &state, const xt::xtensor<double, 1> &control,
21+
double dt) -> xt::xtensor<double, 1>;
2222

23+
24+
};
25+
26+
27+
class CarLike {
28+
public:
29+
auto shift(const xt::xtensor<double, 1> &state, double dt) -> xt::xtensor<double, 1>;
30+
31+
auto dynamic(const xt::xtensor<double, 1> &state, const xt::xtensor<double, 1> &control,
32+
double dt) -> xt::xtensor<double, 1>;
33+
34+
double dist_between_axles;
2335
};
2436

2537

include/geometry.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
#ifndef MPPI_CA_CPP_GEOMETRY_HPP
1111
#define MPPI_CA_CPP_GEOMETRY_HPP
1212

13-
double point_to_line_segment_distance(const xt::xtensor<double, 1> &line_p1,
14-
const xt::xtensor<double, 1> &line_p2,
15-
const xt::xtensor<double, 1> &point);
13+
double pointToLineSegmentDistance(const xt::xtensor<double, 1> &line_p1,
14+
const xt::xtensor<double, 1> &line_p2,
15+
const xt::xtensor<double, 1> &point);
16+
17+
double determinant2D(const xt::xtensor<double, 1> &ab, const xt::xtensor<double, 1> &cd);
18+
19+
20+
std::tuple<bool, xt::xtensor<double, 1>> lineIntersection(const xt::xtensor<double, 1> &line1, const xt::xtensor<double, 1> &line2);
1621

1722

1823
#endif //MPPI_CA_CPP_GEOMETRY_HPP

include/orca_lines.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef MPPI_CA_CPP_ORCA_LINES_HPP
2+
#define MPPI_CA_CPP_ORCA_LINES_HPP
3+
4+
#include <xtensor/xtensor.hpp>
5+
#include <xtensor/xarray.hpp>
6+
#include <xtensor/xview.hpp>
7+
#include <xtensor/xfixed.hpp>
8+
#include <xtensor/xio.hpp>
9+
#include <xtensor/xrandom.hpp>
10+
#include <xtensor-blas/xlinalg.hpp>
11+
#include <iostream>
12+
#include <ctime>
13+
#include <limits>
14+
15+
#include "consts.hpp"
16+
#include "geometry.hpp"
17+
18+
19+
struct ORCALine {
20+
xt::xtensor<double, 1> dir = xt::zeros<double>({2});
21+
xt::xtensor<double, 1> point_lies_on = xt::zeros<double>({2});;
22+
};
23+
24+
struct ORCAParams {
25+
size_t agents_max_num;
26+
double agent_size;
27+
double time_boundary;
28+
double time_step;
29+
double responsibility_factor = 0.5;
30+
bool opt_zero_vel = true;
31+
};
32+
33+
std::vector<ORCALine> computeORCALines(ORCAParams params, xt::xtensor<double, 1> agent_state, xt::xtensor<double, 2> neighbors_states,
34+
xt::xtensor<double, 1> neighbors_sizes);
35+
36+
xt::xtensor<double, 2> computeORCALinesXT(size_t agents_max_num,
37+
double agent_size,
38+
double time_boundary,
39+
double time_step,
40+
double responsibility_factor,
41+
bool opt_zero_vel,
42+
xt::xtensor<double, 1> agent_state,
43+
xt::xtensor<double, 2> neighbors_states,
44+
xt::xtensor<double, 1> neighbors_sizes);
45+
46+
47+
bool linearProgram1(const std::vector<ORCALine> &lines, unsigned long curr, float radius, const xt::xtensor<double, 1> &opt_velocity,
48+
bool direction_opt, xt::xtensor<double, 1> &result);
49+
50+
unsigned long int linearProgram2(const std::vector<ORCALine> &lines, double radius, const xt::xtensor<double, 1> &opt_velocity, bool direction_opt,
51+
xt::xtensor<double, 1> &result);
52+
#endif //MPPI_CA_CPP_ORCA_LINES_HPP

python_bind/converters.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ mppica::MPPIParams convertAlgParams(const py::dict &params_dict) {
3535
params.ma_dist_weight = params_dict["ma_dist_weight"].cast<double>();
3636
params.ma_collision_value = params_dict["ma_collision_weight"].cast<double>();
3737
params.ma_dist_threshold = params_dict["ma_dist_threashhold"].cast<double>();
38+
params.max_safe_distr_step = params_dict["max_safe_distr_step"].cast<int>();
39+
params.car_like_dyn = params_dict["car_like_dyn"].cast<bool>();
3840

3941
return params;
4042
}

python_bind/converters.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
#include "../include/controller.hpp"
2-
#include <pybind11/pybind11.h>
3-
#include "xtensor-python/pyarray.hpp"
1+
42

53
#ifndef MPPI_CA_CPP_PYTHON_CONTROLLER_H
64
#define MPPI_CA_CPP_PYTHON_CONTROLLER_H
75

6+
#include "../include/controller.hpp"
7+
#include <pybind11/pybind11.h>
8+
#include "xtensor-python/pyarray.hpp"
9+
810
namespace py = pybind11;
911

1012
mppica::AgentParams convertAgentParams(const py::dict& params_dict);

python_bind/mppi_ca_cpp.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define FORCE_IMPORT_ARRAY
33
#include <xtensor-python/pytensor.hpp>
44
#include "../include/controller.hpp"
5+
#include "../include/orca_lines.hpp"
56
#include "converters.hpp"
67

78
namespace py = pybind11;
@@ -20,14 +21,6 @@ PYBIND11_MODULE(mppi_ca_cpp, m) {
2021

2122
m.def("convert_agent_params", &convertAgentParams, "Coverts agent params");
2223
m.def("convert_alg_params", &convertAlgParams, "Coverts alg parameters");
24+
m.def("compute_orca_lines", &computeORCALinesXT, "Computes ORCA lines for agents");
2325
}
2426

25-
26-
//PYBIND11_MODULE(bind_module, m) {
27-
// xt::import_numpy();
28-
// py::class_<Controller>(m, "Controller")
29-
// .def(py::init<>())
30-
// .def("next_step", &Controller::nextStep);
31-
//}
32-
33-

0 commit comments

Comments
 (0)