|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import ctypes |
| 4 | +import platform |
| 5 | + |
| 6 | +def get_acados_dir(): |
| 7 | + # current file: openpilot/selfdrive/controls/lib/acados_setup.py |
| 8 | + # acados is at openpilot/third_party/acados |
| 9 | + current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 10 | + op_dir = os.path.abspath(os.path.join(current_dir, '..', '..', '..')) |
| 11 | + return os.path.join(op_dir, 'third_party', 'acados') |
| 12 | + |
| 13 | +def get_acados_lib_path(): |
| 14 | + acados_dir = get_acados_dir() |
| 15 | + if sys.platform.startswith('linux'): |
| 16 | + machine = platform.machine() |
| 17 | + if machine == 'x86_64': |
| 18 | + return os.path.join(acados_dir, 'x86_64', 'lib') |
| 19 | + elif machine == 'aarch64': |
| 20 | + return os.path.join(acados_dir, 'larch64', 'lib') |
| 21 | + elif sys.platform.startswith('darwin'): |
| 22 | + return os.path.join(acados_dir, 'Darwin', 'lib') |
| 23 | + |
| 24 | + # Fallback |
| 25 | + return os.path.join(acados_dir, 'x86_64', 'lib') |
| 26 | + |
| 27 | +def acados_preload(): |
| 28 | + lib_path = get_acados_lib_path() |
| 29 | + if not os.path.exists(lib_path): |
| 30 | + return |
| 31 | + |
| 32 | + if sys.platform.startswith('linux'): |
| 33 | + libs = ['libblasfeo.so', 'libhpipm.so', 'libqpOASES_e.so.3.1'] |
| 34 | + mode = ctypes.RTLD_GLOBAL |
| 35 | + elif sys.platform.startswith('darwin'): |
| 36 | + libs = ['libblasfeo.dylib', 'libhpipm.dylib', 'libqpOASES_e.3.1.dylib'] |
| 37 | + mode = ctypes.RTLD_GLOBAL |
| 38 | + else: |
| 39 | + libs = [] |
| 40 | + mode = 0 |
| 41 | + |
| 42 | + for lib in libs: |
| 43 | + full_path = os.path.join(lib_path, lib) |
| 44 | + if os.path.exists(full_path): |
| 45 | + try: |
| 46 | + ctypes.CDLL(full_path, mode=mode) |
| 47 | + except OSError: |
| 48 | + pass |
| 49 | + |
| 50 | +def prepare_acados_ocp_json(json_file): |
| 51 | + import json |
| 52 | + import tempfile |
| 53 | + |
| 54 | + with open(json_file) as f: |
| 55 | + data = json.load(f) |
| 56 | + |
| 57 | + data['acados_lib_path'] = get_acados_lib_path() |
| 58 | + |
| 59 | + fd, path = tempfile.mkstemp(suffix='.json', text=True) |
| 60 | + with os.fdopen(fd, 'w') as f: |
| 61 | + json.dump(data, f, indent=4) |
| 62 | + |
| 63 | + return path |
0 commit comments