Skip to content

Commit 39eb13e

Browse files
committed
Tree-based trajectory search PointXYori isntead of PointXY
1 parent 9c1afd4 commit 39eb13e

File tree

3 files changed

+70
-44
lines changed

3 files changed

+70
-44
lines changed

cone_detection_lidar/include/cone_detection_lidar/trajectory.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <algorithm>
77
#include <functional>
88
#include <iostream>
9+
// #include "rclcpp/rclcpp.hpp"
910

1011
class SearchConfig
1112
{
@@ -90,7 +91,7 @@ class GridMapTrajectory : public GridMap
9091
return true;
9192
}
9293

93-
std::vector<PointXYori> angualar_search(SearchConfig s, std::vector<signed char> &hpoints, double &max_true_angle)
94+
std::vector<PointXYori> angular_search(SearchConfig s, std::vector<signed char> &hpoints, double &max_true_angle)
9495
{
9596
double search_range = s.search_range_deg * M_PI / 180;
9697
double search_resolution = s.search_resolution_deg * M_PI / 180;
@@ -159,10 +160,17 @@ class GridMapTrajectory : public GridMap
159160

160161
double x_end = s.x_start + s.search_length * cos(max_true_angle);
161162
double y_end = s.y_start + s.search_length * sin(max_true_angle);
162-
PointXYori p_end = PointXYori(x_end, y_end, max_true_angle);
163+
PointXYori p_end = PointXYori(x_end, y_end, max_true_angle * 180 / M_PI);
163164
std::vector<PointXYori> p_end_vector;
164165
p_end_vector.push_back(p_end);
165166
// TODO: return the second longest segment center as well
167+
// TODO: remove this test:
168+
// if (max_true_end - max_true_start < 90){
169+
// double r_x_end = s.search_length * cos(max_true_angle);
170+
// double r_y_end = s.search_length * sin(max_true_angle);
171+
// PointXYori r_end = PointXYori(r_x_end, r_y_end, max_true_start * 180 / M_PI);
172+
// p_end_vector.push_back(r_end);
173+
// }
166174
return p_end_vector;
167175
}
168176
};

cone_detection_lidar/include/cone_detection_lidar/tree.hpp

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,47 @@
99
#include <stack>
1010
#include "pointcloud_to_grid_core.hpp"
1111

12-
class Node
12+
class TreeNode
1313
{
1414
public:
15-
PointXY pos;
15+
PointXYori pos;
1616
float prob = 0.5;
17-
std::vector<Node *> children;
18-
Node(PointXY pos) : pos(pos) {}
17+
std::vector<TreeNode *> children;
18+
TreeNode(PointXYori pos) : pos(pos) {}
1919
};
2020

