2011-01-13 03:26:00 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file uavobjectgeneratormatlab.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief produce matlab code for uavobjects
|
|
|
|
*
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uavobjectgeneratormatlab.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2011-01-22 18:38:43 +01:00
|
|
|
bool UAVObjectGeneratorMatlab::generate(UAVObjectParser* parser,QString templatepath,QString outputpath) {
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
fieldTypeStrMatlab << "int8" << "int16" << "int32"
|
2012-08-16 00:13:19 +02:00
|
|
|
<< "uint8" << "uint16" << "uint32" << "single" << "uint8";
|
|
|
|
fieldSizeStrMatlab << "1" << "2" << "4"
|
|
|
|
<< "1" << "2" << "4" << "4" << "1";
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2011-01-23 21:06:56 +01:00
|
|
|
QDir matlabTemplatePath = QDir( templatepath + QString("ground/openpilotgcs/src/plugins/uavobjects"));
|
2011-01-22 18:38:43 +01:00
|
|
|
QDir matlabOutputPath = QDir( outputpath + QString("matlab") );
|
|
|
|
matlabOutputPath.mkpath(matlabOutputPath.absolutePath());
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
QString matlabCodeTemplate = readFile( matlabTemplatePath.absoluteFilePath( "uavobjecttemplate.m") );
|
|
|
|
|
|
|
|
if (matlabCodeTemplate.isEmpty() ) {
|
|
|
|
std::cerr << "Problem reading matlab templates" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int objidx = 0; objidx < parser->getNumObjects(); ++objidx) {
|
|
|
|
ObjectInfo* info=parser->getObjectByIndex(objidx);
|
2012-08-16 00:13:19 +02:00
|
|
|
int numBytes=parser->getNumBytes(objidx);
|
|
|
|
process_object(info, numBytes);
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
matlabCodeTemplate.replace( QString("$(INSTANTIATIONCODE)"), matlabInstantiationCode);
|
2011-11-09 19:49:50 +01:00
|
|
|
matlabCodeTemplate.replace( QString("$(SWITCHCODE)"), matlabSwitchCode);
|
2011-12-16 22:26:56 +01:00
|
|
|
matlabCodeTemplate.replace( QString("$(CLEANUPCODE)"), matlabCleanupCode);
|
2011-01-13 03:26:00 +01:00
|
|
|
matlabCodeTemplate.replace( QString("$(SAVEOBJECTSCODE)"), matlabSaveObjectsCode);
|
2012-08-16 08:37:26 +02:00
|
|
|
matlabCodeTemplate.replace( QString("$(ALLOCATIONCODE)"), matlabAllocationCode);
|
2012-01-01 20:14:18 +01:00
|
|
|
matlabCodeTemplate.replace( QString("$(EXPORTCSVCODE)"), matlabExportCsvCode);
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2011-01-22 18:38:30 +01:00
|
|
|
bool res = writeFile( matlabOutputPath.absolutePath() + "/OPLogConvert.m", matlabCodeTemplate );
|
2011-01-13 03:26:00 +01:00
|
|
|
if (!res) {
|
|
|
|
cout << "Error: Could not write output files" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true; // if we come here everything should be fine
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the matlab object files
|
|
|
|
*/
|
2012-08-16 00:13:19 +02:00
|
|
|
bool UAVObjectGeneratorMatlab::process_object(ObjectInfo* info, int numBytes)
|
2011-01-13 03:26:00 +01:00
|
|
|
{
|
|
|
|
if (info == NULL)
|
|
|
|
return false;
|
|
|
|
|
2011-11-09 19:49:50 +01:00
|
|
|
//Declare variables
|
2011-01-13 03:26:00 +01:00
|
|
|
QString objectName(info->name);
|
|
|
|
// QString objectTableName(objectName + "Objects");
|
|
|
|
QString objectTableName(objectName);
|
|
|
|
QString tableIdxName(objectName.toLower() + "Idx");
|
|
|
|
QString objectID(QString().setNum(info->id));
|
2012-08-16 00:13:19 +02:00
|
|
|
QString numBytesString=QString("%1").arg(numBytes);
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
|
2011-11-09 19:49:50 +01:00
|
|
|
//===================================================================//
|
|
|
|
// Generate allocation code (will replace the $(ALLOCATIONCODE) tag) //
|
|
|
|
//===================================================================//
|
2012-08-16 08:37:26 +02:00
|
|
|
QString type;
|
|
|
|
QString allocfields;
|
|
|
|
|
|
|
|
matlabInstantiationCode.append("\n\t" + tableIdxName + " = 0;\n");
|
|
|
|
matlabInstantiationCode.append("\t" + objectTableName + "=struct('timestamp', 0");
|
|
|
|
if (!info->isSingleInst) {
|
|
|
|
allocfields.append(",...\n\t\t 'instanceID', 0");
|
|
|
|
}
|
|
|
|
for (int n = 0; n < info->fields.length(); ++n) {
|
|
|
|
// Determine type
|
|
|
|
type = fieldTypeStrMatlab[info->fields[n]->type];
|
|
|
|
// Append field
|
|
|
|
if ( info->fields[n]->numElements > 1 )
|
|
|
|
allocfields.append(",...\n\t\t '" + info->fields[n]->name + "', zeros(" + QString::number(info->fields[n]->numElements, 10) + ",1)");
|
|
|
|
else
|
|
|
|
allocfields.append(",...\n\t\t '" + info->fields[n]->name + "', 0");
|
2012-01-01 20:14:18 +01:00
|
|
|
}
|
2012-08-16 08:37:26 +02:00
|
|
|
allocfields.append(");\n");
|
2012-08-16 00:13:19 +02:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
matlabInstantiationCode.append(allocfields);
|
|
|
|
matlabInstantiationCode.append("\t" + objectTableName.toUpper() + "_OBJID=" + objectID + ";\n");
|
|
|
|
matlabInstantiationCode.append("\t" + objectTableName.toUpper() + "_NUMBYTES=" + numBytesString + ";\n");
|
|
|
|
matlabInstantiationCode.append("\t" + objectName + "FidIdx = [];\n");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
|
2012-01-01 20:14:18 +01:00
|
|
|
//==============================================================//
|
2011-11-09 19:49:50 +01:00
|
|
|
// Generate 'Switch:' code (will replace the $(SWITCHCODE) tag) //
|
2012-01-01 20:14:18 +01:00
|
|
|
//==============================================================//
|
2011-11-09 19:49:50 +01:00
|
|
|
matlabSwitchCode.append("\t\tcase " + objectTableName.toUpper() + "_OBJID\n");
|
2012-08-16 00:13:19 +02:00
|
|
|
matlabSwitchCode.append("\t\t\t" + tableIdxName + " = " + tableIdxName +" + 1;\n");
|
2012-08-16 08:37:26 +02:00
|
|
|
matlabSwitchCode.append("\t\t\t" + objectTableName + "FidIdx(" + tableIdxName + ") = bufferIdx; %#ok<*AGROW>\n");
|
|
|
|
matlabSwitchCode.append("\t\t\tbufferIdx=bufferIdx + " + objectTableName.toUpper() + "_NUMBYTES+1; %+1 is for CRC\n");
|
|
|
|
if(!info->isSingleInst){
|
|
|
|
matlabSwitchCode.append("\t\t\tbufferIdx = bufferIdx + 2; %An extra two bytes for the instance ID\n");
|
2012-08-16 00:13:19 +02:00
|
|
|
}
|
2012-08-16 08:37:26 +02:00
|
|
|
matlabSwitchCode.append("\t\t\tif " + tableIdxName + " >= length(" + objectTableName +"FidIdx) %Check to see if pre-allocated memory is exhausted\n");
|
|
|
|
matlabSwitchCode.append("\t\t\t\t" + objectTableName + "FidIdx(" + tableIdxName + "*2) = 0;\n");
|
|
|
|
matlabSwitchCode.append("\t\t\tend\n");
|
2012-01-01 20:14:18 +01:00
|
|
|
|
2011-11-09 19:49:50 +01:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
//============================================================//
|
|
|
|
// Generate 'Cleanup:' code (will replace the $(CLEANUP) tag) //
|
|
|
|
//============================================================//
|
|
|
|
matlabCleanupCode.append(objectTableName + "FidIdx =" + objectTableName + "FidIdx(1:" + tableIdxName +");\n" );
|
2011-11-09 19:49:50 +01:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
//=================================================================//
|
|
|
|
// Generate functions code (will replace the $(ALLOCATIONCODE) tag) //
|
2011-11-09 19:49:50 +01:00
|
|
|
//=================================================================//
|
2012-01-01 20:14:18 +01:00
|
|
|
//Generate function description comment
|
2012-08-16 08:37:26 +02:00
|
|
|
matlabAllocationCode.append("% " + objectName + " typecasting\n");
|
|
|
|
QString allocationFields;
|
2012-08-16 00:13:19 +02:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
//Add timestamp
|
|
|
|
allocationFields.append("\t" + objectName + ".timestamp = " +
|
|
|
|
"double(typecast(buffer(mcolon(" + objectName + "FidIdx "
|
|
|
|
"- 20, " + objectName + "FidIdx + 4-1 -20)), 'uint32'))';\n");
|
2012-08-16 00:13:19 +02:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
int currentIdx=0;
|
2012-08-16 00:13:19 +02:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
//Add Instance ID, if necessary
|
|
|
|
if(!info->isSingleInst){
|
|
|
|
allocationFields.append("\t" + objectName + ".instanceID = " +
|
|
|
|
"double(typecast(buffer(mcolon(" + objectName + "FidIdx "
|
|
|
|
", " + objectName + "FidIdx + 2-1)), 'uint16'))';\n");
|
|
|
|
currentIdx+=2;
|
|
|
|
}
|
2012-08-16 00:13:19 +02:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
for (int n = 0; n < info->fields.length(); ++n) {
|
|
|
|
// Determine variabel type
|
|
|
|
type = fieldTypeStrMatlab[info->fields[n]->type];
|
|
|
|
|
|
|
|
//Determine variable type length
|
|
|
|
QString size = fieldSizeStrMatlab[info->fields[n]->type];
|
|
|
|
// Append field
|
|
|
|
if ( info->fields[n]->numElements > 1 ){
|
|
|
|
allocationFields.append("\t" + objectName + "." + info->fields[n]->name + " = " +
|
|
|
|
"reshape(double(typecast(buffer(mcolon(" + objectName + "FidIdx + " + QString("%1").arg(currentIdx) +
|
|
|
|
", " + objectName + "FidIdx + " + QString("%1").arg(currentIdx + size.toInt()*info->fields[n]->numElements - 1) + ")), '" + type + "')), "+ QString::number(info->fields[n]->numElements, 10) + ", [] );\n");
|
2012-08-16 00:13:19 +02:00
|
|
|
}
|
2012-08-16 08:37:26 +02:00
|
|
|
else{
|
|
|
|
allocationFields.append("\t" + objectName + "." + info->fields[n]->name + " = " +
|
|
|
|
"double(typecast(buffer(mcolon(" + objectName + "FidIdx + " + QString("%1").arg(currentIdx) +
|
|
|
|
", " + objectName + "FidIdx + " + QString("%1").arg(currentIdx + size.toInt() - 1) + ")), '" + type + "'))';\n");
|
|
|
|
}
|
|
|
|
currentIdx+=size.toInt()*info->fields[n]->numElements;
|
2011-01-13 03:26:00 +01:00
|
|
|
}
|
2012-08-16 08:37:26 +02:00
|
|
|
matlabAllocationCode.append(allocationFields);
|
|
|
|
matlabAllocationCode.append("\n");
|
2011-01-13 03:26:00 +01:00
|
|
|
|
2012-02-09 19:40:00 +01:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
//========================================================================//
|
|
|
|
// Generate objects saving code (will replace the $(SAVEOBJECTSCODE) tag) //
|
|
|
|
//========================================================================//
|
|
|
|
matlabSaveObjectsCode.append(",'"+objectTableName+"'");
|
2012-02-09 19:40:00 +01:00
|
|
|
|
2012-08-16 00:13:19 +02:00
|
|
|
|
2012-08-16 08:37:26 +02:00
|
|
|
//==========================================================================//
|
|
|
|
// Generate objects csv export code (will replace the $(EXPORTCSVCODE) tag) //
|
|
|
|
//==========================================================================//
|
|
|
|
matlabExportCsvCode.append("\tOPLog2csv(" + objectTableName + ", '"+objectTableName+"', logfile);\n");
|
|
|
|
// OPLog2csv(ActuatorCommand, 'ActuatorCommand', logfile)
|
2012-08-16 00:13:19 +02:00
|
|
|
|
2011-01-13 03:26:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|