c++调用matlab动态库函数示例
#include "calggyrocaliservice.h"
#include <QDebug>
#include <QMessageBox>
#include <QThread>
#include "processBias4SMI240.h"
#include "processBiasRepeatibility4SMI240.h"
#include "processBiasStability4SMI240.h"
#include "imuCalibrationFunction4INS370_ENU.h"
#define FILEN_LEN 256
#define FILEN_NUM 22
CAlgGyroCaliService::CAlgGyroCaliService(QObject *parent) : QObject(parent)
{
// 初始化 MATLAB 运行时
if (!mclInitializeApplication(NULL, 0)) {
qDebug() << u8"无法初始化 MATLAB 运行时";
return;
}
}
CAlgGyroCaliService::~CAlgGyroCaliService(){
qDebug()<<u8"终止应用";
mclTerminateApplication();
}
void CAlgGyroCaliService::slotCalDataWork(QStringList filenames){
qDebug()<<filenames;
cwbd_gyro_accel_dllInitialize();
char databuf[FILEN_NUM][FILEN_LEN];
for(int i = 0; i < filenames.size() && i < FILEN_NUM; i++){
::memset(databuf[i], 0x00, FILEN_LEN);
::memcpy(databuf[i], filenames.at(i).toUtf8().data(), filenames.at(i).toUtf8().size());
qDebug()<<"hex:"<<filenames.at(i).toUtf8().toHex(' ');
qDebug()<<"test:"<<filenames.at(i).toUtf8().data()<<databuf[i]<<filenames.at(i).length()<<filenames.at(i).toUtf8().size();
}
const char *dbuf[] = {databuf[2], databuf[3], databuf[4], databuf[5], databuf[6], databuf[7], \
databuf[8], databuf[9], databuf[10], databuf[11], databuf[12], databuf[13], \
databuf[14], databuf[15], databuf[16], databuf[17], databuf[0], databuf[1], \
databuf[18], databuf[19], databuf[20], databuf[21]};
qDebug()<<"data:"<<dbuf[21];
QString restr;
mwArray so1;
mwSize isize = 22;
mwArray sma(isize, (const char **)dbuf);
cwbd_gyro_accel_dll (1, so1, sma);
mxChar *buf[20] = {0};
mwSize size = 1;
mwSize len = so1.NumberOfElements();
so1.GetStringData((const mxChar **)buf, &size, &len);
restr = QString::fromStdU16String(*buf);
qDebug()<<restr;
cwbd_gyro_accel_dllTerminate();
emit sigCalDataResult(restr);
}
void CAlgGyroCaliService::slotCalDataWork01(QString filepath, uint freq, int aix){
switch (aix) {
case 1:
calcalibrationNUE(filepath, freq);
break;
case 0:
default:
calcalibrationENU(filepath, freq);
break;
}
}
//东北天
void CAlgGyroCaliService::calcalibrationENU(QString filepath, uint freq){
// 初始化生成的库
if (!imuCalibrationFunction4INS370_ENUInitialize()) {
qDebug() << u8"无法初始化 imuCalibrationFunction4INS370 库";
return;
}
// 创建输入参数
mwArray fileFolderPath(filepath.toUtf8().data()); // 文件路径
mwArray sampleFreq(freq); // 采样频率
// 创建输出参数
mwArray gyroBias;
mwArray accBias;
mwArray accInstallMatrix;
mwArray gyroInstallMatrix;
// 调用函数
//imuCalibrationFunction4INS370_new(fileFolderPath, sampleFreq);
imuCalibrationFunction4INS370_ENU(4, gyroBias, accBias, accInstallMatrix, gyroInstallMatrix, fileFolderPath, sampleFreq);
// 输出结果
//std::cout << "Gyro Bias:\n"<<gyroBias.NumberOfDimensions() << gyroBias << std::endl;
//std::cout << "Acc Bias:\n" << accBias.NumberOfDimensions() << accBias << std::endl;
//std::cout << "Acc Install Matrix:\n" << accInstallMatrix.NumberOfDimensions() << accInstallMatrix<< std::endl;
//std::cout << "Gyro Install Matrix:\n" << gyroInstallMatrix.NumberOfDimensions() <<gyroInstallMatrix<< std::endl;
//std::cout<<"Other:"<<accInstallMatrix.GetDimensions().NumberOfDimensions()<<accInstallMatrix.GetDimensions()<<gyroBias.GetDimensions()<<gyroBias.ElementSize()<<gyroBias.NumberOfElements()<<std::endl;
//std::cout<<"Type:"<<accInstallMatrix.ClassID()<<gyroBias.ClassID()<<gyroBias.ToString()<<std::endl;
QVector<double> vgyroBias;
QVector<double> vaccBias;
if(gyroInstallMatrix.NumberOfElements() == 9 && accInstallMatrix.NumberOfElements() == 9){
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 3));
vgyroBias.append(gyroInstallMatrix.Get(2, 2, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 2, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 2, 3));
vgyroBias.append(gyroInstallMatrix.Get(2, 3, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 3, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 3, 3));
vaccBias.append(accInstallMatrix.Get(2, 1, 1));
vaccBias.append(accInstallMatrix.Get(2, 1, 2));
vaccBias.append(accInstallMatrix.Get(2, 1, 3));
vaccBias.append(accInstallMatrix.Get(2, 2, 1));
vaccBias.append(accInstallMatrix.Get(2, 2, 2));
vaccBias.append(accInstallMatrix.Get(2, 2, 3));
vaccBias.append(accInstallMatrix.Get(2, 3, 1));
vaccBias.append(accInstallMatrix.Get(2, 3, 2));
vaccBias.append(accInstallMatrix.Get(2, 3, 3));
}
if(gyroBias.NumberOfElements() == 3){
vgyroBias.append(gyroBias.Get(1, 1));
vgyroBias.append(gyroBias.Get(1, 2));
vgyroBias.append(gyroBias.Get(1, 3));
vaccBias.append(accBias.Get(1, 1));
vaccBias.append(accBias.Get(1, 2));
vaccBias.append(accBias.Get(1, 3));
}
//qDebug()<<"vgyroBias:"<<vgyroBias;
//qDebug()<<"vaccBias:"<<vaccBias;
// 清理
imuCalibrationFunction4INS370_ENUTerminate();
emit sigCalDataResult01(vgyroBias, vaccBias);
}
//北天东
void CAlgGyroCaliService::calcalibrationNUE(QString filepath, uint freq){
// 初始化生成的库
if (!imuCalibrationFunction4INS370Initialize()) {
qDebug() << u8"无法初始化 imuCalibrationFunction4INS370 库";
return;
}
// 创建输入参数
mwArray fileFolderPath(filepath.toUtf8().data()); // 文件路径
mwArray sampleFreq(freq); // 采样频率
// 创建输出参数
mwArray gyroBias;
mwArray accBias;
mwArray accInstallMatrix;
mwArray gyroInstallMatrix;
// 调用函数
//imuCalibrationFunction4INS370_new(fileFolderPath, sampleFreq);
imuCalibrationFunction4INS370(4, gyroBias, accBias, accInstallMatrix, gyroInstallMatrix, fileFolderPath, sampleFreq);
// 输出结果
//std::cout << "Gyro Bias:\n"<<gyroBias.NumberOfDimensions() << gyroBias << std::endl;
//std::cout << "Acc Bias:\n" << accBias.NumberOfDimensions() << accBias << std::endl;
//std::cout << "Acc Install Matrix:\n" << accInstallMatrix.NumberOfDimensions() << accInstallMatrix<< std::endl;
//std::cout << "Gyro Install Matrix:\n" << gyroInstallMatrix.NumberOfDimensions() <<gyroInstallMatrix<< std::endl;
//std::cout<<"Other:"<<accInstallMatrix.GetDimensions().NumberOfDimensions()<<accInstallMatrix.GetDimensions()<<gyroBias.GetDimensions()<<gyroBias.ElementSize()<<gyroBias.NumberOfElements()<<std::endl;
//std::cout<<"Type:"<<accInstallMatrix.ClassID()<<gyroBias.ClassID()<<gyroBias.ToString()<<std::endl;
QVector<double> vgyroBias;
QVector<double> vaccBias;
if(gyroInstallMatrix.NumberOfElements() == 9 && accInstallMatrix.NumberOfElements() == 9){
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 3));
vgyroBias.append(gyroInstallMatrix.Get(2, 2, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 2, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 2, 3));
vgyroBias.append(gyroInstallMatrix.Get(2, 3, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 3, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 3, 3));
vaccBias.append(accInstallMatrix.Get(2, 1, 1));
vaccBias.append(accInstallMatrix.Get(2, 1, 2));
vaccBias.append(accInstallMatrix.Get(2, 1, 3));
vaccBias.append(accInstallMatrix.Get(2, 2, 1));
vaccBias.append(accInstallMatrix.Get(2, 2, 2));
vaccBias.append(accInstallMatrix.Get(2, 2, 3));
vaccBias.append(accInstallMatrix.Get(2, 3, 1));
vaccBias.append(accInstallMatrix.Get(2, 3, 2));
vaccBias.append(accInstallMatrix.Get(2, 3, 3));
}
if(gyroBias.NumberOfElements() == 3){
vgyroBias.append(gyroBias.Get(1, 1));
vgyroBias.append(gyroBias.Get(1, 2));
vgyroBias.append(gyroBias.Get(1, 3));
vaccBias.append(accBias.Get(1, 1));
vaccBias.append(accBias.Get(1, 2));
vaccBias.append(accBias.Get(1, 3));
}
//qDebug()<<"vgyroBias:"<<vgyroBias;
//qDebug()<<"vaccBias:"<<vaccBias;
// 清理
imuCalibrationFunction4INS370Terminate();
emit sigCalDataResult01(vgyroBias, vaccBias);
}
void CAlgGyroCaliService::slotCalZeroBiaswork(QString filepath, int caltype, int freq){
qDebug()<<"slotCalZeroBiaswork"<<caltype;
try {
switch(caltype){
case 0:
calZeroBiaswork(filepath, freq);
break;
case 1:
calZBiasStawork(filepath, freq);
break;
case 2:
calZBiasRepwork(filepath, freq);
default:
break;
}
} catch (std::exception e) {
qDebug()<<e.what();
}
}
void CAlgGyroCaliService::calZeroBiaswork(QString filepath, int freq){
qDebug()<<"calZeroBiaswork:"<<filepath;
if (!processBias4SMI240Initialize()) {
qDebug() << u8"无法初始化 imuCalibrationFunction4INS370 库";
return;
}
qDebug()<<"processBias4SMI240Initialize"<<filepath;
QVector<double> vgyroBias;
QVector<double> vaccBias;
mwArray accInstallMatrix;
mwArray gyroInstallMatrix;
mwArray fileFolderPath(filepath.toUtf8().data()); // 示例文件路径
qDebug()<<"processBias4SMI240";
processBias4SMI240(2, gyroInstallMatrix, accInstallMatrix, fileFolderPath);
qDebug()<<"processBias4SMI240 end";
if(gyroInstallMatrix.NumberOfElements() == 3 && accInstallMatrix.NumberOfElements() == 3){
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 3));
vaccBias.append(accInstallMatrix.Get(2, 1, 1));
vaccBias.append(accInstallMatrix.Get(2, 1, 2));
vaccBias.append(accInstallMatrix.Get(2, 1, 3));
}
processBias4SMI240Terminate();
emit sigCalZeroBiaswork(vgyroBias, vaccBias, 0);
}
void CAlgGyroCaliService::calZBiasStawork(QString filepath, int freq){
if (!processBiasStability4SMI240Initialize()) {
qDebug() << u8"无法初始化 imuCalibrationFunction4INS370 库";
return;
}
QVector<double> vgyroBias;
QVector<double> vaccBias;
mwArray accInstallMatrix;
mwArray gyroInstallMatrix;
mwArray fileFolderPath(filepath.toUtf8().data()); // 文件路径
mwArray sampleFreq(200.0);
qDebug()<<"calZBiasStawork:"<<freq;
processBiasStability4SMI240(2, gyroInstallMatrix, accInstallMatrix, fileFolderPath, sampleFreq);
if(gyroInstallMatrix.NumberOfElements() == 3 && accInstallMatrix.NumberOfElements() == 3){
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 3));
vaccBias.append(accInstallMatrix.Get(2, 1, 1));
vaccBias.append(accInstallMatrix.Get(2, 1, 2));
vaccBias.append(accInstallMatrix.Get(2, 1, 3));
}
processBiasStability4SMI240Terminate();
emit sigCalZeroBiaswork(vgyroBias, vaccBias, 1);
}
void CAlgGyroCaliService::calZBiasRepwork(QString filepath, int freq){
if (!processBiasRepeatibility4SMI240Initialize()) {
qDebug() << u8"无法初始化 imuCalibrationFunction4INS370 库";
return;
}
QVector<double> vgyroBias;
QVector<double> vaccBias;
mwArray accInstallMatrix;
mwArray gyroInstallMatrix;
mwArray fileFolderPath(filepath.toUtf8().data()); // 文件路径
mwArray sampleFreq(freq);
processBiasRepeatibility4SMI240(2, gyroInstallMatrix, accInstallMatrix, fileFolderPath, sampleFreq);
if(gyroInstallMatrix.NumberOfElements() == 3 && accInstallMatrix.NumberOfElements() == 3){
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 1));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 2));
vgyroBias.append(gyroInstallMatrix.Get(2, 1, 3));
vaccBias.append(accInstallMatrix.Get(2, 1, 1));
vaccBias.append(accInstallMatrix.Get(2, 1, 2));
vaccBias.append(accInstallMatrix.Get(2, 1, 3));
}
processBiasRepeatibility4SMI240Terminate();
emit sigCalZeroBiaswork(vgyroBias, vaccBias, 2);
}