/gazebo_taskboard/test/test_numpad.py

https://bitbucket.org/abotev/ssnasa_r2_simulator
Python | 183 lines | 123 code | 39 blank | 21 comment | 24 complexity | a5dc202b6dfd886d8a2b9b41838de801 MD5 | raw file
  1. #!/usr/bin/env python
  2. ##
  3. # Copyright (C) 2013 TopCoder Inc., All Rights Reserved.
  4. #
  5. # @author KennyAlive
  6. # @version 1.0
  7. #
  8. ## Tests for numpad
  9. PKG = 'gazebo_taskboard'
  10. NAME = 'test_numpad'
  11. import math
  12. import roslib
  13. roslib.load_manifest(PKG)
  14. import sys, unittest
  15. import os, os.path, threading, time
  16. import rospy, rostest
  17. from std_msgs.msg import String
  18. from helper import *
  19. from gazebo_taskboard.msg import *
  20. from gazebo_taskboard.srv import *
  21. button_states = ['UNSEL', 'UNSEL', 'UNSEL', 'UNSEL', 'UNSEL', 'UNSEL', 'UNSEL', 'UNSEL', 'UNSEL']
  22. led_states = ['OFF', 'OFF', 'OFF', 'OFF', 'OFF', 'OFF', 'OFF', 'OFF', 'OFF']
  23. def onMessage(message):
  24. global button_states
  25. if button_states[0] != message.A02_NUM_PAD_A1:
  26. rospy.loginfo("Num Pad button A1: " + message.A02_NUM_PAD_A1)
  27. button_states[0] = message.A02_NUM_PAD_A1
  28. if button_states[1] != message.A02_NUM_PAD_A2:
  29. rospy.loginfo("Num Pad button A2: " + message.A02_NUM_PAD_A2)
  30. button_states[1] = message.A02_NUM_PAD_A2
  31. if button_states[2] != message.A02_NUM_PAD_A3:
  32. rospy.loginfo("Num Pad button A3: " + message.A02_NUM_PAD_A3)
  33. button_states[2] = message.A02_NUM_PAD_A3
  34. if button_states[3] != message.A02_NUM_PAD_B1:
  35. rospy.loginfo("Num Pad button B1: " + message.A02_NUM_PAD_B1)
  36. button_states[3] = message.A02_NUM_PAD_B1
  37. if button_states[4] != message.A02_NUM_PAD_B2:
  38. rospy.loginfo("Num Pad button B2: " + message.A02_NUM_PAD_B2)
  39. button_states[4] = message.A02_NUM_PAD_B2
  40. if button_states[5] != message.A02_NUM_PAD_B3:
  41. rospy.loginfo("Num Pad button B3: " + message.A02_NUM_PAD_B3)
  42. button_states[5] = message.A02_NUM_PAD_B3
  43. if button_states[6] != message.A02_NUM_PAD_C1:
  44. rospy.loginfo("Num Pad button C1: " + message.A02_NUM_PAD_C1)
  45. button_states[6] = message.A02_NUM_PAD_C1
  46. if button_states[7] != message.A02_NUM_PAD_C2:
  47. rospy.loginfo("Num Pad button C2: " + message.A02_NUM_PAD_C2)
  48. button_states[7] = message.A02_NUM_PAD_C2
  49. if button_states[8] != message.A02_NUM_PAD_C3:
  50. rospy.loginfo("Num Pad button C3: " + message.A02_NUM_PAD_C3)
  51. button_states[8] = message.A02_NUM_PAD_C3
  52. global led_states
  53. if led_states[0] != message.A02_LED_NUM_PAD_A1:
  54. rospy.loginfo("Num Pad led A1: " + message.A02_LED_NUM_PAD_A1)
  55. led_states[0] = message.A02_LED_NUM_PAD_A1
  56. if led_states[1] != message.A02_LED_NUM_PAD_A2:
  57. rospy.loginfo("Num Pad led A2: " + message.A02_LED_NUM_PAD_A2)
  58. led_states[1] = message.A02_LED_NUM_PAD_A2
  59. if led_states[2] != message.A02_LED_NUM_PAD_A3:
  60. rospy.loginfo("Num Pad led A3: " + message.A02_LED_NUM_PAD_A3)
  61. led_states[2] = message.A02_LED_NUM_PAD_A3
  62. if led_states[3] != message.A02_LED_NUM_PAD_B1:
  63. rospy.loginfo("Num Pad led B1: " + message.A02_LED_NUM_PAD_B1)
  64. led_states[3] = message.A02_LED_NUM_PAD_B1
  65. if led_states[4] != message.A02_LED_NUM_PAD_B2:
  66. rospy.loginfo("Num Pad led B2: " + message.A02_LED_NUM_PAD_B2)
  67. led_states[4] = message.A02_LED_NUM_PAD_B2
  68. if led_states[5] != message.A02_LED_NUM_PAD_B3:
  69. rospy.loginfo("Num Pad led B3: " + message.A02_LED_NUM_PAD_B3)
  70. led_states[5] = message.A02_LED_NUM_PAD_B3
  71. if led_states[6] != message.A02_LED_NUM_PAD_C1:
  72. rospy.loginfo("Num Pad led C1: " + message.A02_LED_NUM_PAD_C1)
  73. led_states[6] = message.A02_LED_NUM_PAD_C1
  74. if led_states[7] != message.A02_LED_NUM_PAD_C2:
  75. rospy.loginfo("Num Pad led C2: " + message.A02_LED_NUM_PAD_C2)
  76. led_states[7] = message.A02_LED_NUM_PAD_C2
  77. if led_states[8] != message.A02_LED_NUM_PAD_C3:
  78. rospy.loginfo("Num Pad led C3: " + message.A02_LED_NUM_PAD_C3)
  79. led_states[8] = message.A02_LED_NUM_PAD_C3
  80. class NumPadTest(unittest.TestCase):
  81. def __init__(self, *args):
  82. super(NumPadTest, self).__init__(*args)
  83. def test_numpad(self):
  84. rospy.init_node(NAME, anonymous=True)
  85. rospy.Subscriber(TOPIC_NAME, TaskboardPanelA, onMessage)
  86. rospy.loginfo("Waiting for gazebo ...")
  87. rospy.wait_for_service(SERVICE_MANIPULATE_POWER_COVER)
  88. rospy.wait_for_service(SERVICE_MANIPULATE_POWER_SWITCH)
  89. rospy.wait_for_service(SERVICE_MANIPULATE_NUMPAD)
  90. wait(GUI_WAIT_TIME)
  91. rospy.loginfo("Testing has started")
  92. try:
  93. manipulatePowerCover = rospy.ServiceProxy(SERVICE_MANIPULATE_POWER_COVER, ManipulatePowerCover)
  94. manipulatePowerSwitch = rospy.ServiceProxy(SERVICE_MANIPULATE_POWER_SWITCH, ManipulatePowerSwitch)
  95. manipulateNumPad = rospy.ServiceProxy(SERVICE_MANIPULATE_NUMPAD, ManipulateNumPad)
  96. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  97. # Open power cover
  98. manipulatePowerCover(deg2rad(50), 0.5)
  99. wait()
  100. # Turn on power
  101. manipulatePowerSwitch(deg2rad(30), 0.5)
  102. wait()
  103. ## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  104. # Turn on all leds
  105. for i in range(9):
  106. manipulateNumPad(i, 20.0, 0.25)
  107. wait(1.5)
  108. assert led_states[i] == 'ON'
  109. rospy.loginfo("Case Done.")
  110. # Turn off some leds
  111. for i in range(1, 9, 2):
  112. manipulateNumPad(i, 20.0, 0.5)
  113. wait()
  114. assert led_states[i-1] == 'ON'
  115. assert led_states[i] == 'OFF'
  116. rospy.loginfo("Case Done")
  117. ## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  118. # Turn off the power
  119. manipulatePowerSwitch(deg2rad(-30), 0.5)
  120. wait()
  121. # Close power cover
  122. manipulatePowerCover(deg2rad(-50), 0.5)
  123. wait()
  124. ## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  125. # Check that all leds are off
  126. for i in range(9):
  127. assert led_states[i] == 'OFF'
  128. rospy.loginfo("Case Done")
  129. # Without power led is off
  130. manipulateNumPad(0, 20.0, 1.0)
  131. wait()
  132. assert led_states[0] == 'OFF'
  133. rospy.loginfo("Case Done")
  134. # Press on button long enough and check that it is in SEL state
  135. manipulateNumPad(3, 20.0, 8.0)
  136. wait(1)
  137. assert button_states[3] == 'SEL'
  138. rospy.loginfo("Case Done")
  139. except rospy.ServiceException, e:
  140. print "Service call failed: %s"%e
  141. time.sleep(0.5)
  142. if __name__ == '__main__':
  143. print "Waiting for test to start at time "
  144. rostest.run(PKG, 'test_numpad', NumPadTest)