-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
230 lines (179 loc) · 7.38 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# ////////////////////////////////////////////////////////////////
# // IMPORT STATEMENTS //
# ////////////////////////////////////////////////////////////////
import math
import sys
import time
import threading
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import *
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.slider import Slider
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.clock import Clock
from kivy.animation import Animation
from functools import partial
from kivy.config import Config
from kivy.core.window import Window
from pidev.kivy import DPEAButton
from pidev.kivy import PauseScreen
from time import sleep
import RPi.GPIO as GPIO
from pidev.stepper import stepper
from pidev.Cyprus_Commands import Cyprus_Commands_RPi as cyprus
# ////////////////////////////////////////////////////////////////
# // GLOBAL VARIABLES //
# // CONSTANTS //
# ////////////////////////////////////////////////////////////////
ON = False
OFF = True
HOME = True
TOP = False
OPEN = False
CLOSE = True
YELLOW = .180, 0.188, 0.980, 1
BLUE = 0.917, 0.796, 0.380, 1
DEBOUNCE = 0.1
INIT_RAMP_SPEED = 150
RAMP_LENGTH = 725
# ////////////////////////////////////////////////////////////////
# // DECLARE APP CLASS AND SCREENMANAGER //
# // LOAD KIVY FILE //
# ////////////////////////////////////////////////////////////////
class MyApp(App):
def build(self):
self.title = "Perpetual Motion"
return sm
Builder.load_file('main.kv')
Window.clearcolor = (.1, .1, .1, 1) # (WHITE)
cyprus.open_spi()
# ////////////////////////////////////////////////////////////////
# // SLUSH/HARDWARE SETUP //
# ////////////////////////////////////////////////////////////////
sm = ScreenManager()
#ramp = stepper(port=0, speed=INIT_RAMP_SPEED)
# ////////////////////////////////////////////////////////////////
# // MAIN FUNCTIONS //
# // SHOULD INTERACT DIRECTLY WITH HARDWARE //
# ////////////////////////////////////////////////////////////////
# ////////////////////////////////////////////////////////////////
# // DEFINE MAINSCREEN CLASS THAT KIVY RECOGNIZES //
# // //
# // KIVY UI CAN INTERACT DIRECTLY W/ THE FUNCTIONS DEFINED //
# // CORRESPONDS TO BUTTON/SLIDER/WIDGET "on_release" //
# // //
# // SHOULD REFERENCE MAIN FUNCTIONS WITHIN THESE FUNCTIONS //
# // SHOULD NOT INTERACT DIRECTLY WITH THE HARDWARE //
# ////////////////////////////////////////////////////////////////
class MainScreen(Screen):
gate = ObjectProperty(None)
auto = ObjectProperty(None)
ramp = ObjectProperty(None)
staircase = ObjectProperty(None)
rampSpeed = ObjectProperty(None)
staircaseSpeed = ObjectProperty(None)
title = ObjectProperty(None)
rampSpeedLabel = ObjectProperty(None)
staircaseSpeedLabel = ObjectProperty(None)
anticrash = ObjectProperty(None)
version = cyprus.read_firmware_version()
staircaseSpeedText = '0'
speedramp = 0
stairspeed = 0
cyprus.initialize()
cyprus.setup_servo(2)
s0 = stepper(port=0, micro_steps=32, hold_current=20, run_current=20, accel_current=20, deaccel_current=20,
steps_per_unit=200, speed=8)
cyprus.set_servo_position(2, 0)
cyprus.set_pwm_values(1, period_value=100000, compare_value=50000, compare_mode=cyprus.LESS_THAN_OR_EQUAL)
sleep(6)
cyprus.set_pwm_values(1, period_value=80000, compare_value=0, compare_mode=cyprus.LESS_THAN_OR_EQUAL)
s0.go_until_press(0, 20000)
while s0.isBusy():
sleep(0.5)
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.initialize()
def busymotor(self):
while self.s0.isBusy():
sleep(0.4)
def toggleGate(self):
if self.gate.text == 'Open Gate': # & self.ramp.text == "Press for Ramp":
cyprus.set_servo_position(2, 0.5)
print("gate closed")
self.gate.text = 'Close Gate'
elif self.gate.text == 'Close Gate':
cyprus.set_servo_position(2, 0)
print("gate open")
self.gate.text = 'Open Gate'
def toggleStaircase(self):
if self.staircase.text == 'Staircase On':
cyprus.set_pwm_values(1, period_value=25000, compare_value=50000, compare_mode=cyprus.LESS_THAN_OR_EQUAL)
self.staircase.text = 'Staircase Off'
elif self.staircase.text == 'Staircase Off':
cyprus.set_pwm_values(1, period_value=80000, compare_value=0, compare_mode=cyprus.LESS_THAN_OR_EQUAL)
self.staircase.text = 'Staircase On'
def sliderStaircase(self):
if self.staircase.text == 'Staircase Off':
cyprus.set_pwm_values(1, period_value=25000, compare_value=self.staircaseSpeed.value, compare_mode=cyprus.LESS_THAN_OR_EQUAL)
def toggleRamp(self):# git reflog and git checkout will work to recover the last file that was commited but lost
if self.ramp.text == "Ramp Off":
efresher = threading.Thread(target=self.threadRamp) # thread call to threadRamp()
efresher.start()
def threadRamp(self):
self.s0.relative_move(1)
while self.s0.get_position_in_units() <= 28.5:
self.ramp.text = "Ramp On"
self.speedramp = self.rampSpeed.value
self.s0.go_until_press(1, self.speedramp)
self.s0.go_until_press(0, 50000)
self.busymotor()
self.ramp.text = "Ramp Off"
def sliderRamp(self):
print(self.rampSpeed.value)
def command(self):
self.s0.softStop()
refresher = threading.Thread(target=self.toggleRamp) # thread call to toggleRamp()
refresher.start()
def automatic(self):
print('auto 1 run has started')
self.toggleGate()
sleep(0.15)
self.toggleGate()
sleep(1)
self.toggleRamp()
self.busymotor()
self.toggleStaircase()
sleep(6)
self.toggleStaircase()
def threadautomatic(self):
self.s0.softStop()
refresher1 = threading.Thread(target=self.automatic) # thread call to toggleRamp()
refresher1.start()
def initialize(self):
print("Close gate, stop staircase and home ramp here")
def resetColors(self):
self.ids.gate.color = YELLOW
self.ids.staircase.color = YELLOW
self.ids.ramp.color = YELLOW
self.ids.auto.color = BLUE
def anticrash(self):
self.s0.softStop()
def quit(self):
print("Exit")
MyApp().stop()
sm.add_widget(MainScreen(name='main'))
# ////////////////////////////////////////////////////////////////
# // RUN APP //
# ////////////////////////////////////////////////////////////////
MyApp().run()
cyprus.close_spi()