-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
197 lines (154 loc) · 5.91 KB
/
main.py
File metadata and controls
197 lines (154 loc) · 5.91 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
192
193
194
195
196
197
# Kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty,StringProperty, \
BooleanProperty, ObjectProperty
from kivy.clock import Clock
import sys
# Time
import time
from time import monotonic as timer
from datetime import timedelta
# OBD
import obd
from obd import OBDCommand
# Misc
import decimal
class TestScreen(Widget):
# Timing and Clock
clock_timeofday = StringProperty()
stopwatch_disp = StringProperty('')
stopwatch_dt = 0
stopwatch_start = 0
stopwatch_stop = 0
stopwatch_is_running = BooleanProperty(False)
speed_time_new = None
speed_time_old = None
# OBD globals
connection = None
# Speeds
speed_CAS = NumericProperty(0)
speed_Curr = NumericProperty(0)
# Distances
distance_incODO = NumericProperty(0)
distance_stageODO = NumericProperty(0)
reverse_label = StringProperty('Forward')
reverse_flag = False
cycleNum = 0
locked = False
def test_callback(self):
print('button push')
def unlock(self):
# self.locked = False
pass
def lock(self):
# self.locked = True
pass
def reverse_toggle(self):
if(not self.locked):
self.reverse_flag = not self.reverse_flag
if(self.reverse_flag):
self.reverse_label = 'Reverse'
else:
self.reverse_label = 'Forward'
def zero_CAS(self):
if(not self.locked):
self.speed_CAS = self.speed_Curr
self.cycleNum = 0
def zero_stageODO(self):
if(not self.locked):
self.distance_stageODO = 0
self.distance_incODO = 0
def inc_stageODO(self):
if(not self.locked):
self.distance_stageODO += 100 #distance is in meters
def dec_stageODO(self):
if(not self.locked):
self.distance_stageODO -= 100 #distance is in meters
def inc_incODO(self):
if(not self.locked):
self.distance_incODO += 100
def dec_incODO(self):
if(not self.locked):
self.distance_incODO -= 100
def zero_incODO(self):
if(not self.locked):
self.distance_incODO = 0
def reset_speed(self):
pass
def stopwatch_start_stop(self):
if(not self.locked):
if(self.stopwatch_is_running):
self.stopwatch_is_running = False
else:
self.stopwatch_is_running = True
self.stopwatch_start = timer()
def stopwatch_reset(self):
if(not self.locked):
self.stopwatch_dt = 0
self.stopwatch_start = self.stopwatch_stop
def slow_update(self, dt):
# Update the Clock
self.clock_timeofday = time.strftime('%H : %M : %S')
# Update the speeds (if connected)
if(self.connection is not None and self.connection.is_connected()):
try:
speed_resp = self.connection.query(obd.commands.SPEED)
if(not speed_resp.is_null()):
speed = speed_resp.value.magnitude
self.speed_time_new = speed_resp.time
if(self.speed_time_old is None):
self.speed_time_old = self.speed_time_new
self.speed_Curr = str(speed)
# Compute and update
self.cycleNum+=1
CAS = (self.speed_Curr + (self.cycleNum*self.speed_CAS))/(self.cycleNum+1)
self.speed_CAS = CAS
# Compute distance
# distance (m) = speed (m/s) * time (s)
if(not (self.speed_time_old == self.speed_time_new)):
dtime = self.speed_time_new - self.speed_time_old # in seconds
self.speed_time_old = self.speed_time_new
dspeed = (speed*1000.0)/3600.0 # km/h to m/s
# km 1000 m 1 h 1 min 1000 m
# ------ x -------- x -------- x -------- = -------
# h 1 km 60 min 60 sec 3600 s
# distance (m) = speed (m/s) * time (s)
distance = dspeed * dtime
# Add/Subtract to/from ODOs
if(self.reverse_flag):
self.distance_incODO -= distance
self.distance_stageODO -= distance
else:
self.distance_incODO += distance
self.distance_stageODO += distance
except:
print('OBD2 couldnt read cod')
e = sys.exc_info()[0]
print('Error: ' + e)
def fast_update(self, dt):
# Update the stopwatch, if it's running
if(self.stopwatch_is_running):
self.stopwatch_stop = timer()
self.stopwatch_dt += self.stopwatch_stop - self.stopwatch_start
self.stopwatch_start = self.stopwatch_stop
# rounds to milliseconds for display
self.stopwatch_disp = str(timedelta(seconds=round(self.stopwatch_dt,3)))[:-3] #chops empty microseconds
def connect(self):
if(self.connection is None):
try:
self.connection = obd.OBD(portstr='\\.\\COM5')
except:
print('OBD2 connection caused exception')
e = sys.exc_info()[0]
print('Error: ' + e)
class NavODOApp(App):
def build(self):
# return Label(text='Hello world')
app = TestScreen()
app.connect()
Clock.schedule_interval(app.fast_update, 1.0/60.0)
Clock.schedule_interval(app.slow_update, 1.0/3.0)
return app
if __name__ == '__main__':
NavODOApp().run()