-
Notifications
You must be signed in to change notification settings - Fork 0
/
piGui.py
executable file
·163 lines (124 loc) · 4.25 KB
/
piGui.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
#!/bin/bash
from __future__ import division
import numpy as np
from numpy import genfromtxt
import matplotlib
#import pyautogui
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import pprint
from screeninfo import get_monitors
import time
import Adafruit_PCA9685
KS = genfromtxt('/home/pi/Documents/Github/QED-Springs/5_2/CSV_StateInfo/Ks.csv', delimiter=',')
States = genfromtxt('/home/pi/Documents/Github/QED-Springs/5_2/CSV_StateInfo/States.csv', delimiter=',')
Durations = genfromtxt('/home/pi/Documents/Github/QED-Springs/5_2/CSV_StateInfo/times.csv', delimiter=',')
Poses = genfromtxt('/home/pi/Documents/Github/QED-Springs/5_2/CSV_StateInfo/Poss.csv', delimiter=',')
PosTims = genfromtxt('/home/pi/Documents/Github/QED-Springs/5_2/CSV_StateInfo/Tims.csv', delimiter=',')
width = 5
height = 5
# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()
# Alternatively specify a different address and/or bus:
#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2)
# Configure min and max servo pulse lengths
servo_min = 150 # Min pulse length out of 4096
servo_max = 600 # Max pulse length out of 4096
# Set frequency to 60hz, good for servos.
pwm.set_pwm_freq(60)
w, h = matplotlib.figure.figaspect(3.)
fig = plt.figure(figsize=(width,height))
execMat = np.zeros((height, width))
class Servo(object):
"""A class for a single servo with functions to move it"""
def __init__(self, PinNum):
super(Servo, self).__init__()
self.PinNum = PinNum
def set_servo_angle(self, angle):
pulse = int(angle*(servo_max-servo_min)+servo_min)
print(pulse)
pwm.set_pwm(self.PinNum, 0, pulse)
def moveServos(Poses, Times, Durations, Servos):
t = 0;
y = []
ts = []
maxTime = 25 - max(Durations)
t0 = time.time() - maxTime;
maxIndex = next(i for i,v in enumerate(Times) if v > maxTime)
i = maxIndex
print(Times[i])
while t < Times[-1]:
t = time.time()-t0;
if t > Times[i]:
for j, Servo in enumerate(Servos, 0):
Servo.set_servo_angle(Poses[j][i])
i = i + 1
time.sleep(3)
for Servo in Servos:
Servo.set_servo_angle(.5)
class ExecuteButtonProcessor(object):
def __init__(self, axes, label):
self.axes = axes
self.button = Button(axes, "Execute")
self.button.on_clicked(self.process)
self.intializeServos()
def intializeServos(self):
for i in range(width):
servo = Servo(i)
servo.set_servo_angle(.5)
print("Setting Angle")
def process(self, event):
execMat[execMat == 0] = -1
# print(States[:, width:2*width])
#KsRes = []
PosRes = []
DurRes = []
Servos = []
#theseTimes = []
for i in range(width):
e = States[:, 5:10] # The width of the array for States
loc = np.where(np.all((e-execMat[:,i])==0, axis=1))[0][0]
#KsRes.append(KS[loc, :])
PosRes.append(Poses[loc, :])
DurRes.append(Durations[loc])
Servos.append(Servo(i))
#theseTimes.append(Times[loc])
#for res in KsRes:
# pass
# print(res)
moveServos(PosRes, PosTims, DurRes, Servos)
execMat[execMat == -1] = 0
# print(KsRes)
class ButtonClickProcessor(object):
def __init__(self, axes, label, i, j):
self.axes = axes
self.button = Button(axes, label)
self.button.on_clicked(self.process)
self.button.color = 'Red'
self.button.hovercolor = self.button.color
self.colorMode = 0
self.colors = ['Red', 'Blue']
self.i = i
self.j = j
def process(self, event):
execMat[self.j][self.i] = not execMat[self.j][self.i]
print("execMatGen", execMat)
print(self.i)
print(self.j)
self.colorMode = not self.colorMode
self.button.color = self.colors[self.colorMode]
self.button.hovercolor = self.button.color
pyautogui.moveRel(0, 1)
pyautogui.moveRel(0, -1)
fig.canvas.draw()
buttons = []
buttonAxes = []
for i in range(width):
for j in range(height):
buttonAxes.append(plt.axes([(i)/float(width+1), j/float(height), 1/(float(width)), 1/float(height)]))
buttons.append(ButtonClickProcessor(buttonAxes[-1], "", i, height-1-j))
execButtonAxes = plt.axes([1-1/(width+1), 0, 1/(float(width+1)), height])
execButton = ExecuteButtonProcessor(execButtonAxes, "Execute")
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
plt.show()