-
Notifications
You must be signed in to change notification settings - Fork 73
/
student.py
127 lines (104 loc) · 4.08 KB
/
student.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
#!/usr/bin python3
from teacher import PiggyParent
import sys
import time
class Piggy(PiggyParent):
'''
*************
SYSTEM SETUP
*************
'''
def __init__(self, addr=8, detect=True):
PiggyParent.__init__(self) # run the parent constructor
'''
MAGIC NUMBERS <-- where we hard-code our settings
'''
self.LEFT_DEFAULT = 80
self.RIGHT_DEFAULT = 80
self.MIDPOINT = 1500 # what servo command (1000-2000) is straight forward for your bot?
self.set_motor_power(self.MOTOR_LEFT + self.MOTOR_RIGHT, 0)
self.load_defaults()
def load_defaults(self):
"""Implements the magic numbers defined in constructor"""
self.set_motor_limits(self.MOTOR_LEFT, self.LEFT_DEFAULT)
self.set_motor_limits(self.MOTOR_RIGHT, self.RIGHT_DEFAULT)
self.set_servo(self.SERVO_1, self.MIDPOINT)
def menu(self):
"""Displays menu dictionary, takes key-input and calls method"""
## This is a DICTIONARY, it's a list with custom index values. Python is cool.
# Please feel free to change the menu and add options.
print("\n *** MENU ***")
menu = {"n": ("Navigate", self.nav),
"d": ("Dance", self.dance),
"o": ("Obstacle count", self.obstacle_count),
"s": ("Shy", self.shy),
"f": ("Follow", self.follow),
"c": ("Calibrate", self.calibrate),
"q": ("Quit", self.quit)
}
# loop and print the menu...
for key in sorted(menu.keys()):
print(key + ":" + menu[key][0])
# store the user's answer
ans = str.lower(input("Your selection: "))
# activate the item selected
menu.get(ans, [None, self.quit])[1]()
'''
****************
STUDENT PROJECTS
****************
'''
def dance(self):
"""A higher-ordered algorithm to make your robot dance"""
# TODO: check to see if it's safe before dancing
# lower-ordered example...
self.right(primary=50, counter=50)
time.sleep(2)
self.stop()
def safe_to_dance(self):
""" Does a 360 distance check and returns true if safe """
pass
def shake(self):
""" Another example move """
self.deg_fwd(720)
self.stop()
def example_move(self):
"""this is an example dance move that should be replaced by student-created content"""
self.right() # start rotating right
time.sleep(1) # turn for a second
self.stop() # stop
self.servo(1000) # look right
time.sleep(.25) # give your head time to move
self.servo(2000) # look left
def scan(self):
"""Sweep the servo and populate the scan_data dictionary"""
for angle in range(self.MIDPOINT-350, self.MIDPOINT+350, 3):
self.servo(angle)
self.scan_data[angle] = self.read_distance()
def obstacle_count(self):
"""Does a 360 scan and returns the number of obstacles it sees"""
pass
def nav(self):
print("-----------! NAVIGATION ACTIVATED !------------\n")
print("-------- [ Press CTRL + C to stop me ] --------\n")
print("-----------! NAVIGATION ACTIVATED !------------\n")
# TODO: build self.quick_check() that does a fast, 3-part check instead of read_distance
while self.read_distance() > 250: # TODO: fix this magic number
self.fwd()
time.sleep(.01)
self.stop()
# TODO: scan so we can decide left or right
# TODO: average the right side of the scan dict
# TODO: average the left side of the scan dict
###########
## MAIN APP
if __name__ == "__main__": # only run this loop if this is the main file
p = Piggy()
if sys.version_info < (3, 0):
sys.stdout.write("Sorry, requires Python 3.x\n")
p.quit()
try:
while True: # app loop
p.menu()
except KeyboardInterrupt: # except the program gets interrupted by Ctrl+C on the keyboard.
p.quit()