light_action_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import rospy
19 import actionlib
20 import random
21 
22 from cob_light.msg import *
23 
24 from cob_light.srv import SetLightMode
25 from cob_light.srv import SetLightModeRequest
26 from cob_light.srv import SetLightModeResponse
27 
28 from std_msgs.msg import ColorRGBA
29 
30 class ActionTestScript(object):
31  def __init__(self):
32  self.client = actionlib.SimpleActionClient("/light_torso/set_light", SetLightModeAction)
33  # trying to connect to server
34  rospy.logdebug("waiting for /light_torso/set_light action server to start")
35  if not self.client.wait_for_server(rospy.Duration(5)):
36  # error: server did not respond
37  rospy.logerr("/light_torso/set_light action server not ready within timeout, aborting...")
38  else:
39  rospy.logdebug("/light_torso/set_light action server ready")
40 
41  self.executeMode(0)
42 
43  def executeMode(self, event):
44  rospy.loginfo("setting action mode")
45 
46  mode = self.getLightMode(random.randint(0,14))
47  goal = SetLightModeGoal()
48  goal.mode = mode.mode
49 
50  self.client.send_goal(goal)
51  self.client.wait_for_result()
52  res = self.client.get_result()
53 
54  duration = random.uniform(0.0, 0.2)
55  self.timer = rospy.Timer(rospy.Duration(duration), self.executeMode, True)
56 
57  def getLightMode(self, mode):
58  req = SetLightModeRequest()
59  req.mode.priority = 4
60 
61  if mode == 0:
62  req.mode.mode=LightModes.BREATH
63  req.mode.frequency = 0.3
64  req.mode.pulses = 2
65  req.mode.timeout = 0
66  req.mode.colors.append(ColorRGBA(1.0,1.0,1.0,1.0))
67 
68  elif mode == 1:
69  req.mode.mode=LightModes.FLASH
70  req.mode.frequency = 2
71  req.mode.pulses = 3
72  req.mode.timeout = 0
73  req.mode.colors.append(ColorRGBA(1.0,1.0,1.0,1.0))
74 
75  elif mode == 2:
76  req.mode.mode=LightModes.BREATH
77  req.mode.frequency = 0.4
78  req.mode.pulses = 0
79  req.mode.timeout = 0
80  req.mode.colors.append(ColorRGBA(170.0/255.0, 250.0/255.0, 70.0/255.0, 1.0))
81 
82  elif mode == 3:
83  req.mode.mode=LightModes.BREATH
84  req.mode.frequency = 0.3
85  req.mode.pulses = 0
86  req.mode.timeout = 0
87  req.mode.colors.append(ColorRGBA(70.0/255.0, 140.0/255.0, 250.0/255.0, 1.0))
88 
89  elif mode == 4:
90  req.mode.mode=LightModes.BREATH
91  req.mode.frequency = 0.3
92  req.mode.pulses = 0
93  req.mode.timeout = 0
94  req.mode.colors.append(ColorRGBA(240.0/255.0, 250.0/255.0, 70.0/255.0, 1.0))
95 
96  elif mode == 5:
97  req.mode.mode=LightModes.BREATH
98  req.mode.frequency = 0.3
99  req.mode.pulses = 0
100  req.mode.timeout = 0
101  req.mode.colors.append(ColorRGBA(70.0/255.0, 240.0/255.0, 250.0/255.0, 1.0))
102 
103  elif mode == 6: #static yellow black - barrier tape
104  yellow = ColorRGBA(70.0/255.0, 240.0/255.0, 250.0/255.0, 1.0)
105  black = ColorRGBA()
106  req.mode.mode=LightModes.BREATH
107  req.mode.frequency = 0.3
108  req.mode.pulses = 0
109  req.mode.timeout = 0
110  #58 leds
111  num_segs = 6
112  iterations = int(num_segs/2)
113  seg_div = int(58/num_segs)
114  for i in range(0, iterations):
115  for j in range (0, seg_div):
116  req.mode.colors.append(yellow)
117  for j in range (0, seg_div):
118  req.mode.colors.append(black)
119 
120  elif mode == 7:
121  req.mode.mode=LightModes.BREATH
122  req.mode.frequency = 0.3
123  req.mode.pulses = 0
124  req.mode.timeout = 0
125  req.mode.colors.append(ColorRGBA(250.0/255.0, 70.0/255.0, 70.0/255.0, 1.0))
126 
127  elif mode == 8:
128  req.mode.mode=LightModes.BREATH
129  req.mode.frequency = 0.3
130  req.mode.pulses = 0
131  req.mode.timeout = 0
132  req.mode.colors.append(ColorRGBA(230.0/255.0, 250.0/255.0, 250.0/255.0, 1.0))
133 
134  elif mode == 9:
135  req.mode.mode=LightModes.FADE_COLOR
136  req.mode.frequency = 0.5
137  req.mode.pulses = 0
138  req.mode.timeout = 0
139  req.mode.colors.append(ColorRGBA(1,1,1,1))
140 
141  elif mode == 10:
142  req.mode.mode=LightModes.FLASH
143  req.mode.frequency = 4
144  req.mode.pulses = 0
145  req.mode.timeout = 0
146  req.mode.colors.append(ColorRGBA(230.0/255.0, 250.0/255.0, 250.0/255.0, 1.0))
147 
148  elif mode == 11:
149  req.mode.mode=LightModes.STATIC
150  req.mode.frequency = 1
151  req.mode.pulses = 0
152  req.mode.timeout = 0
153  req.mode.colors.append(ColorRGBA(150.0/255.0, 120.0/255.0, 255.0/255.0, 1.0))
154 
155  elif mode == 12:
156  req.mode.mode=LightModes.STATIC
157  req.mode.frequency = 1
158  req.mode.pulses = 0
159  req.mode.timeout = 0
160  req.mode.colors.append(ColorRGBA(255.0/255.0, 95.0/255.0, 1.0/255.0, 1.0))
161 
162  elif mode == 13: #breath fast in red (error)
163  req.mode.mode=LightModes.BREATH
164  req.mode.frequency = 0.8
165  req.mode.pulses = 0
166  req.mode.timeout = 0
167  req.mode.colors.append(ColorRGBA(1.0, 0.0, 0.0, 1.0))
168 
169  elif mode == 14: #breath fast in red (error)
170  req.mode.mode=LightModes.XMAS
171  req.mode.frequency = 0.8
172  req.mode.pulses = 0
173  req.mode.timeout = 0
174  req.mode.colors.append(ColorRGBA(1.0, 0.0, 0.0, 1.0))
175 
176  return req
177 
178 
179 if __name__ == '__main__':
180  rospy.init_node('light_action_test')
182  rospy.spin()


cob_light
Author(s): Benjamin Maidel
autogenerated on Wed Apr 7 2021 02:11:39