Go to the documentation of this file.00001
00002 import rospy
00003 from PyQt4.QtGui import *
00004 from PyQt4.QtCore import *
00005 import rcommander.tool_utils as tu
00006 import smach
00007
00008 class MySleepTool(tu.ToolBase):
00009
00010 def __init__(self, rcommander):
00011 tu.ToolBase.__init__(self, rcommander, 'my_sleep', 'My Sleep', MySleepState)
00012
00013 def fill_property_box(self, pbox):
00014 formlayout = pbox.layout()
00015 self.time_box = QDoubleSpinBox(pbox)
00016 self.time_box.setMinimum(0)
00017 self.time_box.setMaximum(1000.)
00018 self.time_box.setSingleStep(.2)
00019 self.time_box.setValue(3.)
00020 formlayout.addRow("&Seconds", self.time_box)
00021
00022 def new_node(self, name=None):
00023 if name == None:
00024 nname = self.name + str(self.counter)
00025 else:
00026 nname = name
00027 return MySleepState(nname, self.time_box.value())
00028
00029 def set_node_properties(self, my_node):
00030 self.time_box.setValue(my_node.sleep_time)
00031
00032 def reset(self):
00033 self.time_box.setValue(3.)
00034
00035 class MySleepState(tu.StateBase):
00036
00037 def __init__(self, name, sleep_time):
00038 tu.StateBase.__init__(self, name)
00039 self.sleep_time = sleep_time
00040
00041 def get_smach_state(self):
00042 return MySleepSmachState(self.sleep_time)
00043
00044 class MySleepSmachState(smach.State):
00045
00046 def __init__(self, sleep_time):
00047 smach.State.__init__(self, outcomes=['preempted', 'done'], input_keys=[], output_keys=[])
00048 self.sleep_time = sleep_time
00049
00050 def execute(self, userdata):
00051 r = rospy.Rate(30)
00052 start_time = rospy.get_time()
00053 while (rospy.get_time() - start_time) < self.sleep_time:
00054 r.sleep()
00055 if self.preempt_requested():
00056 self.services_preempt()
00057 return 'preempted'
00058 return 'done'
00059
00060