6 from rospkg
import RosPack
7 from flexbe_core
import BehaviorLibrary
9 from std_msgs.msg
import String, Empty
14 import xml.etree.ElementTree
as ET
24 self.
_pub = rospy.Publisher(
'flexbe/start_behavior', BehaviorSelection, queue_size=100)
25 self.
_preempt_pub = rospy.Publisher(
'flexbe/command/preempt', Empty, queue_size=100)
37 rospy.loginfo(
"%d behaviors available, ready for start request." % self._behavior_lib.count_behaviors())
41 rospy.loginfo(
'Received a new request to start behavior: %s' % goal.behavior_name)
42 be_id, behavior = self._behavior_lib.find_behavior(goal.behavior_name)
44 Logger.logerr(
"Did not find behavior with requested name: %s" % goal.behavior_name)
45 self._as.set_preempted()
48 be_selection = BehaviorSelection()
49 be_selection.behavior_id = be_id
50 be_selection.autonomy_level = 255
51 be_selection.arg_keys = goal.arg_keys
52 be_selection.arg_values = goal.arg_values
53 be_selection.input_keys = goal.input_keys
54 be_selection.input_values = goal.input_values
57 be_filepath_new = os.path.join(self._rp.get_path(behavior[
"package"]),
'src/' + behavior[
"package"] +
'/' + behavior[
"file"] +
'.py')
58 with open(be_filepath_new,
"r") as f: 59 be_content_new = f.read() 61 be_filepath_old = os.path.join(self._rp.get_path(behavior["package"]),
'src/' + behavior[
"package"] +
'/' + behavior[
"file"] +
'_tmp.py')
62 if not os.path.isfile(be_filepath_old):
63 be_selection.behavior_checksum = zlib.adler32(be_content_new)
65 with open(be_filepath_old,
"r") as f: 66 be_content_old = f.read() 68 sqm = difflib.SequenceMatcher(a=be_content_old, b=be_content_new) 69 diffs = [x[1] for x
in sqm.get_grouped_opcodes(0)]
70 for opcode, a0, a1, b0, b1
in diffs:
71 content = be_content_new[b0:b1]
72 be_selection.modifications.append(BehaviorModification(a0, a1, content))
74 be_selection.behavior_checksum = zlib.adler32(be_content_new)
82 self._pub.publish(be_selection)
86 while not rospy.is_shutdown():
88 self._as.publish_feedback(BehaviorExecutionFeedback(self.
_current_state))
92 if self._as.is_preempt_requested():
93 rospy.loginfo(
'Behavior execution preempt requested!')
94 self._preempt_pub.publish()
96 self._as.set_preempted(
'')
100 rospy.logdebug_throttle(1,
'No behavior engine status received yet. Waiting for it...')
104 if self._engine_status.code == BEStatus.ERROR:
105 rospy.logerr(
'Failed to run behavior! Check onboard terminal for further infos.')
107 self._as.set_aborted(
'')
111 rospy.logdebug_throttle(1,
'Behavior execution has not yet started. Waiting for it...')
115 if self._engine_status.code == BEStatus.FINISHED:
116 result = self._engine_status.args[0] \
117 if len(self._engine_status.args) >= 1
else '' 118 rospy.loginfo(
'Finished behavior execution with result "%s"!' % result)
119 self._as.set_succeeded(BehaviorExecutionResult(outcome=result))
122 if self._engine_status.code == BEStatus.FAILED:
123 rospy.logerr(
'Behavior execution failed in state %s!' % str(self.
_current_state))
125 self._as.set_aborted(
'')
130 rospy.loginfo(
'Ready for next behavior start request.')
132 except rospy.ROSInterruptException:
142 rospy.loginfo(
'Behavior execution has started!')