/src/man/noggin/players/GaitTestStates.py

https://github.com/blm511/nbites · Python · 230 lines · 168 code · 50 blank · 12 comment · 27 complexity · 0bd7aeddf187eae7078be90ac4c34979 MD5 · raw file

  1. """
  2. Similar to WalkUnitTest, if a Gait makes it through all the Walk tests
  3. without falling then it is considered stable and passes
  4. """
  5. import man.motion as motion
  6. import man.motion.SweetMoves as SweetMoves
  7. from ..WebotsConfig import WEBOTS_ACTIVE
  8. from .GaitLearnStates import setWalkVector, setGait, revertWebots
  9. import man.noggin.util.GaitTestData as data
  10. from os.path import isfile
  11. try:
  12. import cPickle as pickle
  13. except:
  14. import pickle
  15. #Types (see WalkTestStates.py)
  16. WALK = 0
  17. DURATION = 250
  18. if WEBOTS_ACTIVE:
  19. PICKLE_FILE_PREFIX = ''
  20. START_DELAY = 100
  21. else:
  22. PICKLE_FILE_PREFIX = '/home/nao/gaits/'
  23. START_DELAY = 30
  24. GAITS = ('PSO_endGait.pickle.2198',
  25. 'PSO_endGait.pickle.2230',
  26. 'PSO_endGait.pickle.2322',
  27. )
  28. WORK_ON_NAO = ('PSO_endGait.pickle.1271',
  29. )
  30. UNIT_TEST1 = ((WALK, (.4, 0, 0), 2*DURATION),
  31. (WALK, (0, .4, 0), DURATION),
  32. (WALK, (.2, .2, 0), DURATION),
  33. (WALK, (.4, -.4, 0), DURATION),
  34. (WALK, (.5, 0, .5), DURATION),
  35. (WALK, (0, 0, 0), DURATION/4),
  36. )
  37. # higher speeds
  38. UNIT_TEST_FAST = ((WALK, (.6, 0, 0), DURATION),
  39. (WALK, (0, 0, 0), DURATION/4),
  40. (WALK, (.8, 0, 0), DURATION),
  41. (WALK, (0, 0, 0), DURATION/4),
  42. (WALK, (1, 0, 0), DURATION),
  43. )
  44. TEST_DATA_FILE = PICKLE_FILE_PREFIX + 'gaitUnitTest.pickle'
  45. def gamePlaying(player):
  46. if player.firstFrame():
  47. player.gainsOn()
  48. player.brain.tracker.stopHeadMoves()
  49. initTestData(player)
  50. getDataFromClass(player)
  51. if player.counter == 50:
  52. return player.goLater('gaitTest')
  53. return player.stay()
  54. def gameReady(player):
  55. return player.goLater('gamePlaying')
  56. def saveAndEnd(player):
  57. syncDataToClass(player)
  58. saveTestData(player)
  59. if WEBOTS_ACTIVE:
  60. revertWebots(player)
  61. else:
  62. return player.goLater('sitdown')
  63. if WEBOTS_ACTIVE:
  64. gameInitial=gamePlaying
  65. print "Webots is active!!!!"
  66. else:
  67. gamePenalized = saveAndEnd
  68. print "Webots is in-active!!!!"
  69. def gaitTest(player):
  70. if player.counter == START_DELAY:
  71. if player.gaitCounter >= len(player.gaitTest):
  72. return player.goLater('printResultsStop')
  73. try:
  74. gaitFile = PICKLE_FILE_PREFIX + \
  75. player.gaitTest[player.gaitCounter]
  76. print "gaitFile: ", gaitFile
  77. loadAndSetGait(player, gaitFile)
  78. except:
  79. print "error setting gait: ", player.gaitTest[player.gaitCounter]
  80. player.goLater('sitdown')
  81. player.gaitCounter += 1
  82. player.testCounter = 0
  83. if player.counter == START_DELAY*2:
  84. return player.goLater('walkTest')
  85. return player.stay()
  86. def walkTest(player):
  87. """
  88. This method processes the list of commands until there are
  89. none left
  90. """
  91. isFallen = player.brain.stability.isWBFallen()
  92. if player.firstFrame():
  93. if player.testCounter >= len(player.unitTest):
  94. setWalkVector(player, 0, 0, 0)
  95. player.gaitTestResults.append(True)
  96. return player.goLater('saveAndEnd')
  97. setTestDirection(player)
  98. if isFallen:
  99. print "(GaitUnitTest):: we've fallen down!"
  100. player.gaitTestResults.append(False)
  101. return player.goLater('saveAndEnd')
  102. if player.counter == player.testFrames:
  103. return player.goNow('switchDirections')
  104. return player.stay()
  105. def switchDirections(player):
  106. return player.goNow('walkTest')
  107. def printResultsStop(player):
  108. if player.firstFrame() and WEBOTS_ACTIVE:
  109. i = 0
  110. for gait in player.gaitTest:
  111. print "gait file ", gait, \
  112. " basically stable -> ", player.gaitTestResults[i]
  113. i += 1
  114. return player.goNow('saveAndStop')
  115. return player.stay()
  116. def sitdown(player):
  117. if player.firstFrame():
  118. setWalkVector(player, 0,0,0)
  119. player.executeMove(SweetMoves.SIT_POS)
  120. player.brain.tracker.stopHeadMoves()
  121. return player.stay()
  122. def loadAndSetGait(player, gaitPickleFile):
  123. f = open(gaitPickleFile, 'r')
  124. newGait = pickle.load(f)
  125. f.close()
  126. setGait(player, newGait)
  127. def initTestData(player):
  128. if isfile(TEST_DATA_FILE):
  129. loadTestData(player)
  130. else:
  131. newTestData(player)
  132. saveTestData(player)
  133. def loadTestData(player):
  134. try:
  135. f = open(TEST_DATA_FILE, 'r')
  136. player.testData = pickle.load(f)
  137. f.close()
  138. except:
  139. print "could not load test data file ", TEST_DATA_FILE
  140. def newTestData(player):
  141. print "Initializing new Gait Unit Test Data"
  142. player.testData = data.GaitTestData(GAITS,
  143. UNIT_TEST1)
  144. def saveTestData(player):
  145. print "Saving test data file: ", TEST_DATA_FILE
  146. try:
  147. f = open(TEST_DATA_FILE, 'w')
  148. pickle.dump(player.testData, f)
  149. f.close()
  150. except:
  151. print "could not save test data to file ", TEST_DATA_FILE
  152. # Ugly, ugly, ugly.
  153. def syncDataToClass(player):
  154. td = player.testData
  155. td.gaitTest = player.gaitTest
  156. td.unitTest = player.unitTest
  157. td.gaitTestResults = player.gaitTestResults
  158. td.testCounter = player.testCounter
  159. td.gaitCounter = player.gaitCounter
  160. def getDataFromClass(player):
  161. td = player.testData
  162. player.gaitTest = td.gaitTest
  163. player.unitTest = td.unitTest
  164. player.gaitTestResults = td.gaitTestResults
  165. player.testCounter = td.testCounter
  166. player.gaitCounter = td.gaitCounter
  167. def setTestDirection(player):
  168. currentCommand = player.unitTest[player.testCounter]
  169. player.testCounter += 1
  170. player.testFrames = currentCommand[2]
  171. currentVector = currentCommand[1]
  172. if currentCommand[0] == WALK:
  173. setWalkVector(player,
  174. currentVector[0],
  175. currentVector[1],
  176. currentVector[2],)
  177. else:
  178. player.printf("WARNING! Unrecognized command"
  179. " type in WalkUnitTest")