From 26787e2c5f93947bbf2170cd7637d74ffe6d56e7 Mon Sep 17 00:00:00 2001 From: thetazero Date: Thu, 16 Jun 2022 10:16:32 -0700 Subject: [PATCH 1/4] initial javascript mocksat --- Simulator/mocksat.js | 35 +++++++++++++++++++++++++++++++ Simulator/tests/tests.jl | 45 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 Simulator/mocksat.js diff --git a/Simulator/mocksat.js b/Simulator/mocksat.js new file mode 100644 index 0000000..14ecd39 --- /dev/null +++ b/Simulator/mocksat.js @@ -0,0 +1,35 @@ +const readline = require('readline') + +// while(true){ +// console.log('hello world') +// } +console.log('hello world, from mocksatjs') + +let ω = [] +let b = [] + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}) +rl.on('line', input => { + if (input.length < 4 || input.slice(0, 3) != ">>>") return + input = input.slice(3) + let key = input[0] + let value = input.slice(1) + if (key == 'ω') { + ω = JSON.parse(value) + } else if (key == 'b') { + b = JSON.parse(value) + } else if (key == '?') { + let control = ω.map(v => -v * 0.001) + control = JSON.stringify(control) + console.log(`>>>M${control}`) + } + // console.log(ω, b) + // console.log(key, value) +}) + +rl.on('close', e => { + console.log(`eee${e}`) +}) diff --git a/Simulator/tests/tests.jl b/Simulator/tests/tests.jl index e4c9872..26807ab 100644 --- a/Simulator/tests/tests.jl +++ b/Simulator/tests/tests.jl @@ -1,11 +1,50 @@ # [Simulator/test/tests.jl] -using Test, LinearAlgebra, SatelliteDynamics, Plots +using Test, LinearAlgebra, SatelliteDynamics, Plots, JSON include("../simulator.jl"); using .Simulator +@testset "DeTumbleIO" begin + println("DeTumbleIO") + inp = Pipe() + out = Pipe() + err = Pipe() + proc = run(`node mocksat.js`, inp, out, err, wait = false) + close(out.in) + close(err.in) + Base.start_reading(out.out) + Base.start_reading(err.out) + + println(readline(proc.out)) + + function control_law(ω, b) + write(inp, ">>>ω"*string(ω)*"\n") + write(inp, ">>>b"*string(b)*"\n") + write(inp, ">>>?"*"\n") + control = [0,0,0] + while true + input = readline(proc.out) + if length(input) >= 4 && input[1:3] == ">>>" + if input[4] == 'M' + control = (input[5:length(input)]) + control = JSON.parse(control) + end + break + end + end + # println(control) + return control + end + + (data, title, xlablel, ylabel) = my_sim(control_law) + display(plot(data, title="DeTumbleIO", xlabel=xlablel, ylabel=ylabel)) + +end + + @testset "General Function Call" begin """ Test general function calls and ensure that everything looks as it should """ + # println("General Function Call") x₀ = initialize_orbit() # Ensure that r and v are orthogonal @@ -113,11 +152,11 @@ end # M = -k * (identity(3) - dot(b̂,b̂))*ω M = -k * (I(3) - b̂*b̂')*ω # print(M, ω, b) - print(dot(ω,ω),'\n') + # print(dot(ω,ω),'\n') return M end (data, title, xlablel, ylabel) = my_sim(control_law) display(plot(data, title="DeTumbling", xlabel=xlablel, ylabel=ylabel)) -end \ No newline at end of file +end From 4ef6375051aae1dd1ff6ffe6434f682fcd31f22c Mon Sep 17 00:00:00 2001 From: thetazero Date: Sun, 19 Jun 2022 10:52:05 -0700 Subject: [PATCH 2/4] Preliminary integration --- Simulator/simulator.jl | 1 + Simulator/tests/tests.jl | 81 +++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/Simulator/simulator.jl b/Simulator/simulator.jl index 038879a..e653ec6 100644 --- a/Simulator/simulator.jl +++ b/Simulator/simulator.jl @@ -224,6 +224,7 @@ module Simulator function my_sim(control_fn) x₀ = initialize_orbit() + println("intialized orbit!") # x₀[11:13] .=0 # x₀[11:13] *= x₀[11:13].*10.0 # Spinning very fast diff --git a/Simulator/tests/tests.jl b/Simulator/tests/tests.jl index 26807ab..6d369db 100644 --- a/Simulator/tests/tests.jl +++ b/Simulator/tests/tests.jl @@ -4,44 +4,6 @@ using Test, LinearAlgebra, SatelliteDynamics, Plots, JSON include("../simulator.jl"); using .Simulator -@testset "DeTumbleIO" begin - println("DeTumbleIO") - inp = Pipe() - out = Pipe() - err = Pipe() - proc = run(`node mocksat.js`, inp, out, err, wait = false) - close(out.in) - close(err.in) - Base.start_reading(out.out) - Base.start_reading(err.out) - - println(readline(proc.out)) - - function control_law(ω, b) - write(inp, ">>>ω"*string(ω)*"\n") - write(inp, ">>>b"*string(b)*"\n") - write(inp, ">>>?"*"\n") - control = [0,0,0] - while true - input = readline(proc.out) - if length(input) >= 4 && input[1:3] == ">>>" - if input[4] == 'M' - control = (input[5:length(input)]) - control = JSON.parse(control) - end - break - end - end - # println(control) - return control - end - - (data, title, xlablel, ylabel) = my_sim(control_law) - display(plot(data, title="DeTumbleIO", xlabel=xlablel, ylabel=ylabel)) - -end - - @testset "General Function Call" begin """ Test general function calls and ensure that everything looks as it should """ # println("General Function Call") @@ -160,3 +122,46 @@ end display(plot(data, title="DeTumbling", xlabel=xlablel, ylabel=ylabel)) end + + +@testset "DeTumbleIO" begin + println("DeTumbleIO") + inp = Pipe() + out = Pipe() + err = Pipe() + proc = run(Cmd(`python3 -u main.py`, dir = "/home/thetazero/Documents/pycubed/software_example_beepsat/state_machine/build/" ), inp, out, err, wait = false) + close(out.in) + close(err.in) + Base.start_reading(out.out) + Base.start_reading(err.out) + + println(readline(proc.out)) + + i=0 + + function control_law(ω, b) + println("wrote to sensors") + control = [0,0,0] + while true + write(inp, ">>>ω"*string(ω)*"\n") + write(inp, ">>>b"*string(b)*"\n") + write(inp, ">>>?\n") + input = readline(proc.out) + println(input) + if length(input) >= 4 && input[1:3] == ">>>" + if input[4] == 'M' + control = (input[5:length(input)]) + control = JSON.parse(control) + end + break + end + end + i+=1 + println(i) + return control + end + + (data, title, xlablel, ylabel) = my_sim(control_law) + display(plot(data, title="DeTumbleIO", xlabel=xlablel, ylabel=ylabel)) + +end From 4d531a217d27eb537accade28531af17b75bc499 Mon Sep 17 00:00:00 2001 From: thetazero Date: Mon, 20 Jun 2022 15:09:26 -0700 Subject: [PATCH 3/4] clean up charts --- Simulator/simulator.jl | 15 +++++++++++---- Simulator/tests/tests.jl | 8 ++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Simulator/simulator.jl b/Simulator/simulator.jl index e653ec6..87aade0 100644 --- a/Simulator/simulator.jl +++ b/Simulator/simulator.jl @@ -232,19 +232,26 @@ module Simulator t = Epoch(2020, 11, 30) # Starting time is Nov 30, 2020 dt = 0.5 # Time step, in seconds - N = 10000 + N = 1000000 q_hist = zeros(N, 4) - q_hist[1, :] .= x₀[7:10] + q_hist[1, 1:3] .= x₀[11:13] + q_hist[1, 4] = norm(x₀[11:13]) x = x₀ for i = 1:N - 1 r, v, q, ω = x[1:3], x[4:6], x[7:10], x[11:13] b = IGRF13(r, t) x = rk4(x, J, control_fn(ω, b), t, dt) t += dt # Don't forget to update time (not that it really matters...) - q_hist[i + 1, :] .= x[7:10] + # q_hist[i + 1, :] .= x[7:10] + q_hist[i + 1, 1:3] .= x[11:13] + ω = norm(x[11:13]) + if ω < 0.1 + break + end + q_hist[i + 1, 4] = ω end - return (q_hist, "Satellite Attitude Test", "Time", "Quaternion magic") + return q_hist end end \ No newline at end of file diff --git a/Simulator/tests/tests.jl b/Simulator/tests/tests.jl index 6d369db..f7c56bb 100644 --- a/Simulator/tests/tests.jl +++ b/Simulator/tests/tests.jl @@ -118,8 +118,8 @@ end return M end - (data, title, xlablel, ylabel) = my_sim(control_law) - display(plot(data, title="DeTumbling", xlabel=xlablel, ylabel=ylabel)) + data = my_sim(control_law) + display(plot(data, title="DeTumbling", xlabel="Time (s)", ylabel="Units???", labels=["ω1" "ω2" "ω3" "ω"])) end @@ -161,7 +161,7 @@ end return control end - (data, title, xlablel, ylabel) = my_sim(control_law) - display(plot(data, title="DeTumbleIO", xlabel=xlablel, ylabel=ylabel)) + data = my_sim(control_law) + display(plot(data, title="DeTumbleIO", xlabel="Time (s)", ylabel="Units???")) end From 76ab909eb0f1e07b21b9660befecb8311baceb3b Mon Sep 17 00:00:00 2001 From: thetazero Date: Mon, 20 Jun 2022 16:21:35 -0700 Subject: [PATCH 4/4] clip chart data correctly --- Simulator/simulator.jl | 3 ++- Simulator/tests/tests.jl | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Simulator/simulator.jl b/Simulator/simulator.jl index 87aade0..71400ed 100644 --- a/Simulator/simulator.jl +++ b/Simulator/simulator.jl @@ -245,7 +245,8 @@ module Simulator # q_hist[i + 1, :] .= x[7:10] q_hist[i + 1, 1:3] .= x[11:13] ω = norm(x[11:13]) - if ω < 0.1 + if ω < 0.001 + q_hist = q_hist[1:i, :] break end q_hist[i + 1, 4] = ω diff --git a/Simulator/tests/tests.jl b/Simulator/tests/tests.jl index f7c56bb..bb26472 100644 --- a/Simulator/tests/tests.jl +++ b/Simulator/tests/tests.jl @@ -119,7 +119,7 @@ end end data = my_sim(control_law) - display(plot(data, title="DeTumbling", xlabel="Time (s)", ylabel="Units???", labels=["ω1" "ω2" "ω3" "ω"])) + display(plot(data, title="DeTumbling", xlabel="Time (s)", ylabel="Angular Velocity (rad/s)", labels=["ω1" "ω2" "ω3" "ω"])) end @@ -162,6 +162,6 @@ end end data = my_sim(control_law) - display(plot(data, title="DeTumbleIO", xlabel="Time (s)", ylabel="Units???")) + display(plot(data, title="DeTumbleIO", xlabel="Time (s)", ylabel="Angular Velocity (rad/s)")) end