-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorac_visual_simulator.py
More file actions
164 lines (108 loc) · 3.2 KB
/
Copy pathorac_visual_simulator.py
File metadata and controls
164 lines (108 loc) · 3.2 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import pygame
import numpy as np
import random
import math
WIDTH = 900
HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("ORAC v6 Visual Simulator")
clock = pygame.time.Clock()
# colors
WHITE = (255,255,255)
BLACK = (20,20,20)
RED = (220,50,50)
GREEN = (50,220,100)
YELLOW = (240,200,0)
font = pygame.font.SysFont("consolas",18)
class ORAC:
def __init__(self):
self.threshold = 0.35
self.W = 1.0
self.mode = "NORMAL"
self.fault = "NONE"
def cusum(self, window):
mean = np.mean(window)
return np.mean(np.abs(window - mean))
def detect(self,a,b):
scoreA = self.cusum(a)
scoreB = self.cusum(b)
if scoreA > self.threshold and scoreB < self.threshold:
self.fault = "IMU_A"
elif scoreB > self.threshold and scoreA < self.threshold:
self.fault = "IMU_B"
else:
self.fault = "NONE"
def vitality(self,error):
self.W = 1.0/(1.0 + abs(error)*4)
if self.W < 0.7:
self.mode = "SURVIVAL"
else:
self.mode = "NORMAL"
class Robot:
def __init__(self):
self.x = WIDTH/2
self.y = HEIGHT/2
self.angle = 0
self.speed = 2
self.path = []
def move(self,mode):
if mode == "SURVIVAL":
speed = 1
else:
speed = 2.5
self.angle += random.uniform(-0.05,0.05)
self.x += math.cos(self.angle)*speed
self.y += math.sin(self.angle)*speed
self.path.append((self.x,self.y))
if len(self.path) > 200:
self.path.pop(0)
orac = ORAC()
robot = Robot()
gyroA = []
gyroB = []
fault_timer = 0
inject_fault = False
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f:
inject_fault = True
# simulate sensors
true_rotation = robot.angle + random.uniform(-0.01,0.01)
gyroA_val = true_rotation + random.uniform(-0.02,0.02)
gyroB_val = true_rotation + random.uniform(-0.02,0.02)
if inject_fault:
gyroA_val += random.uniform(-0.6,0.6)
gyroA.append(gyroA_val)
gyroB.append(gyroB_val)
if len(gyroA) > 30:
gyroA.pop(0)
gyroB.pop(0)
if len(gyroA) > 10:
orac.detect(gyroA,gyroB)
err = gyroA[-1] - gyroB[-1]
orac.vitality(err)
robot.move(orac.mode)
screen.fill(BLACK)
# draw path
for p in robot.path:
pygame.draw.circle(screen,(70,70,70),(int(p[0]),int(p[1])),2)
# draw robot
color = GREEN if orac.mode=="NORMAL" else YELLOW
pygame.draw.circle(screen,color,(int(robot.x),int(robot.y)),12)
# text
t1 = font.render(f"Mode: {orac.mode}",True,WHITE)
t2 = font.render(f"Vitality W: {orac.W:.2f}",True,WHITE)
t3 = font.render(f"Fault: {orac.fault}",True,WHITE)
t4 = font.render("Press F to inject sensor fault",True,WHITE)
screen.blit(t1,(20,20))
screen.blit(t2,(20,45))
screen.blit(t3,(20,70))
screen.blit(t4,(20,100))
pygame.display.flip()
pygame.quit()