1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

Patch from Kenn for the matlab processing function to fix issue with multiple

isntance objects
This commit is contained in:
James Cotton 2011-11-09 12:49:50 -06:00
parent 813ac65159
commit 6fd2426a90
3 changed files with 125 additions and 85 deletions

View File

@ -1,58 +1,65 @@
function [] = OPLogConvert()
%% Define indices and arrays of structures to hold data
%% Define indices and arrays of structures to hold data
% THIS FILE IS AUTOMATICALLY GENERATED.
$(ALLOCATIONCODE)
%% Open file
%fid = fopen('log.opl');
[FileName,PathName,FilterIndex] = uigetfile('*.opl');
logfile = strcat(PathName,FileName);
fid = fopen(logfile);
while (1)
%% Read logging header
timestamp = fread(fid, 1, 'uint32');
if (feof(fid)); break; end
datasize = fread(fid, 1, 'int64');
%% Read message header
% get sync field (0x3C, 1 byte)
sync = fread(fid, 1, 'uint8');
if sync ~= hex2dec('3C')
disp ('Wrong sync byte');
return
end
% get msg type (quint8 1 byte ) should be 0x20, ignore the rest?
msgType = fread(fid, 1, 'uint8');
if msgType ~= hex2dec('20')
disp ('Wrong msgType');
return
end
% get msg size (quint16 2 bytes) excludes crc, include msg header and data payload
msgSize = fread(fid, 1, 'uint16');
% get obj id (quint32 4 bytes)
objID = fread(fid, 1, 'uint32');
%% Read object
switch objID
$(SWITCHCODE)
otherwise
disp('Unknown object ID');
msgBytesLeft = datasize - 1 - 1 - 2 - 4;
fread(fid, msgBytesLeft, 'uint8');
end
end
%% Clean Up and Save mat file
fclose(fid);
matfile = strrep(logfile,'opl','mat');
save(matfile $(SAVEOBJECTSCODE));
%% Open log file
% [FileName,PathName,FilterIndex] = uigetfile('*.opl');
FileName='OP-2011-10-22_22-27-09.opl';
PathName='/Users/kenz/Movies/brisk_videos/rover_test_1/';
FilterIndex=1;
logfile = fullfile(PathName,FileName);
fid = fopen(logfile);
% Parse log file, entry by entry
while (1)
%% Read logging header
timestamp = fread(fid, 1, 'uint32');
if (feof(fid)); break; end
datasize = fread(fid, 1, 'int64');
%% Read message header
% get sync field (0x3C, 1 byte)
sync = fread(fid, 1, 'uint8');
if sync ~= hex2dec('3C')
disp ('Wrong sync byte');
return
end
% get msg type (quint8 1 byte ) should be 0x20, ignore the rest?
msgType = fread(fid, 1, 'uint8');
if msgType ~= hex2dec('20')
disp ('Wrong msgType');
return
end
% get msg size (quint16 2 bytes) excludes crc, include msg header and data payload
msgSize = fread(fid, 1, 'uint16');
% get obj id (quint32 4 bytes)
objID = fread(fid, 1, 'uint32');
%% Read object
switch objID
$(SWITCHCODE)
otherwise
disp(['Unknown object ID: 0x' dec2hex(objID)]);
msgBytesLeft = datasize - 1 - 1 - 2 - 4;
fread(fid, msgBytesLeft, 'uint8');
end
end
%% Clean Up and Save mat file
fclose(fid);
matfile = strrep(logfile,'opl','mat');
save(matfile $(SAVEOBJECTSCODE));
%% Object reading functions
$(FUNCTIONSCODE)

View File

