-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurses-robot.py
executable file
·139 lines (102 loc) · 3.66 KB
/
curses-robot.py
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
#!/usr/bin/env python3
import curses
from vision.motiongrid import MotionCheckResult
from robot.robot import Robot
from status import status_reporting
class MotionWindow:
def __init__(self, screen):
self._MOTION_WINDOW_HEIGHT = 6
self.window = screen.subwin(self._MOTION_WINDOW_HEIGHT, 10, screen_height - (self._MOTION_WINDOW_HEIGHT + 1), 0)
self.window.border('|', '|', '-', '-')
self.window.addstr(1, 1, " MOTION ")
def update(self, motion_check_result):
START_POS_Y = 2
START_POS_X = 2
for y in range(3):
for x in range(3):
if motion_check_result.motion_at(x, y):
display_char = "X"
else:
display_char = "O"
self.window.addstr(START_POS_Y + y, START_POS_X + (x * 2), display_char)
self.refresh()
def refresh(self):
self.window.refresh()
class DepthWindow:
def __init__(self, screen, label, y, x):
self.WINDOW_HEIGHT = 4
self.WINDOW_WIDTH = 25
self.window = screen.subwin(self.WINDOW_HEIGHT, self.WINDOW_WIDTH, y, x)
self.label = label
self.update(0.00)
def update(self, depth_cm):
self.window.erase()
self.window.border('|', '|', '-', '-')
self.window.addstr(1, 1, f" {self.label} DEPTH ")
self.window.addstr(2, 2, f"{depth_cm:.2f}")
self.refresh()
def refresh(self):
self.window.refresh()
class LogWindow:
def __init__(self, screen, y, x):
self.window = screen.subwin(5, screen_width - 10, y, x)
def update(self, message):
self.window.erase()
self.window.border('|', '|', '-', '-')
self.window.addstr(1, 1, "LOG")
self.window.addstr(2, 1, f"{message}")
self.refresh()
def refresh(self):
self.window.refresh()
class BehaviorWindow:
def __init__(self, screen):
WINDOW_HEIGHT = 4
WINDOW_WIDTH = 20
self.window = screen.subwin(WINDOW_HEIGHT, WINDOW_WIDTH, screen_height - (WINDOW_HEIGHT + 1), screen_width - (WINDOW_WIDTH + 1))
self.update("LOADING...")
def update(self, behavior):
self.window.erase()
self.window.border('|', '|', '-', '-')
self.window.addstr(1, 1, "BEHAVIOR")
self.window.addstr(2, 1, f"{behavior}")
self.refresh()
def refresh(self):
self.window.refresh()
def should_quit(screen):
try:
return 'q' == screen.getkey()
except:
return False
MOTION_WINDOW_HEIGHT = 6
# The `screen` is a window that acts as the master window
# that takes up the whole screen. Other windows created
# later will get painted on to the `screen` window.
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
screen.nodelay(True)
screen_height, screen_width = screen.getmaxyx()
motion_window = MotionWindow(screen)
left_depth_window = DepthWindow(screen, "Left", 1, 1)
right_depth_window = DepthWindow(screen, "Right", 1, 50)
log_window = LogWindow(screen, 10, 1)
behavior_window = BehaviorWindow(screen)
status_reporting.log = log_window.update
status_reporting.motion_listener = motion_window.update
status_reporting.left_depth_listener = left_depth_window.update
status_reporting.right_depth_listener = right_depth_window.update
status_reporting.behavior_listener = behavior_window.update
screen.refresh()
status_reporting.log("Starting...")
try:
robot = Robot()
robot.quit_monitor = lambda : should_quit(screen)
robot.start()
robot.run()
finally:
curses.nocbreak()
screen.keypad(0)
curses.curs_set(1)
curses.echo()
curses.endwin()