File size: 4,728 Bytes
9869d0d | 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 |
import yaml
import argparse
import numpy as np
def calculate_orientations(points):
"""Calculate orientations for a list of points."""
# Remove consecutive duplicate points
unique_points = [points[0]] if points else []
for p in points[1:]:
# Use a tolerance for float comparison
if not np.allclose(p, unique_points[-1]):
unique_points.append(p)
points = unique_points
waypoints = []
thetas = []
for i in range(len(points) - 1):
p1 = points[i]
p2 = points[i+1]
thetas.append(np.arctan2(p2[1] - p1[1], p2[0] - p1[0]))
if len(points) > 1:
thetas.append(thetas[-1]) # Last point orientation
elif len(points) == 1:
thetas.append(0)
for i in range(len(points)):
waypoints.append({'pose': {'position': {'x': float(points[i][0]), 'y': float(points[i][1])}, 'orientation': {'theta': float(thetas[i])}}})
return waypoints
def generate_square_route(size=5, step=1):
"""Generate a square route."""
points = []
# side 1: from (0,0) to (size,0)
points.extend([(x, 0.0) for x in np.arange(0, size, step)])
# side 2: from (size,0) to (size,size)
points.extend([(size, y) for y in np.arange(0, size, step)])
# side 3: from (size,size) to (0,size)
points.extend([(x, size) for x in np.arange(size, 0, -step)])
# side 4: from (0,size) to (0,0)
points.extend([(0.0, y) for y in np.arange(size, 0, -step)])
# Close the loop
points.append(points[0])
return calculate_orientations(points)
def generate_u_route(width=5, length=10, step=1, num_points=20):
"""Generate a U-shaped route with a smooth curve, starting in the X direction."""
points = []
# Leg 1: along X axis
for x in np.arange(0, length, step):
points.append((x, 0.0))
# Smooth curve (half-circle)
radius = width / 2.0
center_x = length
center_y = radius
for i in range(num_points + 1):
angle = -np.pi/2 + np.pi * i / num_points # angle from -pi/2 to pi/2
x = center_x + radius * np.cos(angle)
y = center_y + radius * np.sin(angle)
points.append((x, y))
# Leg 2: along X axis (reversed)
for x in np.arange(length, 0, -step):
points.append((x, width))
return calculate_orientations(points)
def generate_eighth_route(radius=5, num_points=50):
"""Generate a figure-eight route."""
points = []
t = np.linspace(0, 2 * np.pi, num_points)
x = radius * np.sin(t)
y = radius * np.sin(t) * np.cos(t)
for i in range(len(x)):
points.append((x[i], y[i]))
return calculate_orientations(points)
def generate_sinusoidal_route(length=20, amplitude=5, num_points=50):
"""Generate a sinusoidal route."""
points = []
x = np.linspace(0, length, num_points)
y = amplitude * np.sin(x)
for i in range(len(x)):
points.append((x[i], y[i]))
return calculate_orientations(points)
def main():
parser = argparse.ArgumentParser(description="Generate a route for robot navigation.")
parser.add_argument('shape', choices=['square', 'u', 'eighth', 'sinusoidal'], help="The shape of the route to generate.")
parser.add_argument('-o', '--output', default='params/new_route.yaml', help="The output file name.")
parser.add_argument('--size', type=float, default=5, help="Size parameter for the shape (e.g., side length for square).")
parser.add_argument('--step', type=float, default=1, help="Step size for generating points.")
parser.add_argument('--width', type=float, default=5, help="Width for U-shape.")
parser.add_argument('--length', type=float, default=10, help="Length for U-shape or sinusoidal.")
parser.add_argument('--amplitude', type=float, default=5, help="Amplitude for sinusoidal.")
parser.add_argument('--radius', type=float, default=5, help="Radius for figure-eight.")
parser.add_argument('--num_points', type=int, default=50, help="Number of points for curves.")
args = parser.parse_args()
waypoints = []
if args.shape == 'square':
waypoints = generate_square_route(args.size, args.step)
elif args.shape == 'u':
waypoints = generate_u_route(args.width, args.length, args.step, args.num_points)
elif args.shape == 'eighth':
waypoints = generate_eighth_route(args.radius, args.num_points)
elif args.shape == 'sinusoidal':
waypoints = generate_sinusoidal_route(args.length, args.amplitude, args.num_points)
with open(args.output, 'w') as f:
yaml.dump({'waypoints': waypoints}, f, default_flow_style=False)
print(f"Generated {args.shape} route with {len(waypoints)} waypoints in {args.output}")
if __name__ == '__main__':
main()
|