Skip to content

Commit 26b5029

Browse files
Rendezvous Completed
1 parent 022b537 commit 26b5029

File tree

9 files changed

+181
-100
lines changed

9 files changed

+181
-100
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,6 @@ CATKIN_IGNORE
320320

321321
sphero_stage_2/.vscode
322322
sphero_stage_2/example.yml
323-
sphero_stage_2/cfg
323+
sphero_stage_2/src/mrs_part_02/prac.py
324324
# sphero_stage_2/src/mrs_part_02/rviz_utils/__pycache__
325325
*__pycache__*

sphero_stage_2/launch/launch_params.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# map_name: 'simple_maze_10x10' # Base name of the map
44
# map_name: 'hard_maze_10x10' # Base name of the map
55
map_name: 'empty_10x10' # Base name of the map
6-
num_of_robots: 4 # Number of robots to spawn
6+
num_of_robots: 3 # Number of robots to spawn
77
distribution: 'circle' # One of the options from 'distribution_list'
88
# distribution: 'line' # One of the options from 'distribution_list'
99

sphero_stage_2/launch/sphero_sim.rviz

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,44 +175,34 @@ Visualization Manager:
175175
- Alpha: 1
176176
Class: rviz/RobotModel
177177
Collision Enabled: false
178-
Enabled: true
178+
Enabled: false
179179
Links:
180180
All Links Enabled: true
181181
Expand Joint Details: false
182182
Expand Link Details: false
183183
Expand Tree: false
184184
Link Tree Style: Links in Alphabetic Order
185-
base_link:
186-
Alpha: 1
187-
Show Axes: false
188-
Show Trail: false
189-
Value: true
190185
Name: RobotModel2
191186
Robot Description: robot_description
192187
TF Prefix: robot_2
193188
Update Interval: 0
194-
Value: true
189+
Value: false
195190
Visual Enabled: true
196191
- Alpha: 1
197192
Class: rviz/RobotModel
198193
Collision Enabled: false
199-
Enabled: true
194+
Enabled: false
200195
Links:
201196
All Links Enabled: true
202197
Expand Joint Details: false
203198
Expand Link Details: false
204199
Expand Tree: false
205200
Link Tree Style: Links in Alphabetic Order
206-
base_link:
207-
Alpha: 1
208-
Show Axes: false
209-
Show Trail: false
210-
Value: true
211201
Name: RobotModel3
212202
Robot Description: robot_description
213203
TF Prefix: robot_3
214204
Update Interval: 0
215-
Value: true
205+
Value: false
216206
Visual Enabled: true
217207
- Alpha: 1
218208
Class: rviz/RobotModel
@@ -543,15 +533,15 @@ Visualization Manager:
543533
Marker Topic: /region_/robot_2/odom
544534
Name: Marker
545535
Namespaces:
546-
semicircle: true
536+
{}
547537
Queue Size: 100
548538
Value: true
549539
- Class: rviz/Marker
550540
Enabled: true
551541
Marker Topic: /region_/robot_3/odom
552542
Name: Marker
553543
Namespaces:
554-
semicircle: true
544+
{}
555545
Queue Size: 100
556546
Value: true
557547
- Class: rviz/Marker

sphero_stage_2/launch/start.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import rospkg
1111
import random
1212
import math
13-
14-
13+
import subprocess
14+
import time
1515

1616
def distribute_circle(k, n, center_x=0, center_y=0, radius=1):
1717
x = radius * math.cos(k / n * 2 * math.pi) + center_x
@@ -82,8 +82,18 @@ def create_files(resources, config):
8282

8383
return True
8484

85+
# Function to start ROS master
86+
def start_roscore():
87+
# Starting roscore
88+
subprocess.Popen("roscore")
89+
print("Starting roscore...")
90+
91+
# Wait a bit to ensure roscore has started
92+
time.sleep(1)
8593

8694
def main():
95+
# Start roscore
96+
8797
start_rviz = 'true' if '--rviz' in sys.argv else 'false'
8898

8999
# Set up launch variables. These are hard-coded and they shouldn't be changed.
@@ -124,6 +134,8 @@ def main():
124134
launch.shutdown()
125135

