-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intermediate Pathgen #2266
Merged
Merged
Intermediate Pathgen #2266
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
595b1e1
initial test of tigers system
rishiso 87457a9
clean up and add comment
rishiso 5fd3f64
added line test
rishiso 0a6d243
fix line issue
rishiso 0796cbb
made comments and removed from settle
rishiso 387b992
Fix Code Style On intermediate-pathgen (#2267)
github-actions[bot] 5fce237
make pr changes
sanatd33 132f17a
add cache inter
sanatd33 e5131ef
start cacching intermediates
rishiso b053c40
add params
rishiso e4fd024
use abs angle
petergarud 463dcce
Fix Code Style On intermediate-pathgen (#2269)
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,4 +75,105 @@ Trajectory rrt(const LinearMotionInstant& start, const LinearMotionInstant& goal | |
return path; | ||
} | ||
|
||
static std::optional<std::tuple<double, double, double>> cached_intermediate_tuple_{}; | ||
|
||
Trajectory intermediate(const LinearMotionInstant& start, const LinearMotionInstant& goal, | ||
const MotionConstraints& motion_constraints, RJ::Time start_time, | ||
const rj_geometry::ShapeSet& static_obstacles) { | ||
// if already on goal, no need to move | ||
if (start.position.dist_to(goal.position) < 1e-6) { | ||
return Trajectory{{RobotInstant{Pose(start.position, 0), Twist(), start_time}}}; | ||
} | ||
|
||
// maybe straight line works | ||
Trajectory straight_trajectory = | ||
CreatePath::simple(start, goal, motion_constraints, start_time); | ||
|
||
// If we are very close to the goal (i.e. there physically can't be a robot | ||
// in our way) or the straight trajectory is feasible, we can use it. | ||
if (start.position.dist_to(goal.position) < kRobotRadius || | ||
(!trajectory_hits_static(straight_trajectory, static_obstacles, start_time, nullptr))) { | ||
return straight_trajectory; | ||
} | ||
|
||
// Generate list of intermediate points | ||
std::vector<rj_geometry::Point> intermediates = get_intermediates(start, goal); | ||
|
||
for (int i = 0; i < intermediate::PARAM_num_intermediates; i++) { | ||
rj_geometry::Point final_inter = intermediates[i]; | ||
|
||
// Step through the path from the robot to the final intermediate point | ||
// and test each point on that path as an intermediate point | ||
for (double t = intermediate::PARAM_step_size; t < final_inter.dist_to(start.position); | ||
t += intermediate::PARAM_step_size) { | ||
rj_geometry::Point intermediate = | ||
(final_inter - start.position).normalized(t) + start.position; | ||
Trajectory trajectory = | ||
CreatePath::simple(start, goal, motion_constraints, start_time, {intermediate}); | ||
|
||
// If the trajectory does not hit an obstacle, it is valid | ||
if ((!trajectory_hits_static(trajectory, static_obstacles, start_time, nullptr))) { | ||
auto angle = (final_inter - start.position).angle(); | ||
cached_intermediate_tuple_ = {abs(angle), signbit(angle) ? -1 : 1, | ||
(final_inter - start.position).mag()}; | ||
return trajectory; | ||
} | ||
} | ||
} | ||
|
||
// If all else fails, return the straight-line trajectory | ||
return straight_trajectory; | ||
} | ||
|
||
std::vector<rj_geometry::Point> get_intermediates(const LinearMotionInstant& start, | ||
const LinearMotionInstant& goal) { | ||
std::random_device rd; | ||
std::mt19937 gen(rd()); | ||
// Create a random distribution for the distance between the start | ||
// and the intermediate points | ||
std::uniform_real_distribution<> scale_dist(intermediate::PARAM_min_scale, | ||
intermediate::PARAM_max_scale); | ||
|
||
double angle_range = intermediate::PARAM_max_angle - intermediate::PARAM_min_angle; | ||
// Create a random distribution for the angle between the start | ||
// and the intermediate points | ||
std::uniform_real_distribution<> angle_dist(-angle_range, angle_range); | ||
|
||
std::vector<rj_geometry::Point> intermediates; | ||
std::vector<std::tuple<double, double, double>> inter_tuples; | ||
|
||
for (int i = 0; i < intermediate::PARAM_num_intermediates; i++) { | ||
double angle = angle_dist(gen); | ||
angle += std::copysign(intermediate::PARAM_min_angle, angle); | ||
angle = degrees_to_radians(angle); | ||
double scale = scale_dist(gen); | ||
|
||
// Generate random tuples of distances and angles | ||
inter_tuples.emplace_back(abs(angle), signbit(angle) ? -1 : 1, scale); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes look good |
||
} | ||
|
||
if (cached_intermediate_tuple_) { | ||
inter_tuples.push_back(*cached_intermediate_tuple_); | ||
} | ||
|
||
// Sort the list of tuples by the magnitude of angle | ||
// This ensures that we take paths with | ||
// smaller offsets from the simple path | ||
sort(inter_tuples.begin(), inter_tuples.end()); | ||
|
||
for (int i = 0; i < intermediate::PARAM_num_intermediates; i++) { | ||
double angle = std::get<0>(inter_tuples[i]) * std::get<1>(inter_tuples[i]); | ||
double scale = std::get<2>(inter_tuples[i]); | ||
|
||
double fin_angle = goal.position.angle_to(start.position) + angle; | ||
double fin_length = scale; | ||
|
||
// Convert the distances and angles into a point | ||
intermediates.push_back(start.position + rj_geometry::Point{fin_length * cos(fin_angle), | ||
fin_length * sin(fin_angle)}); | ||
} | ||
|
||
return intermediates; | ||
} | ||
|
||
} // namespace planning::CreatePath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "line.hpp" | ||
|
||
namespace strategy { | ||
|
||
Line::Line(const Position& other) : Position{other} { position_name_ = "Line"; } | ||
|
||
Line::Line(int r_id) : Position{r_id, "Line"} {} | ||
|
||
std::optional<RobotIntent> Line::derived_get_task(RobotIntent intent) { | ||
if (check_is_done()) { | ||
forward_ = !forward_; | ||
} | ||
|
||
if (vertical_) { | ||
if (forward_) { | ||
auto motion_command = planning::MotionCommand{ | ||
"path_target", | ||
planning::LinearMotionInstant{ | ||
rj_geometry::Point{ | ||
field_dimensions_.center_field_loc().x() - (robot_id_ - 3) * 1, | ||
field_dimensions_.center_field_loc().y() - 2.5 + 5 * 0.75, | ||
}, | ||
rj_geometry::Point{0.0, 0.0}, | ||
}, | ||
planning::FaceTarget(), true}; | ||
|
||
intent.motion_command = motion_command; | ||
} else { | ||
auto motion_command = planning::MotionCommand{ | ||
"path_target", | ||
planning::LinearMotionInstant{ | ||
rj_geometry::Point{ | ||
field_dimensions_.center_field_loc().x() - (robot_id_ - 3) * 1, | ||
field_dimensions_.center_field_loc().y() - 4.5 + 5 * 0.75, | ||
}, | ||
rj_geometry::Point{0.0, 0.0}, | ||
}, | ||
planning::FaceTarget(), true}; | ||
|
||
intent.motion_command = motion_command; | ||
} | ||
} else { | ||
if (forward_) { | ||
if (face_target_) { | ||
auto motion_command = planning::MotionCommand{ | ||
"path_target", | ||
planning::LinearMotionInstant{ | ||
rj_geometry::Point{ | ||
field_dimensions_.center_field_loc().x() - 2.5 + 5 * 0.75, | ||
(field_dimensions_.center_field_loc().y() - 1) / 6 * robot_id_ + 1, | ||
}, | ||
rj_geometry::Point{0.0, 0.0}, | ||
}, | ||
planning::FaceTarget(), true}; | ||
|
||
intent.motion_command = motion_command; | ||
} else { | ||
auto motion_command = planning::MotionCommand{ | ||
"path_target", | ||
planning::LinearMotionInstant{ | ||
rj_geometry::Point{ | ||
field_dimensions_.our_defense_area().maxx(), | ||
(field_dimensions_.center_field_loc().y() - 1) / 6 * robot_id_ + 1, | ||
}, | ||
rj_geometry::Point{0.0, 0.0}, | ||
}, | ||
planning::FaceAngle{0}, true}; | ||
|
||
intent.motion_command = motion_command; | ||
} | ||
|
||
} else { | ||
if (face_target_) { | ||
auto motion_command = planning::MotionCommand{ | ||
"path_target", | ||
planning::LinearMotionInstant{ | ||
rj_geometry::Point{ | ||
field_dimensions_.our_defense_area().minx(), | ||
(field_dimensions_.center_field_loc().y() - 1) / 6 * robot_id_ + 1, | ||
}, | ||
rj_geometry::Point{0.0, 0.0}, | ||
}, | ||
planning::FaceTarget(), true}; | ||
|
||
intent.motion_command = motion_command; | ||
} else { | ||
auto motion_command = planning::MotionCommand{ | ||
"path_target", | ||
planning::LinearMotionInstant{ | ||
rj_geometry::Point{ | ||
field_dimensions_.our_defense_area().minx(), | ||
(field_dimensions_.center_field_loc().y() - 1) / 6 * robot_id_ + 1, | ||
}, | ||
rj_geometry::Point{0.0, 0.0}, | ||
}, | ||
planning::FaceAngle{0}, true}; | ||
|
||
intent.motion_command = motion_command; | ||
} | ||
} | ||
} | ||
|
||
return intent; | ||
} | ||
} // namespace strategy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
#include "position.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
|
||
namespace strategy { | ||
class Line : public Position { | ||
public: | ||
Line(const Position& other); | ||
Line(int r_id); | ||
~Line() override = default; | ||
Line(const Line& other) = default; | ||
Line(Line&& other) = default; | ||
|
||
std::string get_current_state() override { return "Line"; } | ||
|
||
private: | ||
std::optional<RobotIntent> derived_get_task(RobotIntent intent) override; | ||
bool forward_ = true; | ||
bool vertical_ = false; | ||
bool face_target_ = false; | ||
}; | ||
} // namespace strategy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add intermediate that works to list of new random ones. Cache that so know have one that works. (only cache if know works). Cache means adding to array of possibilities so still find optimal one. This helps because might be finding worse random positions later and veering off path.