simple_simulator.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 import os
00003 import argparse
00004 import rospkg
00005 import rospy
00006 import time
00007 import ConfigParser
00008 import random
00009 import socket
00010 import math
00011 from nav_msgs.msg import Odometry
00012 from geometry_msgs.msg import Point
00013 from adhoc_communication.srv import ChangeMCMembership
00014 
00015 publishers = [];
00016 position_x = [];
00017 position_y = [];
00018 
00019 rospy.loginfo("Inited Positions and Publishers")
00020 rospy.init_node("test_route_repair")
00021 
00022 
00023 looks = []; # 0 = N ; 1 = E ; 2 = S ; 3 = W
00024 #TMP Size of map  100x100
00025 width = 25
00026 height = 25
00027 
00028 num_robots = rospy.get_param('~num_robots');
00029 distance_to_connect = rospy.get_param('~distance');
00030 
00031 for num_robot in range(0,num_robots):
00032         full_topic = "robot_%i/base_pose_ground_truth"%num_robot
00033         publishers.append(rospy.Publisher(full_topic,Odometry,queue_size=10,latch=True))
00034         x = random.randint(0,width)
00035         y = random.randint(0,height)
00036         position_x.append(x)
00037         position_y.append(y)
00038         looks.append(random.randrange(0,4))
00039 
00040 while not rospy.is_shutdown():
00041         rospy.sleep(1.)
00042         #rospy.loginfo("start")
00043 #Change positions randomply:
00044 #50% forward, 20%Left 20%Right 10%Back
00045         for num_robot in range(0,num_robots):
00046                 val = random.randint(0,10)
00047                 #rospy.loginfo("--%i--"%val)
00048                 if val > 3: #Forward
00049                         if looks[num_robot] == 0:
00050                                 position_x[num_robot] = position_x[num_robot] + 1
00051                         elif looks[num_robot] == 1:
00052                                 position_y[num_robot] = position_y[num_robot] + 1
00053                         elif looks[num_robot] == 2:
00054                                 position_x[num_robot] = position_x[num_robot] - 1
00055                         elif looks[num_robot] == 3:
00056                                 position_y[num_robot] = position_y[num_robot] - 1
00057                 elif val > 2:#Left
00058                         looks[num_robot] = looks[num_robot] - 1
00059                         if looks[num_robot] < 0:
00060                                 looks[num_robot] = 3
00061                 elif val > 1:#Right
00062                         looks[num_robot] = looks[num_robot] + 1
00063                         if looks[num_robot] > 3:
00064                                 looks[num_robot] = 0
00065                 else: #Back
00066                         if looks[num_robot] == 0:
00067                                 position_x[num_robot] = position_x[num_robot] - 1
00068                         elif looks[num_robot] == 1:
00069                                 position_y[num_robot] = position_y[num_robot] - 1
00070                         elif looks[num_robot] == 2:
00071                                 position_x[num_robot] = position_x[num_robot] + 1
00072                         elif looks[num_robot] == 3:
00073                                 position_y[num_robot] = position_y[num_robot] + 1
00074                 if position_x[num_robot] < 1:
00075                         looks[num_robot] = 0
00076                 if position_x[num_robot] > height:
00077                         looks[num_robot] = 3
00078 
00079                 if position_y[num_robot] < 0:
00080                         looks[num_robot] = 1
00081                 if position_y[num_robot] > width:
00082                         looks[num_robot] = 4
00083 
00084 
00085         for num_robot in range(0,num_robots):
00086                 msg = Odometry()
00087                 msg.header.stamp = rospy.Time.now()
00088                 msg.pose.pose.position = Point(position_x[num_robot], position_y[num_robot], 0)
00089                 msg.header.frame_id = str(looks[num_robot])
00090                 publishers[num_robot].publish(msg)
00091 
00092         for cur_robot in range(0,num_robots):
00093                         for compare_robot in range(0,num_robots):
00094                                 if cur_robot == compare_robot:
00095                                         continue
00096                                 else:
00097                                         dx = position_x[cur_robot] - position_x[compare_robot]
00098                                         dy = position_y[cur_robot] - position_y[compare_robot]
00099                                         dis = math.sqrt(dx*dx+dy*dy)
00100                                         #rospy.loginfo("dx:%i"%dx)
00101                                         #rospy.loginfo("dy:%i"%dy)
00102                                         #rospy.loginfo("dis:%i"%dis)
00103                                         if dis < distance_to_connect:
00104                                                 name = "/robot_%i/adhoc_communication/join_mc_group"%cur_robot
00105                                                 group_name = "robot_"+str(compare_robot)
00106                                                 rospy.loginfo("%s"%group_name)
00107                                                 rospy.loginfo(" is calling %s to join group"%name)
00108                                                 join_service = rospy.ServiceProxy(name, ChangeMCMembership)
00109                                                 res = join_service(group_name,1)
00110                                                 #rospy.loginfo("result:%i"%res)
00111                                         #join mc groud


adhoc_communication
Author(s): Guenter Cwioro , Torsten Andre
autogenerated on Thu Jun 6 2019 20:59:43