99# ' process frequency > 25 Hz (40ms delay time) or it might be the case that
1010# ' my computer doesn't have enough power. Anyway, I set this option here to
1111# ' add 40ms delay time to reduce the sampling frequency.
12+ # ' @param running_mean If an integer larger than 0 is chosen, the running mean
13+ # ' of each series is computed, effectively smoothing the signals. Default value
14+ # ' is set to 0 (no running mean is calculated)
1215# ' @inheritParams ar_monitor
1316# '
1417# ' @export
1518ar_plotter <- function (fd , names = NULL , sep_fun = ar_sep_comma ,
1619 reduce_freq = TRUE , flush_time = 0.05 ,
17- eolchar = " \n " , buf_max = 256 , timeout = 5000 ) {
20+ eolchar = " \n " , buf_max = 256 , timeout = 5000 ,
21+ running_mean = 0 ) {
1822 shiny :: runApp(
1923 ar_app(con = fd , names = names , sep_fun = sep_fun ,
2024 flush_time = flush_time , reduce_freq = reduce_freq ,
21- eolchar = eolchar , buf_max = buf_max , timeout = timeout ),
25+ eolchar = eolchar , buf_max = buf_max , timeout = timeout ,
26+ running_mean = running_mean ),
2227 launch.browser = rstudioapi :: viewer
2328 )
2429}
@@ -27,7 +32,9 @@ ar_plotter <- function(fd, names = NULL, sep_fun = ar_sep_comma,
2732
2833ar_app <- function (con , names = NULL , sep_fun = ar_sep_comma ,
2934 flush_time = 0.05 , reduce_freq = TRUE ,
30- eolchar = " \n " , buf_max = 256 , timeout = 5000 ) {
35+ eolchar = " \n " , buf_max = 256 , timeout = 5000 ,
36+ running_mean = 0 ) {
37+
3138 message(" Flushing Port..." )
3239 ar_flush_hard(con , flush_time )
3340 first_dot <- ar_read(con , eolchar , buf_max , timeout )
@@ -38,6 +45,14 @@ ar_app <- function(con, names = NULL, sep_fun = ar_sep_comma,
3845 first_dot <- sep_fun(first_dot )
3946 signal_vars <- seq(length(first_dot ))
4047
48+ if (running_mean > 0 ) {
49+ rmeans <- vector(" list" , length(first_dot ))
50+ for (i in signal_vars ) {
51+ rmeans [[i ]] <- arduinor ::: RunningMean $ new(running_mean )
52+ rmeans [[i ]]$ insert(first_dot [i ])
53+ }
54+ }
55+
4156 if (is.null(names )) {
4257 names(signal_vars ) <- paste(" Var" , signal_vars )
4358 names(first_dot ) <- paste(" Var" , signal_vars )
@@ -113,6 +128,14 @@ ar_app <- function(con, names = NULL, sep_fun = ar_sep_comma,
113128 if (rv $ state ) {
114129 ar_flush_hard(con , 0.04 , FALSE )
115130 realtime <- sep_fun(ar_read(con , eolchar , buf_max , timeout ))
131+
132+ if (running_mean > 0 ) {
133+ for (i in seq(length(realtime ))) {
134+ rmeans [[i ]]$ insert(realtime [[i ]])
135+ realtime [[i ]] <- rmeans [[i ]]$ get_mean()
136+ }
137+ }
138+
116139 if (input $ save ) {
117140 cat(csv_newline(realtime ), file = input $ file , append = TRUE )
118141 }
0 commit comments