-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
executable file
·386 lines (299 loc) · 12.9 KB
/
monitor.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from dash.dependencies import Input, Output, State
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_daq as daq
import math
import plotly.graph_objs as go
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
import base64
import urllib
import rccat
rcCatSerialIO = rccat.SerialIO()
# end handle data import
# create dash app
# external CSS stylesheets
external_stylesheets = [
'https://codepen.io/chriddyp/pen/bWLwgP.css',
{
'href': 'https://stackpath.bootstrapcdn.com/bootswatch/4.4.1/sketchy/bootstrap.min.css',
'rel': 'stylesheet',
'integrity': 'sha384-2kOE+STGAkgemIkUbGtoZ8dJLqfvJ/xzRnimSkQN7viOfwRvWseF7lqcuNXmjwrL',
'crossorigin': 'anonymous'
}
]
app = dash.Dash("RcCat Monitor", external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.H1('RcCat Control'),
html.Div(className="card", children=[
dcc.Tabs(id="main-tabs", children=[
dcc.Tab(label='Connect', className="card-header", children=[
html.Div(className="card-body", children=[
html.P(
'Connect to your RcCat via a serial connection (e.g. /dev/ttyUSB0) or via WiFi relais (e.g. http://rccat.local).'),
html.Div([
dcc.Input(
placeholder='USB tty',
type='text',
value='/dev/ttyUSB0',
id='connect-path'
),
html.Button('Connect', id='connect-button',
className="btn btn-success ml-1", disabled=False),
html.Button('Disconnect', id='disconnect-button',
className="btn btn-danger ml-1", disabled=True),
html.Label('', id='connect-status', className="ml-3")
], className="input-group, p-1"),
])
]),
dcc.Tab(label='Control', className="card-header",
children=[html.Div(className="card-body", children=[
html.P('Use the joystick to remote control your RcCat.'),
daq.Joystick(
id='joystick',
size=250,
className="d-flex justify-content-center"
),
html.Label('', id="joystick-label",
style="visible:hidden"),
html.P(
'Change internal parematers to play with driving characteristics:',
className="mt-3"),
html.Div([
html.Label("Parameter ID: ",
className="mr-1 mt-1"),
dcc.Input(
id="send-parameter-id",
type="number",
value=1
),
html.Label(
"Value: ", className="mr-1 ml-4 mt-1"),
dcc.Input(
id="send-parameter-value",
type="number",
value=100
),
html.Button('Send', id='send-parameter-button',
className="btn btn-primary ml-4"),
], className="input-group p-1"),
html.Label('', id="send-parameter-output",
style="visible:hidden"),
])]),
dcc.Tab(label='Save/Load', className="card-header",
children=[html.Div(className="card-body", children=[
html.P(
"Upload old RcCat log files for data analysis, or download current data."),
dcc.Upload(
id='upload-file',
children=html.Div([
'Drag and Drop or ',
html.A('Select a RcCat log file for upload.')
]),
style={
'vw': '100%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center',
'margin': '10px'
},
),
html.Label('', id="file-label",
style="visible:hidden"),
html.Button('Create Download', id='download-button',
className="btn btn-primary ml-1"),
html.A(
'Download Data',
id='download-link',
download="rawdata.txt",
href="",
target="_blank",
hidden=True,
className="ml-3"
)
])]),
dcc.Tab(label='Telemetry', className="card-header",
children=[html.Div(className="card-body", children=[
html.P('Check the internal telemetry data of the RcCat.'),
html.Div([
html.Button('Refresh', id='refresh-button',
className="btn btn-primary ml-1"),
dcc.Checklist(
options=[
{'label': 'Auto Refresh', 'value': 'ON'},
],
value=[],
labelStyle={'display': 'inline-block'},
id='auto-refresh',
className="pl-5 mr-5 mt-1"
),
html.Label("Frames: ", className="mt-1"),
dcc.Input(
id="time-limit",
type="number",
value=10000
),
html.Button('Clear', id='clear-button',
className="btn btn-primary ml-1"),
], className="input-group p-1"),
dcc.Graph(id='main-graph'),
dcc.Interval(
id='interval-component',
interval=1000, # in milliseconds
n_intervals=0
),
])]),
]),
]),
], className="container")
# connect callback
@app.callback([
Output('connect-button', 'disabled'),
Output('disconnect-button', 'disabled'),
Output('connect-status', 'children'),
],
[
Input('connect-button', 'n_clicks'),
Input('disconnect-button', 'n_clicks'),
],
[
State('connect-path', 'value')
]
)
def connect_and_disconnect(connect_clicks, disconnect_clicks, connect_path_value):
ctx = dash.callback_context
if ctx.triggered and ctx.triggered[0]['value'] is not None:
triggerID = ctx.triggered[0]['prop_id'].split('.')[0]
print(triggerID)
if triggerID == "connect-button":
res = rcCatSerialIO.connect(connect_path_value)
if res == "":
return True, False, "connected"
else:
return False, True, res
if triggerID == "disconnect-button":
rcCatSerialIO.disconnect()
return False, True, "disconnected"
if rcCatSerialIO.status():
return True, False, "connected"
else:
return False, True, "disconnected"
PLOT_LAYOUT = dict(
margin={'l': 40, 'b': 30, 't': 10, 'r': 0},
font=dict(
family='Neucha',
size=15,
color='#555'
),
)
index = 0
@app.callback(Output('interval-component', 'disabled'),
[Input('auto-refresh', 'value')
])
def toggle_auto_refresh(values):
if "ON" in values:
return False
else:
return True
@app.callback(Output('joystick-label', 'children'),
[Input('joystick', 'angle'),
Input('joystick', 'force')
])
def update_joystick(angle, force):
if(isinstance(angle, (int, float)) and isinstance(force, (int, float))):
throttle = math.sin(angle/180.0*math.pi)*force*100.0
steer = math.cos(angle/180.0*math.pi)*force*100.0
rcCatSerialIO.driveCommand(steer, throttle)
return "{} {}".format(throttle, steer)
else:
return ""
@app.callback(Output('send-parameter-output', 'children'),
[Input('send-parameter-button', 'n_clicks')],
[State('send-parameter-id', 'value'),
State('send-parameter-value', 'value')
])
def send_parameter(clicks, param_id, param_value):
if clicks > 0 and (isinstance(param_id, int) and isinstance(param_value, int)):
rcCatSerialIO.setParam(param_id, param_value)
return "{} {}".format(param_id, param_value)
else:
return ""
@app.callback(Output('main-graph', 'figure'),
[Input('interval-component', 'n_intervals'),
Input('refresh-button', 'n_clicks'),
Input('time-limit', 'value'),
Input('clear-button', 'n_clicks'),
])
def update_graph(n, c, limit, clear_clicks):
ctx = dash.callback_context
if ctx.triggered and ctx.triggered[0]['value'] is not None:
triggerID = ctx.triggered[0]['prop_id'].split('.')[0]
print(triggerID)
if triggerID == "clear-button":
rcCatSerialIO.clearBuffer()
if rcCatSerialIO.status():
data = rcCatSerialIO.readData.copy()
fig = make_subplots(
rows=4, cols=1, shared_xaxes=True, vertical_spacing=0.02)
fig.add_trace(go.Scatter(
y=data["steer_rec"][-limit:], mode="lines", name="Steering Receiver"), row=1, col=1)
fig.add_trace(go.Scatter(
y=data["acc_rec"][-limit:], mode="lines", name="Acceleration Receiver"), row=1, col=1)
fig.add_trace(go.Scatter(
y=data["steer_out"][-limit:], mode="lines", name="Steering Output"), row=1, col=1)
fig.add_trace(go.Scatter(
y=data["acc_out"][-limit:], mode="lines", name="Acceleration Output"), row=1, col=1)
fig.add_trace(go.Scatter(
y=data["ax"][-limit:], mode="lines", name="gx"), row=2, col=1)
fig.add_trace(go.Scatter(
y=data["ay"][-limit:], mode="lines", name="gy"), row=2, col=1)
fig.add_trace(go.Scatter(
y=data["az"][-limit:], mode="lines", name="gz"), row=2, col=1)
fig.add_trace(go.Scatter(
y=data["a_tot"][-limit:], mode="lines", name="gtot"), row=2, col=1)
fig.add_trace(go.Scatter(
y=data["dist"][-limit:], mode="lines", name="Ground Dist"), row=2, col=1)
fig.add_trace(go.Scatter(
y=data["pitch"][-limit:], mode="lines", name="Pitch"), row=3, col=1)
fig.add_trace(go.Scatter(
y=data["roll"][-limit:], mode="lines", name="Roll"), row=3, col=1)
fig.add_trace(go.Scatter(
y=data["speed"][-limit:], mode="lines", name="Speed"), row=4, col=1)
fig.add_trace(go.Scatter(
y=data["state"][-limit:], mode="lines", name="State"), row=4, col=1)
fig["layout"]["height"] = 800
fig["layout"].update(**PLOT_LAYOUT)
return fig
else:
return px.scatter(x=[0], y=[0])
@app.callback(
[
Output('file-label', 'children')
],
[
Input('upload-file', 'contents')
],
)
def upload_file(file_data):
_, content_string = file_data.split(',')
decoded = base64.b64decode(content_string)
rcCatSerialIO.importData(decoded)
return ["test"]
@app.callback([Output('download-link', 'hidden'),
Output('download-link', 'href')
],
[Input('download-button', 'n_clicks')
])
def download_file(download_clicks):
res = rcCatSerialIO.getDataString()
data_string = "data:text/tsv;charset=utf-8," + urllib.parse.quote(res)
return len(res) == 0, data_string
# end dash app
if __name__ == '__main__':
app.run_server(host="0.0.0.0")