Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
diy
2018-gr2-project-selbstbalancierender-roboter
Commits
93ab5dde
Commit
93ab5dde
authored
Aug 02, 2018
by
Stefan Steinmüller
Browse files
'realtime' plotting implemented
parent
d72af769
Changes
1
Hide whitespace changes
Inline
Side-by-side
HalloRobot/plot_serial.py
View file @
93ab5dde
...
...
@@ -2,39 +2,78 @@ import serial
import
matplotlib.pyplot
as
plt
import
numpy
as
np
from
collections
import
deque
from
matplotlib.animation
import
FuncAnimation
fig
,
ax_1
=
plt
.
subplots
(
1
,
1
)
ax_2
=
ax_1
.
twinx
()
ax_3
=
ax_1
.
twinx
()
fig
=
plt
.
figure
()
rect
=
[
1
,
1
,
1
,
1
]
ax_1
=
fig
.
add_subplot
(
411
)
ax_2
=
fig
.
add_subplot
(
412
)
ax_3
=
fig
.
add_subplot
(
413
)
ax_4
=
fig
.
add_subplot
(
414
)
x_range
=
deque
([])
max_range
=
0
ax_1
.
set_ylim
([
-
100
,
100
])
ax_2
.
set_ylim
([
-
100
,
100
])
ax_3
.
set_ylim
([
-
100
,
100
])
plt
.
ion
()
#ax_1.set_ylim([-100, 100])
ax_1
.
set_autoscaley_on
(
True
)
#ax_2.set_ylim([-100, 100])
ax_2
.
set_autoscaley_on
(
True
)
#ax_3.set_ylim([-100, 100])
ax_3
.
set_autoscaley_on
(
True
)
#ax_4.set_ylim([-100, 100])
#ax_4.set_autoscaley_on(True)
ax_1_l
,
=
ax_1
.
plot
([],[],
"ro"
)
ax_2_l
,
=
ax_2
.
plot
([],[],
'bo'
)
ax_3_l
,
=
ax_3
.
plot
([],[],
'go'
)
ax_4_l
,
=
ax_4
.
plot
([],[],
'yo'
)
try
:
board
=
serial
.
Serial
(
"/dev/ttyACM1"
)
board
=
serial
.
Serial
(
"/dev/ttyACM1"
,
115200
,
timeout
=
0.1
)
except
Exception
as
e
:
print
(
"an error occured while opening the serial device
\n
{}"
.
format
(
e
))
exit
()
print
(
board
.
readline
())
#def add_points(a):
while
board
:
if
True
:
try
:
# print(max_range, 'max_range')
pass
except
:
max_range
=
0
_txt
=
board
.
readline
().
strip
(
'
\r\n
'
)
txt
=
_txt
.
split
(
","
)
print
(
txt
)
x_range
.
append
(
max_range
+
1
)
max_range
+=
1
try
:
ax_1
.
set_xlim
([
max_range
-
200
,
max_range
+
1
])
ax_2
.
set_xlim
([
max_range
-
200
,
max_range
+
1
])
ax_3
.
set_xlim
([
max_range
-
200
,
max_range
+
1
])
ax_4
.
set_xlim
([
max_range
-
200
,
max_range
+
1
])
plt
.
show
()
ax_1
.
plot
([
max_range
],
[
float
(
txt
[
0
].
strip
())],
'ro'
)
ax_2
.
plot
([
max_range
],
[
float
(
txt
[
1
].
strip
())],
'bo'
)
ax_3
.
plot
([
max_range
],
[
float
(
txt
[
2
].
strip
())],
'go'
)
ax_4
.
plot
([
max_range
],
[
float
(
txt
[
3
].
strip
())],
'yo'
)
while
(
board
):
_txt
=
board
.
readline
()
txt
=
_txt
.
split
(
";"
).
strip
()
x_range
.
append
(
max_range
+
1
)
max_range
+=
1
ax_1
.
set_xlim
([
max_range
-
200
,
max_range
+
1
])
ax_1
.
plot
([
max_range
],
[
txt
[
0
]])
ax_2
.
plot
([
max_range
],
[
txt
[
1
]])
ax_2
.
plot
([
max_range
],
[
txt
[
2
]])
csv_cont
=
[]
try
:
with
open
(
'log_plot.csv'
,
'r'
)
as
csv
:
csv_cont
=
csv
.
readlines
()
except
:
pass
with
open
(
'log_plot.csv'
,
'w'
)
as
csv
:
csv_cont
.
append
(
_txt
)
csv
.
writelines
(
csv_cont
)
csv_cont
=
[]
try
:
with
open
(
'log_plot.csv'
,
'r'
)
as
csv
:
csv_cont
=
csv
.
readlines
()
except
:
pass
with
open
(
'log_plot.csv'
,
'w'
)
as
csv
:
csv_cont
.
append
(
_txt
)
csv
.
writelines
(
csv_cont
)
except
:
pass
#ani = FuncAnimation(fig, add_points, frames=200)
plt
.
pause
(
0.05
)
#plt.show()
plt
.
draw
()
fig
.
canvas
.
draw
()
fig
.
canvas
.
flush_events
()
#ani = FuncAnimation(fig, add_points)
#plt.show()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment