trigger_tool.py
Go to the documentation of this file.
00001 import tool_utils as tu
00002 import PyQt4.QtGui as qtg
00003 import PyQt4.QtCore as qtc
00004 from PyQt4.QtGui import *
00005 from PyQt4.QtCore import *
00006 import smach
00007 import rospy
00008 from msg import Trigger
00009 
00010 
00011 TRIGGER_TOPIC = 'trigger'
00012 
00013 class TriggerTool(tu.ToolBase):
00014 
00015     def __init__(self, rcommander):
00016         tu.ToolBase.__init__(self, rcommander, 'wait_trigger', 'Trigger', TriggerState)
00017         self.trigger_pub = rospy.Publisher(TRIGGER_TOPIC, Trigger)
00018 
00019     def fill_property_box(self, pbox):
00020         formlayout = pbox.layout()
00021 
00022         self.time_out_box = qtg.QDoubleSpinBox(pbox)
00023         self.time_out_box.setMinimum(1.)
00024         self.time_out_box.setMaximum(1000.)
00025         self.time_out_box.setSingleStep(1.)
00026 
00027         self.trigger_button = qtg.QPushButton(pbox)
00028         self.trigger_button.setText('Send Trigger')
00029         self.rcommander.connect(self.trigger_button, qtc.SIGNAL('clicked()'), self.send_trigger_cb)
00030 
00031         formlayout.addRow('&Time out', self.time_out_box)
00032         formlayout.addRow(self.trigger_button)
00033 
00034         self.reset()
00035         pbox.update()
00036 
00037     def send_trigger_cb(self):
00038         self.trigger_pub.publish(Trigger('a trigger!'))
00039         rospy.sleep(.5)
00040         self.trigger_pub.publish(Trigger('a trigger!'))
00041 
00042     def new_node(self, name=None):
00043         if name == None:
00044             nname = self.name + str(self.counter)
00045         else:
00046             nname = name
00047 
00048         return TriggerState(nname, self.time_out_box.value())
00049 
00050     def set_node_properties(self, node):
00051         self.time_out_box.setValue(node.time_out)
00052 
00053     def reset(self):
00054         self.time_out_box.setValue(10.)
00055 
00056 
00057 class TriggerState(tu.StateBase):
00058 
00059     def __init__(self, name, time_out):
00060         tu.StateBase.__init__(self, name)
00061         self.time_out = time_out
00062 
00063     def get_smach_state(self):
00064         return TriggerStateSmach(time_out = self.time_out)
00065 
00066 class TriggerStateSmach(smach.State):
00067     ##
00068     #@param message if self.message is None we will wait for a message, else use provided message
00069     #@param time_out if we have to wait for a message specify how long to wait before timing out
00070     def __init__(self, time_out):
00071         smach.State.__init__(self, 
00072                 outcomes = ['succeeded', 'preempted', 'timed_out'], 
00073                 input_keys = [], output_keys = [])
00074         self.time_out = time_out
00075 
00076     def execute(self, userdata):
00077         msg = None
00078         t_start = rospy.get_time()
00079 
00080         while msg == None:
00081             try:
00082                 msg = rospy.wait_for_message(TRIGGER_TOPIC, Trigger, .1)
00083             except rospy.ROSException, e:
00084                 pass
00085 
00086             if self.preempt_requested():
00087                 self.service_preempt()
00088                 return 'preempted'
00089 
00090             if (self.time_out != None) and ((rospy.get_time() - t_start) > self.time_out):
00091                 return 'timed_out'
00092 
00093         return 'succeeded'
00094 
00095 


rcommander
Author(s): Hai Nguyen (haidai@gmail.com)
autogenerated on Thu Nov 28 2013 11:46:34