1
+ #!/usr/bin/env python
2
+
3
+ """
4
+ Copyright (c) 2013,
5
+ Systems, Robotics and Vision Group
6
+ University of the Balearican Islands
7
+ All rights reserved.
8
+
9
+ Redistribution and use in source and binary forms, with or without
10
+ modification, are permitted provided that the following conditions are met:
11
+ * Redistributions of source code must retain the above copyright
12
+ notice, this list of conditions and the following disclaimer.
13
+ * Redistributions in binary form must reproduce the above copyright
14
+ notice, this list of conditions and the following disclaimer in the
15
+ documentation and/or other materials provided with the distribution.
16
+ * Neither the name of Systems, Robotics and Vision Group, University of
17
+ the Balearican Islands nor the names of its contributors may be used to
18
+ endorse or promote products derived from this software without specific
19
+ prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
25
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ """
32
+
33
+
34
+
35
+ import roslib ; roslib .load_manifest ('launch_tools' )
36
+ import sys
37
+ import rospy
38
+ import rosservice
39
+ import threading
40
+
41
+
42
+ ## Class for calling a service using a timer.
43
+ class TimedService (threading .Thread ):
44
+
45
+ ## The constructor
46
+ # @param self The object pointer.
47
+ # @param name The service name this class is going to call
48
+ # @param freq The desired timer period
49
+ def __init__ (self , name , period ):
50
+ threading .Thread .__init__ (self )
51
+ self ._service_name = name
52
+ self ._service_period = period
53
+
54
+ ## Run function required by threading library
55
+ def run (self ):
56
+ rospy .wait_for_service (self ._service_name )
57
+ rospy .Timer (rospy .Duration (self ._service_period ), self .callback )
58
+ rospy .loginfo ('Initialized timer for service: \n \t * Name: %s\n \t * Period: %f ' , self ._service_name , self ._service_period )
59
+
60
+ ## Timer callback
61
+ # @param event The event that has generated this callback
62
+ def callback (self ,event ):
63
+ rospy .wait_for_service (self ._service_name )
64
+ service_class = rosservice .get_service_class_by_name (self ._service_name )
65
+ try :
66
+ service = rospy .ServiceProxy (self ._service_name ,service_class )
67
+ service ()
68
+ rospy .loginfo ('Service %s called.' , self ._service_name )
69
+ except rospy .ServiceException , e :
70
+ rospy .logwarn ('Service %s call failed: %s' ,self ._service_name ,e )
71
+
72
+ ## @var _service_name
73
+ # The service name going to be called
74
+ _service_name = "service"
75
+
76
+ ## @var _service_period
77
+ # The timer period to call the service
78
+ _service_period = 1.0
79
+
80
+ ## Print usage for people that does not deserve to use this awesome python node.
81
+ def usage ():
82
+ return "%s service period [service period ...]" % sys .argv [0 ]
83
+
84
+ ## main function
85
+ if __name__ == "__main__" :
86
+ rospy .init_node ('services_timer' )
87
+ if len (sys .argv ) >= 3 :
88
+ names = sys .argv [1 :len (sys .argv ):2 ]
89
+ periods = sys .argv [2 :len (sys .argv ):2 ]
90
+ print 'names: ' , names
91
+ print 'periods: ' , periods
92
+ ts_list = []
93
+ for name ,period in zip (names ,periods ):
94
+ ts_list .append (TimedService (str (name ), float (period )))
95
+ for ts in ts_list :
96
+ ts .start ()
97
+ else :
98
+ print usage ()
99
+ sys .exit (1 )
100
+ rospy .spin ()
0 commit comments