-
Notifications
You must be signed in to change notification settings - Fork 15
/
P3_traj_planning.py
79 lines (68 loc) · 2.73 KB
/
P3_traj_planning.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
import numpy as np
from P1_astar import DetOccupancyGrid2D, AStar
from P2_rrt import *
import scipy.interpolate
import matplotlib.pyplot as plt
from HW1.P1_differential_flatness import *
from HW1.P2_pose_stabilization import *
from HW1.P3_trajectory_tracking import *
class SwitchingController(object):
"""
Uses one controller to initially track a trajectory, then switches to a
second controller to regulate to the final goal.
"""
def __init__(self, traj_controller, pose_controller, t_before_switch):
self.traj_controller = traj_controller
self.pose_controller = pose_controller
self.t_before_switch = t_before_switch
def compute_control(self, x, y, th, t):
"""
Inputs:
(x,y,th): Current state
t: Current time
Outputs:
V, om: Control actions
"""
t_final = self.traj_controller.traj_times[-1]
if t < t_final - self.t_before_switch:
return self.traj_controller.compute_control(x, y, th, t)
else:
return self.pose_controller.compute_control(x, y, th, t)
def compute_smoothed_traj(path, V_des, alpha, dt):
"""
Fit cubic spline to a path and generate a resulting trajectory for our
wheeled robot.
Inputs:
path (np.array [N,2]): Initial path
V_des (float): Desired nominal velocity, used as a heuristic to assign nominal
times to points in the initial path
alpha (float): Smoothing parameter (see documentation for
scipy.interpolate.splrep)
dt (float): Timestep used in final smooth trajectory
Outputs:
traj_smoothed (np.array [N,7]): Smoothed trajectory
t_smoothed (np.array [N]): Associated trajectory times
Hint: Use splrep and splev from scipy.interpolate
"""
########## Code starts here ##########
########## Code ends here ##########
return traj_smoothed, t_smoothed
def modify_traj_with_limits(traj, t, V_max, om_max, dt):
"""
Modifies an existing trajectory to satisfy control limits and
interpolates for desired timestep.
Inputs:
traj (np.array [N,7]): original trajecotry
t (np.array [N]): original trajectory times
V_max, om_max (float): control limits
dt (float): desired timestep
Outputs:
t_new (np.array [N_new]) new timepoints spaced dt apart
V_scaled (np.array [N_new])
om_scaled (np.array [N_new])
traj_scaled (np.array [N_new, 7]) new rescaled traj at these timepoints
Hint: This should almost entirely consist of calling functions from Problem Set 1
"""
########## Code starts here ##########
########## Code ends here ##########
return t_new, V_scaled, om_scaled, traj_scaled