-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
191 lines (158 loc) · 6.99 KB
/
main.py
File metadata and controls
191 lines (158 loc) · 6.99 KB
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
import os
os.environ['DISPLAY'] = ":0.0"
import sys
sys.path.insert(0, '.venv/src/pidev')
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.config import Config
from pidev.kivy.PassCodeScreen import PassCodeScreen
from pidev.kivy.PauseScreen import PauseScreen
from pidev.kivy.AdminScreen import AdminScreen
from pidev.kivy.DPEAButton import DPEAButton
from joystick_screen import JoystickScreen
from kivy.animation import Animation
# TODO Lesson 5: Uncomment lines below
# from dpeaDPi.DPiComputer import DPiComputer
# from dpeaDPi.DPiStepper import *
# dpiStepper = DPiStepper()
# dpiStepper.setBoardNumber(0)
# if not dpiStepper.initialize():
# print("Communication with the DPiStepper board failed.")
# dpiComputer = DPiComputer()
# if not dpiComputer.initialize():
# print("Communication with the DPiComputer board failed.")
# -------------------------------------------------------------
class MotorButtonsGUI(App):
"""
Class to handle running the GUI Application
"""
def build(self):
"""
Build the application
:return: Kivy Screen Manager instance
"""
Builder.load_file('main.kv')
Builder.load_file('joystick_screen.kv')
sm = ScreenManager()
sm.add_widget(MainScreen(name='main'))
sm.add_widget(JoystickScreen(name='joystick'))
sm.add_widget(PassCodeScreen(name='passCode'))
sm.add_widget(AdminScreen(name='admin'))
sm.add_widget(PauseScreen(name='pauseScene'))
PassCodeScreen.set_admin_events_screen('admin')
return sm
Window.clearcolor = (1, 1, 1, 1) # White
class MainScreen(Screen):
"""
Class to handle the main screen and its associated touch events
"""
# Variables for the Main Screen
stepper_scheduled = False
servo_scheduled = False
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
def counter_action(self):
"""
This function increases the displayed count for the counter button by one
Variables used/altered:
self.ids.counter_button.text
Called by counter_button
"""
print("Call to counter_action")
self.ids.counter_button.text = str(int(self.ids.counter_button.text) + 1)
def schedule_servo_motor(self):
"""
This function should enable or disable the servo motor from listening to a switch event.
When you press a button, it "wakes up" the servo and then moves according to a switch being pressed or not.
Variables used/altered:
self.servo_scheduled
self.ids.servo_motor_script_button.text
It results in a call being placed to servo_motor_listener
Called by a Kivy button
"""
print("Call to schedule_servo_motor")
if not self.servo_scheduled:
Clock.schedule_interval(self.servo_motor_listener, 0.05)
self.ids.servo_motor_button.fill_color = 'green'
self.ids.servo_motor_button.text = 'Servo ON'
self.servo_scheduled = True
print("Scheduled servo motor")
else:
Clock.unschedule(self.servo_motor_listener)
self.ids.servo_motor_button.fill_color = 'red'
self.ids.servo_motor_button.text = 'Servo OFF'
self.servo_scheduled = False
print("Unscheduled servo motor")
def servo_motor_listener(self, dt=0):
"""
This function reads a switch and activates the servo motor based on that input.
Variables used/altered:
dpiComputer
It results in the servo motor being to either 180° or 0°.
Called by schedule_servo_motor
"""
print("Call to servo_motor_listener")
# if switch is pressed:
# TODO Lesson 5: Move servo one direction
# else:
# TODO Lesson 5: Move servo the other direction
def schedule_stepper_motor(self):
"""
This function should behave similarly to the scheduling function for the servo.
When activated the motor should start moving based off of the slider position.
This function
"""
print("Call to schedule_stepper_motor")
if not self.stepper_scheduled:
Clock.schedule_interval(self.stepper_motor_listener, 0.05)
self.ids.stepper_motor_button.fill_color = 'green'
self.ids.stepper_motor_button.text = 'Stepper ON'
self.stepper_scheduled = True
print("Scheduled stepper motor")
else:
Clock.unschedule(self.stepper_motor_listener)
self.ids.stepper_motor_button.fill_color = 'red'
self.ids.stepper_motor_button.text = 'Stepper OFF'
self.stepper_scheduled = False
print("Unscheduled stepper motor")
def stepper_motor_listener(self, dt=None):
"""
This function is responsible for running the stepper motor based off of the slider.
When the slider is in the middle, the stepper motor should be disabled.
When the slider is moved to the right, the motor increases in speed in the clockwise direction.
When the slider is moved left of the midpoint, the motor increases in speed in the counter-clockwise direction.
"""
print("Call to stepper_motor_listener")
if self.ids.position == 0:
print("yep")
# TODO Lesson 5: If zero, decelerate, stop, and disable
# TODO Lesson 5: If positive, spin stepper CW. Speed should increase with slider.
# TODO Lesson 5: If negative, spin stepper CCW. Speed should increase with slider.
# TODO Lesson 5: Utilize the helper function below to clean up your motor control code
def set_motor_speed_by_revs_per_sec(self, revs_per_sec, stepper_num=0):
""" This is a helper function that sets the speed of a stepper motor by a specified revolutions per second"""
microstepping = 8
dpiStepper.setMicrostepping(microstepping)
speed_steps_per_second = (200 * microstepping) * revs_per_sec
accel_steps_per_second_per_second = speed_steps_per_second
dpiStepper.setSpeedInStepsPerSecond(stepper_num, speed_steps_per_second)
dpiStepper.setAccelerationInStepsPerSecondPerSecond(stepper_num, accel_steps_per_second_per_second)
def switch_screen(self):
self.manager.current = 'joystick'
print("Triggered switch to Joystick Screen")
def admin_action(self):
"""
Hidden admin button touch event. Transitions to passCodeScreen.
This method is called from pidev/kivy/PassCodeScreen.kv
:return: None
"""
self.manager.current = 'passCode'
if __name__ == "__main__":
# Makes the window auto full screen
Config.set('graphics', 'fullscreen', 'auto')
Config.set('graphics', 'window_state', 'maximized')
Config.write()
MotorButtonsGUI().run()