126136
if __name__ == '__main__':
137+
start_roscore()
138+
127139
rospy.init_node('stage_launcher', anonymous=True)
128140

129141
print("\033[36m \nInitialized launcher node. Starting launch process. \033[0m")

sphero_stage_2/resources/worlds/empty_10x10_2.world

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ floorplan
1010
)
1111

1212
# throw in robots
13-
sphero( pose [ 0.000 0.000 0 0.000 ] name "sphero_0" color "red")
14-
sphero( pose [ 0.000 1.000 0 0.000 ] name "sphero_1" color "red")
13+
sphero( pose [ 0.400 0.000 0 0.000 ] name "sphero_0" color "red")
14+
sphero( pose [ -0.400 0.000 0 0.000 ] name "sphero_1" color "red")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include "definitions.inc"
2+
3+
# load an environment bitmap
4+
floorplan
5+
(
6+
name "empty_10x10"
7+
bitmap "../maps/empty_10x10/empty_10x10.bmp"
8+
size [10 10 1] # duzina sirina visina
9+
pose [ 0 0 0 0 ]
10+
)
11+
12+
# throw in robots
13+
sphero( pose [ 0.400 0.000 0 0.000 ] name "sphero_0" color "red")
14+
sphero( pose [ -0.200 0.346 0 0.000 ] name "sphero_1" color "red")
15+
sphero( pose [ -0.200 -0.346 0 0.000 ] name "sphero_2" color "red")

sphero_stage_2/src/mrs_part_02/main_2.py

Lines changed: 65 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_goal(self, goal):
2929

3030
print("New goal received: ({}, {})".format(goal.pose.position.x, goal.pose.position.y))
3131
self.target_received = True
32-
self.target = [goal.pose.position.x, goal.pose.position.y]
32+
# self.target = [goal.pose.position.x, goal.pose.position.y]
3333

3434
def send_velocity(self, frame_id, send_velocity_v):
3535

@@ -46,87 +46,110 @@ def calculate_distance(self, point1, point2):
4646
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
4747
return distance
4848

49+
# update all parameters of the boid
50+
def update_param(self, agent_msg, agent, boids):
4951

50-
52+
agent.position_v = get_agent_position(agent_msg) # current position of the agent
53+
agent.target_list = [boids[value].position_v for value in agent.edges]
54+
agent.position_error_list = [agent.target_list[i]-agent.position_v for i in range(len(agent.edges))]
55+
agent.pos_diff_list = [agent.position_error_list[i].norm() for i in range(len(agent.edges))]
56+
agent.arrive() # update the current velocity of the agent
57+
self.target_reached_list[agent.id] = agent.target_reached
5158

5259
def odom_callback(self, msg):
53-
60+
5461
frame_id = msg.header.frame_id
5562
id = frame_id[7]
5663

5764
if frame_id not in self.cmd_vel_pub:
5865
self.cmd_vel_pub[frame_id] = rospy.Publisher("{}cmd_vel".format(frame_id[:9]), Twist, queue_size=10)
5966

60-
self.agent= self.agents[int(id)]
61-
62-
if self.target_received:
63-
_, nav_velocity_v, self.target_reached_list[int(id)] = self.agent.get_cmd_vel(msg, self.target, self.target_reached_list[int(id)])
64-
if all(self.target_reached_list):
65-
self.target_received = False
66-
self.target_reached_list = [False for i in range(self.num_of_robots)]
67-
68-
print("Target position reached!")
69-
else:
70-
nav_velocity_v = Vector2()
71-
72-
# """ ===== Total Velocity ======"""
73-
74-
self.Total_velocity = self.nav_weight * nav_velocity_v
75-
self.send_velocity(frame_id, self.Total_velocity)
76-
77-
78-
79-
80-
67+
self.agent= self.agents[int(id)] # taking instance of that robot
68+
self.agent.position_v = get_agent_position(msg)
8169

70+
if self.agent.edges != [None]: # if not a stub born
8271

72+
if self.target_received:
73+
self.update_param(msg, self.agent, self.agents)
74+
if all(self.target_reached_list):
75+
self.target_received = False
8376

