RobotLoggingUtil.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "RobotLoggingUtil.h"
  2. #include <stdio.h>
  3. #include "LinearMath/btAlignedObjectArray.h"
  4. #include "../Importers/ImportURDFDemo/urdfStringSplit.h"
  5. static bool readLine(FILE* file, btAlignedObjectArray<char>& line)
  6. {
  7. int c = 0;
  8. for (c = fgetc(file); (c != EOF && c != '\n'); c = fgetc(file))
  9. {
  10. line.push_back(c);
  11. }
  12. line.push_back(0);
  13. return (c == EOF);
  14. }
  15. int readMinitaurLogFile(const char* fileName, btAlignedObjectArray<std::string>& structNames, std::string& structTypes, btAlignedObjectArray<MinitaurLogRecord>& logRecords, bool verbose)
  16. {
  17. int retVal = 0;
  18. FILE* f = fopen(fileName, "rb");
  19. if (f)
  20. {
  21. if (verbose)
  22. {
  23. printf("Opened file %s\n", fileName);
  24. }
  25. btAlignedObjectArray<char> line0Buf;
  26. bool eof = readLine(f, line0Buf);
  27. btAlignedObjectArray<char> line1Buf;
  28. eof |= readLine(f, line1Buf);
  29. std::string line0 = &line0Buf[0];
  30. structTypes = &line1Buf[0];
  31. btAlignedObjectArray<std::string> separators;
  32. separators.push_back(",");
  33. urdfStringSplit(structNames, line0, separators);
  34. if (verbose)
  35. {
  36. printf("Num Fields = %d\n", structNames.size());
  37. }
  38. btAssert(structTypes.size() == structNames.size());
  39. if (structTypes.size() != structNames.size())
  40. {
  41. retVal = eCorruptHeader;
  42. }
  43. int numStructsRead = 0;
  44. if (structTypes.size() == structNames.size())
  45. {
  46. while (!eof)
  47. {
  48. unsigned char blaat[1024];
  49. size_t s = fread(blaat, 2, 1, f);
  50. if (s != 1)
  51. {
  52. eof = true;
  53. retVal = eInvalidAABBAlignCheck;
  54. break;
  55. }
  56. if ((blaat[0] != 0xaa) || (blaat[1] != 0xbb))
  57. {
  58. if (verbose)
  59. {
  60. printf("Expected 0xaa0xbb, terminating\n");
  61. }
  62. }
  63. if (verbose)
  64. {
  65. printf("Reading structure %d\n", numStructsRead);
  66. }
  67. MinitaurLogRecord record;
  68. for (int i = 0; i < structNames.size(); i++)
  69. {
  70. switch (structTypes[i])
  71. {
  72. case 'I':
  73. {
  74. size_t s = fread(blaat, sizeof(int), 1, f);
  75. if (s != 1)
  76. {
  77. eof = true;
  78. retVal = eCorruptValue;
  79. break;
  80. }
  81. int v = (int)*(unsigned int*)blaat;
  82. if (s == 1)
  83. {
  84. if (verbose)
  85. {
  86. printf("%s = %d\n", structNames[i].c_str(), v);
  87. }
  88. record.m_values.push_back(v);
  89. }
  90. break;
  91. }
  92. case 'i':
  93. {
  94. size_t s = fread(blaat, sizeof(int), 1, f);
  95. if (s != 1)
  96. {
  97. eof = true;
  98. retVal = eCorruptValue;
  99. break;
  100. }
  101. int v = *(int*)blaat;
  102. if (s == 1)
  103. {
  104. if (verbose)
  105. {
  106. printf("%s = %d\n", structNames[i].c_str(), v);
  107. }
  108. record.m_values.push_back(v);
  109. }
  110. break;
  111. }
  112. case 'f':
  113. {
  114. float v;
  115. size_t s = fread(&v, sizeof(float), 1, f);
  116. if (s != 1)
  117. {
  118. eof = true;
  119. break;
  120. }
  121. if (s == 1)
  122. {
  123. if (verbose)
  124. {
  125. printf("%s = %f\n", structNames[i].c_str(), v);
  126. }
  127. record.m_values.push_back(v);
  128. }
  129. break;
  130. }
  131. case 'B':
  132. {
  133. char v;
  134. size_t s = fread(&v, sizeof(char), 1, f);
  135. if (s != 1)
  136. {
  137. eof = true;
  138. break;
  139. }
  140. if (s == 1)
  141. {
  142. if (verbose)
  143. {
  144. printf("%s = %d\n", structNames[i].c_str(), v);
  145. }
  146. record.m_values.push_back(v);
  147. }
  148. break;
  149. }
  150. default:
  151. {
  152. if (verbose)
  153. {
  154. printf("Unknown type\n");
  155. }
  156. retVal = eUnknownType;
  157. btAssert(0);
  158. }
  159. }
  160. }
  161. logRecords.push_back(record);
  162. numStructsRead++;
  163. }
  164. if (verbose)
  165. {
  166. printf("numStructsRead = %d\n", numStructsRead);
  167. }
  168. if (retVal == 0)
  169. {
  170. retVal = numStructsRead;
  171. }
  172. }
  173. //read header and
  174. }
  175. else
  176. {
  177. if (verbose)
  178. {
  179. printf("Could not open file %s", fileName);
  180. }
  181. retVal = eMinitaurFileNotFound;
  182. }
  183. return retVal;
  184. }
  185. FILE* createMinitaurLogFile(const char* fileName, btAlignedObjectArray<std::string>& structNames, std::string& structTypes)
  186. {
  187. FILE* f = fopen(fileName, "wb");
  188. if (f)
  189. {
  190. for (int i = 0; i < structNames.size(); i++)
  191. {
  192. int len = strlen(structNames[i].c_str());
  193. fwrite(structNames[i].c_str(), len, 1, f);
  194. if (i < structNames.size() - 1)
  195. {
  196. fwrite(",", 1, 1, f);
  197. }
  198. }
  199. int sz = sizeof("\n");
  200. fwrite("\n", sz - 1, 1, f);
  201. fwrite(structTypes.c_str(), strlen(structTypes.c_str()), 1, f);
  202. fwrite("\n", sz - 1, 1, f);
  203. }
  204. return f;
  205. }
  206. void appendMinitaurLogData(FILE* f, std::string& structTypes, const MinitaurLogRecord& logData)
  207. {
  208. if (f)
  209. {
  210. unsigned char buf[2] = {0xaa, 0xbb};
  211. fwrite(buf, 2, 1, f);
  212. if (structTypes.length() == logData.m_values.size())
  213. {
  214. for (int i = 0; i < logData.m_values.size(); i++)
  215. {
  216. switch (structTypes[i])
  217. {
  218. case 'i':
  219. case 'I':
  220. {
  221. fwrite(&logData.m_values[i].m_intVal, sizeof(int), 1, f);
  222. break;
  223. }
  224. case 'f':
  225. {
  226. fwrite(&logData.m_values[i].m_floatVal, sizeof(float), 1, f);
  227. break;
  228. }
  229. case 'B':
  230. {
  231. fwrite(&logData.m_values[i].m_charVal, sizeof(char), 1, f);
  232. break;
  233. }
  234. default:
  235. {
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. void closeMinitaurLogFile(FILE* f)
  243. {
  244. if (f)
  245. {
  246. fclose(f);
  247. }
  248. }