-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswarm_io.py
115 lines (101 loc) · 4.17 KB
/
swarm_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
class SwarmIO:
def __init__(self):
pass
def read_cbs_plan_file(self, file_path):
paths = {}
with open(file_path, 'r') as file:
for line in file:
line = line.strip()
if not line:
continue # Skip empty lines
# Split the line into robot ID and path data
if ':' not in line:
raise ValueError(f"Invalid line in plan file: '{line}'")
robot_id_str, path_data = line.split(':', 1)
robot_id = int(robot_id_str.replace('Robot', '').strip())
# Parse the path data
path_points = path_data.split(';')
paths[robot_id] = []
for point in path_points:
if point.strip(): # Skip empty segments
coords = point.split(',')
if len(coords) != 4:
raise ValueError(f"Invalid path point: '{point}' in Robot {robot_id}")
x, y, th, t = map(float, coords)
paths[robot_id].append([x, y, th, t])
return paths
def read_map_file(self, file_path):
robots = []
goals = []
obstacles = []
with open(file_path, 'r') as file:
for line in file:
parts = line.strip().split(',')
if parts[0] == 'robot':
robots.append({
'id': int(parts[1]),
'x': float(parts[2]),
'y': float(parts[3]),
'orientation': float(parts[4])
})
elif parts[0] == 'goal':
goals.append({
'id': int(parts[1]),
'x': float(parts[2]),
'y': float(parts[3]),
'orientation': float(parts[4])
})
elif parts[0] == 'obstacle':
obstacles.append({
'x1': float(parts[1]),
'y1': float(parts[2]),
'x2': float(parts[3]),
'y2': float(parts[4])
})
elif parts[0] == 'formation':
pass
elif parts[0] == 'leader':
pass
else:
raise ValueError(f"Invalid line in map file: '{line}'")
return robots, goals, obstacles
def write_cbs_output_file(self, paths, output_path):
with open(output_path, 'w') as file:
for robot_id, path in paths.items():
path_str = '; '.join([f'{x:.2f},{y:.2f},{th:.2f},{t:.2f}' for x, y, th, t in path])
file.write(f'Robot {robot_id}: {path_str}\n')
def write_rrt_plan_file(self, filename, path):
with open(filename, 'w') as f:
for waypoint in path:
# x, y, th, t
# print(waypoint)
f.write("{},{},{},{}\n".format(waypoint[0], waypoint[1], waypoint[2], waypoint[3]))
def readFormation(self, file_path):
formations = []
leader = None
with open(file_path, 'r') as file:
for line in file:
parts = line.strip().split(',')
if parts[0] == 'formation':
formations.append({
'filename': parts[1],
'time': float(parts[2]),
})
elif parts[0] == 'leader':
leader = {
'x': float(parts[1]),
'y': float(parts[2]),
}
return formations, leader
def read_formation_file(self, file_path):
formation = []
with open(file_path, 'r') as file:
for line in file:
parts = line.strip().split(',')
formation.append({
'x': float(parts[2]),
'y': float(parts[3]),
'orientation': float(parts[4])
})
return formation