-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_controller.py
More file actions
294 lines (245 loc) · 10.4 KB
/
Copy pathmain_controller.py
File metadata and controls
294 lines (245 loc) · 10.4 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
Main Controller
Coordinates AFM, AZR, and APT modules to complete path tracking
"""
import numpy as np
import matplotlib.pyplot as plt
import csv
import sys
import os
# Add project root to path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from map_manager import MapManager
from controllers.AFM import AFM
from controllers.AZR import AZR
from controllers.APT import APT
def run_main_controller(map_type, waypoints=None):
"""
Run main controller path tracking
Args:
map_type: Map type to use
waypoints: List of waypoints with mode change information
Each waypoint is a dict: {'position': (x, y), 'mode': 'afm|azr|apt', 'direction': 'left|right'} (direction only for apt)
Returns:
positions: List of positions at 0.1 second intervals
rmse: Root mean square error
heading_error: Heading error in degrees
success_rate: Success rate in percentage
time: Completion time in seconds
"""
# Create map
manager = MapManager()
env = manager.create_map(map_type)
# Get initial state and reference path
initial_state = np.array([
env.initial_state['x'],
env.initial_state['y'],
env.initial_state['psi'],
0.0 # Initial speed
])
reference_path = env.reference_path
if waypoints is None and map_type == 'tri_mode_composite':
waypoints = [
{
'position': (env.apt_switch_x, env.lower_center_y),
'mode': 'afm',
'heading': 0.0,
},
{
'position': (env.apt_resume_x, env.upper_center_y),
'mode': 'apt',
'direction': 'left',
'heading': 0.0,
},
{
'position': (env.rotation_x, env.upper_center_y),
'mode': 'afm',
'heading': 0.0,
},
{
'position': (env.rotation_x, env.upper_center_y),
'mode': 'azr',
},
{
'position': env.end_point,
'mode': 'afm',
'heading': np.pi,
},
]
# Print reference path information
print(f"Reference path length: {len(reference_path)}")
print(f"Reference path start: {reference_path[0]}")
print(f"Reference path end: {reference_path[-1]}")
print(f"Reference path middle: {reference_path[len(reference_path)//2]}")
# Create controllers
afm = AFM(map_type=map_type)
azr = AZR(map_type=map_type)
apt = APT(map_type=map_type)
# Track path
print(f"Starting main controller path tracking - {map_type}")
# Default to AFM if no waypoints provided
if waypoints is None:
# Use AFM to track the full reference path
states, controls, rmse, heading_error, time, success_rate = afm.nmpc.track_path(
initial_state, reference_path, max_time=100.0, goal=env.end_point, goal_heading=env.end_heading
)
else:
# Use waypoints with mode changes
current_position = (env.initial_state['x'], env.initial_state['y'])
current_heading = env.initial_state['psi']
all_states = []
total_time = 0.0
# Process waypoints
for i, waypoint in enumerate(waypoints):
waypoint_pos = waypoint['position']
mode = waypoint['mode']
direction = waypoint.get('direction', 'left')
print(f"\n=== Processing waypoint {i+1}: {waypoint_pos} with {mode} mode ===")
if mode == 'afm':
# Use AFM to move to waypoint
wp_heading = waypoint.get('heading', current_heading)
# Get reference path from environment
if hasattr(env, '_generate_reference_path'):
reference_path = env._generate_reference_path()
else:
reference_path = env.reference_path
# Track path using full reference path
states, _, _, _, waypoint_time, _ = afm.track_path(
np.array([current_position[0], current_position[1], current_heading, 0.0]),
reference_path,
max_time=50.0, goal=waypoint_pos, goal_heading=wp_heading
)
all_states.extend(states)
total_time += waypoint_time
elif mode == 'azr':
# Use AZR to reverse direction at current position - stop any previous movement
print("Stopping AFM movement and reversing direction in-place...")
# Reverse direction in-place
final_pos, final_heading, azr_states = azr.reverse_direction(
current_position,
current_heading,
return_trajectory=True
)
if len(azr_states) > 0:
all_states.extend(azr_states)
total_time += max(0.0, (len(azr_states) - 1) * 0.1)
current_position = tuple(final_pos)
current_heading = final_heading
elif mode == 'apt':
# Use APT to translate to waypoint (pure translation) - stop any previous movement
wp_heading = waypoint.get('heading', current_heading)
final_pos, final_heading, apt_states = apt.translate(
(current_position[0], current_position[1], current_heading),
(waypoint_pos[0], waypoint_pos[1], wp_heading),
direction=direction,
return_trajectory=True
)
if len(apt_states) > 0:
all_states.extend(apt_states)
waypoint_time = max(0.0, (len(apt_states) - 1) * 0.1)
total_time += waypoint_time
current_position = final_pos
current_heading = final_heading
# Update current state (already updated for APT mode)
if mode == 'afm' and 'states' in locals() and len(states) > 0:
current_position = (states[-1][0], states[-1][1])
current_heading = states[-1][2]
# Process final leg to end
print(f"\n=== Processing final leg to end: {env.end_point} ===")
# Get reference path from environment
if hasattr(env, '_generate_reference_path'):
reference_path = env._generate_reference_path()
else:
reference_path = env.reference_path
# Track path using full reference path
states, controls, rmse, heading_error, final_time, success_rate = afm.track_path(
np.array([current_position[0], current_position[1], current_heading, 0.0]),
reference_path,
max_time=50.0, goal=env.end_point, goal_heading=env.end_heading
)
all_states.extend(states)
total_time += final_time
states = np.array(all_states)
time = total_time
# Extract positions at 0.1 second intervals
positions = []
dt = 0.1
current_time = 0.0
state_index = 0
while current_time < time and state_index < len(states):
# Find the state closest to current_time
while state_index < len(states) - 1 and (state_index + 1) * 0.1 <= current_time:
state_index += 1
positions.append(states[state_index][:2])
current_time += dt
# Add the final position
if len(states) > 0:
positions.append(states[-1][:2])
# Visualization
fig, ax = plt.subplots(figsize=(10, 6))
# Draw map
env.draw_track(ax)
# Draw tracking path
if len(states) > 0:
ax.plot(states[:, 0], states[:, 1], 'b-', label='Main Controller Tracking Path')
# Draw vehicle at final position
final_state = states[-1]
env._draw_vehicle_at(ax, final_state[0], final_state[1], final_state[2], color='green')
ax.set_aspect('equal')
ax.set_xlabel('X (m)')
ax.set_ylabel('Y (m)')
ax.set_title(f'{map_type} - Main Controller Path Tracking')
ax.legend()
ax.grid(True)
# Save image
output_path = f'outputs/{map_type}-main_controller.png'
plt.savefig(output_path, dpi=150, bbox_inches='tight')
plt.close()
print(f"\n{map_type} tracking results:")
print(f"RMSE: {rmse:.4f} m")
print(f"Heading error: {heading_error:.4f} deg")
print(f"Success rate: {success_rate:.2f}%")
print(f"Completion time: {time:.2f} s")
print(f"Image saved to: {output_path}")
return positions, rmse, heading_error, success_rate, time
def main():
"""
Main function
"""
map_types = ['map_a', 'map_b', 'map_c', 'tri_mode_composite'] # Process all maps
results = []
all_positions = {}
for map_type in map_types:
print(f"\n=== Processing map: {map_type} ===")
positions, rmse, heading_error, success_rate, time = run_main_controller(map_type)
results.append((map_type, rmse, heading_error, success_rate, time))
all_positions[map_type] = positions
# Save positions to CSV file
csv_path = 'outputs/main_controller_vehicle_positions.csv'
with open(csv_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
# Write header
header = []
for map_type in map_types:
header.extend([f'{map_type}_x', f'{map_type}_y'])
writer.writerow(header)
# Find maximum number of positions
max_len = max(len(positions) for positions in all_positions.values())
# Write data rows
for i in range(max_len):
row = []
for map_type in map_types:
if i < len(all_positions[map_type]):
x, y = all_positions[map_type][i]
row.extend([x, y])
else:
row.extend(['', ''])
writer.writerow(row)
print(f"\nPositions saved to: {csv_path}")
# Print summary results
print("\n=== Summary Results ===")
print("Map Type\tRMSE(m)\tHeading Error(deg)\tSuccess Rate(%)\tCompletion Time(s)")
for result in results:
print(f"{result[0]}\t{result[1]:.4f}\t{result[2]:.4f}\t\t{result[3]:.2f}\t\t{result[4]:.2f}")
if __name__ == "__main__":
main()