@ -50,7 +50,7 @@ bool UAVObjectGeneratorMatlab::generate(UAVObjectParser* parser,QString template
}
matlabCodeTemplate.replace( QString("$(ALLOCATIONCODE)"), matlabAllocationCode);
matlabCodeTemplate.replace( QString("$(SWITCHCODE)"), matlabSwithCode);
matlabCodeTemplate.replace( QString("$(SWITCHCODE)"), matlabSwitchCode);
matlabCodeTemplate.replace( QString("$(SAVEOBJECTSCODE)"), matlabSaveObjectsCode);
matlabCodeTemplate.replace( QString("$(FUNCTIONSCODE)"), matlabFunctionsCode);
@ -71,6 +71,7 @@ bool UAVObjectGeneratorMatlab::process_object(ObjectInfo* info)
if (info == NULL)
return false;
//Declare variables
QString objectName(info->name);
// QString objectTableName(objectName + "Objects");
QString objectTableName(objectName);
@ -80,39 +81,71 @@ bool UAVObjectGeneratorMatlab::process_object(ObjectInfo* info)
QString objectID(QString().setNum(info->id));
QString isSingleInst = boolTo01String( info->isSingleInst );
// Generate allocation code (will replace the $(ALLOCATIONCODE) tag)
matlabAllocationCode.append("\n " + tableIdxName + " = 1;\n");
matlabAllocationCode.append(" " + objectTableName + ".timestamp = 0;\n");
QString type;
QString allocfields;
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(" " + objectTableName + "(1)." + info->fields[n]->name + " = zeros(1," + QString::number(info->fields[n]->numElements, 10) + ");\n");
else
allocfields.append(" " + objectTableName + "(1)." + info->fields[n]->name + " = 0;\n");
}
//===================================================================//
// Generate allocation code (will replace the $(ALLOCATIONCODE) tag) //
//===================================================================//
// matlabSwitchCode.append("\t\tcase " + objectID + "\n");
matlabAllocationCode.append("\n\t" + tableIdxName + " = 1;\n");
QString type;
QString allocfields;
if (0){
matlabAllocationCode.append("\t" + objectTableName + ".timestamp = 0;\n");
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("\t" + objectTableName + "(1)." + info->fields[n]->name + " = zeros(1," + QString::number(info->fields[n]->numElements, 10) + ");\n");
else
allocfields.append("\t" + objectTableName + "(1)." + info->fields[n]->name + " = 0;\n");
}
}
else{
matlabAllocationCode.append("\t" + objectTableName + "=struct('timestamp', 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(1," + QString::number(info->fields[n]->numElements, 10) + ")");
else
allocfields.append(",...\n\t\t '" + info->fields[n]->name + "', 0");
}
allocfields.append(");\n");
}
matlabAllocationCode.append(allocfields);
matlabAllocationCode.append("\t" + objectTableName.toUpper() + "_OBJID=" + objectID + ";\n");
// Generate 'swith:' code (will replace the $(SWITCHCODE) tag)
matlabSwithCode.append(" case " + objectID + "\n");
matlabSwithCode.append(" " + objectTableName + "(" + tableIdxName +") = " + functionCall + ";\n");
matlabSwithCode.append(" " + tableIdxName + " = " + tableIdxName +" + 1;\n");
// Generate objects saving code code (will replace the $(SAVEOBJECTSCODE) tag)
//=============================================================//
// Generate 'Switch:' code (will replace the $(SWITCHCODE) tag) //
//=============================================================//
matlabSwitchCode.append("\t\tcase " + objectTableName.toUpper() + "_OBJID\n");
matlabSwitchCode.append("\t\t\t" + objectTableName + "(" + tableIdxName +") = " + functionCall + ";\n");
matlabSwitchCode.append("\t\t\t" + tableIdxName + " = " + tableIdxName +" + 1;\n");
//=============================================================================//
// Generate objects saving code code (will replace the $(SAVEOBJECTSCODE) tag) //
//=============================================================================//
matlabSaveObjectsCode.append(",'"+objectTableName+"'");
// Generate functions code (will replace the $(FUNCTIONSCODE) tag)
matlabFunctionsCode.append("%%\n% " + objectName + " read function\n");
//=================================================================//
// Generate functions code (will replace the $(FUNCTIONSCODE) tag) //
//=================================================================//
//Generate function description comment
matlabFunctionsCode.append("function [" + objectName + "] = " + functionCall + "\n");
matlabFunctionsCode.append(" if " + isSingleInst + "\n");
matlabFunctionsCode.append(" headerSize = 8;\n");
matlabFunctionsCode.append(" else\n");
matlabFunctionsCode.append(" " + objectName + ".instanceID = fread(fid, 1, 'uint16');\n");
matlabFunctionsCode.append(" headerSize = 10;\n");
matlabFunctionsCode.append(" end\n\n");
matlabFunctionsCode.append(" " + objectName + ".timestamp = timestamp;\n");
matlabFunctionsCode.append("\tif " + isSingleInst + "\n");
matlabFunctionsCode.append("\t\theaderSize = 8;\n");
matlabFunctionsCode.append("\telse\n");
matlabFunctionsCode.append("\t\t" + objectName + ".instanceID = fread(fid, 1, 'uint16');\n");
matlabFunctionsCode.append("\t\theaderSize = 10;\n");
matlabFunctionsCode.append("\tend\n\n");
matlabFunctionsCode.append("\t" + objectName + ".timestamp = timestamp;\n");
// Generate functions code, actual fields of the object
QString funcfields;
@ -122,16 +155,16 @@ bool UAVObjectGeneratorMatlab::process_object(ObjectInfo* info)
type = fieldTypeStrMatlab[info->fields[n]->type];
// Append field
if ( info->fields[n]->numElements > 1 )
funcfields.append(" " + objectName + "." + info->fields[n]->name + " = double(fread(fid, " + QString::number(info->fields[n]->numElements, 10) + ", '" + type + "'));\n");
funcfields.append("\t" + objectName + "." + info->fields[n]->name + " = double(fread(fid, " + QString::number(info->fields[n]->numElements, 10) + ", '" + type + "'));\n");
else
funcfields.append(" " + objectName + "." + info->fields[n]->name + " = double(fread(fid, 1, '" + type + "'));\n");
funcfields.append("\t" + objectName + "." + info->fields[n]->name + " = double(fread(fid, 1, '" + type + "'));\n");
}
matlabFunctionsCode.append(funcfields);
matlabFunctionsCode.append(" % read CRC\n");
matlabFunctionsCode.append(" fread(fid, 1, 'uint8');\n");
matlabFunctionsCode.append("\t% read CRC\n");
matlabFunctionsCode.append("\tfread(fid, 1, 'uint8');\n");
matlabFunctionsCode.append("end\n\n");
matlabFunctionsCode.append("\n\n");
return true;
}

View File

@ -37,7 +37,7 @@ public:
private:
bool process_object(ObjectInfo* info);
QString matlabAllocationCode;
QString matlabSwithCode;
QString matlabSwitchCode;
QString matlabSaveObjectsCode;
QString matlabFunctionsCode;
QStringList fieldTypeStrMatlab;