Skip to content

Commit cf1c7b4

Browse files
authored
fix jerky cruise control (#456)
1 parent 8a270c2 commit cf1c7b4

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

selfdrive/controls/lib/long_mpc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ def __init__(self, mpc_id):
2121

2222

2323
def reset_mpc(self):
24-
self.libmpc = libmpc_py.libmpc
24+
ffi, self.libmpc = libmpc_py.get_libmpc(self.mpc_id)
2525
if self.mpc_id == 0:
2626
self.libmpc.init(0.0, 1.0, 0.0, 50.0, 10000.0)
2727
else:
2828
self.libmpc.init(1.0, 1.0, 0.0, 5.0, 10000.0)
2929

30-
self.mpc_solution = libmpc_py.ffi.new("log_t *")
31-
self.cur_state = libmpc_py.ffi.new("state_t *")
30+
self.mpc_solution = ffi.new("log_t *")
31+
self.cur_state = ffi.new("state_t *")
3232

3333
self.cur_state[0].x_ego = 0
3434
self.cur_state[0].v_ego = 0

selfdrive/controls/lib/longitudinal_mpc_lib/SConscript

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ if GetOption('mpc_generate'):
4444

4545

4646
mpc_files = ["longitudinal_mpc.c"] + generated_c
47-
env.SharedLibrary('mpc', mpc_files, LIBS=['m', 'qpoases'], LIBPATH=['lib_qp'], CPPPATH=cpp_path)
47+
env.SharedLibrary('mpc0', mpc_files, LIBS=['m', 'qpoases'], LIBPATH=['lib_qp'], CPPPATH=cpp_path)
48+
env.SharedLibrary('mpc1', mpc_files, LIBS=['m', 'qpoases'], LIBPATH=['lib_qp'], CPPPATH=cpp_path)

selfdrive/controls/lib/longitudinal_mpc_lib/libmpc_py.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,38 @@
44
from common.ffi_wrapper import suffix
55

66
mpc_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)))
7-
libmpc_fn = os.path.join(mpc_dir, "libmpc"+suffix())
87

9-
ffi = FFI()
10-
ffi.cdef("""
11-
const int MPC_N = 32;
8+
def _get_libmpc(mpc_id):
9+
libmpc_fn = os.path.join(mpc_dir, "libmpc%d%s" % (mpc_id, suffix()))
1210

13-
typedef struct {
14-
double x_ego, v_ego, a_ego;
15-
} state_t;
11+
ffi = FFI()
12+
ffi.cdef("""
13+
const int MPC_N = 32;
1614
15+
typedef struct {
16+
double x_ego, v_ego, a_ego;
17+
} state_t;
1718
18-
typedef struct {
19-
double x_ego[MPC_N+1];
20-
double v_ego[MPC_N+1];
21-
double a_ego[MPC_N+1];
22-
double t[MPC_N+1];
23-
double j_ego[MPC_N];
24-
double cost;
25-
} log_t;
2619
20+
typedef struct {
21+
double x_ego[MPC_N+1];
22+
double v_ego[MPC_N+1];
23+
double a_ego[MPC_N+1];
24+
double t[MPC_N+1];
25+
double j_ego[MPC_N];
26+
double cost;
27+
} log_t;
2728
28-
void init(double xCost, double vCost, double aCost, double jerkCost, double constraintCost);
29-
int run_mpc(state_t * x0, log_t * solution,
30-
double target_x[MPC_N+1], double target_v[MPC_N+1], double target_a[MPC_N+1],
31-
double min_a, double max_a);
32-
""")
3329
34-
libmpc = ffi.dlopen(libmpc_fn)
30+
void init(double xCost, double vCost, double aCost, double jerkCost, double constraintCost);
31+
int run_mpc(state_t * x0, log_t * solution,
32+
double target_x[MPC_N+1], double target_v[MPC_N+1], double target_a[MPC_N+1],
33+
double min_a, double max_a);
34+
""")
35+
36+
return (ffi, ffi.dlopen(libmpc_fn))
37+
38+
mpcs = [_get_libmpc(0), _get_libmpc(1)]
39+
40+
def get_libmpc(mpc_id):
41+
return mpcs[mpc_id]

0 commit comments

Comments
 (0)