Browse Source

Update rendezvous.h to match new style

- Remove typedef from structs (LVLHRelativeState, CWValidityResult, CWGuidanceSolution)
- Convert comments to // style with descriptive comments above functions
- Move parameter comments inline
- Add return descriptions inline with function declarations
- Remove decorative section separators (// ======)
- Keep section headers as simple // comments
main
cinnaboot 3 months ago
parent
commit
3e6566e1f9
  1. 321
      src/rendezvous.h

321
src/rendezvous.h

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

Loading…
Cancel
Save