-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
200 lines (164 loc) · 6.83 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
from kivymd.app import MDApp
from kivy.clock import Clock
from kivy.core.window import Window
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.widget import MDWidget
from kivymd.uix.button import MDRectangleFlatButton
from kivymd.uix.behaviors.toggle_behavior import MDToggleButton
from kivy.properties import ObjectProperty, NumericProperty, BooleanProperty, StringProperty
from kivymd.uix.slider import MDSlider
from kivy_garden.graph import Graph, MeshLinePlot
import BTHandler
class SprungSlider(MDSlider):
def sliderReleased(self):
if (not self.active):
self.value = 0
class Toggle(MDRectangleFlatButton, MDToggleButton):
pass
class MainGraph(Graph, MDWidget):
pass
class VoltGraph(Graph, MDWidget):
pass
class ControllerMain(MDWidget):
pObj = ObjectProperty(None)
iObj = ObjectProperty(None)
dObj = ObjectProperty(None)
trimObj = ObjectProperty(None)
speedObj = ObjectProperty(None)
enableObj = ObjectProperty(None)
connectBtn = ObjectProperty(None)
btStatus = ObjectProperty(None)
graphStack = ObjectProperty(None)
pidLockout = BooleanProperty(None)
connectLockout = BooleanProperty(None)
consoleText = StringProperty(None)
voltageText = StringProperty("")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.btHandler = BTHandler.BTHandler("COM6", 1/20)
Clock.schedule_interval(self.regularBTUpdate, 1/22)
self.initGraphs()
def regularBTUpdate(self, _):
status = self.btHandler.connectionStatus()
if (status):
self.btStatus.working = False
self.btStatus.text = status
self.connectBtn.disabled = False
if status.lower().find("error") != -1: # error connecting/disconnecting
self.connectBtn.text = 'Connect'
self.connectBtn.state = 'normal'
if self.connectBtn.state == 'normal':
self.connectLockout = True
self.connectBtn.text = 'Connect'
self.voltageText = ""
self.consoleText = ""
self.voltLine.points = []
self.actDegLine.points = []
self.setDegLine.points = []
else:
self.connectBtn.text = 'Disconnect'
self.connectLockout = False
# if not connected, return
if (self.connectBtn.state != 'down'):
return
# pid Locking
if self.pidLockout and self.btHandler.get("PIDenable"):
pidVals = self.btHandler.getMany('p', 'i', 'd')
self.pObj.value = pidVals['p']
self.iObj.value = pidVals['i']
self.dObj.value = pidVals['d']
self.pidLockout = False
print('PID enabled')
elif not self.btHandler.get('PIDenable'):
self.pidLockout = True
# update speed and trim
self.btHandler.set(speed=self.speedObj.value, trim=self.trimObj.value)
# get logging and update UI
botData = self.btHandler.getMany("messages", 'logs')
self.btHandler.clearLists('messages', 'logs')
self.enableObj.state = 'down' if self.btHandler.isEnabled() else 'normal'
for message in botData['messages']:
self.consoleText += (message)
if self.consoleText.count('\n') > 10:
lines = self.consoleText.splitlines(True)[-10:]
self.consoleText = "".join(lines)
if not botData['logs']:
return
for packet in botData['logs']:
self.voltLine.points.append((packet[0], packet[1]))
self.setDegLine.points.append((packet[0], packet[2]))
self.actDegLine.points.append((packet[0], packet[3]))
self.voltageText = str(self.voltLine.points[-1][1])+ ' V'
# Scroll Graphs if needed
if self.voltLine.points[-1][0] > self.voltGraph.xmax:
self.voltLine.points = self.voltLine.points[-301:]
self.setDegLine.points = self.setDegLine.points[-301:]
self.actDegLine.points = self.actDegLine.points[-301:]
max = self.voltLine.points[-1][0]
min = max - 15
self.voltGraph.xmin = min
self.voltGraph.xmax = max
self.mainGraph.xmin = min
self.mainGraph.xmax = max
def initGraphs(self):
self.voltLine = MeshLinePlot(color=[1, 0, 0, 1])
# self.voltLine.points = [(x/20, .1) for x in range(300)]
self.voltGraph = VoltGraph()
self.voltGraph.add_plot(self.voltLine)
self.graphStack.add_widget(self.voltGraph)
self.setDegLine = MeshLinePlot(color=[1, 1, 0, 1])
# self.setDegLine.points = [(x/20, 90) for x in range(300)]
self.actDegLine = MeshLinePlot(color=[1, 0, 0, 1])
# self.actDegLine.points = [(x/20, 0) for x in range(300)]
self.mainGraph = MainGraph()
self.mainGraph.add_plot(self.setDegLine)
self.mainGraph.add_plot(self.actDegLine)
self.graphStack.add_widget(self.mainGraph)
def sendPID(self):
self.btHandler.set(p=self.pObj.value,
i=self.iObj.value, d=self.dObj.value)
self.btHandler.set(sendPID=True)
def savePID(self):
self.btHandler.set(p=self.pObj.value,
i=self.iObj.value, d=self.dObj.value)
self.btHandler.set(sendPID=True, savePID=True)
def enablePressed(self, state):
self.btHandler.setEnable(state == 'down')
def stepPressed(self, buttonText):
self.step = float(buttonText)
def connectPressed(self, state):
self.connectBtn.disabled = True
self.btStatus.working = True
if (state == 'down'):
self.btHandler.requestConnect()
self.connectBtn.text = "Disconnect"
self.btStatus.text = "Connecting..."
elif (state == 'normal'):
self.btHandler.requestConnect(disconnect=True)
self.btStatus.text = "disconnecting..."
class MainApp(MDApp):
def build(self):
self.title = "Balance Bot Controller"
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Teal"
self.theme_cls.primary_hue = '600'
self.theme_cls.primary_dark_hue = '900'
self.theme_cls.primary_light_hue = '400'
self.theme_cls.accent_palette = "DeepPurple"
self.controller = ControllerMain()
return self.controller
def on_stop(self):
self.controller.btHandler.exit()
class NumSpinner(MDBoxLayout):
step = NumericProperty(1)
min = NumericProperty(1)
def plusPressed(self):
self.value += self.step
def minusPressed(self):
self.value -= self.step
def textUpdated(self, str):
self.value = max(float(str), self.min)
if __name__ == "__main__":
# Window.custom_titlebar = True
Window.maximize()
MainApp().run()