Skip to content

Commit 38b0c5d

Browse files
author
redto0
committed
merge plus new code
1 parent 1c3670a commit 38b0c5d

5 files changed

Lines changed: 33 additions & 113 deletions

File tree

include/polynomial_planner/backend.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class Polynomial {
4242
};
4343

4444
namespace backend {
45-
4645
// std::vector<double> FitPolynomial(const std::vector<double>& x, const std::vector<double>& y, int degree);
4746

4847
std::vector<cv::Point2d> getLeftContour(std::vector<double> message);

src/PolynomialPlannerAi_node.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ PolynomialPlannerAi::PolynomialPlannerAi(const rclcpp::NodeOptions& options) : N
2121
// RGB_INFO PARAMETER DO NOT DELETE
2222
this->declare_parameter("camera_frame", "mid_cam_link");
2323

24+
// Frame params
25+
this->declare_parameter("path_frame", "odom");
26+
2427
this->rgb_info_sub = this->create_subscription<sensor_msgs::msg::CameraInfo>(
2528
"/camera/mid/rgb/camera_info", 1,
2629
[this](sensor_msgs::msg::CameraInfo::ConstSharedPtr ci) { this->rgb_model.fromCameraInfo(ci); });
@@ -71,18 +74,34 @@ void PolynomialPlannerAi::polynomial_cb(phnx_msgs::msg::Contours::SharedPtr msg,
7174

7275
//std::string frame_id = this->get_parameter("camera_frame").as_string();
7376
//std::string frame_id = "notemptystring";
74-
// TODO camera frame_id is wrong
7577
auto frame_id = this->get_parameter(std::string("camera_frame")).as_string();
7678
std::optional<nav_msgs::msg::Path> path_optional =
7779
backend::create_path(cv_points_left, cv_points_right, camera_rgb, frame_id);
7880
nav_msgs::msg::Path path;
7981

80-
8182
if (path_optional.has_value()) {
8283
path = path_optional.value();
8384
std::string p = std::to_string(path.poses.size());
8485
RCLCPP_INFO(this->get_logger(), p.c_str());
8586
path.header.frame_id = this->get_parameter(std::string("camera_frame")).as_string();
87+
path.header.stamp = this->get_clock()->now();
88+
89+
try {
90+
// Transform path to odom. While the path is in the same location, its now WRT where the kart started, which
91+
// means that as the kart moves and this path is transformed back WRT the kart, the path will have moved location.
92+
auto trans = this->tf2_buffer->lookupTransform(this->get_parameter("path_frame").as_string(),
93+
path.header.frame_id, rclcpp::Time{});
94+
for (auto& pose : path.poses) {
95+
tf2::doTransform(pose, pose, trans);
96+
pose.header.frame_id = this->get_parameter("path_frame").as_string();
97+
pose.header.stamp = this->get_clock()->now();
98+
}
99+
path.header.frame_id = this->get_parameter("path_frame").as_string();
100+
} catch (tf2::LookupException& e) {
101+
RCLCPP_INFO(this->get_logger(), "Could not look up odom!");
102+
return;
103+
}
104+
86105
this->path_pub->publish(path); // error invalid operator *path
87106
// Extract and print coefficients
88107
// RCLCPP_INFO(this->get_logger(), "Received Polynomial Coefficients:");

src/PolynomialPlanner_node.cpp

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#include "polynomial_planner/PolynomialPlanner_node.hpp"
23

34
// Required for doTransform
@@ -85,6 +86,8 @@ void polynomial_pb(std_msgs::msg::Float32MultiArray::SharedPtr msg, image_geomet
8586
for (int i = 0; i < no_coeff; i++) {
8687
coeff.push_back(msg->data[i]);
8788
}
89+
// TODO fix params
90+
nav_msgs::msg::Path path = backend::create_path(coeff, camera_rgb, frame_id);
8891

8992
std::string p = std::to_string(camera_rgb.cx());
9093
RCLCPP_INFO(this->get_logger(), p.c_str());
@@ -127,48 +130,3 @@ void polynomial_pb(std_msgs::msg::Float32MultiArray::SharedPtr msg, image_geomet
127130
return;
128131
}
129132
}
130-
131-
/* not so obvious reference:
132-
geometry_msgs::msg::PoseArray PolynomialPlanner::project_to_world(const std::vector<cv::Point2d>& object_locations,
133-
const cv::Mat& depth) {
134-
geometry_msgs::msg::PoseArray poses{};
135-
poses.header.frame_id = this->get_parameter(std::string{"camera_frame"}).as_string();
136-
137-
// Rotation that rotates left 90 and backwards 90.
138-
// This converts from camera coordinates in OpenCV to ROS coordinates
139-
tf2::Quaternion optical_to_ros{};
140-
optical_to_ros.setRPY(-M_PI / 2, 0.0, -M_PI / 2);
141-
142-
for (const cv::Point2d& center : object_locations) {
143-
if (!this->rgb_model.initialized()) {
144-
continue;
145-
}
146-
147-
// Project pixel to camera space
148-
auto ray = this->rgb_model.projectPixelTo3dRay(center);
149-
// The oak-d uses shorts in mm, sim uses f32 in m
150-
float dist = depth.type() == 2 ? (float)depth.at<short>(center) / 1000 : depth.at<float>(center);
151-
152-
// If depth unavailable, then skip
153-
if (dist == INFINITY || dist >= 10 || dist <= 0) {
154-
continue;
155-
}
156-
157-
// Just resize the ray to be a vector at the distance of the depth pixel. This is in camera space
158-
auto point_3d = dist / cv::norm(ray) * ray;
159-
160-
// Convert from camera space to ros coordinates ("World" but wrt camera mount)
161-
tf2::Vector3 tf_vec{point_3d.x, point_3d.y, point_3d.z};
162-
auto world_vec = tf2::quatRotate(optical_to_ros, tf_vec);
163-
164-
geometry_msgs::msg::Pose p{};
165-
p.position.x = world_vec.x();
166-
p.position.y = world_vec.y();
167-
p.position.z = world_vec.z();
168-
poses.poses.push_back(p);
169-
}
170-
171-
poses.header.stamp = this->get_clock()->now();
172-
return poses;
173-
}
174-
*/

src/backend.cpp

Lines changed: 8 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
std::optional<nav_msgs::msg::Path> backend::create_path(std::vector<cv::Point2d>& left_contours,
1515
std::vector<cv::Point2d>& right_contours,
16-
image_geometry::PinholeCameraModel& rgb_info_sub,
16+
image_geometry::PinholeCameraModel& camera_info,
1717
std::string_view frame_id) {
1818
// take in contours
1919
// DO NOT THE Polynomials
@@ -30,8 +30,8 @@ std::optional<nav_msgs::msg::Path> backend::create_path(std::vector<cv::Point2d>
3030
std::vector<cv::Point2d> cam_path; // this is the vector of path plannign points in camera space
3131
// ground_path.emplace_back(cv::Point2d(0, 0));
3232

33-
int width = rgb_info_sub.fullResolution().width; // camera space sizes!
34-
int height = rgb_info_sub.fullResolution().height; // Camera space sizes!
33+
int width = camera_info.fullResolution().width; // camera space sizes!
34+
int height = camera_info.fullResolution().height; // Camera space sizes!
3535

3636
bool is_right_valid = true; // stores if Polynomial was intizatized!
3737
bool is_left_valid = true; // left and right respectively
@@ -51,11 +51,11 @@ std::optional<nav_msgs::msg::Path> backend::create_path(std::vector<cv::Point2d>
5151
bool is_left_bigger = left_contours.size() > right_contours.size();
5252

5353
if (is_left_bigger) {
54-
bigger_array = cameraPixelToGroundPos(left_contours, rgb_info_sub);
55-
smaller_array = cameraPixelToGroundPos(right_contours, rgb_info_sub);
54+
bigger_array = cameraPixelToGroundPos(left_contours, camera_info);
55+
smaller_array = cameraPixelToGroundPos(right_contours, camera_info);
5656
} else {
57-
smaller_array = cameraPixelToGroundPos(left_contours, rgb_info_sub);
58-
bigger_array = cameraPixelToGroundPos(right_contours, rgb_info_sub);
57+
smaller_array = cameraPixelToGroundPos(left_contours, camera_info);
58+
bigger_array = cameraPixelToGroundPos(right_contours, camera_info);
5959
}
6060

6161
for (int i = 0; i < smaller_array.size(); i++) {
@@ -89,7 +89,7 @@ std::optional<nav_msgs::msg::Path> backend::create_path(std::vector<cv::Point2d>
8989

9090
// convert from camera to ground
9191
// done further ahead already
92-
// std::vector<cv::Point2d> ground_path = cameraPixelToGroundPos(cam_path, rgb_info_sub);
92+
// std::vector<cv::Point2d> ground_path = cameraPixelToGroundPos(cam_path, camera_info);
9393

9494
nav_msgs::msg::Path msg{};
9595
// msg.header.frame_id = frame;
@@ -162,59 +162,3 @@ std::vector<cv::Point2d> backend::cameraPixelToGroundPos(std::vector<cv::Point2d
162162

163163
return rwpoints;
164164
}
165-
166-
// // why are the pointer things the way they are
167-
// // TODO: make it not die when z is too small
168-
// // or make z not too small
169-
// std::vector<cv::Point2d> backend::cameraPixelToGroundPos(std::vector<cv::Point2d>& pixels,
170-
// image_geometry::PinholeCameraModel rgb_info_sub, int i) {
171-
// // Rotation that rotates left 90 and backwards 90.
172-
// // This converts from camera coordinates in OpenCV to ROS coordinates
173-
// tf2::Quaternion optical_to_ros{};
174-
// // set the Roll Pitch YAW
175-
// /// optical_to_ros.setRPY(0.0, 0.0, 0.0);
176-
// optical_to_ros.setRPY(-M_PI / 2, 0.0, -M_PI / 2);
177-
178-
// std::vector<cv::Point2d> rwpoints;
179-
// const double camera_height = 0.6; // meters
180-
181-
// for (cv::Point2d& pixel : pixels) {
182-
// // gotta rectify the pixel before we raycast
183-
// // pixel.y += 120;
184-
// // pixel.x += 320;
185-
// // cv::Point2d rectPixel = rgb_info_sub.rectifyPoint(pixel);
186-
// cv::Point3d ray = rgb_info_sub.projectPixelTo3dRay(pixel);
187-
188-
// // safety check
189-
// if (fabs(ray.y) < 1e-6) { // Near zero check
190-
// // RCLCPP_WARN(rclcpp::get_logger("backend"), "Invalid ray projection (y near zero)");
191-
// continue; // Skip this point
192-
// }
193-
194-
// // -- CAMERA COORDINATES --
195-
// // positive x = +X TO CAMERA
196-
// // positive y = STRAIGHT TO GROUND
197-
// // positive z = OUT OF CAMERA
198-
// // hopefully
199-
200-
// // ask zach for the trig, extend ray to the floor.
201-
// double divisor = ray.y / camera_height; // divide by how high off the ground the camera is!
202-
// ray.x = ray.x / divisor;
203-
// ray.y = ray.y / divisor;
204-
// ray.z = ray.z / divisor;
205-
// // ray /= ray.z / -0.6;
206-
// // ray.z = (ray.z * -1); // we don't really care abt z, since it -will- *should* always just be cameraHeight
207-
// // tf2::Vector3 tf_vec{ray.z, -ray.x, -ray.y};
208-
// tf2::Vector3 tf_vec{ray.x, ray.y, ray.z};
209-
// tf_vec = tf2::quatRotate(optical_to_ros, tf_vec);
210-
211-
// //return type world_vec, use this is
212-
213-
// cv::Point2d dvector(tf_vec.x(), tf_vec.y());
214-
215-
// // push back vectors
216-
// rwpoints.push_back(dvector);
217-
// }
218-
219-
// return rwpoints;
220-
// }

tests/unit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <rclcpp/rclcpp.hpp>
44

5-
#include "polynomial_planner/PolynomialPlanner_node.hpp"
5+
// #include "polynomial_planner/PolynomialPlanner_node.hpp"
66

77
TEST(PolynomialPlanner, Test1) {}
88

0 commit comments

Comments
 (0)