-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathscope.py
More file actions
177 lines (144 loc) · 5.8 KB
/
Copy pathscope.py
File metadata and controls
177 lines (144 loc) · 5.8 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
# Scope screen for ServoTune program.
import time
import dmmlib as dmm
# Data storage
time_window = 2 # seconds
samples_per_second = 155 # Samples per second target rate.
# Due to python slowness, its always less than target.
# But a greater limit to how fast we can sample is the DMM servo controller,
# which has a tendency to drop queries if they are sent quickly.
# the serial communications speed should be able to handle 500 position queries
# per second, but sending queries at a rate above about 50 per second causes some
# queries to get dropped. Very frustrating.
#
# Also, if scope has been running for a while, it starts to get slow. this
# probably due to the python heap getting more complex over time, slowing
# down python execution. If you use the scope mode a lot, its a good idea
# to restart ths program frequently. Python may not have been the best choice
# for implementing something like the scope mode.
value_data = []
time_data = []
requested_times = []
x_origin = 0
aquiring_active = False
print("scope init")
random_avg = 0
def dummy_data():
# Make dummy data for testing scope
global random_avg
import random
r = random.uniform(-0.1, 0.1)
random_avg = random_avg*0.95 + r
return random_avg + r
ReqCount = 0
def update_data(start=False):
# Called periodically to add data to the graph.
global aquiring_active, requested_times
if not aquiring_active:
unwrapped_plot()
return
root.after(int(1000 / samples_per_second), update_data) # Schedule next update
dmm.RecvData(0) # Read serial to get previous position
numgot = len(dmm.DecodedQueue)
numnewpos = 0
if numgot:
for n in range (0, numgot):
rx_item = dmm.DecodedQueue[n]
if rx_item[1] == 0x1b:
value_data.append(rx_item[2])
t = requested_times[n]
if t == 0: print("Zero time!!!") # somehow got out of sync or something!
time_data.append(t)
numnewpos += 1
elif rx_item[1] == 0x10:
# Main gain reply for syncronization. Find corresponding dummy timestamp.
#print(" got mg")
for k in range (n, len(requested_times)):
if requested_times[k] == 0:
if k != n:
print("DMM ignored Pos requests: ",k-n)
requested_times = requested_times[k-n:] # Discard excess timestamps
break;
dmm.DecodedQueue = []
requested_times = requested_times[numgot:] # don't clear -- may have more replies pending
if numnewpos: update_plot(numnewpos)
dmm.ReqPosRead() # Request next position read
now = time.time() # Remember when request was sent (this has less jitter than received tiem)
requested_times.append(now)
global ReqCount
ReqCount += 1
if ReqCount & 7 == 0:
# Send occasional unrelated command to detect when position requests were ignored
# in order to get back in sync with the timestamps I saved when sending the request
dmm.SendCommand(0x18)
requested_times.append(0)
#print("req mg")
def unwrapped_plot():
# Updates the graph so that latest point is on the right side of the graph.
canvas.delete("graph")
height = canvas.winfo_height()
x_scale = canvas.winfo_width() / time_window
y_scale = height / 0x10000
x_origin = time_data[-1]-time_window
xo = -1
for i in range (1, len(value_data)):
x = x_scale * (time_data[i]-x_origin)
y = height/2-(value_data[i]-graph_center_val)*y_scale
if xo >= 0:
canvas.create_line(xo,yo,x,y,fill="white", tags="graph", width=2)
xo = x; yo = y
last_wrap_len = 0
graph_center_val = -1
def update_plot(numgot):
global x_origin, time_data, value_data, last_wrap_len, graph_center_val
height = canvas.winfo_height()
x_scale = canvas.winfo_width() / time_window
y_scale = height / 0x10000
if len(value_data) < numgot+2: return
if graph_center_val == -1: graph_center_val = value_data[0]
if time_data[-1] > x_origin+time_window:
x_origin += time_window
if last_wrap_len:
# Trim aquired data to just what is visible.
print("Trim ",last_wrap_len,"points from data", len(time_data), len(value_data), len(requested_times))
time_data = time_data[last_wrap_len:]
value_data = value_data[last_wrap_len:]
last_wrap_len = len(value_data)
#print("pts to check", len(value_data))
min_p = 1000000000; max_p = -1000000000
for p in value_data:
min_p = min(min_p,p)
max_p = max(max_p,p)
graph_center_val = int((min_p+max_p)/2)
xo = -1
for n in range(-2-numgot, -1):
# Calculate where to put new line segment
x = x_scale * (time_data[n]-x_origin)
#offset = value_data[-1] & 0xffff0000
y = height/2-(value_data[n]-graph_center_val)*y_scale
if xo >= 0:
canvas.create_line(xo,yo,x,y,fill="white", tags="graph", width=2)
else:
# Erase slightly ahead of write point so there is a gap
xe = x_scale * (time_data[-1]-x_origin)
canvas.create_rectangle(x+2, 0, xe+10, height, fill="gray", outline="")
xo = x; yo = y
def start_aquring():
global value_data, time_data, aquiring_active, x_origin, requested_times
dmm.ShowReplies = False
dmm.RecvData()
dmm.DecodedQueue = []
dmm.SaveDecoded = True
value_data = []
time_data = []
requested_times = []
x_origin = time.time()
canvas.delete("graph")
aquiring_active = True
update_data(True)
print("scope start")
def stop():
global aquiring_active
aquiring_active = False
dmm.SaveDecoded = False
print("scope stop")