duel.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import roslib; roslib.load_manifest('pr2_sith')
00003 import rospy
00004 from pr2_precise_trajectory import *
00005 from pr2_precise_trajectory.full_controller import FullPr2Controller, OPEN, CLOSED
00006 import sys
00007 import yaml
00008 import copy
00009 import random
00010 import subprocess
00011 import urlgrabber, string
00012 
00013 from dynamic_reconfigure.server import Server
00014 from pr2_sith.cfg import TheForceConfig
00015 
00016 PACKAGE_PREFIX = 'package://'
00017 
00018 def rospack_find(package):
00019     process = subprocess.Popen(['rospack', 'find', package], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
00020     (stdout, stderr) = process.communicate()
00021     if len(stderr) > 0:
00022         raise Exception(stderr)
00023     else:
00024         return string.strip(stdout)
00025 
00026 def get(url):
00027     mod_url = url
00028     if url.find(PACKAGE_PREFIX) == 0:
00029         mod_url = url[len(PACKAGE_PREFIX):]
00030         pos = mod_url.find('/')
00031         if pos == -1:
00032             raise Exception("Could not parse package:// format into file:// format for "+url)
00033 
00034         package = mod_url[0:pos]
00035         mod_url = mod_url[pos:]
00036         package_path = rospack_find(package)
00037 
00038         mod_url = "file://" + package_path + mod_url;
00039 
00040     return urlgrabber.urlopen(mod_url) 
00041 
00042 
00043 class Duel:
00044     def __init__(self):
00045         self.forward_time = 1.9
00046         self.back_time = 0.8
00047         self.controller = FullPr2Controller([RIGHT, RIGHT_HAND])
00048         self.srv = Server(TheForceConfig, self.callback)
00049         rospy.on_shutdown(self.done)
00050 
00051     def move_arm(self, move):
00052         move[0][TIME] = self.forward_time
00053         move[1][TIME] = self.back_time
00054         self.controller.do_action(move)
00055 
00056     def setup(self, start):
00057         self.controller.do_action(start)
00058         self.controller.hands[RIGHT_HAND].change_position(OPEN)
00059         raw_input('Press enter to close grip\n')
00060         self.controller.hands[RIGHT_HAND].change_position(CLOSED)
00061 
00062     def autosetup(self, start):
00063         self.controller.hands[RIGHT_HAND].change_position(OPEN)
00064         self.controller.do_action(start)
00065         self.controller.hands[RIGHT_HAND].change_position(CLOSED)
00066 
00067     def callback(self, config, level):
00068         self.forward_time = config.forward_time
00069         self.back_time = config.back_time
00070         self.controller.trigger = config.trigger_conditions
00071         self.controller.acceleration_trigger = config.acceleration_trigger
00072         self.controller.slip_trigger = config.slip_trigger
00073         return config
00074 
00075     def done(self):
00076         # drop the lightsaber when we're done
00077         self.controller.hands[RIGHT_HAND].change_position(OPEN)
00078         
00079 
00080 if __name__ == '__main__':
00081     starts = []
00082     attacks = []
00083 
00084     for i in range(1, 7):
00085         for (move_type, scripts) in zip(['start', 'attack'], [starts, attacks]):
00086             movements = yaml.load( get( 'package://pr2_sith/scripts/%s%d.yaml'%(move_type, i)))
00087             scripts.append(movements)
00088     transitions = [range(1,7), [3,4,5], [1,4,5], [2,3,5,6], range(2,7), [1,2]]
00089 
00090     moves = []
00091     for start, attack_indexes in zip(starts, transitions):
00092         if start==starts[3]: #never works
00093             continue
00094         for attack in [ attacks[i-1] for i in attack_indexes ]:
00095             moves.append( start + attack )
00096 
00097     rospy.init_node('sith')
00098     duel = Duel()
00099 
00100     if len(sys.argv)>1:
00101         if sys.argv[1]=='--setup':
00102             duel.setup(starts[0])
00103         if sys.argv[1]=='--autosetup':
00104             auto = copy.deepcopy(starts[0])
00105             auto[0]['transition'] = 'impact'
00106             duel.autosetup(auto)
00107 
00108     while not rospy.is_shutdown():
00109         move = random.choice(moves)
00110         duel.move_arm( move )


pr2_sith
Author(s): David V. Lu!!
autogenerated on Wed Aug 26 2015 15:46:39