-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayback.py
executable file
·178 lines (160 loc) · 5.74 KB
/
playback.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python
import pygame
import random
from itertools import groupby
import time
import serial
import recorder_handlers
from ConfigParser import *
class application:
def __init__(self, file):
# Initialize the things
self.time_since_event = time.time()
self.ini_file = file
self.LoadIniData()
self.joymap = [3,4,1,2] # link between joystick and the servo to move
self.joyreverse = [0,1,0,0,0]
self.servo_pos = []
for i in self.joymap:
self.servo_pos.append(90)
usbport = self.cp.get('Variables', 'usbport')
try:
self.ser = serial.Serial(usbport, 9600, timeout=1)
except:
print " did you plug something into " + usbport + "? an eyeball for example?"
exit()
f = self.cp.get('Variables', 'recording_file')
self.rdata = recorder_handlers.RecordingData(f)
self.playlist = self.rdata.get_names()
pygame.init()
self.pygame = pygame
if(pygame.joystick.get_count() < 1):
# no joysticks found
print "Please connect a joystick.\n"
self.Quit()
else:
# create joystick object from first joystick in list
self.Joy0 = pygame.joystick.Joystick(0)
self.Joy0.init()
def find_events(self):
events = pygame.event.get()
for e in events:
if e.type == pygame.JOYBUTTONDOWN:
if (e.dict['button'] == 5):
print "button 5"
if (e.dict['button'] == 7):
print "button 7"
quit()
if e.type == pygame.JOYAXISMOTION:
pos = e.dict['value']
pos = int(90 + round(pos * 90, 0))
n = e.dict['axis']
if (n >= 0 and n < 5):
# constructs the string that reflects joystick
# position
self.ServoMove(n,pos)
self.time_since_event = time.time()
def PlayBack(self, name):
bits = self.rdata.get_data(name)
cointossresult = random.randrange(0,100)
# cointossfreq is a percentage of the time we'll do nothing
cointossfreq = 30
if cointossresult < cointossfreq:
print "pausing"
else :
print "next thing: %s" % name
if len(bits) == 0:
print "This name: %s is not in the recording file" % name
else:
bits = self.ProcessBits(bits)
self.ServoCenter()
counter = 0
for i in range(0,len(bits)):
self.find_events()
while(time.time() - self.time_since_event < 1):
self.find_events()
servo, angle = bits[i]['pos_str'].split()
time.sleep(bits[i]['delta'])
if cointossresult >= cointossfreq:
self.ServoMove(int(servo), int(angle))
def LoadIniData(self):
FileName = self.ini_file
self.cp=ConfigParser()
try:
self.cp.readfp(open(FileName,'r'))
# f.close()
except IOError:
raise Exception,'NoFileError'
return
def ProcessBits(self,rows):
# this removes duplicates, easy.
rows = [ key for key,_ in groupby(rows)]
l = {}
for i in range(0,len(rows)):
l[i] = {}
t, l[i]['pos_str'] = rows[i].split(' ', 1)
seconds, useconds = t.split(':')
l[i]['time'] = float("%2.6lf" % float(seconds + "." + useconds))
pos_old = l[0]['pos_str']
# t_old = l[0]['time']
t_old = 0
new = {}
count = 0
for i in range(1,len(rows)):
pos = l[i]['pos_str']
t = l[i]['time']
if pos != pos_old:
new[count] = {}
new[count]['delta'] = l[i]['time'] - t_old
new[count]['pos_str'] = pos_old
pos_old = pos
t_old = t
count = count + 1
new[count] = {}
new[count]['delta'] = l[i]['time'] - t_old
new[count]['pos_str'] = pos_old
return new
def ServoSlowMove(self, a2, b2, c2, d2, inc):
(a1, b1, c1, d1) = self.servo_pos
i1 = float(a2 - a1) / inc
i2 = float(b2 - b1) / inc
i3 = float(c2 - c1) / inc
i4 = float(d2 - d1) / inc
for i in range(0, inc):
a1 = float(a1) + i1
b1 = float(b1) + i2
c1 = float(c1) + i3
d1 = float(d1) + i4
self.ServoMove(0, int(a1))
self.ServoMove(1, int(b1))
self.ServoMove(2, int(c1))
self.ServoMove(3, int(d1))
def ServoCenter(self):
self.ServoSlowMove(90,90,90,90,40)
def ServoMove(self,servo, angle):
self.servo_pos[servo] = angle
servo = self.joymap[servo]
if self.joyreverse[servo]:
angle = 180 - angle
if (0 <= angle <= 180):
self.ser.write(chr(255))
self.ser.write(chr(servo))
self.ser.write(chr(angle))
else:
print "Servo angle must be an integer between 0 and 180.\n"
def ServoMove(self,servo, angle):
servo = self.joymap[servo]
if self.joyreverse[servo]:
angle = 180 - angle
if (0 <= angle <= 180):
self.ser.write(chr(255))
self.ser.write(chr(servo))
self.ser.write(chr(angle))
else:
print "Servo angle must be an integer between 0 and 180.\n"
if __name__ == "__main__":
a = application('./recorder.ini')
while (1):
play_this = a.playlist[random.randrange(len(a.playlist))]
a.PlayBack(play_this)
quit()