如何连续地将数据从LabVIEW发送到R? (代码帮助)

时间:2019-07-12 18:52:22

标签: r labview

我正在尝试将LabVIEW中的实时数据(轴承和温度的振动)带入用R编写的应用程序中,以创建控制图。它可以工作一段时间,但最终会崩溃并显示以下错误消息:

Error in aggregate.data.frame(B, list(rep(1:(nrow(B)%/%n + 1), each = n,  :
    no rows to aggregate

此过程与LabVIEW采集数据并将其投影到两个Excel文件中一样。这些文件在R代码中读取,并用于在R中投影控制图。该过程成功完成了一段时间,并且失败时刻并不总是相同的。有时,控制图将运行6-7分钟,其他时候将在2分钟内崩溃。

我怀疑Excel文件的更新速度不够快,因此R代码在该Excel文件为空时会尝试读取该文件。

任何建议都会很棒!谢谢!

我试图降低每秒采集的样本量。那没有用。

getwd()
setwd("C:/Users/johnd/Desktop/R Data")

while(1) {

  A = fread("C:/Users/johnd/Desktop/R Data/a1.csv" , skip = 4  , header = FALSE , col.names = c("t1","B2","t2","AM","t3","M","t4","B1"))
  t1 = A$t1
  B2 = A$B2
  t2 = A$t2
  AM = A$AM
  t3 = A$t3
  M = A$M
  t4 = A$t4
  B1 = A$B1

  B = fread("C:/Users/johnd/Desktop/R Data/b1.csv" , skip = 4  , header = FALSE , col.names = c("T1","small","T2","big"))
  T1 = B$T1
  small = B$small
  T2 = B$T2
  big = B$big

  DJ1 = A[seq(1,nrow(A),1),c('t1','B2','AM','M','B1')]
  DJ1

  n = 16
  DJ2 = aggregate(B,list(rep(1:(nrow(B)%/%n+1),each=n,len=nrow(B))),mean)[-1]
  DJ2

  #------------------------------------------------------------------------
  DJ6 = cbind(DJ1[,'B1'],DJ2[,c('small','big')]) # creates matrix for these three indicators
  DJ6


  #--------------T2 Hand made---------------------------------------------------------------------

  new_B1 = DJ6[,'B1']
  new_small = DJ6[,'small']   ### decompose the DJ6 matrix into vectors for each indicator(temperature, big & small accelerometers)
  new_big = DJ6[,'big']

  new_B1
  new_small
  new_big

  mean_B1 = as.numeric(colMeans(DJ6[,'B1']))
  mean_small = as.numeric(colMeans(DJ6[,'small']))    ##decomposes into vectors of type numeric 
  mean_big = as.numeric(colMeans(DJ6[,'big']))

  cov_inv = data.matrix(solve(cov(DJ6)))   # obtain inverse covariance matrix 
  cov_inv

  p = ncol(DJ6) #changed to pull number of parameters by taking the number of coumns in OG matrix   #p=3   # #ofQuality Characteristics 
  m=64 # #of samples (10 seconds of data)
  a_alpha = 0.99
  f= qf(a_alpha , df1 = p,df2 = (m-p))  ### calculates the F-Statistic for our data    
  f
  UCL = (p*(m+1)*(m-1)*(f))/(m*(m-p))   ###produces upper control limit 
  UCL

  diff_B1 = new_B1-mean_B1
  diff_small = new_small-mean_small
  diff_big = new_big-mean_big

  DJ7 = cbind(diff_B1, diff_small , diff_big) #produces matrix of difference between average and observations (x-(x-bar))
  DJ7
  # DJ8 = data.matrix(DJ7[1,])
  # DJ8
  DJ9 = data.matrix(DJ7)     ### turns matrix into appropriate numeric form   
  DJ9

  # T2.1.1 = DJ8 %*% cov_inv %*% t(DJ8)
  # T2.1.1

  # T2.1 = t(as.matrix(DJ9[1,])) %*% cov_inv %*% as.matrix(DJ9[1,])
  # T2.1

  #T2 <- NULL
  for(i in 1:64){   #### creates vector of T^2 statistic 

    T2<- t(as.matrix(DJ9[i,])) %*% cov_inv %*% as.matrix(DJ9[i,])   # calculation of T^2 test statistic   ## there is no calculation of x-double bar

    write.table(T2,"C:/Users/johnd/Desktop/R Data/c1.csv",append=T,sep="," , col.names = FALSE)#
     #
    DJ12 <-fread("C:/Users/johnd/Desktop/R Data/c1.csv" , header = FALSE ) #
  }
  # DJ12

  DJ12$V1 = 1:nrow(DJ12)  
  # plot(DJ12 , type='l')

  p1 = nrow(DJ12)-m
  p2 = nrow(DJ12)

  plot(DJ12[p1:p2,], type ='o', ylim =c(0,15), ylab="T2 Chart" , xlab="Data points")  ### plots last 640 points     
  # plot(DJ12[p1:p2,], type ='o' , ylim =c(0,15) , ylab="T2 Chart" , xlab="Data points")
  abline(h=UCL , col="red") ## displays upper control limit 


  Sys.sleep(1)
}

1 个答案:

答案 0 :(得分:0)

  

该过程成功完成了一段时间,失败时刻并不总是相同的。有时,控制图会运行6-7分钟,其他时候会在2分钟内崩溃。

     

我怀疑Excel文件的更新速度不够快,因此R代码尝试在该Excel文件为空时读取该文件。

您的怀疑是正确的。

使用当前设计,R应用程序可能会崩溃,这取决于它相对于LabVIEW应用程序运行的速度。这称为比赛条件;您必须从代码中消除竞争条件。

一种快速而肮脏的解决方案

一种避免崩溃的简单解决方案是调用NROW来检查是否存在任何数据。如果没有可用数据,请不要致电aggregate。此处描述:error message in r: no rows to aggregate

更强大的解决方案

更好的解决方案是使用TCP之类的通信协议将数据从LabVIEW流传输到R,而不是使用CSV文件传输实时数据。例如,您的R程序可以在TCP套接字上侦听数据。在运行数据处理代码之前,让它等待LabVIEW发送数据。