#include "mex.h"
#include "matrix.h"
#include <iostream>
#include <cstring>
#include <memory>
// Fix the Eigen include issue by using the standard approach
#include <Eigen/Dense>
#include <Eigen/Core>
// Now include TinyMPC headers (after Eigen is properly included)
#include "tinympc/tiny_api.hpp"
#include "tinympc/types.hpp"
#include "tinympc/codegen.hpp"
// Global solver pointer - matches Python structure
static std::unique_ptr<TinySolver> g_solver = nullptr;
// Helper function to convert MATLAB array to Eigen matrix
Eigen::MatrixXd matlab_to_eigen(const mxArray* mx_array) {
if (!mxIsDouble(mx_array) || mxIsComplex(mx_array)) {
mexErrMsgIdAndTxt("TinyMPC:InvalidInput", "Input must be a real double array");
}
size_t rows = mxGetM(mx_array);
size_t cols = mxGetN(mx_array);
double* data = mxGetPr(mx_array);
return Eigen::Map<Eigen::MatrixXd>(data, rows, cols);
}
// Helper function to convert Eigen matrix to MATLAB array
mxArray* eigen_to_matlab(const Eigen::MatrixXd& eigen_mat) {
size_t rows = eigen_mat.rows();
size_t cols = eigen_mat.cols();
mxArray* mx_array = mxCreateDoubleMatrix(rows, cols, mxREAL);
double* data = mxGetPr(mx_array);
// Copy data (Eigen is column-major, MATLAB is column-major, so direct copy)
std::memcpy(data, eigen_mat.data(), rows * cols * sizeof(double));
return mx_array;
}
// Setup function - initialize the solver
void setup_solver(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
// Expected arguments: A, B, fdyn, Q, R, rho, nx, nu, N, verbose
if (nrhs != 10) {
mexErrMsgIdAndTxt("TinyMPC:InvalidInput", "setup requires 10 input arguments: A, B, fdyn, Q, R, rho, nx, nu, N, verbose");
}
// Extract matrices
auto A = matlab_to_eigen(prhs[0]);
auto B = matlab_to_eigen(prhs[1]);
auto fdyn = matlab_to_eigen(prhs[2]);
auto Q = matlab_to_eigen(prhs[3]);
auto R = matlab_to_eigen(prhs[4]);
double rho = mxGetScalar(prhs[5]);
// Extract problem dimensions
int nx = (int)mxGetScalar(prhs[6]);
int nu = (int)mxGetScalar(prhs[7]);
int N = (int)mxGetScalar(prhs[8]);
int verbose = (int)mxGetScalar(prhs[9]);
if (verbose) {
mexPrintf("Setting up TinyMPC solver with nx=%d, nu=%d, N=%d, rho=%f\n", nx, nu, N, rho);
}
try {
// Create solver using exact same process as Python PyTinySolver constructor
TinySolver* solver_ptr = nullptr;
// Convert Eigen matrices to tinyMatrix (matching Python bindings)
tinyMatrix A_tiny = A.cast<tinytype>();
tinyMatrix B_tiny = B.cast<tinytype>();
tinyMatrix fdyn_tiny = fdyn.cast<tinytype>();
tinyMatrix Q_tiny = Q.cast<tinytype>();
tinyMatrix R_tiny = R.cast<tinytype>();
// Setup solver
int status = tiny_setup(&solver_ptr, A_tiny, B_tiny, fdyn_tiny, Q_tiny, R_tiny,
(tinytype)rho, nx, nu, N, verbose);
if (status != 0) {
mexErrMsgIdAndTxt("TinyMPC:SetupFailed", "tiny_setup failed with status %d", status);
}
// Store solver (transfer ownership)
g_solver.reset(solver_ptr);
if (verbose) {
mexPrintf("TinyMPC solver setup completed successfully\n");
}
// Return status (0 for success)
plhs[0] = mxCreateDoubleScalar(0);
} catch (const std::exception& e) {
mexErrMsgIdAndTxt("TinyMPC:SetupException", "Setup failed: %s", e.what());
}
}
// Set initial state
void set_x0(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
if (nrhs != 2) {
mexErrMsgIdAndTxt("TinyMPC:InvalidInput", "set_x0 requires 2 input arguments");
}
if (!g_solver) {
mexErrMsgIdAndTxt("TinyMPC:NotInitialized", "Solver not initialized");
}
auto x0 = matlab_to_eigen(prhs[0]);
int verbose = (int)mxGetScalar(prhs[1]);
tinyVector x0_tiny = x0.cast<tinytype>();
// Use the API function
int status = tiny_set_x0(g_solver.get(), x0_tiny);
if (status != 0) {
mexErrMsgIdAndTxt("TinyMPC:SetX0Failed", "tiny_set_x0 failed with status %d", status);
}
if (verbose) {
mexPrintf("Initial state set\n");
}
}
// Set state reference
void set_x_ref(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
if (nrhs != 2) {
mexErrMsgIdAndTxt("TinyMPC:InvalidInput", "set_x_ref requires 2 input arguments");
}
if (!g_solver) {
mexErrMsgIdAndTxt("TinyMPC:NotInitialized", "Solver not initialized");
}
auto x_ref = matlab_to_eigen(prhs[0]);
int verbose = (int)mxGetScalar(prhs[1]);
tinyMatrix x_ref_tiny = x_ref.cast<tinytype>();
// Use the API function
int status = tiny_set_x_ref(g_solver.get(), x_ref_tiny);
if (status != 0) {
mexErrMsgIdAndTxt("TinyMPC:SetXRefFailed", "tiny_set_x_ref failed with status %d", status);
}
if (verbose) {
mexPrintf("State reference set\n");
}
}
// Set input reference
void set_u_ref(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
if (nrhs != 2) {
mexErrMsgIdAndTxt("TinyMPC:InvalidInput", "set_u_ref requires 2 input arguments");
}
if (!g_solver) {
mexErrMsgIdAndTxt("TinyMPC:NotInitialized", "Solver not initialized");
}
auto u_ref = matlab_to_eigen(prhs[0]);
int verbose = (int)mxGetScalar(prhs[1]);
tinyMatrix u_ref_tiny = u_ref.cast<tinytype>();
// Use the API function
int status = tiny_set_u_ref(g_solver.get(), u_ref_tiny);
if (status != 0) {
mexErrMsgIdAndTxt("TinyMPC:SetURefFailed", "tiny_set_u_ref failed with status %d", status);
}
if (verbose) {
mexPrintf("Input reference set\n");
}
}
// Set bound constraints
void set_bound_constraints(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
if (!g_solver) mexErrMsgIdAndTxt("TinyMPC:NotInitialized", "Solver not initialized");
auto x_min = matlab_to_eigen(prhs[0]);
auto x_max = matlab_to_eigen(prhs[1]);
auto u_min = matlab_to_eigen(prhs[2]);
auto u_max = matlab_to_eigen(prhs[3]);
int verbose = (int)mxGetScalar(prhs[4]);
tinyMatrix x_min_tiny = x_min.cast<tinytype>();
tinyMatrix x_max_tiny = x_max.cast<tinytype>();
tinyMatrix u_min_tiny = u_min.cast<tinytype>();
tinyMatrix u_max_tiny = u_max.cast<tinytype>();
int status = tiny_set_bound_constraints(g_solver.get(), x_min_tiny, x_max_tiny, u_min_tiny, u_max_tiny);
if (status != 0) mexErrMsgIdAndTxt("TinyMPC:SetBoundConstraintsFailed", "status %d", status);
// Auto-enable bound constraints after successful setting
g_solver->settings->en_state_bound = 1;
g_solver->settings->en_input_bound = 1;
if (verbose) mexPrintf("Bound constraints set\n");
}
// Solve the MPC problem (matches Python solve)
void solve_mpc(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
if (nrhs != 1) {
mexErrMsgIdAndTxt("TinyMPC:InvalidInput", "solve requires 1 input argument");
}
if (!g_solver) {
mexErrMsgIdAndTxt("TinyMPC:NotInitialized", "Solver not initialized");
}
int verbose = (int)mxGetScalar(prhs[0]);
// Solve the problem (exactly like Python PyTinySolver::solve)
int status = tiny_solve(g_solver.get());
if (verbose) {
mexPrintf("Solve completed with status: %d\n", status);
}
// Return status (always 0 in Python bindings)
plhs[0] = mxCreateDoubleScalar(0);
}
// Get solution
void get_solution(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
if (nrhs != 1) {
mexErrMsgIdAndTxt("TinyMPC:InvalidInput", "get_solution requires 1 input argument");
}
if (!g_solver) {
mexErrMsgIdAndTxt("TinyMPC:NotInitialized", "Solver not in

xinkai1688
- 粉丝: 416
最新资源
- 基于Python_Tornado框架的微信公众号文章智能语音播报与摘要生成系统_集成MySQL数据库存储_TextRank4ZH文本摘要算法_LXML解析库_异步处理技术_支持复制.zip
- 基于Python3调用科大讯飞AIUI的WebAPI接口实现智能语音交互与自然语言处理的开发工具包_封装数据格式解析与响应处理模块_简化后端集成流程_提升开发效率与代码可维护性_适.zip
- 基于Python3开发的配音阁语音合成工具_支持多种音色选择包括中文主播特色语音合成方言主播英文主播童声主播等_通过调用配音阁API实现高质量语音合成功能_适用于有声读物制作视频配.zip
- 基于Python38和pywin32与matplotlib库开发的Excel文件自动化检查工具_支持命令行参数与拖拽操作的批处理脚本_用于快速检测xlsx表格文件的数据完整性与.zip
- 基于Python27的科大讯飞离线语音合成SDK集成项目_实现Linux环境下的本地语音生成功能_支持自定义文本转语音输出_适用于智能硬件语音提示和无网络场景的语音播报应用_技术.zip
- 基于Python3与开源语音识别库构建的智能家居语音控制中心项目_支持讯飞与百度双引擎语音识别与合成技术_实现自定义唤醒词与多设备联动控制_通过SWIG30和Sox音频处理工具集.zip
- 基于Python的语音与文本双向转换工具集_支持实时麦克风输入和音频文件读取的语音识别及文字转语音功能_集成百度API和科大讯飞API的语音转文字服务_采用SpeechRecogn.zip
- 基于Python和OpenCV的实时人脸识别与情绪分析系统_深度学习_卷积神经网络_人脸检测_表情分类_实时视频处理_数据增强_模型训练_性能优化_用户界面设计_用于智能监控_人机.zip
- 基于讯飞语音识别技术的智能语音交互示例项目_语音识别_语音合成_自然语言处理_实时音频流处理_多语言支持_离线识别_云端API调用_语音唤醒_智能对话_语音指令解析_情感分析_语音.zip
- 基于Python和Django框架的智能在线学习平台_提供个性化课程推荐与实时互动教学功能_集成机器学习算法与大数据分析技术_支持视频直播与录播课程管理_包含作业提交与自动批改系统.zip
- 基于Python与LightGBM_XGBoost的科大讯飞AI智能营销算法大赛初赛解决方案_包含特征工程_模型融合与参数调优的完整代码实现_适用于数据挖掘竞赛新手学习参考_技术栈.zip
- 基于Python开发的语音性别与年龄识别系统_讯飞接口交互_语音信号处理_性别判断_年龄预测_音频特征提取_人工智能语音分析_多维度生物特征识别_用于智能客服用户画像构建_个性化服.zip
- 基于讯飞语音识别SDK的Android语音转文字应用开发示例_讯飞开放平台语音识别SDK集成与配置_AndroidStudio开发环境搭建与语音识别功能实现_语音识别结果监听与处.zip
- 基于讯飞语音识别与合成技术的智能聊天机器人客户端项目_语音交互界面设计_多轮对话管理_实时语音转文字_文字转语音播报_Android移动端应用开发_讯飞SDK集成_用户友好界面优化.zip
- 基于讯飞语音识别与合成技术的智能语音交互系统_语音识别_语音合成_自然语言处理_实时转写_多语种支持_离线语音_智能助手_语音控制_音频处理_深度学习_神经网络_API集成_跨平台.zip
- 基于Python与OpenCV的智能视觉识别机器人系统_图像处理_深度学习_目标检测_路径规划_自主导航_多传感器融合_实时控制_ROS框架_嵌入式开发_用于教育研究及工业自动化场.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


