9
9
# ' process frequency > 25 Hz (40ms delay time) or it might be the case that
10
10
# ' my computer doesn't have enough power. Anyway, I set this option here to
11
11
# ' 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)
12
15
# ' @inheritParams ar_monitor
13
16
# '
14
17
# ' @export
15
18
ar_plotter <- function (fd , names = NULL , sep_fun = ar_sep_comma ,
16
19
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 ) {
18
22
shiny :: runApp(
19
23
ar_app(con = fd , names = names , sep_fun = sep_fun ,
20
24
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 ),
22
27
launch.browser = rstudioapi :: viewer
23
28
)
24
29
}
@@ -27,7 +32,9 @@ ar_plotter <- function(fd, names = NULL, sep_fun = ar_sep_comma,
27
32
28
33
ar_app <- function (con , names = NULL , sep_fun = ar_sep_comma ,
29
34
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
+
31
38
message(" Flushing Port..." )
32
39
ar_flush_hard(con , flush_time )
33
40
first_dot <- ar_read(con , eolchar , buf_max , timeout )
@@ -38,6 +45,14 @@ ar_app <- function(con, names = NULL, sep_fun = ar_sep_comma,
38
45
first_dot <- sep_fun(first_dot )
39
46
signal_vars <- seq(length(first_dot ))
40
47
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
+
41
56
if (is.null(names )) {
42
57
names(signal_vars ) <- paste(" Var" , signal_vars )
43
58
names(first_dot ) <- paste(" Var" , signal_vars )
@@ -113,6 +128,14 @@ ar_app <- function(con, names = NULL, sep_fun = ar_sep_comma,
113
128
if (rv $ state ) {
114
129
ar_flush_hard(con , 0.04 , FALSE )
115
130
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
+
116
139
if (input $ save ) {
117
140
cat(csv_newline(realtime ), file = input $ file , append = TRUE )
118
141
}
0 commit comments