37 import roslib; roslib.load_manifest(
'kobuki_auto_docking')
39 import sys, select, termios, tty, os
41 from datetime
import datetime
45 from std_msgs.msg
import String, Empty
46 from geometry_msgs.msg
import Twist
47 from kobuki_msgs.msg
import DigitalOutput, Led, Sound, SensorState, DockInfraRed, ExternalPower, MotorPower
66 settings = termios.tcgetattr(sys.stdin.fileno())
72 rospy.init_node(
"dock_drive_control")
88 'motor_power':rospy.Publisher(
'/mobile_base/commands/motor_power',MotorPower),
91 'debug':rospy.Publisher(
'/dock_drive/debug/mode_shift', String),
92 'external_power':rospy.Publisher(
'/mobile_base/commands/external_power',ExternalPower),
93 'digital_output':rospy.Publisher(
'/mobile_base/commands/digital_output',DigitalOutput),
94 'led1':rospy.Publisher(
'/mobile_base/commands/led1',Led),
95 'led2':rospy.Publisher(
'/mobile_base/commands/led2',Led),
96 'sound':rospy.Publisher(
'/mobile_base/commands/sound',Sound),
97 'cmd_vel':rospy.Publisher(
'/mobile_base/commands/velocity',Twist),
100 'core':rospy.Subscriber(
'/mobile_base/sensors/core', SensorState, self.
sensorsCallback),
101 'dock_ir':rospy.Subscriber(
'/mobile_base/sensors/dock_ir', DockInfraRed, self.
dockIRCallback),
104 '1':(self.
pub[
'debug'].publish, String(
'enable') ,
'enable'),
105 '2':(self.
pub[
'debug'].publish, String(
'run') ,
'run'),
106 '3':(self.
pub[
'debug'].publish, String(
'stop') ,
'stop'),
107 '4':(self.
pub[
'debug'].publish, String(
'disable'),
'disable'),
117 'r':(self.toggleMotor,False,'disabled'),
119 'a':(self.
pub[
'sound'].publish,Sound(Sound.ON),
'sound.on'),
120 's':(self.
pub[
'sound'].publish,Sound(Sound.OFF),
'sound.off'),
121 'd':(self.
pub[
'sound'].publish,Sound(Sound.RECHARGE),
'sound.recharge'),
122 'f':(self.
pub[
'sound'].publish,Sound(Sound.BUTTON),
'sound.button'),
123 'z':(self.
pub[
'sound'].publish,Sound(Sound.ERROR),
'sound.error'),
124 'x':(self.
pub[
'sound'].publish,Sound(Sound.CLEANINGSTART),
'sound.cleaningstart'),
125 'c':(self.
pub[
'sound'].publish,Sound(Sound.CLEANINGEND),
'sound.cleaningend'),
126 'q':(rospy.signal_shutdown,
'user reuest',
'quit'),
127 'Q':(rospy.signal_shutdown,
'user reuest',
'quit'),
136 external_power = DigitalOutput() 137 external_power.values = [ True, True, True, True ] 138 external_power.mask = [ True, True, True, True ] 140 digital_output = DigitalOutput() 141 digital_output.values = [ True, True, True, True ] 142 digital_output.mask = [ True, True, True, True ] 147 leds[0].value = Led.GREEN 148 leds[1].value = Led.GREEN 151 while not pub_ext_pwr.get_num_connections(): 153 pub_ext_pwr.publish(external_power) 155 while not pub_dgt_out.get_num_connections(): 157 pub_dgt_out.publish(digital_output) 159 while not pub[0].get_num_connections(): 161 pub[0].publish(leds[0]) 163 while not pub[1].get_num_connections(): 165 pub[1].publish(leds[1]) 169 while not rospy.is_shutdown():
174 fd = sys.stdin.fileno()
175 new = termios.tcgetattr(fd)
176 new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
177 new[6][termios.VMIN] = 0
178 new[6][termios.VTIME] = 1
179 termios.tcsetattr(fd, termios.TCSANOW, new)
180 return sys.stdin.read(1)
191 if len(akey)
and ord(akey)==91:
194 if key ==
'A': self.
message =
'Up Arrow' ; self.cmd_vel.linear.x += 0.05
195 if key ==
'B': self.
message =
'Down Arrow' ; self.cmd_vel.linear.x -= 0.05
196 if key ==
'C': self.
message =
'Right Arrow' ; self.cmd_vel.angular.z -= 0.33
197 if key ==
'D': self.
message =
'Left Arrow' ; self.cmd_vel.angular.z += 0.33
199 if key
in self.keyBindings.keys():
204 if type(f)
is types.InstanceType
or type(f)
is types.MethodType
or type(f)
is types.FunctionType : f(v)
211 self.
message =
"unknown key: " + str(len(key)) +
": " + str(ord(key))
214 elif key in keyBindings1.keys(): 215 external_power.values[keyBindings1[key]] ^= True 216 printStatus(external_power.values, digital_output.values, leds) 217 pub_ext_pwr.publish(external_power) 219 elif key in keyBindings3.keys(): 220 digital_output.values[keyBindings3[key]] ^= True 221 printStatus(external_power.values, digital_output.values, leds) 222 pub_dgt_out.publish(digital_output) 224 elif key in keyBindings2.keys(): 225 leds[keyBindings2[key]].value = colorBindings[ leds[keyBindings2[key]].value ] 226 printStatus(external_power.values, digital_output.values, leds) 227 pub[keyBindings2[key]].publish(leds[keyBindings2[key]]) 228 elif key in keyBindings4.keys(): 229 pub_sounds.publish(keyBindings4[key]) 234 sys.stdout.write(
'\n\r')
236 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
240 self.
pub[
'motor_power'].publish(MotorPower(MotorPower.ON))
243 self.
pub[
'motor_power'].publish(MotorPower(MotorPower.OFF))
248 if not rospy.is_shutdown():
253 if not rospy.is_shutdown():
257 if not rospy.is_shutdown():
261 rem = float(commands.getoutput(
"grep \"^remaining capacity\" /proc/acpi/battery/" + self.
bat_name +
"/state | awk '{ print $3 }'"))
262 full = float(commands.getoutput(
"grep \"^last full capacity\" /proc/acpi/battery/" + self.
bat_name +
"/info | awk '{ print $4 }'"))
263 self.
state = commands.getoutput(
"grep \"^charging state\" /proc/acpi/battery/" + self.
bat_name +
"/state | awk '{ print $3 }'")
268 num = self.
sub[
'core'].get_num_connections()
270 self.
messages =
'core is disconnected({0:d})'.format(num)
272 self.
messages =
'core is connected({0:d})'.format(num)
282 print " Dock Drive Controller" 283 print "----------------------" 287 print "4: cancel_dock" 295 sys.stdout.write(
' \r')
296 sys.stdout.write(
'[ \033[1m' + self.
message +
'\033[0m ]')
299 sys.stdout.write(
'[Laptop: ' +
"{0:2.2f}".format(self.
percentage) +
'% - ' + self.
state +
']')
302 if self.sensors.charger:
303 src_str =
'(adaptor)' if self.sensors.charger&16
else '(dock)' 305 chg = self.sensors.charger&6
306 if chg==0: chg_str =
'discharging' 307 elif chg==2: chg_str =
'fully charged' 308 else: chg_str =
'charging' 309 sys.stdout.write(
'[Robot: ' +
"{0:2.1f}".format(self.sensors.battery/10.) +
'V - ' + chg_str + src_str +
']')
311 for idx in range(0,4): 313 sys.stdout.write('[ \033[1mOn\033[0m] ') 315 sys.stdout.write('[Off] ') 316 sys.stdout.write('| [\033[1m'+textBindings[leds[0].value]+'\033[0m] ') 317 sys.stdout.write('[\033[1m'+textBindings[leds[1].value]+'\033[0m] | ') 319 for idx in range(0,4): 321 sys.stdout.write('[ \033[1mOn\033[0m] ') 323 sys.stdout.write('[Off] ') 325 sys.stdout.write(
'\r')
329 PATH =
'/proc/acpi/battery' 331 if os.path.exists(PATH):
332 bat = os.walk(PATH).next()
338 if __name__ ==
'__main__':
342 except rospy.ROSInterruptException:
pass
def toggleMotor(self, on_off)
def sensorsCallback(self, data)
def stateCallback(self, event)
def keyopCallback(self, event)
def batteryCallback(self, event)
def dockIRCallback(self, data)