-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathORAC_CubeSat.py
More file actions
130 lines (80 loc) · 2.29 KB
/
Copy pathORAC_CubeSat.py
File metadata and controls
130 lines (80 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import numpy as np
import matplotlib.pyplot as plt
import random
class ORAC_CubeSat:
def __init__(self):
self.threshold = 0.25
self.W = 1.0
self.mode = "NORMAL"
self.fault = "NONE"
def cusum(self,data):
mean = np.mean(data)
return np.mean(np.abs(data - mean))
def detect(self,a,b):
scoreA = self.cusum(a)
scoreB = self.cusum(b)
if scoreA > self.threshold and scoreB < self.threshold:
self.fault = "GYRO_A"
elif scoreB > self.threshold and scoreA < self.threshold:
self.fault = "GYRO_B"
else:
self.fault = "NONE"
def vitality(self,error):
self.W = 1/(1+abs(error)*3)
if self.W < 0.7:
self.mode = "SAFE"
else:
self.mode = "NORMAL"
def simulate_orbit():
t = np.linspace(0,100,500)
attitude = np.sin(t/10)
return t,attitude
def run_simulation():
t,true_att = simulate_orbit()
gyroA=[]
gyroB=[]
faults=[]
vitality=[]
modes=[]
orac = ORAC_CubeSat()
drift=False
for i in range(len(t)):
val=true_att[i]
gA=val + random.uniform(-0.02,0.02)
gB=val + random.uniform(-0.02,0.02)
if i==200:
drift=True
if drift:
gA += random.uniform(-0.5,0.5)
gyroA.append(gA)
gyroB.append(gB)
if len(gyroA)>20:
gyroA.pop(0)
gyroB.pop(0)
if len(gyroA)>10:
orac.detect(gyroA,gyroB)
err=gA-gB
orac.vitality(err)
faults.append(orac.fault)
vitality.append(orac.W)
modes.append(orac.mode)
return t,true_att,gyroA,gyroB,vitality,modes
def plot_results():
t,true_att,gyroA,gyroB,W,modes = run_simulation()
plt.figure(figsize=(10,6))
plt.subplot(2,1,1)
plt.title("CubeSat Attitude Sensors")
plt.plot(true_att,label="True")
plt.plot(gyroA,label="Gyro A")
plt.plot(gyroB,label="Gyro B")
plt.legend()
plt.subplot(2,1,2)
plt.title("ORAC Vitality Index")
plt.plot(W,label="Vitality W")
plt.axhline(0.7,color='r',linestyle='--')
plt.legend()
plt.tight_layout()
plt.savefig("orac_cubesat_demo.png")
plt.show()
if __name__=="__main__":
plot_results()