test_output.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2012, Yujin Robot
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 #
12 # * Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # * Redistributions in binary form must reproduce the above
15 # copyright notice, this list of conditions and the following
16 # disclaimer in the documentation and/or other materials provided
17 # with the distribution.
18 # * Neither the name of the Yujin Robot nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34 
35 # Author: Younghun Ju <yhju@yujinrobot.com> <yhju83@gmail.com>
36 
37 import roslib; roslib.load_manifest('kobuki_testsuite')
38 import rospy
39 import sys, select, termios, tty
40 import time
41 from datetime import datetime
42 
43 from kobuki_msgs.msg import DigitalOutput, Led, Sound
44 
45 
46 def getKey():
47  tty.setraw(sys.stdin.fileno())
48  rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
49  if rlist:
50  key = sys.stdin.read(1)
51  else:
52  key = ''
53 
54  #termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
55  return key
56 
57 def printStatus(ext_values, dgt_values, leds):
58  sys.stdout.write('\r ')
59  for idx in range(0,4):
60  if ext_values[idx]:
61  sys.stdout.write('[ \033[1mOn\033[0m] ')
62  else:
63  sys.stdout.write('[Off] ')
64  sys.stdout.write('| [\033[1m'+textBindings[leds[0].value]+'\033[0m] ')
65  sys.stdout.write('[\033[1m'+textBindings[leds[1].value]+'\033[0m] | ')
66 
67  for idx in range(0,4):
68  if dgt_values[idx]:
69  sys.stdout.write('[ \033[1mOn\033[0m] ')
70  else:
71  sys.stdout.write('[Off] ')
72 
73  sys.stdout.write('\r')
74  sys.stdout.flush()
75 
76 keyBindings1 = {
77  '1':0,
78  '2':1,
79  '3':2,
80  '4':3,
81 }
82 
83 keyBindings2 = {
84  '5':0,
85  '6':1,
86 }
87 
88 keyBindings3 = {
89  '7':0,
90  '8':1,
91  '9':2,
92  '0':3,
93 }
94 
95 keyBindings4 = {
96  'a':Sound.ON,
97  's':Sound.OFF,
98  'd':Sound.RECHARGE,
99  'f':Sound.BUTTON,
100  'z':Sound.ERROR,
101  'x':Sound.CLEANINGSTART,
102  'c':Sound.CLEANINGEND,
103 }
104 
105 colorBindings = {
106  Led.GREEN:Led.ORANGE,
107  Led.ORANGE:Led.RED,
108  Led.RED:Led.BLACK,
109  Led.BLACK:Led.GREEN,
110 }
111 
112 textBindings = {
113  Led.GREEN:' Green',
114  Led.ORANGE:'Orange',
115  Led.RED:' Red',
116  Led.BLACK:' Black',
117 }
118 
119 
120 settings = termios.tcgetattr(sys.stdin)
121 
122 def clearing():
123  sys.stdout.write('\n\r')
124  sys.stdout.flush()
125  termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
126 
127 #rospy initial setup
128 rospy.init_node("test_output")
129 rospy.on_shutdown(clearing)
130 rate = rospy.Rate(10)
131 
132 pub_ext_pwr = rospy.Publisher('/mobile_base/commands/external_power',DigitalOutput)
133 pub_dgt_out = rospy.Publisher('/mobile_base/commands/digital_output',DigitalOutput)
134 pub = []
135 pub.append(rospy.Publisher('/mobile_base/commands/led1',Led))
136 pub.append(rospy.Publisher('/mobile_base/commands/led2',Led))
137 pub_sounds = rospy.Publisher('/mobile_base/commands/sound',Sound)
138 
139 # initial values
140 external_power = DigitalOutput()
141 external_power.values = [ True, True, True, True ]
142 external_power.mask = [ True, True, True, True ]
143 
144 digital_output = DigitalOutput()
145 digital_output.values = [ True, True, True, True ]
146 digital_output.mask = [ True, True, True, True ]
147 
148 leds = []
149 leds.append(Led())
150 leds.append(Led())
151 leds[0].value = Led.GREEN
152 leds[1].value = Led.GREEN
153 
154 # initialize outputs
155 while not pub_ext_pwr.get_num_connections():
156  rate.sleep()
157 pub_ext_pwr.publish(external_power)
158 
159 while not pub_dgt_out.get_num_connections():
160  rate.sleep()
161 pub_dgt_out.publish(digital_output)
162 
163 while not pub[0].get_num_connections():
164  rate.sleep()
165 pub[0].publish(leds[0])
166 
167 while not pub[1].get_num_connections():
168  rate.sleep()
169 pub[1].publish(leds[1])
170 
171 # statement
172 print ""
173 print "Control Every Output of Kobuki"
174 print "------------------------------"
175 print "1: Toggle the state of 3.3V"
176 print "2: Toggle the state of 5V"
177 print "3: Toggle the state of 12V5A(arm)"
178 print "4: Toggle the state of 12V1A(kinect)"
179 print ""
180 print "5: Control Led #1"
181 print "6: Control Led #2"
182 print ""
183 print "7~0: Toggle the state of DigitalOut_0~3"
184 print ""
185 print "Play Sounds"
186 print "-----------"
187 print "a: On s: Off d: Recharge f: Button z: Error x: CleaningStart c: CleaningEnd"
188 print ""
189 print "q: Quit"
190 print ""
191 print ""
192 print " 3.3V 5.0V 12V5A 12V1A | Led #1 Led #2 | DO_0 DO_1 DO_2 DO_3"
193 #print " [ On] [Off] [ On] [Off] | [Orange] [Orange] | [ On] [Off] [Off] [ On]"
194 
195 while not rospy.is_shutdown():
196  printStatus(external_power.values, digital_output.values, leds)
197  key = getKey()
198  if key == '': continue
199  if key == 'q' or key == 'Q':
200  rospy.signal_shutdown('user reuest')
201 
202  elif key in keyBindings1.keys():
203  external_power.values[keyBindings1[key]] ^= True
204  printStatus(external_power.values, digital_output.values, leds)
205  pub_ext_pwr.publish(external_power)
206 
207  elif key in keyBindings3.keys():
208  digital_output.values[keyBindings3[key]] ^= True
209  printStatus(external_power.values, digital_output.values, leds)
210  pub_dgt_out.publish(digital_output)
211 
212  elif key in keyBindings2.keys():
213  leds[keyBindings2[key]].value = colorBindings[ leds[keyBindings2[key]].value ]
214  printStatus(external_power.values, digital_output.values, leds)
215  pub[keyBindings2[key]].publish(leds[keyBindings2[key]])
216  elif key in keyBindings4.keys():
217  pub_sounds.publish(keyBindings4[key])
218  #rate.sleep()
219 
def printStatus(ext_values, dgt_values, leds)
Definition: test_output.py:57
def publish(pub, v, sec)
def getKey()
Definition: test_output.py:46
def clearing()
Definition: test_output.py:122


kobuki_testsuite
Author(s): Jorge Santos Simon
autogenerated on Mon Jun 10 2019 13:45:22