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
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 import roslib
00070 roslib.load_manifest('cob_generic_states')
00071 import rospy
00072 import smach
00073 import smach_ros
00074
00075 from simple_script_server import *
00076 sss = simple_script_server()
00077
00078 import tf
00079 from tf.transformations import *
00080 from actionlib_msgs.msg import *
00081
00082
00083
00084
00085
00086 class initialize(smach.State):
00087
00088 def __init__(self):
00089
00090 smach.State.__init__(
00091 self,
00092 outcomes=['initialized', 'failed'],
00093 input_keys=['listener', 'message'],
00094 output_keys=['listener', 'message'])
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 def execute(self, userdata):
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 handle_head = sss.init("head")
00116 if handle_head.get_error_code() != 0:
00117 return 'failed'
00118
00119 handle_torso = sss.init("torso")
00120 if handle_torso.get_error_code() != 0:
00121 return 'failed'
00122
00123 handle_tray = sss.init("tray")
00124 if handle_tray.get_error_code() != 0:
00125 return 'failed'
00126
00127
00128
00129
00130
00131 handle_sdh = sss.init("sdh")
00132
00133
00134
00135 handle_base = sss.init("base")
00136 if handle_base.get_error_code() != 0:
00137 return 'failed'
00138
00139
00140
00141
00142
00143 handle_head = sss.recover("head")
00144 if handle_head.get_error_code() != 0:
00145 return 'failed'
00146
00147 handle_torso = sss.recover("torso")
00148 if handle_torso.get_error_code() != 0:
00149 return 'failed'
00150
00151 handle_tray = sss.recover("tray")
00152 if handle_tray.get_error_code() != 0:
00153 return 'failed'
00154
00155 handle_arm = sss.recover("arm")
00156
00157
00158
00159
00160
00161
00162
00163 handle_base = sss.recover("base")
00164 if handle_base.get_error_code() != 0:
00165 return 'failed'
00166
00167
00168 sss.set_light("green")
00169
00170 userdata.message = []
00171 userdata.message.append(3)
00172 userdata.message.append("Finished initializing components")
00173 return 'initialized'
00174
00175
00176
00177 class interrupt(smach.State):
00178
00179 def __init__(self):
00180
00181 smach.State.__init__(
00182 self,
00183 outcomes=['no_interruption', 'interrupted'],
00184 input_keys=['message'],
00185 output_keys=['message'])
00186
00187
00188
00189
00190
00191
00192 def execute(self, userdata):
00193
00194 print "\nHas task been interrupted?\n"
00195 while True:
00196 var = raw_input("1 = no, 2 = yes\n")
00197 if var == str(1) or var == str(2):
00198 break
00199 if var == str(1):
00200 userdata.message = []
00201 userdata.message.append(3)
00202 userdata.message.append("Task has not been interrupted, continuing task")
00203 return 'no_interruption'
00204 else:
00205 userdata.message = []
00206 userdata.message.append(4)
00207 userdata.message.append("Task has been interrupted")
00208 return 'interrupted'
00209
00210
00211
00212
00213 class approach_pose(smach.State):
00214
00215 def __init__(self, pose = "", mode = "omni", move_second = "False"):
00216
00217 smach.State.__init__(
00218 self,
00219 outcomes=['succeeded', 'failed'],
00220 input_keys=['pose', 'message'],
00221 output_keys=['pose', 'message'])
00222
00223 self.pose = pose
00224 self.mode = mode
00225 self.move_second = move_second
00226
00227 def execute(self, userdata):
00228
00229
00230 if self.pose != "":
00231 pose = self.pose
00232 elif type(userdata.pose) is str:
00233 pose = userdata.pose
00234 elif type(userdata.pose) is list:
00235 pose = []
00236 pose.append(userdata.pose[0])
00237 pose.append(userdata.pose[1])
00238 pose.append(userdata.pose[2])
00239 else:
00240 userdata.message = []
00241 userdata.message.append(5)
00242 userdata.message.append("Invalid userdata 'pose'")
00243 userdata.message.append(userdata.pose)
00244 return 'failed'
00245
00246
00247 handle_base = sss.move("base", pose, False)
00248 move_second = self.move_second
00249
00250 timeout = 0
00251 while True:
00252 if (handle_base.get_state() == 3) and (not move_second):
00253
00254 handle_base = sss.move("base", pose, False)
00255 move_second = True
00256 elif (handle_base.get_state() == 3) and (move_second):
00257 return 'succeeded'
00258
00259
00260 service_full_name = '/base_controller/is_moving'
00261 try:
00262 rospy.wait_for_service(service_full_name,rospy.get_param('server_timeout',3))
00263 except rospy.ROSException, e:
00264 error_message = "%s"%e
00265 rospy.logerr("<<%s>> service not available, error: %s",service_full_name, error_message)
00266 return 'failed'
00267
00268
00269 try:
00270 is_moving = rospy.ServiceProxy(service_full_name,Trigger)
00271 resp = is_moving()
00272 except rospy.ServiceException, e:
00273 error_message = "%s"%e
00274 rospy.logerr("calling <<%s>> service not successfull, error: %s",service_full_name, error_message)
00275 return 'failed'
00276
00277
00278 if not resp.success.data:
00279 if timeout > 10:
00280 sss.say(["I can not reach my target position because my path or target is blocked"],False)
00281 timeout = 0
00282 else:
00283 timeout = timeout + 1
00284 rospy.sleep(1)
00285 else:
00286 timeout = 0
00287
00288
00289
00290 class approach_pose_without_retry(smach.State):
00291
00292 def __init__(self, pose = ""):
00293
00294 smach.State.__init__(
00295 self,
00296 outcomes=['succeeded', 'failed'],
00297 input_keys=['pose', 'message'],
00298 output_keys=['pose', 'message'])
00299
00300 sub_move_base = rospy.Subscriber("/move_base/status", GoalStatusArray, self.cb_move_base)
00301 self.pose = pose
00302
00303
00304
00305 def execute(self, userdata):
00306
00307
00308 if self.pose != "":
00309 pose = self.pose
00310 elif type(userdata.pose) is str:
00311 pose = userdata.pose
00312 elif type(userdata.pose) is list:
00313 pose = []
00314 pose.append(userdata.pose[0])
00315 pose.append(userdata.pose[1])
00316 pose.append(userdata.pose[2])
00317 else:
00318 userdata.message = []
00319 userdata.message.append(5)
00320 userdata.message.append("Invalid userdata 'pose'")
00321 userdata.message.append(userdata.pose)
00322 return 'failed'
00323
00324
00325 handle_base = sss.move("base", pose, False)
00326 move_second = False
00327
00328 timeout = 0
00329 while True:
00330 if (handle_base.get_state() == 3) and (not move_second):
00331
00332 handle_base = sss.move("base", pose, False)
00333 move_second = True
00334 elif (handle_base.get_state() == 3) and (move_second):
00335 userdata.message = []
00336 userdata.message.append(3)
00337 userdata.message.append("Pose was succesfully reached")
00338 return 'succeeded'
00339
00340
00341 service_full_name = '/base_controller/is_moving'
00342 try:
00343 rospy.wait_for_service(service_full_name,rospy.get_param('server_timeout',3))
00344 except rospy.ROSException, e:
00345 error_message = "%s"%e
00346 rospy.logerr("<<%s>> service not available, error: %s",service_full_name, error_message)
00347 return 'failed'
00348
00349
00350 try:
00351 is_moving = rospy.ServiceProxy(service_full_name,Trigger)
00352 resp = is_moving()
00353 except rospy.ServiceException, e:
00354 error_message = "%s"%e
00355 rospy.logerr("calling <<%s>> service not successfull, error: %s",service_full_name, error_message)
00356 return 'failed'
00357
00358
00359 if not resp.success.data:
00360 if timeout > 10:
00361 sss.say(["I can not reach my target position because my path or target is blocked, I will abort."],False)
00362 rospy.wait_for_service('base_controller/stop',10)
00363 try:
00364 stop = rospy.ServiceProxy('base_controller/stop',Trigger)
00365 resp = stop()
00366 except rospy.ServiceException, e:
00367 error_message = "%s"%e
00368 rospy.logerr("calling <<%s>> service not successfull, error: %s",service_full_name, error_message)
00369 return 'failed'
00370 else:
00371 timeout = timeout + 1
00372 rospy.sleep(1)
00373 else:
00374 timeout = 0
00375
00376 def cb_move_base(self, msg):
00377 self.move_base_status = msg
00378
00379
00380
00381 class linear_movement(smach.State):
00382
00383 def __init__(self):
00384
00385 smach.State.__init__(
00386 self,
00387 outcomes=['succeeded', 'failed'],
00388 input_keys=['message'],
00389 output_keys=['message'])
00390
00391
00392
00393 def execute(self, userdata):
00394
00395 print "\nApproach in a linear way\n"
00396
00397 print "\nHas destination been reached?\n"
00398 while True:
00399 var = raw_input("1 = yes, 2 = no\n")
00400 if var == str(1) or var == str(2):
00401 break
00402 if var == str(1):
00403 userdata.message = []
00404 userdata.message.append(3)
00405 userdata.message.append("Destination has been reached")
00406 return 'succeeded'
00407 else:
00408 userdata.message = []
00409 userdata.message.append(2)
00410 userdata.message.append("Could not reach destination")
00411 return 'failed'
00412
00413
00414
00415 class back_away(smach.State):
00416
00417 def __init__(self):
00418
00419 smach.State.__init__(
00420 self,
00421 outcomes=['backed_away', 'failed'],
00422 input_keys=['message'],
00423 output_keys=['message'])
00424
00425
00426
00427 def execute(self, userdata):
00428
00429 print "Backed away successfully?\n"
00430 while True:
00431 var = raw_input("1 = yes, 2 = no\n")
00432 if var == str(1) or var == str(2):
00433 break
00434 if var == str(1):
00435 userdata.message = []
00436 userdata.message.append(3)
00437 userdata.message.append("Backed away")
00438 return 'backed_away'
00439 else:
00440 userdata.message = []
00441 userdata.message.append(2)
00442 userdata.message.append("Failed to back away")
00443 return 'failed'
00444
00445
00446
00447 class message(smach.State):
00448
00449 def __init__(self):
00450
00451 smach.State.__init__(
00452 self,
00453 outcomes=['no_message_sent', 'quit'],
00454 input_keys=['message'],
00455 output_keys=['message'])
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 def execute(self, userdata):
00467
00468 while True:
00469
00470 message = userdata.message
00471 if message[0] >= 0:
00472 if message[0] == 0:
00473 message[0] = "QUIT MESSAGE"
00474 print "\n", userdata.message, "\n"
00475 return 'quit'
00476 elif message[0] == 1:
00477 message[0] = "INFO MESSAGE"
00478 print "\n", userdata.message, "\n"
00479 elif message[0] == 2:
00480 message[0] = "ERROR MESSAGE"
00481 print "\n", userdata.message, "\n"
00482 elif message[0] == 3:
00483 message[0] = "STATUS MESSAGE"
00484 print "\n", userdata.message, "\n"
00485 elif message[0] == 4:
00486 message[0] = "INTERRUPT MESSAGE"
00487 print "\n", userdata.message, "\n"
00488 else:
00489 message[0] = "INVALID USERDATA MESSAGE"
00490 print "\n", userdata.message, "\n"
00491 userdata.message[0] = -1
00492
00493
00494