libstp
Robotics library for Botball
Loading...
Searching...
No Matches
diagonal_motion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <vector>
5
6#include "motion/motion.hpp"
10
11#include <Eigen/Core>
12
13namespace libstp::motion
14{
22 {
23 double angle_rad{0.0}; // Travel angle: 0 = forward, pi/2 = right, -pi/2 = left
24 double distance_m{0.0}; // Distance along the travel direction
25 double speed_scale{1.0}; // 0-1 fraction of AxisConstraints.max_velocity
26
29 double target_heading_rad{0.0};
30
36 };
37
40 {
41 double time_s{0.0}; // elapsed since start
42 double dt{0.0}; // this cycle's dt
43 // Position
44 double target_m{0.0}; // target distance along travel direction
45 double position_m{0.0}; // actual position along travel direction
46 double predicted_m{0.0}; // profile setpoint position
47 double cross_track_m{0.0}; // cross-track drift (perpendicular to travel)
48 // Errors
49 double distance_error_m{0.0}; // target - actual along travel
50 double actual_error_m{0.0}; // target - actual
51 double yaw_error_rad{0.0}; // heading error
52 // Velocity
54 // Commands (after clamp + scaling)
55 double cmd_vx_mps{0.0}; // final commanded vx
56 double cmd_vy_mps{0.0}; // final commanded vy
57 double cmd_wz_radps{0.0}; // final commanded omega
58 // Raw PID outputs (before clamp/scaling)
59 double pid_primary_raw{0.0}; // profiled PID raw output
60 double pid_cross_raw{0.0}; // cross-track PID raw output
61 double pid_heading_raw{0.0}; // heading PID raw output
62 // Profile state
65 // State
66 double heading_rad{0.0}; // current heading
67 double speed_scale{1.0};
68 double heading_scale{1.0};
69 bool saturated{false};
70 };
71
79 class DiagonalMotion final : public Motion
80 {
81 public:
83
84 void start() override;
85 void update(double dt) override;
86 [[nodiscard]] bool isFinished() const override;
87
89 [[nodiscard]] const std::vector<DiagonalMotionTelemetry>& getTelemetry() const { return telemetry_; }
90
91 private:
92 void complete();
93
95 double max_velocity_{0.0}; // computed from speed_scale * AxisConstraints
96 double cos_angle_{1.0}; // precomputed cos(angle_rad)
97 double sin_angle_{0.0}; // precomputed sin(angle_rad)
98 std::unique_ptr<foundation::PidController> heading_pid_;
99 ProfiledPIDController profiled_pid_;
100 Eigen::Vector2d initial_position_m_{Eigen::Vector2d::Zero()};
101 double initial_heading_rad_{0.0};
102 bool finished_{false};
103 double speed_scale_{1.0};
104 double heading_scale_{1.0};
105 int unsaturated_cycles_{0};
106
107 // Velocity tracking for settling detection
108 double prev_primary_position_{0.0};
109 double filtered_velocity_{0.0};
110 static constexpr double kVelocityFilterAlpha{0.3};
111 static constexpr double kSettlingVelocity{0.02}; // m/s - must be nearly stopped to declare done
112
113 // Telemetry
114 double elapsed_time_{0.0};
115 std::vector<DiagonalMotionTelemetry> telemetry_;
116 };
117}
const std::vector< DiagonalMotionTelemetry > & getTelemetry() const
void update(double dt) override
DiagonalMotion(MotionContext ctx, DiagonalMotionConfig config)
bool isFinished() const override
Motion(MotionContext ctx)
Definition motion.hpp:35