如何将数据从LabVIEW连续发送到R?(代码帮助)
我正在尝试将LabVIEW(轴承振动和温度)的实时数据输入到用R编写的应用程序中,以创建控制图。它会工作一段时间,但最终会崩溃,并显示以下错误消息:如何将数据从LabVIEW连续发送到R?(代码帮助),r,labview,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中投影控制图。该过程成功一段时间,失败
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)
}
getwd()
setwd(“C:/Users/johnd/Desktop/R数据”)
而(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=澳元t1
B2=B2澳元
t2=t2澳元
AM=A$AM
t3=澳元t3
M=百万澳元
t4=澳元t4
B1=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
小=B$小
T2=B$T2
大=B$大
DJ1=A[seq(1,nrow(A),1),c('t1','B2','AM','M','B1')]
DJ1
n=16
DJ2=聚合(B,列表(代表(1:(nrow(B)%/%n+1),每个=n,len=nrow(B)),平均值)[-1]
DJ2
#------------------------------------------------------------------------
DJ6=cbind(DJ1[,'B1'],DJ2[,c('small','big'))#为这三个指标创建矩阵
DJ6
#--------------T2手工制作---------------------------------------------------------------------
新的_B1=DJ6[,'B1']
new_small=DJ6[,'small']####将DJ6矩阵分解为每个指示器的向量(温度、大小加速度计)
new_big=DJ6[,'big']
新_B1
新的小型
纽约大酒店
mean_B1=数值形式(colMeans(DJ6[,'B1']))
mean_small=as.numeric(colMeans(DJ6[,'small']))##分解为numeric类型的向量
mean_big=as.numeric(colMeans(DJ6[,'big']))
cov_inv=数据。矩阵(求解(cov(DJ6))#获得逆协方差矩阵
cov_inv
p=ncol(DJ6)#更改为通过获取质量特征的OG矩阵中的coumn数#p=3##来提取参数数
m=64个样本(10秒数据)
a_α=0.99
f=qf(a_alpha,df1=p,df2=(m-p))35;##计算数据的f统计量
F
UCL=(p*(m+1)*(m-1)*(f))/(m*(m-p))###产生控制上限
伦敦大学学院
差异B1=新值B1-平均值B1
diff\u small=新的\u small-平均值\u small
diff\u big=新的\u big-平均值\u big
DJ7=cbind(diff_B1,diff_small,diff_big)#生成平均值和观测值之间的差值矩阵(x-(x-bar))
DJ7
#DJ8=data.matrix(DJ7[1,])
#DJ8
矩阵(DJ7)####将矩阵转换为适当的数值形式
DJ9
#T2.1.1=DJ8%*%cov\U inv%*%t(DJ8)
#T2.1.1
#T2.1=t(作为矩阵(DJ9[1,])%*%cov\u inv%*%as.matrix(DJ9[1,]))
#T2.1
#T2
该过程成功一段时间,失败时刻并不总是相同的时间。有时控制图会运行6-7分钟,其他时间会在2分钟内崩溃
我怀疑Excel文件的更新速度不够快,因此R代码在Excel文件为空时尝试读取该文件
你的怀疑是对的
在当前设计中,R应用程序可能会崩溃,具体取决于它相对于LabVIEW应用程序的运行速度。这称为“竞争条件”;必须从代码中消除竞争条件
又快又脏的解决办法
避免崩溃的一个简单解决方案是调用NROW
检查是否存在任何数据。如果没有可用的数据,请不要调用aggregate
。下面介绍了这一点:
更稳健的解决方案
更好的解决方案是使用通信协议(如TCP)将数据从LabVIEW传输到R,而不是使用CSV文件传输实时数据。例如,您的R程序可以侦听TCP套接字上的数据。在运行数据处理代码之前,让它等待数据从LabVIEW发送
- 下面是在R中使用
socketConnection
的示例:
- 以下是在LabVIEW中通过TCP发送/接收数据的示例:
该过程成功一段时间,失败时刻并不总是相同的时间。有时控制图会运行6-7分钟,其他时间会在2分钟内崩溃
我怀疑Excel文件的更新速度不够快,因此R代码在Excel文件为空时尝试读取该文件
你的怀疑是对的
在当前设计中,R应用程序可能会崩溃,具体取决于它相对于LabVIEW应用程序的运行速度。这称为“竞争条件”;必须从代码中消除竞争条件
又快又脏的解决办法
避免崩溃的一个简单解决方案是调用NROW
检查是否存在任何数据。如果没有可用的数据,请不要调用aggregate
。下面介绍了这一点:
更稳健的解决方案
更好的解决方案是使用通信协议(如TCP)将数据从LabVIEW传输到R,而不是使用CSV文件传输实时数据。例如,您的R程序可以侦听TCP套接字上的数据。在运行数据处理代码之前,让它等待数据从LabVIEW发送
- 下面是在R中使用
socketConnection
的示例:
- 以下是在LabVIEW中通过TCP发送/接收数据的示例: