Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Simulator/mocksat.js
Original file line number Diff line number Diff line change
@@ -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}`)
})
17 changes: 13 additions & 4 deletions Simulator/simulator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,26 +224,35 @@ 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

J = [0.3 0 0; 0 0.3 0; 0 0 0.3] # Arbitrary inertia matrix for the Satellite
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
54 changes: 49 additions & 5 deletions Simulator/tests/tests.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
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