我想在.cpp中创建一个控制器来使用acados控制一个简单的摆,以学习如何将它用于真正的控制器。
如何将 acados 生成的代码包含在 .cpp 程序中。那就是:
我在 acados 中创建了一些代码来解决 OPC。它有效,并且使用 AcadosSim 我模拟了钟摆。我可以使用以下命令从 python 界面生成 c 文件:
builder = CMakeBuilder()
ocp_solver = AcadosOcpSolver(ocp, json_file = 'acados_ocp.json')
ocp_solver.generate(ocp,cmake_builder=builder)
并且会创建一个带有
c_generated_code
的文件夹 CMakeLists.txt
。
如果我这样做:
cd c_generated_code
make
我得到了一些
main_pendulum
和 main_sim_pendulum
可执行文件,我可以运行它们并且它们可以工作。所以 code_ Generation 似乎工作没有问题。
如果我在这个
CMakeLists.txt
中启用一些选项,如果我已经获取了与acados相关的环境变量,我会生成一些可以正确运行的可执行文件:
export ACADOS_SOURCE_DIR="$HOME/acados"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$HOME/acados/lib"
如果我尝试包含此头文件:
#include "c_generated_code/acados_solver_pendulum.h"
并修改c代码的
CMakeLists.txt
,如下所示:
export(TARGETS acados_ocp_solver_pendulum FILE ${CMAKE_CURRENT_BINARY_DIR}/acados_ocp_solver_pendulumConfig.cmake)
和根
CMakeLists.txt
像这样:
add_subdirectory(c_generated_code)
find_package(acados_ocp_solver_pendulum REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR} )
target_link_libraries(passive_simulation acados_ocp_solver_pendulum #other_libs)
但我仍然收到此错误:
fatal error: 'acados/utils/types.h' file not found
如果使用 python 接口生成的
CMakeLists.txt
,我会得到以下输出。
Scanning dependencies of target ocp_pendulum
[ ... ] to avoid cluttering i do not show these
[ 55%] Building C object CMakeFiles/ocp_pendulum.dir/acados_solver_pendulum.c.o
[ 55%] Built target ocp_pendulum
Scanning dependencies of target model_pendulum
[ 72%] Building C object CMakeFiles/model_pendulum.dir/pendulum_model/pendulum_impl_dae_jac_x_xdot_u_z.c.o
[ 72%] Built target model_pendulum
Scanning dependencies of target sim_pendulum
[ 77%] Building C object CMakeFiles/sim_pendulum.dir/acados_sim_solver_pendulum.c.o
[ 77%] Built target sim_pendulum
Scanning dependencies of target main_pendulum
[ 83%] Building C object CMakeFiles/main_pendulum.dir/main_pendulum.c.o
[ 88%] Linking C executable main_pendulum
[ 88%] Built target main_pendulum
Scanning dependencies of target acados_solver_pendulum
[ 94%] Linking C shared library libacados_solver_pendulum.so
[ 94%] Built target acados_solver_pendulum
Scanning dependencies of target acados_ocp_solver_pendulum
[100%] Linking C shared library libacados_ocp_solver_pendulum.so
[100%] Built target acados_ocp_solver_pendulum
建设成功!
我的目录结构是这样的:
.
├── acados_ocp.json
├── acados_ocp_nlp.json
├── build
│ ├── #some files
├── c_generated_code
│ ├── acados_sim_solver_pendulum.c
│ ├── acados_sim_solver_pendulum.h
│ ├── acados_solver_pendulum.c
│ ├── acados_solver_pendulum.h
│ ├── acados_solver_pendulum.o
│ ├── acados_solver.pxd
│ ├── build
│ ├── CMakeLists.txt
│ ├── libacados_ocp_solver_pendulum.so
│ ├── main_pendulum.c
│ ├── main_sim_pendulum.c
│ ├── Makefile
│ ├── pendulum_constraints
│ ├── pendulum_cost
│ └── pendulum_model
├── CMakeLists.txt
├── passive_simulation.cc
├── pendulum_model.py
├── pendulum_ocp.py
通常的 acados 目录结构没有任何名为 utils/types.h 的文件。检查此头文件的使用位置,如果与 acados 的构建结构相关,请将您的问题发布到 https://discourse.acados.org/ 上。