Last active
June 3, 2019 12:48
-
-
Save cserteGT3/554c828e70ccd9758b11a3b591b4f4b8 to your computer and use it in GitHub Desktop.
Another real time plotting example (https://discourse.julialang.org/t/real-time-plotting-with-makie/20432/8)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sockets | |
using Logging | |
using Makie, AbstractPlotting | |
using Observables | |
using LazyJSON | |
# Stores the last received string. | |
const input_string = Node("") | |
# Stores the parsed vector. | |
const input_vector = Node(zeros(6)) | |
# used as trigger to update the plot | |
const lastUpdate_ = Node(time_ns()); | |
function servertask() | |
# ... | |
# TCP server that reads the messages into: input_string | |
# it's a JSON string like: {"actual_qd": [0.0, 0.0, -0.0, 0.0, 0.0, 0.0], "actual_q": [0.0, 0.0, -0.0, 0.0, 0.0, 0.0]} | |
# ... | |
end | |
# at a 125Hz this means the last 10 seconds | |
const MAX_SIZE = 1250; | |
# 6 arrays for the 6 subplots | |
FxV = rand(MAX_SIZE); | |
# ... 5 more times | |
tV = rand(MAX_SIZE); | |
fxNode = Node(FxV); | |
# ... 5 more times | |
tvNode = Node(tV); | |
s1 = lines(fxNode); | |
# ... 5 more times | |
# put in an array to handle the axes and the limit updates easier | |
sArr = [s1, s2, s3, s4, s5, s6]; | |
yArr = ["$i" for i in 1:6] | |
scene = vbox(hbox(sArr[3], sArr[2], sArr[1]), hbox(sArr[6], sArr[5], sArr[4])); | |
for i in 1:6 | |
sArr[i][Axis][:names][:axisnames] = ("Time", yArr[i]); | |
end | |
# this function makes a "fix sized" array | |
function pushTo!(A, newX, maxSize) | |
if size(A, 1) < maxSize | |
push!(A, newX) | |
else | |
popfirst!(A) | |
push!(A, newX) | |
end | |
end | |
function getJSONvalue(str, jsonid) | |
try | |
jsonD = LazyJSON.value(str) | |
input_vector[] = convert(Array{Float64,1}, jsonD[jsonid]) | |
return true | |
catch | |
@info "JSON parse error caught: $jsonid; $str" | |
return false | |
end | |
end | |
# this called every time when a new message is parsed | |
# updates the vectors (not the observables) | |
function updateplotvectors(f) | |
pushTo!(FxV, f[1], MAX_SIZE) | |
# ... 5 more times | |
# get the current time | |
current_time = time_ns() | |
# save the time in seconds - not used currently | |
ttt = (current_time-startUp)/1000000000 | |
#pushTo!(tV, ttt, MAX_SIZE) | |
# lastUpdate_ is used to trigger further updates (but it is throttled down) | |
lastUpdate_[] = current_time | |
end | |
# this function updates the observables (= the plot itself) | |
function updatePlot(val) | |
fxNode[] = FxV | |
# ... 5 more times | |
# update limits: | |
for i in 1:6 | |
AbstractPlotting.update_limits!(sArr[i]) | |
end | |
AbstractPlotting.update!(scene) | |
end | |
# JSON name | |
const JOINT = "actual_q" | |
# throttle plot updates to 4 times / second | |
lastUpdate = throttle(0.25, lastUpdate_) | |
# process the input string | |
h_input = on(str->getJSONvalue(str, JOINT), input_string) | |
# when an input string is processed, update the vectors | |
h_array = on(updateplotvectors, input_vector) | |
# when the throttled observable updates, update the plot | |
h_plot = on(updatePlot, lastUpdate) | |
########### | |
# Running # | |
########### | |
#show the plot | |
scene | |
SERVER_PORT = 4002 | |
servtask = Task(servertask) | |
const startUp = time_ns() | |
schedule(servtask) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment