Go to the documentation of this file.00001
00002 import roslib; roslib.load_manifest('collvoid_controller')
00003 import rospy
00004 import string
00005 import actionlib
00006 import math
00007 import random
00008
00009 from std_msgs.msg import String
00010 from geometry_msgs.msg import PoseWithCovarianceStamped,PoseStamped
00011 from nav_msgs.msg import Odometry
00012 from socket import gethostname
00013 from collvoid_msgs.msg import PoseTwistWithCovariance
00014 from move_base_msgs.msg import *
00015
00016 import tf
00017
00018 THRESHOLD = 0.22
00019
00020 def dist(a, b):
00021 return math.sqrt(math.pow(a.position.x - b.position.x, 2) + math.pow(a.position.y - b.position.y, 2))
00022
00023
00024 class ControllerRobots():
00025
00026 def __init__(self):
00027 self.initialize()
00028
00029
00030 def initialize(self):
00031 self.stopped = False
00032
00033 self.client = actionlib.SimpleActionClient('move_base', MoveBaseAction)
00034 self.client.wait_for_server()
00035
00036 self.circling = False
00037
00038 self.sub_commands_robot = rospy.Subscriber("/commands_robot", String, self.cb_commands_robot)
00039 self.sub_position_share = rospy.Subscriber("/position_share", PoseTwistWithCovariance, self.cb_common_positions)
00040
00041 self.sub_goal = rospy.Subscriber("/goal", PoseStamped, self.cb_goal)
00042 self.sub_ground_truth = rospy.Subscriber("base_pose_ground_truth", Odometry, self.cb_ground_truth)
00043
00044 self.pub_init_guess = rospy.Publisher("initialpose", PoseWithCovarianceStamped)
00045 self.pub_commands_robot = rospy.Publisher("/commands_robot", String)
00046
00047
00048 self.hostname = rospy.get_namespace()
00049 self.noise_std = rospy.get_param("/noise_std", 0.0)
00050
00051 if (self.hostname == "/"):
00052 self.hostname = gethostname()
00053 self.goals = rospy.get_param("/%s/goals"%self.hostname,[])
00054 else:
00055 self.goals = rospy.get_param("%sgoals"%self.hostname,[])
00056 if len(self.goals)>0:
00057 rospy.loginfo("goals: %s"%str(self.goals))
00058 self.cur_goal = 0
00059 self.num_goals = len(self.goals["x"])
00060 self.cur_goal_msg = self.return_cur_goal()
00061 rospy.loginfo("Name: %s",self.hostname)
00062
00063
00064
00065 def return_cur_goal(self):
00066 goal = MoveBaseGoal()
00067 goal.target_pose.pose.position.x = self.goals["x"][self.cur_goal]
00068 goal.target_pose.pose.position.y = self.goals["y"][self.cur_goal]
00069 q = tf.transformations.quaternion_from_euler(0,0, self.goals["ang"][self.cur_goal], axes='sxyz')
00070 goal.target_pose.pose.orientation.x = q[0]
00071 goal.target_pose.pose.orientation.y = q[1]
00072 goal.target_pose.pose.orientation.z = q[2]
00073 goal.target_pose.pose.orientation.w = q[3]
00074
00075 goal.target_pose.header.frame_id = "/map"
00076 return goal
00077
00078
00079 def cb_common_positions(self,msg):
00080 if self.stopped or not self.circling:
00081 return
00082 if msg.robot_id == self.hostname:
00083 if dist(msg.pose.pose, self.cur_goal_msg.target_pose.pose) < THRESHOLD:
00084 rospy.loginfo("Reached goal, sending new goal")
00085 self.cur_goal += 1
00086 if (self.cur_goal == self.num_goals):
00087 self.cur_goal = 0
00088 self.cur_goal_msg = self.return_cur_goal()
00089 str = "%s Start"%self.hostname
00090 self.pub_commands_robot.publish(String(str))
00091
00092
00093 def cb_goal(self,msg):
00094 self.sent_goal = MoveBaseGoal()
00095 self.sent_goal.target_pose.pose.position = msg.pose.position
00096 self.sent_goal.target_pose.pose.orientation = msg.pose.orientation
00097 self.sent_goal.target_pose.header = msg.header;
00098 print str(self.sent_goal)
00099
00100 def cb_ground_truth(self, msg):
00101 self.ground_truth = PoseWithCovarianceStamped()
00102
00103 self.ground_truth.header.frame_id = "/map"
00104 self.ground_truth.pose.pose = msg.pose.pose
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 def publish_init_guess(self, noise_cov, noise_std):
00119 if not (self.ground_truth == None):
00120 self.ground_truth.pose.pose.position.x += random.gauss(0, noise_std)
00121 self.ground_truth.pose.pose.position.y += random.gauss(0, noise_std)
00122
00123 self.ground_truth.header.stamp = rospy.Time.now()
00124 self.ground_truth.pose.covariance[0] = noise_cov
00125 self.ground_truth.pose.covariance[7] = noise_cov
00126 self.ground_truth.pose.covariance[35] = noise_cov / 4.0
00127 self.pub_init_guess.publish(self.ground_truth)
00128
00129
00130 def cb_commands_robot(self,msg):
00131
00132 if msg.data == "all WP change" or msg.data == "%s WP change"%self.hostname:
00133 self.stopped = not(self.stopped)
00134
00135
00136 if self.stopped:
00137 rospy.loginfo("I am stopped %s", self.hostname)
00138 return
00139
00140 if (msg.data == "all init Guess"):
00141 self.publish_init_guess(0.01, self.noise_std)
00142
00143 if msg.data == "all Stop" or msg.data == "%s Stop"%self.hostname:
00144 self.client.cancel_all_goals()
00145
00146 if msg.data == "all Start" or msg.data == "%s Start"%self.hostname:
00147 self.client.send_goal(self.cur_goal_msg)
00148
00149 if msg.data == "all circle" or msg.data == "%s circle"%self.hostname:
00150 self.circling = not(self.circling)
00151 rospy.loginfo("Set circling to %s", str(self.circling));
00152 if msg.data == "all next Goal" or msg.data == "%s next Goal"%self.hostname:
00153 self.cur_goal += 1
00154 if (self.cur_goal == self.num_goals):
00155 self.cur_goal = 0
00156 self.cur_goal_msg = self.return_cur_goal()
00157 self.client.send_goal(self.cur_goal_msg)
00158
00159 rospy.loginfo("Send new Goal")
00160
00161 if msg.data == "all send delayed Goal" or msg.data == "%s send delayed Goal"%self.hostname:
00162 self.client.send_goal(self.sent_goal)
00163
00164 def msg_to_quaternion(msg):
00165 return [msg.x, msg.y, msg.z, msg.w]
00166
00167
00168 if __name__ == '__main__':
00169 rospy.init_node('controller_robots')
00170 controller_waypoints = ControllerRobots()
00171
00172 r = rospy.Rate(20)
00173 while not rospy.is_shutdown():
00174 r.sleep()
00175 rospy.delete_param("/move_base/")