Skip to content

Commit

Permalink
Added super class function to uipdate local and global Trajectories.
Browse files Browse the repository at this point in the history
Did some refactroing on stanlye and MPC controllers to move sub functions into the super class.

Started type setting function and adding good documentation.
  • Loading branch information
mehulgoel873 committed Feb 28, 2024
1 parent ea4ae46 commit 728ed59
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion rb_ws/src/buggy/scripts/auton/autonsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def planner_tick(self, curr_pose):
# update local trajectory via path planner
self.cur_traj, self.local_controller.current_traj_index = self.path_planner.compute_traj(
other_pose,
other_speed, self.local_controller.current_traj_index, curr_pose)
other_speed, self.local_controller.current_glob_index, curr_pose)
if __name__ == "__main__":
rospy.init_node("auton_system")
parser = argparse.ArgumentParser()
Expand Down
24 changes: 22 additions & 2 deletions rb_ws/src/buggy/scripts/auton/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pose import Pose
from sensor_msgs.msg import NavSatFix
from world import World
from typing import Tuple


class Controller(ABC):
Expand All @@ -22,7 +23,7 @@ class Controller(ABC):
WHEELBASE = SC_WHEELBASE
current_traj_index = 0

def __init__(self, start_index, buggy_name) -> None:
def __init__(self, start_index, buggy_name, global_trajectory : Trajectory) -> None:
self.buggy_name = buggy_name
# self.trajectory_forward_1 = rospy.Publisher(
# buggy_name + "/auton/debug/forward1_navsat", NavSatFix, queue_size=1
Expand All @@ -40,6 +41,8 @@ def __init__(self, start_index, buggy_name) -> None:
# self.forward_publishers = [self.trajectory_forward_1, self.trajectory_forward_2, self.trajectory_forward_3]
# self.backward_publishers = [self.trajectory_backward_1]
self.current_traj_index = start_index
self.current_glob_index = 0
self.global_trajectory = global_trajectory

@abstractmethod
def compute_control(
Expand Down Expand Up @@ -91,7 +94,24 @@ def plot_trajectory(
navsat.longitude = backward_gps[1]
self.backward_publishers[i-1].publish(navsat)


def updateTrajectoryIndexes(self, current_pose : Pose, local_trajectory : Trajectory): #How do I return void type in type casting
traj_index = local_trajectory.get_closest_index_on_path(
current_pose.x,
current_pose.y,
start_index=self.current_traj_index,
end_index=self.current_traj_index + 20,
subsample_resolution=1000,
)
self.current_traj_index = max(traj_index, self.current_traj_index)

glob_index = self.global_trajectory.get_closest_index_on_path(
current_pose.x,
current_pose.y,
start_index=self.current_glob_index,
start_index=self.current_glob_index + 2,
subsample_resolution=1000,
)
self.current_glob_index = max(glob_index, self.current_glob_index)



20 changes: 9 additions & 11 deletions rb_ws/src/buggy/scripts/auton/model_predictive_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class ModelPredictiveController(Controller):
state_cost_diag = np.diag(state_cost)
control_cost_diag = np.diag(control_cost)

def __init__(self, buggy_name, start_index=0, ref_trajectory=None, ROS=False) -> None:
def __init__(self, buggy_name, global_traj : Trajectory, start_index=0, ref_trajectory=None, ROS=False) -> None:
# instantiate parent
super(ModelPredictiveController, self).__init__(start_index, buggy_name)
super(ModelPredictiveController, self).__init__(start_index, buggy_name, global_traj)

# Internal variables
self.current_traj_index = 0 # Where in the trajectory we are currently
Expand Down Expand Up @@ -348,7 +348,7 @@ def transform_trajectory(trajectory, transform_matrix):

first_iteration = True

def compute_trajectory(self, current_pose: Pose, trajectory: Trajectory, current_speed: float):
def compute_trajectory(self, current_pose: Pose, trajectory: Trajectory, current_speed: float, ):
"""
Computes the desired control output given the current state and reference trajectory
Expand All @@ -375,14 +375,12 @@ def compute_trajectory(self, current_pose: Pose, trajectory: Trajectory, current
t = time.time()
self.current_speed = current_speed

traj_index = trajectory.get_closest_index_on_path(
current_pose.x,
current_pose.y,
start_index=self.current_traj_index,
end_index=self.current_traj_index + 20,
subsample_resolution=1000,
)
self.current_traj_index = max(traj_index, self.current_traj_index)

#Finding index on local path
#Do the same on global trajectory
super.updateTrajectoryIndexes(current_pose, trajectory)


traj_distance = trajectory.get_distance_from_index(self.current_traj_index)
traj_distance += self.LOOKAHEAD_TIME * self.current_speed

Expand Down
12 changes: 3 additions & 9 deletions rb_ws/src/buggy/scripts/auton/stanley_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class StanleyController(Controller):
CROSS_TRACK_GAIN = 1
HEADING_GAIN = 0.3

def __init__(self, buggy_name, start_index=0) -> None:
super(StanleyController, self).__init__(start_index, buggy_name)
def __init__(self, buggy_name, global_traj : Trajectory, start_index=0) -> None:
super(StanleyController, self).__init__(start_index, buggy_name, global_traj)
self.debug_reference_pos_publisher = rospy.Publisher(
buggy_name + "/auton/debug/reference_navsat", NavSatFix, queue_size=1
)
Expand Down Expand Up @@ -58,13 +58,7 @@ def compute_control(
front_x = x + StanleyController.WHEELBASE * np.cos(heading)
front_y = y + StanleyController.WHEELBASE * np.sin(heading)

traj_index = trajectory.get_closest_index_on_path(
front_x,
front_y,
start_index=self.current_traj_index - 20,
end_index=self.current_traj_index + 50,
)
self.current_traj_index = max(traj_index, self.current_traj_index)
super.updateTrajectoryIndexes(current_pose, trajectory)

# Calculate heading error

Expand Down

0 comments on commit 728ed59

Please sign in to comment.