diff --git a/src/rendezvous.h b/src/rendezvous.h index 639c623..2195f1d 100644 --- a/src/rendezvous.h +++ b/src/rendezvous.h @@ -5,290 +5,183 @@ #include "orbital_mechanics.h" #include "orbital_objects.h" -/** - * Rendezvous Module - * - * Provides Clohessy-Wiltshire (Hill's) equations-based guidance for spacecraft rendezvous. - * Supports both spacecraft-to-spacecraft and spacecraft-to-body rendezvous in circular, - * coplanar orbits. - * - * Validity Limits (dimensionless, scale with orbital radius): - * - Spatial: 5% of orbital radius (x/r, y/r, z/r < 0.05) - * - Time: 2.0 radians of orbital motion (n*dt < 2.0, ~1/3 orbit) - */ +// Rendezvous Module +// Provides Clohessy-Wiltshire (Hill's) equations-based guidance for spacecraft rendezvous. +// Supports both spacecraft-to-spacecraft and spacecraft-to-body rendezvous in circular, +// coplanar orbits. +// +// Validity Limits (dimensionless, scale with orbital radius): +// - Spatial: 5% of orbital radius (x/r, y/r, z/r < 0.05) +// - Time: 2.0 radians of orbital motion (n*dt < 2.0, ~1/3 orbit) + // CW validity thresholds (dimensionless) #define CW_SPATIAL_LIMIT_FRACTION 0.05 // 5% of orbital radius #define CW_TIME_LIMIT_N_DT 2.0 // ~2 radians of orbital motion // Relative state in LVLH frame -typedef struct { +struct LVLHRelativeState { double radial; // x: radial separation (positive outward) double along_track; // y: along-track separation (positive in direction of motion) double cross_track; // z: cross-track separation double v_radial; // radial velocity double v_along_track;// along-track velocity double v_cross_track;// cross-track velocity -} LVLHRelativeState; +}; // CW validity result -typedef struct { +struct CWValidityResult { bool spatial_valid; // Within spatial limits bool time_valid; // Within time limits bool overall_valid; // Both spatial and time valid double spatial_fraction; // max(|x|,|y|,|z|) / orbital_radius double n_dt; // n * time_since_linearization double expected_error; // Estimated error percentage -} CWValidityResult; +}; // CW guidance solution -typedef struct { +struct CWGuidanceSolution { bool valid; // Whether solution is valid double delta_v_magnitude; // Required delta-v (m/s) double burn_direction_radial; // Radial component (unit vector) double burn_direction_along_track; // Along-track component double burn_direction_cross_track; // Cross-track component double time_to_intercept; // Time to reach target (s) -} CWGuidanceSolution; +}; + -// ============================================================================ // Utility Functions -// ============================================================================ -/** - * Transform Cartesian position/velocity to LVLH (Local Vertical Local Horizontal) frame - * - * LVLH basis vectors: - * - r_hat: Radial direction (from parent to object) - * - v_hat: Along-track direction (velocity direction for circular orbit) - * - h_hat: Cross-track direction (orbit normal) - * - * @param position Position vector in inertial frame - * @param velocity Velocity vector in inertial frame - * @param parent_mass Mass of central body - * @param out_r_hat Output: radial unit vector - * @param out_v_hat Output: along-track unit vector - * @param out_h_hat Output: cross-track unit vector - */ +// Transform Cartesian position/velocity to LVLH (Local Vertical Local Horizontal) frame +// LVLH basis vectors: +// - r_hat: Radial direction (from parent to object) +// - v_hat: Along-track direction (velocity direction for circular orbit) +// - h_hat: Cross-track direction (orbit normal) void cartesian_to_lvlh_basis( Vec3 position, Vec3 velocity, double parent_mass, - Vec3* out_r_hat, - Vec3* out_v_hat, - Vec3* out_h_hat + Vec3* out_r_hat, // Output: radial unit vector + Vec3* out_v_hat, // Output: along-track unit vector + Vec3* out_h_hat // Output: cross-track unit vector ); -/** - * Project relative state onto LVLH basis - * - * @param rel_pos Relative position (target - chaser) - * @param r_hat Radial unit vector - * @param v_hat Along-track unit vector - * @param h_hat Cross-track unit vector - * @param rel_vel Relative velocity - * @param out Relative state in LVLH frame - */ +// Project relative state onto LVLH basis void project_to_lvlh_frame( - Vec3 rel_pos, - Vec3 r_hat, - Vec3 v_hat, - Vec3 h_hat, - Vec3 rel_vel, - LVLHRelativeState* out + Vec3 rel_pos, // Relative position (target - chaser) + Vec3 r_hat, // Radial unit vector + Vec3 v_hat, // Along-track unit vector + Vec3 h_hat, // Cross-track unit vector + Vec3 rel_vel, // Relative velocity + LVLHRelativeState* out // Output: Relative state in LVLH frame ); -/** - * Transform LVLH relative state back to Cartesian - * - * @param lvlh Relative state in LVLH frame - * @param r_hat Radial unit vector - * @param v_hat Along-track unit vector - * @param h_hat Cross-track unit vector - * @param chaser_pos Chaser position (for absolute position calculation) - * @param out_r_cart Output: relative position in Cartesian - * @param out_v_cart Output: relative velocity in Cartesian - */ +// Transform LVLH relative state back to Cartesian void lvlh_to_cartesian( - LVLHRelativeState* lvlh, - Vec3 r_hat, - Vec3 v_hat, - Vec3 h_hat, - Vec3 chaser_pos, - Vec3* out_r_cart, - Vec3* out_v_cart + LVLHRelativeState* lvlh, // Relative state in LVLH frame + Vec3 r_hat, // Radial unit vector + Vec3 v_hat, // Along-track unit vector + Vec3 h_hat, // Cross-track unit vector + Vec3 chaser_pos, // Chaser position (for absolute position calculation) + Vec3* out_r_cart, // Output: relative position in Cartesian + Vec3* out_v_cart // Output: relative velocity in Cartesian ); -// ============================================================================ + // CW Validity Functions -// ============================================================================ -/** - * Check if CW equations are valid for current relative state - * - * Validity criteria: - * - Spatial: max(|x|,|y|,|z|) / orbital_radius < 0.05 - * - Time: n * dt < 2.0 (where dt is time since last linearization) - * - * @param chaser Chaser spacecraft - * @param target Target (body or spacecraft) - * @param parent Central body - * @param current_time Current simulation time - * @return CWValidityResult with validity flags and error estimates - */ -CWValidityResult check_cw_validity( - Spacecraft* chaser, - void* target, // Can be Spacecraft* or CelestialBody* - CelestialBody* parent, - double current_time +// Check if CW equations are valid for current relative state +// Validity criteria: +// - Spatial: max(|x|,|y|,|z|) / orbital_radius < 0.05 +// - Time: n * dt < 2.0 (where dt is time since last linearization) +CWValidityResult check_cw_validity( // Returns: CWValidityResult with validity flags and error estimates + Spacecraft* chaser, // Chaser spacecraft + void* target, // Target (body or spacecraft) + CelestialBody* parent, // Central body + double current_time // Current simulation time ); -/** - * Compute mean motion for given orbital radius - * - * @param parent_mass Mass of central body - * @param orbital_radius Orbital radius - * @return Mean motion n = sqrt(mu / a^3) - */ -double compute_mean_motion( - double parent_mass, - double orbital_radius +// Compute mean motion for given orbital radius +double compute_mean_motion( // Returns: Mean motion n = sqrt(mu / a^3) + double parent_mass, // Mass of central body + double orbital_radius // Orbital radius ); -// ============================================================================ + // CW Guidance Functions -// ============================================================================ -/** - * Solve CW equations for rendezvous guidance - * - * Uses closed-form CW solutions to compute required delta-v for interception - * - * CW Equations (linearized relative motion): - * x'' - 2n*y' - 3n^2*x = 0 - * y'' + 2n*x' = 0 - * z'' + n^2*z = 0 - * - * @param chaser Chaser spacecraft - * @param target Target - * @param parent Central body - * @param time_to_intercept Desired time to intercept - * @param current_time Current simulation time - * @return CWGuidanceSolution with required delta-v - */ -CWGuidanceSolution solve_cw_guidance( - Spacecraft* chaser, - void* target, - CelestialBody* parent, - double time_to_intercept, - double current_time +// Solve CW equations for rendezvous guidance +// Uses closed-form CW solutions to compute required delta-v for interception +// CW Equations (linearized relative motion): +// x'' - 2n*y' - 3n^2*x = 0 +// y'' + 2n*x' = 0 +// z'' + n^2*z = 0 +CWGuidanceSolution solve_cw_guidance( // Returns: CWGuidanceSolution with required delta-v + Spacecraft* chaser, // Chaser spacecraft + void* target, // Target + CelestialBody* parent, // Central body + double time_to_intercept, // Desired time to intercept + double current_time // Current simulation time ); -/** - * Calculate optimal time to intercept for minimum delta-v - * - * For circular coplanar orbits, optimal intercept occurs at: - * - Half the relative orbital period for along-track separation - * - Adjusted for radial separation - * - * @param lvlh Relative state in LVLH frame - * @param mean_motion Mean motion of reference orbit - * @return Optimal time to intercept - */ -double calculate_optimal_intercept_time( - LVLHRelativeState* lvlh, - double mean_motion +// Calculate optimal time to intercept for minimum delta-v +// For circular coplanar orbits, optimal intercept occurs at: +// - Half the relative orbital period for along-track separation +// - Adjusted for radial separation +double calculate_optimal_intercept_time( // Returns: Optimal time to intercept + LVLHRelativeState* lvlh, // Relative state in LVLH frame + double mean_motion // Mean motion of reference orbit ); -// ============================================================================ + // Rendezvous Target Management -// ============================================================================ -/** - * Initialize rendezvous target structure - * - * @param target Target structure to initialize - * @param target_index Index of target object - * @param is_spacecraft_target True if target is spacecraft, false if body - * @param approach_distance Distance to start approach phase - * @param capture_distance Distance for capture - * @param max_relative_velocity Max closing speed for capture - */ +// Initialize rendezvous target structure void initialize_rendezvous_target( - RendezvousTarget* target, - int target_index, - bool is_spacecraft_target, - double approach_distance, - double capture_distance, - double max_relative_velocity + RendezvousTarget* target, // Target structure to initialize + int target_index, // Index of target object + bool is_spacecraft_target, // True if target is spacecraft, false if body + double approach_distance, // Distance to start approach phase + double capture_distance, // Distance for capture + double max_relative_velocity // Max closing speed for capture ); -/** - * Update rendezvous state machine based on current relative state - * - * State transitions: - * - PLANNING -> APPROACHING: when within approach_distance - * - APPROACHING -> MATCHING: when relative velocity < threshold - * - MATCHING -> COMPLETE: when within capture_distance AND relative velocity < max - * - Any -> FAILED: if CW validity is lost or distance increases - * - * @param chaser Chaser spacecraft - * @param target Rendezvous target - * @param parent Central body - * @param current_time Current simulation time - * @param target_obj Target object (Spacecraft* or CelestialBody*) - */ +// Update rendezvous state machine based on current relative state +// State transitions: +// - PLANNING -> APPROACHING: when within approach_distance +// - APPROACHING -> MATCHING: when relative velocity < threshold +// - MATCHING -> COMPLETE: when within capture_distance AND relative velocity < max +// - Any -> FAILED: if CW validity is lost or distance increases void update_rendezvous_state( - Spacecraft* chaser, - RendezvousTarget* target, - CelestialBody* parent, - double current_time, - void* target_obj // Can be Spacecraft* or CelestialBody* + Spacecraft* chaser, // Chaser spacecraft + RendezvousTarget* target, // Rendezvous target + CelestialBody* parent, // Central body + double current_time, // Current simulation time + void* target_obj // Target object (Spacecraft* or CelestialBody*) ); -// ============================================================================ + // Burn Application Functions -// ============================================================================ -/** - * Apply CW guidance burn to chaser spacecraft - * - * @param chaser Chaser spacecraft - * @param solution CW guidance solution - * @param parent Central body - * @param current_time Current simulation time - */ +// Apply CW guidance burn to chaser spacecraft void apply_cw_guidance_burn( - Spacecraft* chaser, - CWGuidanceSolution* solution, - CelestialBody* parent, - double current_time + Spacecraft* chaser, // Chaser spacecraft + CWGuidanceSolution* solution, // CW guidance solution + CelestialBody* parent, // Central body + double current_time // Current simulation time ); -/** - * Calculate relative velocity magnitude between chaser and target - * - * @param chaser Chaser spacecraft - * @param target Target - * @param parent Central body - * @return Relative velocity magnitude (m/s) - */ -double calculate_relative_velocity_magnitude( - Spacecraft* chaser, - void* target, - CelestialBody* parent +// Calculate relative velocity magnitude between chaser and target +double calculate_relative_velocity_magnitude( // Returns: Relative velocity magnitude (m/s) + Spacecraft* chaser, // Chaser spacecraft + void* target, // Target + CelestialBody* parent // Central body ); -/** - * Calculate distance between chaser and target - * - * @param chaser Chaser spacecraft - * @param target Target - * @return Distance (m) - */ -double calculate_rendezvous_distance( - Spacecraft* chaser, - void* target +// Calculate distance between chaser and target +double calculate_rendezvous_distance( // Returns: Distance (m) + Spacecraft* chaser, // Chaser spacecraft + void* target // Target ); #endif // RENDEZVOUS_H