Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 import rospy
00037 import actionlib
00038 import random
00039 import copy
00040
00041 from naoqi_driver.naoqi_node import NaoqiNode
00042
00043 from naoqi_bridge_msgs.msg import(
00044 BlinkAction,
00045 BlinkResult,
00046 BlinkFeedback,
00047 FadeRGB
00048 )
00049
00050 class NaoLeds(NaoqiNode):
00051
00052 NODE_NAME = "nao_leds"
00053
00054 def __init__( self ):
00055
00056
00057 NaoqiNode.__init__( self, self.NODE_NAME )
00058
00059
00060 self.proxy = self.get_proxy( "ALLeds" )
00061
00062
00063 random.seed( rospy.Time.now().to_nsec() )
00064
00065
00066 self.subscriber = rospy.Subscriber(
00067 "fade_rgb",
00068 FadeRGB,
00069 self.fade_rgb)
00070
00071
00072 self.actionlib = actionlib.SimpleActionServer(
00073 "blink",
00074 BlinkAction,
00075 self.run_blink,
00076 False
00077 )
00078 self.actionlib.start()
00079
00080 def fade_rgb(self, request) :
00081 hexColor = int(
00082 int(request.color.r*255) << 16 |
00083 int(request.color.g*255) << 8 |
00084 int(request.color.b*255)
00085 )
00086
00087 self.proxy.fadeRGB(
00088 request.led_name,
00089 hexColor,
00090 request.fade_duration.to_sec()
00091 )
00092
00093 def run_blink( self, request ):
00094
00095
00096 third_of_duration = request.blink_duration / 3.0
00097
00098
00099 bg_msg = FadeRGB();
00100 bg_msg.led_name = "FaceLeds"
00101 bg_msg.color = request.bg_color
00102 bg_msg.fade_duration = third_of_duration
00103
00104
00105 blink_msg = copy.deepcopy( bg_msg )
00106
00107
00108 feedback = BlinkFeedback()
00109 result = BlinkResult()
00110 result.still_blinking = False
00111
00112
00113 bad_request = False
00114 reason = ""
00115 if not request.colors:
00116 bad_request = True
00117 reason = "No colors to blink were specified"
00118 elif request.blink_duration.to_sec() <= 0.0:
00119 bad_request = True
00120 reason = "Blink duration cannot be 0"
00121 elif request.blink_rate_mean <= 0.0 or request.blink_rate_sd <= 0.0:
00122 bad_request = True
00123 reason = "Invalid parameter for blink rate"
00124
00125 if bad_request:
00126 rospy.logwarn("Bad Blink request: {}".format(reason))
00127 self.actionlib.set_aborted(result, reason)
00128 return
00129
00130
00131 sleep_mean = request.blink_rate_mean
00132 sleep_sd = request.blink_rate_sd
00133 max_sleep_time = sleep_mean + 3* sleep_sd
00134
00135
00136 while ( not self.actionlib.is_preempt_requested() and
00137 not rospy.is_shutdown() ) :
00138
00139
00140 blink_msg.color = random.choice( request.colors )
00141
00142
00143 self.fade_rgb( blink_msg )
00144
00145 rospy.sleep( third_of_duration )
00146
00147 self.fade_rgb( bg_msg )
00148
00149
00150 feedback.last_color = blink_msg.color
00151 self.actionlib.publish_feedback( feedback )
00152
00153
00154 sleep_time = random.gauss( sleep_mean, sleep_sd )
00155
00156 if sleep_time < 0:
00157 sleep_time = 0
00158 elif sleep_time > max_sleep_time :
00159 sleep_time = max_sleep_time
00160
00161 rospy.sleep( sleep_time )
00162
00163
00164 if self.actionlib.is_new_goal_available() :
00165 result.still_blinking = True
00166
00167
00168 self.actionlib.set_preempted( result )
00169
00170 if __name__ == '__main__':
00171 node = NaoLeds()
00172 rospy.loginfo( node.NODE_NAME + " running..." )
00173 rospy.spin()
00174 rospy.loginfo( node.NODE_NAME + " stopped." )
00175 exit(0)