HoloLib
High-performance holonomic (X-Drive) control library for VEX V5
Loading...
Searching...
No Matches
obstacle_manager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Eigen/Core"
4#include "Eigen/Dense"
5#include <cstddef>
6#include <vector>
7
8namespace hololib {
9/**
10 * @brief Represents a physical obstacle on the field.
11 */
12struct Obstacle {
13 Eigen::Vector2f position; /**< 2D coordinate position of the obstacle */
14 float radius; /**< Radius of the obstacle used for avoidance */
15};
16
17/**
18 * @brief Manages a collection of obstacles and provides avoidance computations.
19 *
20 * Provides methods for checking path intersections and generating vectors to
21 * steer away from potential collisions.
22 */
24public:
25 /**
26 * @brief Default constructor for ObstacleManager.
27 */
28 ObstacleManager() = default;
29
30 enum class AvoidanceMode { On, Off };
31
32 /**
33 * @brief Set the dimensions of the robot to define its bounding box for
34 * collisions.
35 *
36 * @param width Width of the robot.
37 * @param length Length of the robot.
38 */
39 void setRobotDimensions(float width, float length);
40
41 /**
42 * @brief Add a circular obstacle to the field representation.
43 *
44 * @param x X coordinate of the obstacle.
45 * @param y Y coordinate of the obstacle.
46 * @param radius Radius of the obstacle.
47 */
48 void addObstacle(float x, float y, float radius);
49
50 /**
51 * @brief Remove an obstacle by its index.
52 *
53 * @param index The index of the obstacle in the internal vector.
54 */
55 void removeObstacle(size_t index);
56
57 /**
58 * @brief Clear all tracked obstacles.
59 */
61
62 /**
63 * @brief Get the list of currently tracked obstacles.
64 *
65 * @return const std::vector<Obstacle>& Reference to the obstacle vector.
66 */
67 const std::vector<Obstacle>& getObstacles() const;
68
69 /**
70 * @brief Check if a path segment intersects with any obstacle.
71 *
72 * @param start Start point of the segment.
73 * @param end End point of the segment.
74 * @param safety_margin Extra margin to add to the robot/obstacle bounds.
75 * @param out_obstacle Output parameter for the intersected obstacle.
76 * @param out_closest Output parameter for the closest point of intersection.
77 * @return true if an intersection occurs, false otherwise.
78 */
79 bool checkIntersection(const Eigen::Vector2f& start, const Eigen::Vector2f& end, float safety_margin,
80 Obstacle& out_obstacle, Eigen::Vector2f& out_closest) const;
81
82 /**
83 * @brief Calculate a target vector that avoids collisions while navigating
84 * towards a goal.
85 *
86 * @param robot_pos Current robot position.
87 * @param target_pos Desired goal position.
88 * @param safety_margin Extra margin around obstacles.
89 * @param clearance Desired clearance distance from obstacles.
90 * @param robot_heading_rad Current robot heading in radians.
91 * @param recursion_depth Internal use for recursive path-finding limitations.
92 * @return Eigen::Vector2f The corrected target point to aim for.
93 */
94 Eigen::Vector2f getAvoidanceTarget(const Eigen::Vector2f& robot_pos, const Eigen::Vector2f& target_pos,
95 float safety_margin, float clearance, float robot_heading_rad,
96 int recursion_depth = 0) const;
97
98 /**
99 * @brief Get an artificial potential field target vector using the manager's configured
100 * parameters (`pf_ka`, `pf_kr`, `pf_influence_radius`).
101 *
102 * @param robot_pos Current robot position.
103 * @param target_pos Desired goal position.
104 * @param robot_heading_rad Current robot heading in radians.
105 * @return Eigen::Vector2f The resultant vector from potential fields.
106 */
107 Eigen::Vector2f getPotentialFieldTarget(const Eigen::Vector2f& robot_pos, const Eigen::Vector2f& target_pos,
108 float robot_heading_rad) const;
109
110 /**
111 * @brief Set the avoidance mode.
112 */
114
116
117 /**
118 * @brief Set safety margin and clearance used by avoidance routines.
119 */
120 void setAvoidanceParams(float safetyMargin, float clearance);
121
122 /**
123 * @brief Set potential field parameters.
124 */
125 void setPotentialFieldParams(float ka, float kr, float influenceRadius);
126
127private:
128 std::vector<Obstacle> obstacles;
129 float robot_width = 18.0f;
130 float robot_length = 18.0f;
131 AvoidanceMode avoidanceMode = AvoidanceMode::Off;
132 float avoidanceSafetyMargin = 4.0f;
133 float avoidanceClearance = 8.0f;
134 float pf_ka = 5.0f;
135 float pf_kr = 50.0f;
136 float pf_influence_radius = 15.0f;
137};
138} // namespace hololib
Manages a collection of obstacles and provides avoidance computations.
void setRobotDimensions(float width, float length)
Set the dimensions of the robot to define its bounding box for collisions.
Eigen::Vector2f getPotentialFieldTarget(const Eigen::Vector2f &robot_pos, const Eigen::Vector2f &target_pos, float robot_heading_rad) const
Get an artificial potential field target vector using the manager's configured parameters (pf_ka,...
const std::vector< Obstacle > & getObstacles() const
Get the list of currently tracked obstacles.
void setAvoidanceMode(AvoidanceMode mode)
Set the avoidance mode.
void clearObstacles()
Clear all tracked obstacles.
void setPotentialFieldParams(float ka, float kr, float influenceRadius)
Set potential field parameters.
Eigen::Vector2f getAvoidanceTarget(const Eigen::Vector2f &robot_pos, const Eigen::Vector2f &target_pos, float safety_margin, float clearance, float robot_heading_rad, int recursion_depth=0) const
Calculate a target vector that avoids collisions while navigating towards a goal.
bool checkIntersection(const Eigen::Vector2f &start, const Eigen::Vector2f &end, float safety_margin, Obstacle &out_obstacle, Eigen::Vector2f &out_closest) const
Check if a path segment intersects with any obstacle.
void setAvoidanceParams(float safetyMargin, float clearance)
Set safety margin and clearance used by avoidance routines.
void addObstacle(float x, float y, float radius)
Add a circular obstacle to the field representation.
ObstacleManager()=default
Default constructor for ObstacleManager.
AvoidanceMode getAvoidanceMode()
void removeObstacle(size_t index)
Remove an obstacle by its index.
Represents a physical obstacle on the field.
Eigen::Vector2f position
2D coordinate position of the obstacle
float radius
Radius of the obstacle used for avoidance.