dummy_behavior.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2020 Mojin Robotics GmbH
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 import random
18 import rospy
19 import actionlib
20 
21 from std_msgs.msg import String, Header
22 from cob_sound.msg import SayAction, SayGoal
23 from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
24 from control_msgs.msg import FollowJointTrajectoryAction, FollowJointTrajectoryGoal, FollowJointTrajectoryResult
25 from trajectory_msgs.msg import JointTrajectory, JointTrajectoryPoint
26 from cob_srvs.srv import SetString, SetStringRequest
27 from geometry_msgs.msg import PoseStamped, Pose, Point, Quaternion
28 
29 charger_pose = PoseStamped(header=Header(frame_id='map'),
30  pose=Pose(position=Point(10, 10, 0),
31  orientation=Quaternion(1, 0, 0, 0)))
32 
33 charger_arm_traj = FollowJointTrajectoryGoal(trajectory=JointTrajectory(points=[JointTrajectoryPoint(positions=[0])]))
34 
35 if __name__ == "__main__":
36  rospy.init_node("dummy_behavior")
37 
38  move_base_ac = actionlib.SimpleActionClient('move_base', MoveBaseAction)
39  say_ac = actionlib.SimpleActionClient('say', SayAction)
40  move_arm_ac = actionlib.SimpleActionClient('/arm/joint_trajectory_controller/follow_joint_trajectory',
41  FollowJointTrajectoryAction)
42  dock_srv = rospy.ServiceProxy('/dock', SetString)
43 
44  def command_callback(msg):
45  if 'charge' in msg.data:
46  rospy.loginfo("I'm told to go charge, lets go")
47  move_base_ac.send_goal_and_wait(MoveBaseGoal(target_pose=charger_pose))
48 
49  rospy.loginfo("I'm going to dock")
50  dock_result = dock_srv(SetStringRequest('charger'))
51 
52  if not dock_result.success:
53  rospy.logwarn("I borked docking, let's try again")
54  say_ac.send_goal_and_wait(SayGoal('Docking has failed, lets try again'))
55 
56  dock_srv(SetStringRequest('charger'))
57  # And now let's hope it does succeed
58 
59  rospy.loginfo("Let's use my arm to plug myself in")
60  move_arm_ac.send_goal_and_wait(charger_arm_traj)
61  arm_successful = move_arm_ac.get_result().error_code == FollowJointTrajectoryResult.SUCCESSFUL
62  if not arm_successful:
63  rospy.logwarn("I borked using my arm, let's try again")
64  say_ac.send_goal_and_wait(SayGoal('My arm has failed, lets try again'))
65  move_arm_ac.send_goal_and_wait(charger_arm_traj)
66  # Again, lets assume it works this time
67 
68  rospy.loginfo("Ah, finally charging, lets that juice flow in")
69  sentence = random.choice(["Jummy, fresh juice!", "Ah, some juice!"])
70  say_ac.send_goal_and_wait(SayGoal(sentence))
71 
72  rospy.loginfo("Test succeeded, I'm done")
73 
74  command_subscriber = rospy.Subscriber('/command', String, command_callback)
75 
76  rospy.loginfo("I'm waiting for your command, e.g. to go charge")
77 
78  rospy.spin()
def command_callback(msg)


scenario_test_tools
Author(s): Loy van Beek
autogenerated on Wed Apr 7 2021 03:03:18