Skip to content

Commit

Permalink
Orbit Prop (#13)
Browse files Browse the repository at this point in the history
* orbit propagation basic implementation

* double .zig

* add orbit prop to readme

* add gif for readme

* add gif to readme

* add html example into test files

* more readme tweaks
  • Loading branch information
ATTron authored Jul 7, 2024
1 parent 540ecbc commit 1cc0b29
Show file tree
Hide file tree
Showing 12 changed files with 173,274 additions and 38 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- [x] VITA49 Packets
- [x] Vita49 Stream Parser
- [x] TLE Support
- [x] Orbital Propagation
- [x] RK4
- [ ] Orbital Maneuvers
- [ ] Impulse Maneuvers
- [ ] Phase Maneuvers
Expand Down Expand Up @@ -101,6 +103,51 @@ pub fn main() !void {
```

#### Orbit Prop for the next 3 days

```zig
const std = @import("std");
const astroz = @import("astroz");
const TLE = astroz.tle.TLE;
const spacecraft = astroz.spacecraft;
const Spacecraft = spacecraft.Spacecraft;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const test_tle =
\\1 55909U 23035B 24187.51050877 .00023579 00000+0 16099-2 0 9998
\\2 55909 43.9978 311.8012 0011446 278.6226 81.3336 15.05761711 71371
;
var tle = try TLE.parse(raw_tle, gpa);
defer tle.deinit();
var test_sc = Spacecraft.create("dummy_sc", test_tle, 300.000, spacecraft.Satellite_Size.Cube, constants.earth, std.testing.allocator);
defer test_sc.deinit();
try test_sc.propagate(
test_sc.tle.first_line.epoch,
test_sc.tle.first_line.epoch + 3 * 86400.0, // 2 days worth of orbit predictions
1,
);
for (test_sc.orbit_predictions.items) |iter| {
const r = math.sqrt(iter.state[0] * iter.state[0] + iter.state[1] * iter.state[1] + iter.state[2] * iter.state[2]);
std.debug.print("Next Prediction is: {any}", .{iter});
}
}
```

<img src="https://raw.githubusercontent.com/ATTron/astroz/88ae02a3ffcc70b13dcd91d510e9f65f9768f96a/assets/cut.gif" width="400" height="400" alt="visualization of orbit prop"/>

**NOTE: The HTML for this visualization lives in this repo `test/files/orbit_interactive.html`**

#### Setup Vita49 Parser

##### W/ Callback
Expand Down
Binary file added assets/cut.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});

_ = b.addModule("astroz.spacecraft", .{
.root_source_file = b.path("src/spacecraft.zig"),
.target = target,
.optimize = optimize,
});

const coord_unit_tests = b.addTest(.{
.root_source_file = b.path("src/coordinates.zig"),
});
Expand Down Expand Up @@ -110,6 +116,12 @@ pub fn build(b: *std.Build) void {

const run_tle_unit_tests = b.addRunArtifact(tle_unit_tests);

const spacecraft_unit_tests = b.addTest(.{
.root_source_file = b.path("src/spacecraft.zig"),
});

const run_spacecraft_unit_tests = b.addRunArtifact(spacecraft_unit_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_coord_unit_tests.step);
test_step.dependOn(&run_ccsds_unit_tests.step);
Expand All @@ -118,4 +130,5 @@ pub fn build(b: *std.Build) void {
test_step.dependOn(&run_vita49_unit_tests.step);
test_step.dependOn(&run_parsers_unit_tests.step);
test_step.dependOn(&run_tle_unit_tests.step);
test_step.dependOn(&run_spacecraft_unit_tests.step);
}
10 changes: 10 additions & 0 deletions src/calculations.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const constants = @import("constants.zig");
const math = std.math;

pub fn degrees_to_radians(degrees: f64) f64 {
Expand All @@ -8,3 +9,12 @@ pub fn degrees_to_radians(degrees: f64) f64 {
pub fn radians_to_degrees(degrees: f64) f64 {
return (degrees * 180.0) / math.pi;
}

pub fn mean_motion_to_radians_per_minute(m_motion: f64) f64 {
return m_motion * 2.0 * math.pi / (24.0 * 60.0);
}

pub fn mean_motion_to_semi_major_axis(m_motion: f64) f64 {
const a = math.pow(f64, (constants.earth.mu / (m_motion * 2.0 * math.pi / (24.0 * 3600.0)) * 2 * 2), 1.0 / 3.0);
return a;
}
34 changes: 21 additions & 13 deletions src/constants.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const au = 1.49597871e+11;
// Year 2000
pub const j2k = 2000.0;

const Celestial_Body = struct {
pub const Celestial_Body = struct {
mass: f64, // kg
mu: f64, // km^3/s^2
m_fraction_solar_system: f64,
Expand All @@ -25,10 +25,14 @@ const Celestial_Body = struct {
velocity: f64, // km/s
eccentricity: f64,
inclination: f64, // degrees
oblateness: ?f64,
j2_pertrubation: f64,
sea_level_density: f64,
scale_height: f64,

const Self = @This();

fn new(mass: f64, mu: f64, m_fraction_solar_system: f64, m_radius: ?f64, eq_radius: ?f64, p_radius: ?f64, semi_major_axis: ?f64, perihelion: ?f64, aphelion: ?f64, period: ?f64, velocity: ?f64, eccentricity: ?f64, inclination: ?f64) Self {
fn new(mass: f64, mu: f64, m_fraction_solar_system: f64, m_radius: ?f64, eq_radius: ?f64, p_radius: ?f64, semi_major_axis: ?f64, perihelion: ?f64, aphelion: ?f64, period: ?f64, velocity: ?f64, eccentricity: ?f64, inclination: ?f64, oblateness: ?f64, j2_pertrubation: f64, sea_level_density: f64, scale_height: f64) Self {
return .{
.mass = mass,
.mu = mu,
Expand All @@ -43,21 +47,25 @@ const Celestial_Body = struct {
.velocity = velocity orelse 0.0,
.eccentricity = eccentricity orelse 0.0,
.inclination = inclination orelse 0.0,
.oblateness = oblateness orelse null,
.j2_pertrubation = j2_pertrubation,
.sea_level_density = sea_level_density,
.scale_height = scale_height,
};
}
};

pub const sun = Celestial_Body.new(1.8842e30, 1.32712e11, 9.98657e-1, null, 695700, null, null, null, null, null, null, null, null);
pub const mercury = Celestial_Body.new(3.30101e23, 2.20319e4, 1.65789e-7, 2439.4, 2440.53, 2438.26, 5.79091e7, 4.60009e7, 6.98173e7, 87.97, 47.87, 0.20564, 7.01);
pub const venus = Celestial_Body.new(4.86732e24, 3.24859e5, 2.44455e-6, 6051.8, 6051.8, 6051.8, 1.08209e8, 1.07477e8, 1.08940e8, 224.70, 35.02, 0.00676, 3.39);
pub const earth = Celestial_Body.new(5.97219e24, 3.98600e5, 2.99946e-6, 6371.0084, 6378.1366, 6356.7519, 1.49598e8, 1.47100e8, 1.52096e8, 365.26, 29.78, 0.01670, 0.00);
pub const moon = Celestial_Body.new(7.34581e22, 4.90280e3, 3.68934e-8, 1737.4, null, null, 3.83398e5, 3.62106e5, 4.04689e5, 27.18, 1.03, 0.05555, 23.71);
pub const mars = Celestial_Body.new(6.41693e23, 4.28284e4, 3.22282e-7, 3389.50, 3396.19, 3376.20, 2.27939e8, 2.06645e8, 2.49233e8, 686.97, 24.13, 0.09342, 1.85);
pub const jupiter = Celestial_Body.new(1.89852e27, 1.26713e8, 9.53510e-4, 69911, 71492, 66854, 7.78321e8, 7.40603e8, 8.16038e8, 4332.52, 13.06, 0.04846, 1.30);
pub const saturn = Celestial_Body.new(5.68460e26, 3.79406e7, 2.85502e-4, 58232, 60268, 54364, 1.42910e9, 1.35096e9, 1.50724e9, 10783.05, 9.64, 0.05468, 2.49);
pub const uranus = Celestial_Body.new(8.68192e25, 5.79456e6, 4.36039e-5, 25362, 25559, 24973, 2.87479e9, 2.73854e9, 3.01104e9, 30768.84, 6.79, 0.04739, 0.77);
pub const neptune = Celestial_Body.new(1.02431e26, 6.83653e6, 5.14447e-5, 24622, 24764, 24341, 4.50489e9, 4.46384e9, 4.54594e9, 60357.05, 5.43, 0.00911, 1.77);
pub const pluto = Celestial_Body.new(1.46158e22, 9.75500e2, 7.34061e-9, 1188.3, null, null, 5.91540e9, 4.44212e9, 7.38868e9, 90821.51, 4.74, 0.24906, 17.14);
pub const sun = Celestial_Body.new(1.8842e30, 1.32712e11, 9.98657e-1, null, 695700, null, null, null, null, null, null, null, null, null, 0.0000002, 1e-12, 50000.0);
pub const mercury = Celestial_Body.new(3.30101e23, 2.20319e4, 1.65789e-7, 2439.4, 2440.53, 2438.26, 5.79091e7, 4.60009e7, 6.98173e7, 87.97, 47.87, 0.20564, 7.01, 0.000, 0.00006, 1e-12, 200.0);
pub const venus = Celestial_Body.new(4.86732e24, 3.24859e5, 2.44455e-6, 6051.8, 6051.8, 6051.8, 1.08209e8, 1.07477e8, 1.08940e8, 224.70, 35.02, 0.00676, 3.39, 0.000, 0.000027, 65.0, 15.9);
pub const earth = Celestial_Body.new(5.97219e24, 3.98600e5, 2.99946e-6, 6371.0084, 6378.1366, 6356.7519, 1.49598e8, 1.47100e8, 1.52096e8, 365.26, 29.78, 0.01670, 0.00, 0.003353, 0.00108263, 1.225, 7.249);
pub const moon = Celestial_Body.new(7.34581e22, 4.90280e3, 3.68934e-8, 1737.4, null, null, 3.83398e5, 3.62106e5, 4.04689e5, 27.18, 1.03, 0.05555, 23.71, 0.0012, 0.0002027, 5e-13, 100.0);
pub const mars = Celestial_Body.new(6.41693e23, 4.28284e4, 3.22282e-7, 3389.50, 3396.19, 3376.20, 2.27939e8, 2.06645e8, 2.49233e8, 686.97, 24.13, 0.09342, 1.85, 0.00648, 0.001964, 0.020, 11.1);
pub const jupiter = Celestial_Body.new(1.89852e27, 1.26713e8, 9.53510e-4, 69911, 71492, 66854, 7.78321e8, 7.40603e8, 8.16038e8, 4332.52, 13.06, 0.04846, 1.30, 0.06487, 0.014736, 0.16, 27.0);
pub const saturn = Celestial_Body.new(5.68460e26, 3.79406e7, 2.85502e-4, 58232, 60268, 54364, 1.42910e9, 1.35096e9, 1.50724e9, 10783.05, 9.64, 0.05468, 2.49, 0.09796, 0.016298, 0.19, 59.5);
pub const uranus = Celestial_Body.new(8.68192e25, 5.79456e6, 4.36039e-5, 25362, 25559, 24973, 2.87479e9, 2.73854e9, 3.01104e9, 30768.84, 6.79, 0.04739, 0.77, 0.02293, 0.003343, 0.42, 27.7);
pub const neptune = Celestial_Body.new(1.02431e26, 6.83653e6, 5.14447e-5, 24622, 24764, 24341, 4.50489e9, 4.46384e9, 4.54594e9, 60357.05, 5.43, 0.00911, 1.77, 0.01708, 0.003411, 0.45, 19.7);
pub const pluto = Celestial_Body.new(1.46158e22, 9.75500e2, 7.34061e-9, 1188.3, null, null, 5.91540e9, 4.44212e9, 7.38868e9, 90821.51, 4.74, 0.24906, 17.14, 0.002, 0.00039, 1e-6, 50.0);

test "Test Celestial Bodies Made" {
try std.testing.expectEqual(5.97219e24, earth.mass);
Expand Down
1 change: 1 addition & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub const calculations = @import("calculations.zig");
pub const vita49 = @import("vita49.zig");
pub const parsers = @import("parsers.zig");
pub const tle = @import("tle.zig");
pub const spacecraft = @import("spacecraft.zig");
51 changes: 28 additions & 23 deletions src/parsers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ pub fn Parser(comptime Frame: type) type {
}
}
}
// else {
// const first_frame = try Frame.new(file_content, self.allocator, null);
// _ = try self.packets.append(first_frame);
// file_content = file_content[first_frame.header.packet_size * 4 - 1 ..];
// while (file_content.len > 4) {
// const new_frame = try Frame.new(file_content, self.allocator, null);
// file_content = file_content[new_frame.header.packet_size * 4 - 1 ..];
//
// try self.packets.append(new_frame);
// if (callback != null) {
// callback.?(new_frame);
// }
// }
// }
} else if (std.mem.eql(u8, @typeName(Frame), "ccsds.CCSDS")) {
if (sync_pattern) |sp| {
var i: usize = 0;
Expand Down Expand Up @@ -110,30 +124,21 @@ pub fn Parser(comptime Frame: type) type {
}
}
}
// else {
// const first_frame = try Frame.new(file_content, self.allocator, null);
// _ = try self.packets.append(first_frame);
// file_content = file_content[first_frame.header.packet_size + 5 ..];
// while (file_content.len > 4) {
// const new_frame = try Frame.new(file_content, self.allocator, null);
// file_content = file_content[first_frame.header.packet_size + 5 ..];
//
// try self.packets.append(new_frame);
// if (callback != null) {
// callback.?(new_frame);
// }
// }
// }
}
// if (std.mem.eql(u8, @typeName(Frame), "vita49.Vita49")) {
// file_content = file_content[first_frame.header.packet_size * 4 - 1 ..];
// while (file_content.len > 4) {
// const new_frame = try Frame.new(file_content, null);
// file_content = file_content[new_frame.header.packet_size * 4 - 1 ..];
//
// try self.packets.append(new_frame);
// if (callback != null) {
// callback.?(new_frame);
// }
// }
// } else if (std.mem.eql(u8, @typeName(Frame), "ccsds.CCSDS")) {
// file_content = file_content[first_frame.header.packet_size + 5 ..];
// while (file_content.len > 4) {
// const new_frame = try Frame.new(file_content, null);
// file_content = file_content[first_frame.header.packet_size + 5 ..];
//
// try self.packets.append(new_frame);
// if (callback != null) {
// callback.?(new_frame);
// }
// }
// }
}

pub fn start(self: *Self, comptime callback: ?fn (Frame) void) !void {
Expand Down
Loading

0 comments on commit 1cc0b29

Please sign in to comment.