2121
class Tree
2222
{
2323

2424
public:
2525
// create a root node and reserve memory for it
26-
Node *root = new Node(PointXY(0.0, 0.0));
26+
TreeNode *root = new TreeNode(PointXYori(0.0, 0.0, 0.0));
2727

28-
Node *getRoot()
28+
TreeNode *getRoot()
2929
{
3030
return root;
3131
}
3232

33-
Node *modifyRoot(PointXY pos)
33+
TreeNode *modifyRoot(PointXYori pos)
3434
{
3535
root->pos = pos;
3636
return root;
3737
}
3838

39-
Node *modifyNode(Node *node, PointXY pos)
39+
TreeNode *modifyNode(TreeNode *node, PointXYori pos)
4040
{
4141
node->pos = pos;
4242
return node;
4343
}
4444

45-
Node *addNode(Node *parent, PointXY pos)
45+
TreeNode *addNode(TreeNode *parent, PointXYori pos)
4646
{
47-
Node *newNode = new Node(pos);
47+
TreeNode *newNode = new TreeNode(pos);
4848
parent->children.push_back(newNode);
4949
return newNode;
5050
}
5151
// recursive function to visit all nodes in the tree
52-
void printTree(Node *node, std::string indent = "")
52+
void printTree(TreeNode *node, std::string indent = "")
5353
{
5454
if (node != nullptr)
5555
{
@@ -61,36 +61,60 @@ class Tree
6161
{
6262
std::cout << indent << "(" << node->pos.x << ", " << node->pos.y << ")" << std::endl;
6363
}
64-
for (Node *child : node->children)
64+
for (TreeNode *child : node->children)
6565
{
66-
Node *father = node;
66+
TreeNode *father = node;
6767
std::cout << "[" << father->pos.x << ", " << father->pos.y << "] ";
6868
printTree(child, indent + "->");
6969
}
7070
}
7171
}
72+
// a recursive function to return all the leaves of the tree as a node
73+
std::vector<TreeNode*> getLeaves(TreeNode* node)
74+
{
75+
std::vector<TreeNode*> leaves;
76+
77+
if (node != nullptr)
78+
{
79+
if (node->children.empty())
80+
{
81+
leaves.push_back(node);
82+
}
83+
else
84+
{
85+
for (TreeNode* child : node->children)
86+
{
87+
std::vector<TreeNode*> childLeaves = getLeaves(child);
88+
leaves.insert(leaves.end(), childLeaves.begin(), childLeaves.end());
89+
}
90+
}
91+
}
92+
93+
return leaves;
94+
}
95+
7296
// alternative way to print (and walk thru) the whole tree (non-recursive)
73-
void printTreeAlt(Node *root)
97+
void printTreeAlt(TreeNode *root)
7498
{
7599
if (root == nullptr)
76100
{
77101
return;
78102
}
79103

80-
std::stack<Node *> stack;
104+
std::stack<TreeNode *> stack;
81105
stack.push(root);
82106

83107
while (!stack.empty())
84108
{
85-
Node *node = stack.top();
109+
TreeNode *node = stack.top();
86110
stack.pop();
87111

88112
if (node != root)
89113
{
90114
std::cout << "(" << node->pos.x << ", " << node->pos.y << ")" << std::endl;
91115
}
92116

93-
for (Node *child : node->children)
117+
for (TreeNode *child : node->children)
94118
{
95119
std::cout << "[" << node->pos.x << ", " << node->pos.y << "] ";
96120
stack.push(child);
@@ -99,7 +123,7 @@ class Tree
99123
}
100124

101125
// recursive function to visualize the tree in rviz
102-
void markerTree(Node *node, visualization_msgs::msg::Marker &line1_marker, std::string indent = "")
126+
void markerTree(TreeNode *node, visualization_msgs::msg::Marker &line1_marker, std::string indent = "")
103127
{
104128
if (node != nullptr)
105129
{
@@ -115,9 +139,9 @@ class Tree
115139
p_node.z = 0.0;
116140
line1_marker.points.push_back(p_node);
117141
}
118-
for (Node *child : node->children)
142+
for (TreeNode *child : node->children)
119143
{
120-
Node *father = node;
144+
TreeNode *father = node;
121145
geometry_msgs::msg::Point p_father;
122146
p_father.x = father->pos.x;
123147
p_father.y = father->pos.y;

cone_detection_lidar/src/grid_trajectory.cpp

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -229,48 +229,42 @@ class PointCloudToGrid : public rclcpp::Node
229229
}
230230
}
231231
}
232+
// search_iter is how deep the tree will be (e.g. 6 = 6 levels deep, max 6^2 = 36 nodes in the tree, if every node has 2 children)
232233
if (search_iter > 6)
233234
{
234235
search_iter = 6;
235236
}
236-
std::vector<PointXYori> traj_end_points(1);
237+
std::vector<TreeNode *> traj_end_points(1);
237238
double traj_search_angle = 0.0;
238239
Tree tree;
239-
240-
traj_end_points[0] = PointXYori(0.0, 0.0, 0.0);
241-
tree.modifyRoot(PointXY(0.0, 0.0));
240+
tree.modifyRoot(PointXYori(0.0, 0.0, search_start_mid_deg));
242241
auto node_actual = tree.getRoot();
243-
double traj_search_start_mid = search_start_mid_deg;
244242
SearchConfig config;
243+
// iterate through search_iter -> deepness of the tree
245244
for (int i = 1; i <= search_iter; i++)
246245
{
247-
for (size_t i = 0; i < traj_end_points.size(); ++i)
246+
// traj_end_points are the leaves of the tree
247+
traj_end_points = tree.getLeaves(node_actual);
248+
// for every leaf, search for new end points
249+
for (size_t t = 0; t < traj_end_points.size(); t++)
248250
{
249-
config.x_start = traj_end_points[i].x;
250-
config.y_start = traj_end_points[i].y;
251+
config.x_start = traj_end_points[t]->pos.x;
252+
config.y_start = traj_end_points[t]->pos.y;
253+
config.search_start_mid_deg = traj_end_points[t]->pos.ori;
251254
config.search_range_deg = search_range_deg;
252255
config.search_resolution_deg = search_resolution_deg;
253-
config.search_start_mid_deg = traj_search_start_mid;
254256
config.search_length = search_length;
255-
std::vector<PointXYori> actual_end_points = grid_map.angualar_search(config, hpoints, traj_search_angle);
256-
node_actual = tree.addNode(node_actual, traj_end_points[i]);
257-
traj_search_start_mid = traj_search_angle * 180 / M_PI;
258-
// TODO: test this logic with multiple actual end points
259-
for(size_t j = 0; j < actual_end_points.size(); ++j)
257+
std::vector<PointXYori> new_end_points = grid_map.angular_search(config, hpoints, traj_search_angle);
258+
// new end points are added to the tree (typically 1 or 2)
259+
for (size_t j = 0; j < new_end_points.size(); j++)
260260
{
261-
if (j == 0){
262-
traj_end_points[i] = actual_end_points[j]; // replace the current end point with the first actual end point
263-
}
264-
else{
265-
traj_end_points.insert(traj_end_points.begin() + i, actual_end_points[j]); // insert if there are more than one actual end points
266-
}
261+
tree.addNode(traj_end_points[t], new_end_points[j]);
267262
}
268263
}
269264
}
270-
271265
visualization_msgs::msg::MarkerArray mark_array;
272266
visualization_msgs::msg::Marker debug1_marker, line1_marker;
273-
init_debug_marker(debug1_marker, traj_end_points[0].x, traj_end_points[0].y, 1);
267+
init_debug_marker(debug1_marker, traj_end_points[0]->pos.x, traj_end_points[0]->pos.y, 1);
274268
init_line_marker(line1_marker, 2);
275269
debug1_marker.header.frame_id = input_msg->header.frame_id;
276270
debug1_marker.header.stamp = this->now();

0 commit comments

Comments
 (0)