forked from michaeljfriedman/robo-fiesta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomwalk.py
128 lines (105 loc) · 3.2 KB
/
randomwalk.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
'''
Authors: Michael Friedman
Created: 11/12/16
Description: Class implements a random walk. Randomly moves the vehicle
left, right, or forward.
'''
import sys
import random
from vehicle import Vehicle
from time import sleep
import threading
class RandomWalk:
'''
Initializes the RandomWalk object for a vehicle that plays a song.
Args:
song_filename: string path to filename containing song to play
'''
def __init__(self):
self._moving = False
self._vehicle = Vehicle(24, 18, 2)
'''
Getter for vehicle
'''
@property
def vehicle(self):
return self._vehicle
'''
Plays the vehicle's song
'''
def _play(self):
self._vehicle.buzzer.play_song(self._song_filename)
def _walk(self):
# Setup
channel_left_motor = 24
channel_right_motor = 18
channel_buzzer = 2
vehicle = Vehicle(channel_left_motor, channel_right_motor, channel_buzzer)
# Play song
vehicle.buzzer.play_song('ghostbusters.txt')
# Main loop
self._moving = True
while self._moving:
# Take a random step
move = random.randint(0, 7)
song = random.randint(0, 7)
range_turn_left = range(0, 3)
range_turn_right= range(3, 6)
range_move = range(6, 8)
range_ghostbusters = range(0, 2)
range_a_unlocked = range(2, 4)
range_coin = range(4, 6)
range_starwars = range(6, 7)
range_callmemaybe = range(7, 8)
if move in range_turn_left:
vehicle.turn(Vehicle.LEFT)
elif move in range_turn_right:
vehicle.turn(Vehicle.RIGHT)
else:
vehicle.move(Vehicle.FORWARD)
if not vehicle.buzzer.is_playing:
if song in range_ghostbusters:
vehicle.buzzer.play_song('ghostbusters.txt')
elif song in range_a_unlocked:
vehicle.buzzer.play_song('a_unlocked.txt')
elif song in range_coin:
vehicle.buzzer.play_song('coin.txt')
elif song in range_starwars:
vehicle.buzzer.play_song('starwars.txt')
else:
vehicle.buzzer.play_song('callmemaybe.txt')
else:
sleep(1)
# Loop ends when stop() is called
vehicle.stop()
vehicle.buzzer.stop_song()
vehicle.destroy()
'''
Starts the random walk
'''
def start(self):
threading.Thread(target=self._walk).start()
'''
Stops the random walk
'''
def stop(self):
self._moving = False
'''
Executes random walk
Args:
song_filename: filename
'''
def main():
rw = RandomWalk()
rw.start()
msg = 'Command? (l/r/x)\n'
cmd = raw_input(msg)
while (cmd != 'x'):
if cmd == 'l':
rw.vehicle.turn(Vehicle.LEFT)
elif cmd == 'r':
rw.vehicle.turn(Vehicle.RIGHT)
cmd = raw_input(msg)
rw.stop()
if __name__ == '__main__':
main()