process_imu.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. '''
  2. * This file is part of ORB-SLAM3
  3. *
  4. * Copyright (C) 2017-2021 Carlos Campos, Richard Elvira, Juan J. Gómez Rodríguez, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  5. * Copyright (C) 2014-2016 Raúl Mur-Artal, José M.M. Montiel and Juan D. Tardós, University of Zaragoza.
  6. *
  7. * ORB-SLAM3 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * ORB-SLAM3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  12. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with ORB-SLAM3.
  16. * If not, see <http://www.gnu.org/licenses/>.
  17. '''
  18. import sys
  19. import numpy as np
  20. import matplotlib.pyplot as plt
  21. class dataset:
  22. # imu_sync = np.zeros((1,7))
  23. # acc = np.zeros((1,7))
  24. # gyro = np.zeros((1,7))
  25. def __init__(self, dirName):
  26. self.name = dirName
  27. self.acc = np.zeros((1, 4))
  28. self.gyro = np.zeros((1, 4))
  29. self.timesCam = np.zeros((1, 1))
  30. timesName = self.name + "/cam0/times.txt"
  31. timeFile = open(timesName, "r")
  32. i = 0
  33. next = 0
  34. for line in timeFile:
  35. currentline = line.split(",")
  36. if i%2 == 0:
  37. self.timesCam[next] = currentline
  38. next = next + 1
  39. self.timesCam = np.pad(self.timesCam, ((0, 1), (0, 0)), mode='constant', constant_values=0)
  40. i = i + 1
  41. print(i, "/", next)
  42. accName = self.name + "/IMU/acc.txt"
  43. accFile = open(accName, "r")
  44. i = 0
  45. for line in accFile:
  46. currentline = line.split(",")
  47. for j in range(0, 4):
  48. self.acc[i][j] = currentline[j]
  49. self.acc = np.pad(self.acc, ((0, 1), (0, 0)), mode='constant', constant_values=0)
  50. i = i + 1
  51. gyroName = self.name + "/IMU/gyro.txt"
  52. gyroFile = open(gyroName, "r")
  53. i = 0
  54. for line in gyroFile:
  55. currentline = line.split(",")
  56. for j in range(0, 4):
  57. self.gyro[i][j] = currentline[j]
  58. self.gyro = np.pad(self.gyro, ((0, 1), (0, 0)), mode='constant', constant_values=0)
  59. i = i + 1
  60. self.timesCam = np.delete(self.timesCam, self.timesCam.shape[0] - 1, axis=0)
  61. self.acc = np.delete(self.acc, self.acc.shape[0] - 1, axis=0)
  62. self.gyro = np.delete(self.gyro, self.gyro.shape[0] - 1, axis=0)
  63. print("Finished")
  64. def interpolate(self):
  65. self.imuSync = np.zeros((self.gyro.shape[0], 7))
  66. print("shape = ", self.imuSync.shape)
  67. totAcc = self.acc.shape[0]
  68. totGyro = self.gyro.shape[0]
  69. idxAcc = 0
  70. idxGyro = 0
  71. print(self.acc[idxAcc][0])
  72. print(self.gyro[idxGyro][0])
  73. while (self.acc[idxAcc][0] > self.gyro[idxGyro][0]):
  74. idxGyro = idxGyro + 1
  75. idxSync = 0
  76. while (idxAcc + 1 < totAcc and idxGyro < totGyro):
  77. # variables for interpolation
  78. deltaTimeAcc = self.acc[idxAcc + 1, 0] - self.acc[idxAcc, 0]
  79. deltaAcc = self.acc[idxAcc + 1, 1:4] - self.acc[idxAcc, 1:4]
  80. while (idxGyro < totGyro and self.acc[idxAcc + 1, 0] >= self.gyro[idxGyro, 0]):
  81. self.imuSync[idxSync, 0] = self.gyro[idxGyro, 0]
  82. # Interpolate accelerometer
  83. self.imuSync[idxSync, 4:7] = self.acc[idxAcc, 1:4] + (
  84. self.gyro[idxGyro, 0] - self.acc[idxAcc, 0]) * deltaAcc / deltaTimeAcc
  85. # Load gyroscope
  86. self.imuSync[idxSync, 1:4] = self.gyro[idxGyro, 1:4]
  87. idxGyro = idxGyro + 1
  88. idxSync = idxSync + 1
  89. idxAcc = idxAcc + 1
  90. self.imuSync = np.delete(self.imuSync, range(idxSync, totGyro), axis=0)
  91. def plotGyro(self):
  92. for i in range(1, 4):
  93. plt.plot(self.imuSync[:, 0], self.imuSync[:, i], label=str("acc ") + str(i))
  94. plt.xlabel("time (s)")
  95. plt.ylabel("ang. vel. (rad/s)")
  96. plt.title("Gyroscope")
  97. plt.legend()
  98. plt.show()
  99. def plotAcc(self):
  100. for i in range(4, 7):
  101. plt.plot(self.imuSync[:, 0], self.imuSync[:, i], label=str("acc ") + str(i))
  102. plt.xlabel("time (s)")
  103. plt.ylabel("acc (m/s^2)")
  104. plt.title("Accelerometer")
  105. plt.legend()
  106. plt.show()
  107. def saveSynchronized(self):
  108. imuName = self.name + "/imu0.csv"
  109. imuFile = open(imuName, "w")
  110. imuFile.write("#timestamp [ns],w_RS_S_x [rad s^-1],w_RS_S_y [rad s^-1],w_RS_S_z [rad s^-1],a_RS_S_x [m s^-2],a_RS_S_y [m s^-2],a_RS_S_z [m s^-2]\n")
  111. for row in self.imuSync:
  112. i = 0
  113. for num in row:
  114. if i == 0:
  115. imuFile.write(str((int)(1e9 * num)))
  116. i = 1
  117. else:
  118. imuFile.write("," + str(num))
  119. imuFile.write("\n")
  120. def saveCorrectTimes(self):
  121. timesName = self.name + "/cam0/corrTimes.txt"
  122. timesFile = open(timesName, "w")
  123. print("self.timesCam shape ", self.timesCam.shape)
  124. for row in self.timesCam:
  125. i = 0
  126. for num in row:
  127. timesFile.write(str((int) (num)))
  128. timesFile.write("\n")
  129. if __name__ == '__main__':
  130. if len(sys.argv)!=2 and len(sys.argv)!=3:
  131. print('Number of arguments != 2 and 3')
  132. sys.exit()
  133. dirName = sys.argv[1]
  134. print('Processing :', dirName)
  135. myDataset = dataset(dirName)
  136. myDataset.interpolate()
  137. myDataset.plotAcc()
  138. myDataset.saveSynchronized()
  139. if len(sys.argv)==3:
  140. myDataset.saveCorrectTimes()