2017-02-09 17:48:50 +00:00
|
|
|
#include "RobotLoggingUtil.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "LinearMath/btAlignedObjectArray.h"
|
|
|
|
|
|
|
|
#include "../Importers/ImportURDFDemo/urdfStringSplit.h"
|
|
|
|
|
|
|
|
static bool readLine(FILE* file, btAlignedObjectArray<char>& line)
|
|
|
|
{
|
|
|
|
int c = 0;
|
2018-09-23 21:17:31 +00:00
|
|
|
for (c = fgetc(file); (c != EOF && c != '\n'); c = fgetc(file))
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
|
|
|
line.push_back(c);
|
|
|
|
}
|
|
|
|
line.push_back(0);
|
|
|
|
return (c == EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
int readMinitaurLogFile(const char* fileName, btAlignedObjectArray<std::string>& structNames, std::string& structTypes, btAlignedObjectArray<MinitaurLogRecord>& logRecords, bool verbose)
|
|
|
|
{
|
|
|
|
int retVal = 0;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
FILE* f = fopen(fileName, "rb");
|
2017-02-09 17:48:50 +00:00
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("Opened file %s\n", fileName);
|
|
|
|
}
|
|
|
|
btAlignedObjectArray<char> line0Buf;
|
2018-09-23 21:17:31 +00:00
|
|
|
bool eof = readLine(f, line0Buf);
|
2017-02-09 17:48:50 +00:00
|
|
|
btAlignedObjectArray<char> line1Buf;
|
2018-09-23 21:17:31 +00:00
|
|
|
eof |= readLine(f, line1Buf);
|
2017-02-09 17:48:50 +00:00
|
|
|
std::string line0 = &line0Buf[0];
|
|
|
|
structTypes = &line1Buf[0];
|
2018-09-23 21:17:31 +00:00
|
|
|
|
2017-02-09 17:48:50 +00:00
|
|
|
btAlignedObjectArray<std::string> separators;
|
|
|
|
separators.push_back(",");
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
urdfStringSplit(structNames, line0, separators);
|
2017-02-09 17:48:50 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
printf("Num Fields = %d\n", structNames.size());
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
|
|
|
btAssert(structTypes.size() == structNames.size());
|
|
|
|
if (structTypes.size() != structNames.size())
|
|
|
|
{
|
|
|
|
retVal = eCorruptHeader;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
int numStructsRead = 0;
|
2017-02-09 17:48:50 +00:00
|
|
|
|
|
|
|
if (structTypes.size() == structNames.size())
|
|
|
|
{
|
|
|
|
while (!eof)
|
|
|
|
{
|
|
|
|
unsigned char blaat[1024];
|
2018-09-23 21:17:31 +00:00
|
|
|
size_t s = fread(blaat, 2, 1, f);
|
|
|
|
if (s != 1)
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
eof = true;
|
2017-02-09 17:48:50 +00:00
|
|
|
retVal = eInvalidAABBAlignCheck;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((blaat[0] != 0xaa) || (blaat[1] != 0xbb))
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("Expected 0xaa0xbb, terminating\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
printf("Reading structure %d\n", numStructsRead);
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
|
|
|
MinitaurLogRecord record;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < structNames.size(); i++)
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
|
|
|
switch (structTypes[i])
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
case 'I':
|
2017-04-12 22:02:47 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
size_t s = fread(blaat, sizeof(int), 1, f);
|
|
|
|
if (s != 1)
|
|
|
|
{
|
|
|
|
eof = true;
|
|
|
|
retVal = eCorruptValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int v = (int)*(unsigned int*)blaat;
|
|
|
|
if (s == 1)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("%s = %d\n", structNames[i].c_str(), v);
|
|
|
|
}
|
|
|
|
record.m_values.push_back(v);
|
|
|
|
}
|
2017-04-12 22:02:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
case 'i':
|
2017-04-12 22:02:47 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
size_t s = fread(blaat, sizeof(int), 1, f);
|
|
|
|
if (s != 1)
|
2017-04-12 22:02:47 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
eof = true;
|
|
|
|
retVal = eCorruptValue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
int v = *(int*)blaat;
|
|
|
|
if (s == 1)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("%s = %d\n", structNames[i].c_str(), v);
|
|
|
|
}
|
|
|
|
record.m_values.push_back(v);
|
2017-04-12 22:02:47 +00:00
|
|
|
}
|
2017-02-09 17:48:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
case 'f':
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
float v;
|
|
|
|
size_t s = fread(&v, sizeof(float), 1, f);
|
|
|
|
if (s != 1)
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
eof = true;
|
|
|
|
break;
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (s == 1)
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("%s = %f\n", structNames[i].c_str(), v);
|
|
|
|
}
|
|
|
|
record.m_values.push_back(v);
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
break;
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
case 'B':
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
char v;
|
|
|
|
size_t s = fread(&v, sizeof(char), 1, f);
|
|
|
|
if (s != 1)
|
|
|
|
{
|
|
|
|
eof = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (s == 1)
|
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("%s = %d\n", structNames[i].c_str(), v);
|
|
|
|
}
|
|
|
|
record.m_values.push_back(v);
|
|
|
|
}
|
2017-02-09 17:48:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
default:
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
printf("Unknown type\n");
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
retVal = eUnknownType;
|
|
|
|
btAssert(0);
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logRecords.push_back(record);
|
|
|
|
numStructsRead++;
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
printf("numStructsRead = %d\n", numStructsRead);
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
if (retVal == 0)
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
|
|
|
retVal = numStructsRead;
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
//read header and
|
|
|
|
}
|
|
|
|
else
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("Could not open file %s", fileName);
|
|
|
|
}
|
|
|
|
retVal = eMinitaurFileNotFound;
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE* createMinitaurLogFile(const char* fileName, btAlignedObjectArray<std::string>& structNames, std::string& structTypes)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
FILE* f = fopen(fileName, "wb");
|
2017-02-09 17:48:50 +00:00
|
|
|
if (f)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < structNames.size(); i++)
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
|
|
|
int len = strlen(structNames[i].c_str());
|
2018-09-23 21:17:31 +00:00
|
|
|
fwrite(structNames[i].c_str(), len, 1, f);
|
|
|
|
if (i < structNames.size() - 1)
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
fwrite(",", 1, 1, f);
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int sz = sizeof("\n");
|
2018-09-23 21:17:31 +00:00
|
|
|
fwrite("\n", sz - 1, 1, f);
|
|
|
|
fwrite(structTypes.c_str(), strlen(structTypes.c_str()), 1, f);
|
|
|
|
fwrite("\n", sz - 1, 1, f);
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void appendMinitaurLogData(FILE* f, std::string& structTypes, const MinitaurLogRecord& logData)
|
|
|
|
{
|
|
|
|
if (f)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
unsigned char buf[2] = {0xaa, 0xbb};
|
|
|
|
fwrite(buf, 2, 1, f);
|
2017-02-09 17:48:50 +00:00
|
|
|
if (structTypes.length() == logData.m_values.size())
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < logData.m_values.size(); i++)
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
switch (structTypes[i])
|
2017-02-09 17:48:50 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
{
|
|
|
|
fwrite(&logData.m_values[i].m_intVal, sizeof(int), 1, f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'f':
|
|
|
|
{
|
|
|
|
fwrite(&logData.m_values[i].m_floatVal, sizeof(float), 1, f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'B':
|
|
|
|
{
|
|
|
|
fwrite(&logData.m_values[i].m_charVal, sizeof(char), 1, f);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
}
|
2017-02-09 17:48:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void closeMinitaurLogFile(FILE* f)
|
|
|
|
{
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|