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/simulator.jl b/Simulator/simulator.jl index 038879a..71400ed 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 @@ -231,19 +232,27 @@ 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.001 + q_hist = q_hist[1:i, :] + 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 e4c9872..bb26472 100644 --- a/Simulator/tests/tests.jl +++ b/Simulator/tests/tests.jl @@ -1,11 +1,12 @@ # [Simulator/test/tests.jl] -using Test, LinearAlgebra, SatelliteDynamics, Plots +using Test, LinearAlgebra, SatelliteDynamics, Plots, JSON include("../simulator.jl"); using .Simulator @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 +114,54 @@ 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)) + data = my_sim(control_law) + display(plot(data, title="DeTumbling", xlabel="Time (s)", ylabel="Angular Velocity (rad/s)", labels=["ω1" "ω2" "ω3" "ω"])) -end \ No newline at end of file +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 = my_sim(control_law) + display(plot(data, title="DeTumbleIO", xlabel="Time (s)", ylabel="Angular Velocity (rad/s)")) + +end