77+
print("Target position reached!")
8478

79+
self.Total_velocity = self.nav_weight * self.agent.cur_vel_v
80+
self.send_velocity(frame_id, self.Total_velocity)
8581

8682

8783
def __init__(self):
8884

8985
# initialize
90-
self.count = 1
9186
self.target_received = False
92-
87+
self.num_of_robots = rospy.get_param("/num_of_robots")
9388

94-
# fixed parameters
95-
self.num_of_robots = rospy.get_param("/num_of_robots")
96-
self.target = [0.0, 0.0]
97-
self.velocity = [0.0, 0.0] # Initial velocity
98-
self.initial_time = rospy.get_time()
99-
self.cmd_vel_pub = {}
100-
self.target_reached_list = [False for i in range(self.num_of_robots)]
101-
89+
""" Edges and Adjacency matrix """
90+
# Adjacency matrix
91+
self.adj_mat = np.zeros((self.num_of_robots, self.num_of_robots))
92+
self.edges = {}
93+
self.edges[0] = [1] # self.edges[1] = [2, 3]: 1 listens to 2 and 3
94+
self.edges[1] = [2] # self.edges[2] = [0] : 2 listens to no one
95+
self.edges[2] = [None] # self.edges[3] = [1, 3]: 3 listens to 1 and 3
96+
97+
""" Build adjacency matrix """
98+
for key, values in self.edges.items():
99+
for value in values:
100+
if value == None: # stub born
101+
continue
102+
self.adj_mat[key, value] = 1
103+
104+
""" List Parameters to be updated by all robots """
105+
self.cmd_vel_pub = {}
106+
self.target_reached_list = [False for i in range(self.num_of_robots)]
107+
self.target_lists = [list() for i in range(self.num_of_robots)]
108+
self.position_error_list = [list() for i in range(self.num_of_robots)]
109+
self.pos_diff_list = [list() for i in range(self.num_of_robots)]
110+
111+
102112
# Tunable parameters
103-
self.max_speed = 0.6
113+
self.max_speed = 0.6
104114
self.slowing_radius = 1.0
105-
self.search_radius = 2.0
115+
self.search_radius = 2.0
106116

107117
# Weights
108118
self.nav_weight = 1
119+
self.nav_velocity_v = Vector2()
109120
self.Total_velocity = Vector2()
110121

111-
112122
# self.map_subscriber = rospy.Subscriber("/map", nav_msgs.msg.OccupancyGrid, self.read_map_callback_2)
113123
self.move_goal_sub = rospy.Subscriber("/move_base_simple/goal", PoseStamped, self.get_goal)#None # TODO: subscriber to /move_base_simple/goal published by rviz
114124

115-
rate = rospy.Rate(10)
116-
rate.sleep()
117-
118-
self.agents = [Boid(initial_velocity_x=0.0, initial_velocity_y=0.0, max_speed_mag=self.max_speed,
119-
slowing_radius=self.slowing_radius) for _ in range(self.num_of_robots)]
125+
126+
self.agents = [Boid(self.adj_mat, self.edges[id], id, self.position_error_list[id], self.pos_diff_list[id], self.target_lists[id],
127+
self.max_speed, self.slowing_radius, self.search_radius) for id in range(self.num_of_robots)]
128+
129+
130+
for key, values in self.edges.items():
131+
if values == [None]:
132+
self.target_reached_list[key] = True
133+
self.target_lists[key] = [None]
134+
self.position_error_list[key] = [0]
135+
self.pos_diff_list[key] = [0]
136+
else:
137+
self.target_lists[key] = [self.agents[value].position_v for value in values]
138+
120139

121140
[rospy.Subscriber("robot_{}/odom".format(i), Odometry, self.odom_callback) for i in range(self.num_of_robots)]
122141

123142

143+
124144

125145

126146
if __name__ == '__main__':
127147
try:
128148
rospy.init_node('consensus_mrs_2')
129149
robot = Robot_move()
150+
# rate = rospy.Rate(10)
151+
# rate.sleep()
152+
130153
rospy.spin()
131154

132155
except rospy.ROSInterruptException:

0 commit comments

Comments
 (0)