|
| 1 | +#include "vortex/utils/waypoint_utils.hpp" |
| 2 | + |
| 3 | +#include <yaml-cpp/yaml.h> |
| 4 | +#include <cmath> |
| 5 | +#include <vortex/utils/math.hpp> |
| 6 | + |
| 7 | +namespace vortex::utils::waypoints { |
| 8 | + |
| 9 | +Pose compute_waypoint_goal(const Pose& incoming_waypoint, |
| 10 | + WaypointMode mode, |
| 11 | + const Pose& current_state) { |
| 12 | + Pose waypoint_out = incoming_waypoint; |
| 13 | + |
| 14 | + switch (mode) { |
| 15 | + case WaypointMode::FULL_POSE: |
| 16 | + break; |
| 17 | + |
| 18 | + case WaypointMode::ONLY_POSITION: |
| 19 | + waypoint_out.set_ori(current_state.ori_quaternion()); |
| 20 | + break; |
| 21 | + |
| 22 | + case WaypointMode::FORWARD_HEADING: { |
| 23 | + double dx = incoming_waypoint.x - current_state.x; |
| 24 | + double dy = incoming_waypoint.y - current_state.y; |
| 25 | + double forward_heading = std::atan2(dy, dx); |
| 26 | + |
| 27 | + waypoint_out.set_ori(Eigen::Quaterniond( |
| 28 | + Eigen::AngleAxisd(forward_heading, Eigen::Vector3d::UnitZ()))); |
| 29 | + break; |
| 30 | + } |
| 31 | + |
| 32 | + case WaypointMode::ONLY_ORIENTATION: |
| 33 | + waypoint_out.set_pos(current_state.pos_vector()); |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | + return waypoint_out; |
| 38 | +} |
| 39 | + |
| 40 | +bool has_converged(const Pose& state, |
| 41 | + const Pose& waypoint_goal, |
| 42 | + WaypointMode mode, |
| 43 | + double convergence_threshold) { |
| 44 | + const Eigen::Vector3d ep = state.pos_vector() - waypoint_goal.pos_vector(); |
| 45 | + |
| 46 | + const Eigen::Vector3d ea = vortex::utils::math::quaternion_error( |
| 47 | + state.ori_quaternion(), waypoint_goal.ori_quaternion()); |
| 48 | + |
| 49 | + const double err = [&] { |
| 50 | + switch (mode) { |
| 51 | + case WaypointMode::ONLY_POSITION: |
| 52 | + return ep.norm(); |
| 53 | + case WaypointMode::ONLY_ORIENTATION: |
| 54 | + return ea.norm(); |
| 55 | + case WaypointMode::FORWARD_HEADING: |
| 56 | + return std::sqrt(ep.squaredNorm() + ea(2) * ea(2)); |
| 57 | + case WaypointMode::FULL_POSE: |
| 58 | + default: |
| 59 | + return std::sqrt(ep.squaredNorm() + ea.squaredNorm()); |
| 60 | + } |
| 61 | + }(); |
| 62 | + |
| 63 | + return err < convergence_threshold; |
| 64 | +} |
| 65 | + |
| 66 | +Pose apply_pose_offset(const Pose& base, const Pose& offset) { |
| 67 | + const Eigen::Vector3d p_base = base.pos_vector(); |
| 68 | + const Eigen::Quaterniond q_base = base.ori_quaternion().normalized(); |
| 69 | + |
| 70 | + const Eigen::Vector3d p_offset = offset.pos_vector(); |
| 71 | + const Eigen::Quaterniond q_offset = offset.ori_quaternion().normalized(); |
| 72 | + |
| 73 | + const Eigen::Vector3d p_target = p_base + p_offset; |
| 74 | + const Eigen::Quaterniond q_target = (q_base * q_offset).normalized(); |
| 75 | + |
| 76 | + return Pose::from_eigen(p_target, q_target); |
| 77 | +} |
| 78 | + |
| 79 | +Pose load_waypoint_from_yaml(const std::string& file_path, |
| 80 | + const std::string& identifier) { |
| 81 | + YAML::Node root = YAML::LoadFile(file_path); |
| 82 | + |
| 83 | + if (!root[identifier]) { |
| 84 | + throw std::runtime_error("Waypoint '" + identifier + "' not found in " + |
| 85 | + file_path); |
| 86 | + } |
| 87 | + |
| 88 | + const auto& wp = root[identifier]; |
| 89 | + |
| 90 | + const double x = wp["position"]["x"].as<double>(); |
| 91 | + const double y = wp["position"]["y"].as<double>(); |
| 92 | + const double z = wp["position"]["z"].as<double>(); |
| 93 | + |
| 94 | + const double roll = wp["orientation"]["roll"].as<double>(); |
| 95 | + const double pitch = wp["orientation"]["pitch"].as<double>(); |
| 96 | + const double yaw = wp["orientation"]["yaw"].as<double>(); |
| 97 | + |
| 98 | + const Eigen::Quaterniond q = |
| 99 | + vortex::utils::math::euler_to_quat(roll, pitch, yaw); |
| 100 | + |
| 101 | + return Pose{.x = x, |
| 102 | + .y = y, |
| 103 | + .z = z, |
| 104 | + .qw = q.w(), |
| 105 | + .qx = q.x(), |
| 106 | + .qy = q.y(), |
| 107 | + .qz = q.z()}; |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace vortex::utils::waypoints |
0 commit comments